forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYCL][HIP] Add coarse-grained memory advice for HIP-AMD
- Loading branch information
Showing
5 changed files
with
161 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// RUN: %{build} -o %t1.out | ||
// REQUIRES: hip_amd | ||
// RUN: %{run} %t1.out | ||
|
||
//==---------------- memadvise_cuda.cpp ------------------------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "sycl/context.hpp" | ||
#include "sycl/detail/pi.h" | ||
#include "sycl/device.hpp" | ||
#include <iostream> | ||
#include <sycl/sycl.hpp> | ||
#include <vector> | ||
|
||
int main() { | ||
sycl::queue q; | ||
sycl::device dev = q.get_device(); | ||
sycl::context ctx = q.get_context(); | ||
if (!dev.get_info<sycl::info::device::usm_shared_allocations>()) { | ||
std::cout << "Shared USM is not supported. Skipping test." << std::endl; | ||
return 0; | ||
} | ||
|
||
constexpr size_t size = 100; | ||
void *ptr = sycl::malloc_shared(size, dev, ctx); | ||
if (ptr == nullptr) { | ||
std::cout << "Allocation failed!" << std::endl; | ||
return -1; | ||
} | ||
|
||
// NOTE: PI_MEM_ADVICE_CUDA_* advice values are mapped to the HIP backend too. | ||
std::vector<int> valid_advices{ | ||
PI_MEM_ADVICE_CUDA_SET_READ_MOSTLY, | ||
PI_MEM_ADVICE_CUDA_UNSET_READ_MOSTLY, | ||
PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION, | ||
PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION, | ||
PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY, | ||
PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY, | ||
PI_MEM_ADVICE_CUDA_SET_PREFERRED_LOCATION_HOST, | ||
PI_MEM_ADVICE_CUDA_UNSET_PREFERRED_LOCATION_HOST, | ||
PI_MEM_ADVICE_CUDA_SET_ACCESSED_BY_HOST, | ||
PI_MEM_ADVICE_CUDA_UNSET_ACCESSED_BY_HOST, | ||
PI_MEM_ADVICE_HIP_SET_COARSE_GRAINED, | ||
PI_MEM_ADVICE_HIP_UNSET_COARSE_GRAINED, | ||
}; | ||
for (int advice : valid_advices) { | ||
q.mem_advise(ptr, size, advice); | ||
} | ||
|
||
q.wait_and_throw(); | ||
std::cout << "Test passed." << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// RUN: %{build} -o %t1.out | ||
// REQUIRES: hip_amd | ||
// RUN: %{run} %t1.out | ||
|
||
#include <iostream> | ||
#include <sycl/sycl.hpp> | ||
|
||
namespace kernels { | ||
class SquareKrnl final { | ||
public: | ||
SquareKrnl(int *ptr) : mPtr(ptr) {} | ||
|
||
void operator()(sycl::id<1>) const { | ||
// mPtr value squared here | ||
*mPtr = (*mPtr) * (*mPtr); | ||
} | ||
|
||
private: | ||
int *mPtr; | ||
}; | ||
} // namespace kernels | ||
|
||
int main() { | ||
sycl::queue q; | ||
sycl::device dev = q.get_device(); | ||
sycl::context ctx = q.get_context(); | ||
if (!dev.get_info<sycl::info::device::usm_shared_allocations>()) { | ||
std::cout << "Shared USM is not supported. Skipping test.\n"; | ||
return 0; | ||
} | ||
|
||
int *ptr = sycl::malloc_shared<int>(1, q); | ||
|
||
// Hint that data coherency during simultaneous execution on | ||
// both host and device is not necessary | ||
constexpr int MemAdviseCoarseGrained = PI_MEM_ADVICE_HIP_SET_COARSE_GRAINED; | ||
q.mem_advise(ptr, sizeof(int), MemAdviseCoarseGrained); | ||
|
||
// Call this routine TesCoherency function! | ||
constexpr int number = 9; | ||
constexpr int expected = 81; | ||
*ptr = number; | ||
q.submit([&](sycl::handler &h) { | ||
h.parallel_for(sycl::range{1}, kernels::SquareKrnl(ptr)); | ||
}); | ||
// Synchronise the underlying stream the work is run on before host access. | ||
q.wait(); | ||
// Check if caches are flushed correctly and same memory is between devices. | ||
if (*ptr != expected) { | ||
std::cout << "Coarse-grained mode coherency failed. Value = " << *ptr | ||
<< '\n'; | ||
return 1; | ||
} | ||
|
||
sycl::free(ptr, q); | ||
return 0; | ||
} |