-
Notifications
You must be signed in to change notification settings - Fork 744
/
MultipleDevices.cpp
149 lines (125 loc) · 4.29 KB
/
MultipleDevices.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out
// RUN: %t.out
//===- MultipleDevices.cpp - Test checking multi-device execution --------===//
//
// 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 <CL/sycl.hpp>
#include <iostream>
using namespace cl::sycl;
int multidevice_test(queue MyQueue1, queue MyQueue2) {
const size_t N = 100;
buffer<int, 1> BufA(range<1>{N});
buffer<int, 1> BufB(range<1>{N});
buffer<int, 1> BufC(range<1>{N});
buffer<int, 1> BufD(range<1>{N});
MyQueue1.submit([&](handler &cgh) {
auto A = BufA.get_access<access::mode::write>(cgh);
cgh.parallel_for<class init_a>(
range<1>{N}, [=](id<1> index) { A[index[0]] = index[0]; });
});
MyQueue2.submit([&](handler &cgh) {
auto B = BufB.get_access<access::mode::write>(cgh);
cgh.parallel_for<class init_b>(
range<1>{N}, [=](id<1> index) { B[index[0]] = N - index[0]; });
});
MyQueue2.submit([&](handler& cgh) {
auto A = BufA.get_access<access::mode::read>(cgh);
auto B = BufB.get_access<access::mode::read_write>(cgh);
auto C = BufC.get_access<access::mode::write>(cgh);
cgh.parallel_for<class op1>(range<1>{N}, [=](id<1> index) {
B[index[0]] = B[index[0]] + A[index[0]];
C[index[0]] = B[index[0]] - index[0];
});
});
MyQueue2.submit([&](handler &cgh) {
auto D = BufD.get_access<access::mode::write>(cgh);
cgh.parallel_for<class init_d>(range<1>{N},
[=](id<1> index) { D[index[0]] = 1; });
});
MyQueue1.submit([&](handler& cgh) {
auto B = BufB.get_access<access::mode::read>(cgh);
auto C = BufC.get_access<access::mode::read>(cgh);
auto D = BufD.get_access<access::mode::read_write>(cgh);
cgh.parallel_for<class op2>(range<1>{N}, [=](id<1> index) {
D[index[0]] = D[index[0]] + B[index[0]] - C[index[0]];
});
});
auto FinalD = BufD.get_access<access::mode::read>();
std::cout << "Result:" << std::endl;
for (size_t i = 0; i < N; i++) {
// A[index[0]] = index[0];
int A = i;
// B[index[0]] = N - index[0];
int B = N - i;
// B[index[0]] = B[index[0]] + A[index[0]];
B = B + A;
// C[index[0]] = B[index[0]] - index[0];
int C = B - i;
// D[index[0]] = 1;
int D = 1;
// D[index[0]] = D[index[0]] + B[index[0]] - C[index[0]];
D = D + B - C;
int Expected = D;
if (FinalD[i] != D) {
std::cout << "Wrong value for element " << i
<< " Expected: " << Expected << " Got: " << FinalD[i]
<< std::endl;
return -1;
}
}
std::cout << "Good computation!" << std::endl;
return 0;
}
int main() {
host_selector hostSelector;
cpu_selector CPUSelector;
gpu_selector GPUSelector;
int Result = -1;
try {
queue MyQueue1(hostSelector);
queue MyQueue2(hostSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch(cl::sycl::runtime_error &) {
std::cout << "Skipping host and host" << std::endl;
}
try {
queue MyQueue1(hostSelector);
queue MyQueue2(CPUSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch(cl::sycl::runtime_error &) {
std::cout << "Skipping host and CPU" << std::endl;
}
try {
queue MyQueue1(CPUSelector);
queue MyQueue2(CPUSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch(cl::sycl::runtime_error &) {
std::cout << "Skipping CPU and CPU" << std::endl;
}
try {
queue MyQueue1(CPUSelector);
queue MyQueue2(GPUSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch(cl::sycl::runtime_error &) {
std::cout << "Skipping CPU and GPU" << std::endl;
}
try {
queue MyQueue1(hostSelector);
queue MyQueue2(GPUSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch(cl::sycl::runtime_error &) {
std::cout << "Skipping host and GPU" << std::endl;
}
try {
queue MyQueue1(GPUSelector);
queue MyQueue2(GPUSelector);
Result &= multidevice_test(MyQueue1, MyQueue2);
} catch (cl::sycl::runtime_error &) {
std::cout << "Skipping GPU and GPU" << std::endl;
}
return Result;
}