1
0
mirror of https://github.com/fumiama/base16384-sycl.git synced 2026-06-05 00:32:49 +08:00

feat: add test USM implicit data movement

This commit is contained in:
源文雨
2025-11-03 16:38:07 +08:00
parent 1fb202fa89
commit ed2915e97b
5 changed files with 58 additions and 4 deletions

View File

@@ -146,5 +146,7 @@ int main() {
sycl::free(data, q);
std::cout << "Test Passed!!!" << std::endl;
return 0;
}

View File

@@ -34,5 +34,7 @@ int main() {
}
}
std::cout << "Test Passed!!!" << std::endl;
return 0;
}

View File

@@ -22,5 +22,8 @@ int main() {
}
std::cout << "Device is CPU: OK" << std::endl;
std::cout << "Test Passed!!!" << std::endl;
return 0;
}

View File

@@ -11,23 +11,23 @@ int main() {
sycl::queue Q;
std::array<int, N> host_array;
int *device_array = sycl::malloc_device<int>(N, Q);
int* device_array = sycl::malloc_device<int>(N, Q);
for (int i = 0; i < N; i++) {
host_array[i] = N;
}
// We will learn how to simplify this example later
Q.submit([&](sycl::handler &h) {
Q.submit([&](sycl::handler& h) {
// copy hostArray to deviceArray
h.memcpy(device_array, &host_array[0], N * sizeof(int));
}).wait();
Q.submit([&](sycl::handler &h) {
Q.submit([&](sycl::handler& h) {
h.parallel_for(N, [=](sycl::id<1> i) { device_array[i]++; });
}).wait();
Q.submit([&](sycl::handler &h) {
Q.submit([&](sycl::handler& h) {
// copy deviceArray back to hostArray
h.memcpy(&host_array[0], device_array, N * sizeof(int));
}).wait();
@@ -42,5 +42,7 @@ int main() {
}
}
std::cout << "Test Passed!!!" << std::endl;
return 0;
}

View File

@@ -0,0 +1,45 @@
// Figure 3-5. USM implicit data movement
// from book - Data Parallel C++
// https://link.springer.com/book/10.1007/978-1-4842-5574-2
#include <sycl/sycl.hpp>
constexpr int N = 42;
int main() {
sycl::queue Q;
int* host_array = malloc_host<int>(N, Q);
int* shared_array = malloc_shared<int>(N, Q);
for (int i = 0; i < N; i++) {
// Initialize hostArray on host
host_array[i] = i;
}
// We will learn how to simplify this example later
Q.submit([&](sycl::handler& h) {
h.parallel_for(N, [=](sycl::id<1> i) {
// access sharedArray and hostArray on device
shared_array[i] = host_array[i] + 1;
});
}).wait();
for (int i = 0; i < N; i++) {
// Verify that sharedArray[i] equals hostArray[i] + 1
if (shared_array[i] != host_array[i] + 1) {
std::cout << "Error at index " << i << ": expected " << (host_array[i] + 1) << ", got "
<< shared_array[i] << std::endl;
free(shared_array, Q);
free(host_array, Q);
return 1;
}
}
free(shared_array, Q);
free(host_array, Q);
std::cout << "Test Passed!!!" << std::endl;
return 0;
}