-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathvec-buffer-host.cpp
More file actions
206 lines (175 loc) · 6.21 KB
/
vec-buffer-host.cpp
File metadata and controls
206 lines (175 loc) · 6.21 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//==============================================================
// Copyright © 2022 Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#include <array>
#include <chrono>
#include <iostream>
#include <sycl/sycl.hpp>
#include "align.hpp"
template <typename T> using VectorAllocator = AlignedAllocator<T>;
template <typename T> using AlignedVector = std::vector<T, VectorAllocator<T>>;
constexpr size_t array_size = (1 << 15);
template <typename AccT> auto get_accessor_pointer(const AccT &acc) {
return acc.template get_multi_ptr<sycl::access::decorated::no>().get();
}
// Snippet1 Begin
int VectorAdd0(sycl::queue &q, AlignedVector<int> &a, AlignedVector<int> &b,
AlignedVector<int> &sum, int iter) {
sycl::range num_items{a.size()};
const sycl::property_list props = {sycl::property::buffer::use_host_ptr()};
for (int i = 0; i < iter; i++) {
sycl::buffer a_buf(a, props);
sycl::buffer b_buf(b, props);
sycl::buffer sum_buf(sum.data(), num_items, props);
{
sycl::host_accessor a_host_acc(a_buf);
std::cout << "add0: buff memory address =" << a_host_acc.get_pointer()
<< "\n";
std::cout << "add0: address of vector a = " << a.data() << "\n";
}
q.submit([&](auto &h) {
// Input accessors
sycl::accessor a_acc(a_buf, h, sycl::read_only);
sycl::accessor b_acc(b_buf, h, sycl::read_only);
// Output accessor
sycl::accessor sum_acc(sum_buf, h, sycl::write_only, sycl::no_init);
sycl::stream out(1024 * 1024, 1 * 128, h);
h.parallel_for(num_items, [=](auto i) {
if (i[0] == 0)
out << "add0: dev addr = " << get_accessor_pointer(a_acc) << "\n";
sum_acc[i] = a_acc[i] + b_acc[i];
});
});
}
q.wait();
return (0);
}
// Snippet1 End
// Snippet2 Begin
int VectorAdd1(sycl::queue &q, const AlignedVector<int> &a,
const AlignedVector<int> &b, AlignedVector<int> &sum, int iter) {
sycl::range num_items{a.size()};
const sycl::property_list props = {sycl::property::buffer::use_host_ptr()};
for (int i = 0; i < iter; i++) {
sycl::buffer a_buf(a, props);
sycl::buffer b_buf(b, props);
sycl::buffer sum_buf(sum.data(), num_items, props);
{
sycl::host_accessor a_host_acc(a_buf);
std::cout << "add1: buff memory address =" << a_host_acc.get_pointer()
<< "\n";
std::cout << "add1: address of vector aa = " << a.data() << "\n";
}
q.submit([&](auto &h) {
// Input accessors
sycl::accessor a_acc(a_buf, h, sycl::read_only);
sycl::accessor b_acc(b_buf, h, sycl::read_only);
// Output accessor
sycl::accessor sum_acc(sum_buf, h, sycl::write_only, sycl::no_init);
sycl::stream out(16 * 1024, 16 * 1024, h);
h.parallel_for(num_items, [=](auto i) {
if (i[0] == 0)
out << "add1: dev addr = " << get_accessor_pointer(a_acc) << "\n";
sum_acc[i] = a_acc[i] + b_acc[i];
});
});
}
q.wait();
return (0);
}
// Snippet2 End
// Snippet3 Begin
int VectorAdd2(sycl::queue &q, AlignedVector<int> &a, AlignedVector<int> &b,
AlignedVector<int> &sum, int iter) {
sycl::range num_items{a.size()};
const sycl::property_list props = {sycl::property::buffer::use_host_ptr()};
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < iter; i++) {
sycl::buffer a_buf(a, props);
sycl::buffer b_buf(b, props);
sycl::buffer sum_buf(sum.data(), num_items, props);
q.submit([&](auto &h) {
// Input accessors
sycl::accessor a_acc(a_buf, h, sycl::read_only);
sycl::accessor b_acc(b_buf, h, sycl::read_only);
// Output accessor
sycl::accessor sum_acc(sum_buf, h, sycl::write_only, sycl::no_init);
h.parallel_for(num_items,
[=](auto i) { sum_acc[i] = a_acc[i] + b_acc[i]; });
});
}
q.wait();
auto end = std::chrono::steady_clock::now();
std::cout << "Vector add2 completed on device - took "
<< (end - start).count() << " u-secs\n";
return ((end - start).count());
}
// Snippet3 End
// Snippet4 Begin
int VectorAdd3(sycl::queue &q, const AlignedVector<int> &a,
const AlignedVector<int> &b, AlignedVector<int> &sum, int iter) {
sycl::range num_items{a.size()};
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < iter; i++) {
sycl::buffer a_buf(a);
sycl::buffer b_buf(b);
sycl::buffer sum_buf(sum.data(), num_items);
auto e = q.submit([&](auto &h) {
// Input accessors
sycl::accessor a_acc(a_buf, h, sycl::read_only);
sycl::accessor b_acc(b_buf, h, sycl::read_only);
// Output accessor
sycl::accessor sum_acc(sum_buf, h, sycl::write_only, sycl::no_init);
h.parallel_for(num_items,
[=](auto i) { sum_acc[i] = a_acc[i] + b_acc[i]; });
});
}
q.wait();
auto end = std::chrono::steady_clock::now();
std::cout << "Vector add3 completed on device - took "
<< (end - start).count() << " u-secs\n";
return ((end - start).count());
}
// Snippet4 End
void InitializeArray(AlignedVector<int> &a) {
for (size_t i = 0; i < a.size(); i++)
a[i] = i;
}
void Initialize(AlignedVector<int> &a) {
for (size_t i = 0; i < a.size(); i++)
a[i] = 0;
}
int main() {
sycl::queue q(sycl::default_selector_v);
VectorAllocator<int> alloc;
AlignedVector<int> a(array_size, alloc);
AlignedVector<int> b(array_size, alloc);
AlignedVector<int> sum(array_size, alloc);
InitializeArray(a);
InitializeArray(b);
std::cout << "Running on device: "
<< q.get_device().get_info<sycl::info::device::name>() << "\n";
std::cout << "Vector size: " << a.size() << "\n";
// jit the code
VectorAdd1(q, a, b, sum, 1);
// check results
Initialize(sum);
VectorAdd1(q, a, b, sum, 1);
for (size_t i = 0; i < a.size(); i++)
if (sum[i] != static_cast<int>(2 * i)) {
std::cout << "add1 Did not match\n";
}
Initialize(sum);
VectorAdd0(q, a, b, sum, 1);
for (size_t i = 0; i < a.size(); i++)
if (sum[i] != static_cast<int>(2 * i)) {
std::cout << "add0 Did not match\n";
}
Initialize(sum);
VectorAdd2(q, a, b, sum, 1000);
Initialize(sum);
VectorAdd3(q, a, b, sum, 1000);
return 0;
}