This repository was archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsaxpy.cc
More file actions
76 lines (62 loc) · 2.07 KB
/
Copy pathsaxpy.cc
File metadata and controls
76 lines (62 loc) · 2.07 KB
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
#include "tutorial.hpp"
const float xval(1);
const float yval(2);
const float zval(2);
const float aval(3);
const float correct = (zval + aval * xval + yval);
int main(int argc, char * argv[])
{
if (argc < 2) {
std::cerr << "Usage: saxpy.x <vector length>" << std::endl;
return argc;
}
size_t length = std::atoi(argv[1]);
std::cout << "SAXPY with " << length << " elements" << std::endl;
// host data
std::vector<float> h_X(length,xval);
std::vector<float> h_Y(length,yval);
std::vector<float> h_Z(length,zval);
sycl::queue q(sycl::default_selector{});
try {
const float A(aval);
sycl::buffer<float,1> d_X { h_X.data(), sycl::range<1>(h_X.size()) };
sycl::buffer<float,1> d_Y { h_Y.data(), sycl::range<1>(h_Y.size()) };
sycl::buffer<float,1> d_Z { h_Z.data(), sycl::range<1>(h_Z.size()) };
q.submit([&](sycl::handler& h) {
#if 1
auto X = d_X.get_access<sycl::access::mode::read>(h);
auto Y = d_Y.get_access<sycl::access::mode::read>(h);
auto Z = d_Z.get_access<sycl::access::mode::read_write>(h);
#else
sycl::accessor X(d_X,h,sycl::read_only);
sycl::accessor Y(d_Y,h,sycl::read_only);
sycl::accessor Z(d_Z,h,sycl::read_write);
#endif
h.parallel_for<class axpy>( sycl::range<1>{length}, [=] (sycl::id<1> it) {
const size_t i = it[0];
Z[i] += A * X[i] + Y[i];
});
});
q.wait();
}
catch (sycl::exception & e) {
std::cout << e.what() << std::endl;
return 1;
}
// check for correctness
size_t errors(0);
for (size_t i=0; i<length; ++i) {
if ( std::abs(h_Z[i] - correct) > FLT_MIN) {
++errors;
}
}
if (errors) {
std::cerr << "There were " << errors << " errors!" << std::endl;
for (size_t i=0; i<length; ++i) {
std::cout << i << "," << h_Z[i] << "\n";
}
return 1;
}
std::cout << "Program completed without error." << std::endl;
return 0;
}