-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplebuffer.cxx
71 lines (59 loc) · 1.74 KB
/
simplebuffer.cxx
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
#include <iostream>
#include <chrono>
#include <thread>
#include <CL/sycl.hpp>
using namespace std::chrono_literals;
using namespace sycl;
int main(int argc, char **argv) {
constexpr int N = 16;
int h_a[N];
int i;
std::string arg_device_type;
if (argc < 2) {
arg_device_type = "default";
} else {
arg_device_type = argv[1];
}
queue q;
if (arg_device_type == "cpu") {
q = queue( cpu_selector{} );
} else if (arg_device_type == "gpu") {
q = queue( gpu_selector{} );
} else if (arg_device_type == "default") {
q = queue( default_selector{} );
} else {
std::cout << "Usage: " << argv[0] << " cpu|gpu|default" << std::endl;
return 1;
}
auto dev = q.get_device();
std::string type;
if (dev.is_cpu()) {
type = "CPU ";
} else if (dev.is_gpu()) {
type = "GPU ";
} else if (dev.is_host()) {
type = "HOST ";
} else {
type = "OTHER";
}
std::cout << "[" << type << "] "
<< dev.get_info<info::device::name>()
<< " {" << dev.get_info<info::device::vendor>() << "}"
<< std::endl;
{
// scope for device vector, to force synchronize at the end and allow
// host access without explicit host accessor
buffer<int, 1> d_a_buf(h_a, range<1>(N));
q.submit([&](handler & cgh) {
auto d_a_write = d_a_buf.get_access<access::mode::discard_write>(cgh);
cgh.parallel_for<class FillVector>(range<1>(N), [=](id<1> idx) {
d_a_write[idx[0]] = idx[0]*idx[0];
});
});
}
q.wait();
for (i=0; i<N; i++) {
std::cout << i << ": " << h_a[i] << std::endl;
}
return 0;
}