-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector-add.cpp
81 lines (65 loc) · 2.07 KB
/
vector-add.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
#include <algorithm>
#include <iostream>
#include <sycl/sycl.hpp>
#include "util.hpp"
using namespace sycl;
constexpr size_t N = 1024 * 1024;
void vector_add(const std::vector<float> &a, const std::vector<float> &b, std::vector<float> &c) {
for (int i = 0; i < N; ++i) {
c[i] = a[i] + b[i];
}
}
void vector_add(
queue &q,
buffer<float, 1> &a_buf, buffer<float, 1> &b_buf, buffer<float, 1> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor A{a_buf, h, read_only};
accessor B{b_buf, h, read_only};
accessor C{c_buf, h, write_only};
// BEGIN CODE SNIP
h.parallel_for(range{N}, [=](id<1> idx) {
C[idx] = A[idx] + B[idx];
});
// END CODE SNIP
});
}
void test_performance() {
std::vector<float> a(N), b(N), c(N);
std::cout << "CPU single core: ";
benchmark_func([&] { vector_add(a, b, c); });
// wrap buffer lifecycle
{
queue q{cpu_selector_v};
buffer<float, 1> a_buf{a}, b_buf{b}, c_buf{c};
std::cout << "CPU SYCL: ";
benchmark_sycl_kernel([&](queue &q) { vector_add(q, a_buf, b_buf, c_buf); }, q);
}
// wrap buffer lifecycle
{
queue q{gpu_selector_by_cu};
buffer<float, 1> a_buf{a}, b_buf{b}, c_buf{c};
std::cout << "GPU SYCL: ";
benchmark_sycl_kernel([&](queue &q) { vector_add(q, a_buf, b_buf, c_buf); }, q);
}
}
void test_acc() {
// Initialize input and output memory on the host
std::vector<float> a(N), b(N), c(N);
std::fill(a.begin(), a.end(), 1);
std::fill(b.begin(), b.end(), 2);
std::fill(c.begin(), c.end(), 0);
queue gpu_q{gpu_selector_by_cu};
// wrap buffer lifecycle
{
buffer<float, 1> a_buf{a}, b_buf{b}, c_buf{c};
vector_add(gpu_q, a_buf, b_buf, c_buf);
}
// Check that all outputs match expected value
bool passed = std::all_of(c.begin(), c.end(), [](float i) { return i == 3; });
std::cout << (passed ? "SUCCESS" : "FAILURE") << std::endl;
}
int main() {
test_acc();
test_performance();
}