Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 319aeb6

Browse files
committed
added mutlti device context test
1 parent 533dc7a commit 319aeb6

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

SYCL/Basic/multi_device_context.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %HOST_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
// REQUIRES: cuda
7+
8+
#include <stdlib.h>
9+
#include <sycl.hpp>
10+
11+
sycl::event add(sycl::queue q, sycl::buffer<int> buff, int *usm,
12+
sycl::event e) {
13+
return q.submit([&](sycl::handler &cgh) {
14+
auto acc = buff.get_access<sycl::access::mode::read_write>(cgh);
15+
cgh.depends_on(e);
16+
cgh.single_task([=]() { acc[0] += *usm; });
17+
});
18+
}
19+
20+
int main() {
21+
sycl::platform plat = sycl::platform::get_platforms()[0];
22+
auto devices = plat.get_devices();
23+
if (devices.size() < 2) {
24+
std::cout << "Need two devices for the test!" << std::endl;
25+
return 0;
26+
}
27+
28+
sycl::device dev1 = devices[0];
29+
sycl::device dev2 = devices[1];
30+
31+
sycl::context ctx{{dev1, dev2}};
32+
33+
sycl::queue q1{ctx, dev1};
34+
sycl::queue q2{ctx, dev2};
35+
36+
int a = 1;
37+
int b = 2;
38+
int c = 4;
39+
int d = 5;
40+
{
41+
sycl::buffer<int> buff1(&a, 1);
42+
sycl::buffer<int> buff2(&b, 1);
43+
44+
// test copying usm
45+
int *usm1 = sycl::malloc_device<int>(1, q1);
46+
int *usm2 = sycl::malloc_device<int>(1, q2);
47+
sycl::event e1 = q1.memcpy(usm1, &c, 1);
48+
sycl::event e2 = q2.memcpy(usm2, &d, 1);
49+
50+
// test combination of usm and buffers in a kernel
51+
sycl::event e3 = add(q1, buff1, usm1, e1);
52+
sycl::event e4 = add(q2, buff2, usm2, e2);
53+
54+
// change values in usm to ensure results are distinct
55+
sycl::event e5 = q1.memcpy(usm1, &d, 1, e3);
56+
sycl::event e6 = q2.memcpy(usm2, &c, 1, e4);
57+
58+
// use each buffer on the other device than before - tests that copying
59+
// between devices works
60+
add(q1, buff2, usm1, e5);
61+
add(q2, buff1, usm2, e6);
62+
}
63+
assert(a == 1 + 2 * 4);
64+
assert(b == 2 + 2 * 5);
65+
66+
return 0;
67+
}

0 commit comments

Comments
 (0)