mirror of
https://github.com/fumiama/base16384-sycl.git
synced 2026-06-28 08:40:27 +08:00
feat: add test USM implicit data movement
This commit is contained in:
@@ -146,5 +146,7 @@ int main() {
|
|||||||
|
|
||||||
sycl::free(data, q);
|
sycl::free(data, q);
|
||||||
|
|
||||||
|
std::cout << "Test Passed!!!" << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,5 +34,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Test Passed!!!" << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -22,5 +22,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Device is CPU: OK" << std::endl;
|
std::cout << "Device is CPU: OK" << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Test Passed!!!" << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -11,23 +11,23 @@ int main() {
|
|||||||
sycl::queue Q;
|
sycl::queue Q;
|
||||||
|
|
||||||
std::array<int, N> host_array;
|
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++) {
|
for (int i = 0; i < N; i++) {
|
||||||
host_array[i] = N;
|
host_array[i] = N;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We will learn how to simplify this example later
|
// We will learn how to simplify this example later
|
||||||
Q.submit([&](sycl::handler &h) {
|
Q.submit([&](sycl::handler& h) {
|
||||||
// copy hostArray to deviceArray
|
// copy hostArray to deviceArray
|
||||||
h.memcpy(device_array, &host_array[0], N * sizeof(int));
|
h.memcpy(device_array, &host_array[0], N * sizeof(int));
|
||||||
}).wait();
|
}).wait();
|
||||||
|
|
||||||
Q.submit([&](sycl::handler &h) {
|
Q.submit([&](sycl::handler& h) {
|
||||||
h.parallel_for(N, [=](sycl::id<1> i) { device_array[i]++; });
|
h.parallel_for(N, [=](sycl::id<1> i) { device_array[i]++; });
|
||||||
}).wait();
|
}).wait();
|
||||||
|
|
||||||
Q.submit([&](sycl::handler &h) {
|
Q.submit([&](sycl::handler& h) {
|
||||||
// copy deviceArray back to hostArray
|
// copy deviceArray back to hostArray
|
||||||
h.memcpy(&host_array[0], device_array, N * sizeof(int));
|
h.memcpy(&host_array[0], device_array, N * sizeof(int));
|
||||||
}).wait();
|
}).wait();
|
||||||
@@ -42,5 +42,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Test Passed!!!" << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
45
tests/fig-3-5_usm-implicit-data-movement.cpp
Normal file
45
tests/fig-3-5_usm-implicit-data-movement.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user