summaryrefslogtreecommitdiff
path: root/modules/sycl/tests/basic_ops.cpp
blob: 4fdd4f6e6e8eafc110d0a5bdb905f0d8d5787120 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <forstio/test/suite.hpp>

#include <cmath>
#include <string>

#include "../c++/lbm.hpp"
#include "skip.hpp"

/**
 * Minimal confirmation that the device layer actually works, rather than
 * merely compiles.
 *
 * Everything else in the suite is host side, so a device allocation that never
 * happened, a copy that moved nothing, or a kernel that silently did not run
 * would all look exactly like success. Each test here therefore round trips a
 * value: write it somewhere, read it back from the other side, compare.
 */

namespace {
namespace sch {
using namespace kel::lbm::sch;
using T = Float64;
}

using kel::lbm::data;
using Dev = kel::lbm::encode::Sycl<kel::lbm::enc::Native>;

SAW_TEST("Sycl / device allocation and FixedArray round trip"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	acpp::sycl::queue q;
	lbm::device dev;

	using Sch = sch::FixedArray<sch::UInt64, 4u>;

	data<Sch> host;
	for(uint64_t i = 0u; i < 4u; ++i){
		host.at({i}).set(i * 10u + 1u);
	}

	data<Sch, Dev> device_data{dev.get_handle()};

	auto eov = dev.copy_to_device(host, device_data);
	SAW_EXPECT(eov.is_value(), "copy to device failed");

	// Clear the host copy so a no-op read back cannot pass by accident.
	data<Sch> back;
	for(uint64_t i = 0u; i < 4u; ++i){
		back.at({i}).set(0u);
	}

	auto eov2 = dev.copy_to_host(device_data, back);
	SAW_EXPECT(eov2.is_value(), "copy to host failed");
	dev.get_handle().wait();

	for(uint64_t i = 0u; i < 4u; ++i){
		SAW_EXPECT(back.at({i}).get() == i * 10u + 1u,
			std::string{"round trip lost element "} + std::to_string(i)
			+ ": " + std::to_string(back.at({i}).get()));
	}
}

SAW_TEST("Sycl / a kernel actually runs and writes"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	/**
	 * The one that catches a kernel which never launched: the host never
	 * writes these values, so they can only come from the device.
	 */
	lbm::device dev;
	auto& q = dev.get_handle();

	using Sch = sch::FixedArray<sch::UInt64, 8u>;

	data<Sch> host;
	for(uint64_t i = 0u; i < 8u; ++i){
		host.at({i}).set(0u);
	}

	data<Sch, Dev> device_data{q};
	auto eov = dev.copy_to_device(host, device_data);
	SAW_EXPECT(eov.is_value(), "copy to device failed");
	q.wait();

	data<sch::Ptr<Sch>, Dev> view{device_data};

	q.submit([&](acpp::sycl::handler& h){
		h.parallel_for(acpp::sycl::range<1u>{8u}, [=](acpp::sycl::id<1u> idx){
			data<sch::FixedArray<sch::UInt64,1u>> index;
			index.at({{0u}}).set(idx[0]);
			view.at(index).set(idx[0] * 7u + 3u);
		});
	}).wait();

	auto eov2 = dev.copy_to_host(device_data, host);
	SAW_EXPECT(eov2.is_value(), "copy to host failed");
	q.wait();

	for(uint64_t i = 0u; i < 8u; ++i){
		SAW_EXPECT(host.at({i}).get() == i * 7u + 3u,
			std::string{"kernel did not write element "} + std::to_string(i)
			+ ": got " + std::to_string(host.at({i}).get()));
	}
}

SAW_TEST("Sycl / dynamic Array round trips with its extent"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	// Array carries a runtime extent, so the device side has to pick it up
	// rather than assume one.
	lbm::device dev;
	auto& q = dev.get_handle();

	using Sch = sch::Array<sch::Float64, 1u>;

	data<Sch> host{data<sch::FixedArray<sch::UInt64,1u>>{{6u}}};
	SAW_EXPECT(host.size().get() == 6u, "host array should be sized from its meta");
	for(uint64_t i = 0u; i < 6u; ++i){
		host.at({i}).set(static_cast<double>(i) * 0.5);
	}

	data<Sch, Dev> device_data{q};
	auto eov = dev.copy_to_device(host, device_data);
	SAW_EXPECT(eov.is_value(), "copy to device failed");
	q.wait();

	data<Sch> back{data<sch::FixedArray<sch::UInt64,1u>>{{6u}}};
	for(uint64_t i = 0u; i < 6u; ++i){
		back.at({i}).set(-1.0);
	}

	auto eov2 = dev.copy_to_host(device_data, back);
	SAW_EXPECT(eov2.is_value(), "copy to host failed");
	q.wait();

	for(uint64_t i = 0u; i < 6u; ++i){
		SAW_EXPECT(back.at({i}).get() == static_cast<double>(i) * 0.5,
			std::string{"array round trip lost element "} + std::to_string(i));
	}
}

SAW_TEST("Sycl / Struct members round trip independently"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	/**
	 * A struct copy walks member by member. If it reused one member's pointer
	 * or skipped one, the others would still look right -- so check each.
	 */
	lbm::device dev;
	auto& q = dev.get_handle();

	using Sch = sch::Struct<
		sch::Member<sch::FixedArray<sch::UInt64,3u>, "counts">,
		sch::Member<sch::FixedArray<sch::Float64,3u>, "weights">
	>;

	data<Sch> host;
	for(uint64_t i = 0u; i < 3u; ++i){
		host.template get<"counts">().at({i}).set(i + 100u);
		host.template get<"weights">().at({i}).set(static_cast<double>(i) + 0.25);
	}

	data<Sch, Dev> device_data{q};
	auto eov = dev.copy_to_device(host, device_data);
	SAW_EXPECT(eov.is_value(), "copy to device failed");
	q.wait();

	data<Sch> back;
	for(uint64_t i = 0u; i < 3u; ++i){
		back.template get<"counts">().at({i}).set(0u);
		back.template get<"weights">().at({i}).set(0.0);
	}

	auto eov2 = dev.copy_to_host(device_data, back);
	SAW_EXPECT(eov2.is_value(), "copy to host failed");
	q.wait();

	for(uint64_t i = 0u; i < 3u; ++i){
		SAW_EXPECT(back.template get<"counts">().at({i}).get() == i + 100u,
			std::string{"counts lost element "} + std::to_string(i));
		SAW_EXPECT(back.template get<"weights">().at({i}).get()
				== static_cast<double>(i) + 0.25,
			std::string{"weights lost element "} + std::to_string(i));
	}
}

SAW_TEST("Sycl / Chunk round trips and is addressable in a kernel"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	// Chunk is the type the whole fluid solver runs on, so it is the one that
	// matters most.
	lbm::device dev;
	auto& q = dev.get_handle();

	constexpr uint64_t nx = 4u;
	constexpr uint64_t ny = 3u;
	using Sch = sch::Chunk<sch::UInt64, 0u, nx, ny>;

	data<Sch> host;
	lbm::iterator<2u>::apply([&](const auto& index){
		host.at(index).set(0u);
	},{},host.get_dims());

	data<Sch, Dev> device_data{q};
	auto eov = dev.copy_to_device(host, device_data);
	SAW_EXPECT(eov.is_value(), "copy to device failed");
	q.wait();

	data<sch::Ptr<Sch>, Dev> view{device_data};

	// Write a position dependent value so a transposed or flattened index
	// shows up as a mismatch rather than a plausible constant.
	q.submit([&](acpp::sycl::handler& h){
		h.parallel_for(acpp::sycl::range<2u>{nx,ny}, [=](acpp::sycl::id<2u> idx){
			data<sch::FixedArray<sch::UInt64,2u>> index;
			index.at({0u}).set(idx[0]);
			index.at({1u}).set(idx[1]);
			view.at(index).set(idx[0] * 1000u + idx[1]);
		});
	}).wait();

	auto eov2 = dev.copy_to_host(device_data, host);
	SAW_EXPECT(eov2.is_value(), "copy to host failed");
	q.wait();

	uint64_t checked = 0u;
	lbm::iterator<2u>::apply([&](const auto& index){
		auto x = index.at({0u}).get();
		auto y = index.at({1u}).get();
		SAW_EXPECT(host.at(index).get() == x * 1000u + y,
			std::string{"chunk cell ("} + std::to_string(x) + "," + std::to_string(y)
			+ ") = " + std::to_string(host.at(index).get()));
		++checked;
	},{},host.get_dims());

	SAW_EXPECT(checked == nx * ny, "expected to visit every cell");
}

SAW_TEST("Sycl / make_view reaches struct members on device"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	/**
	 * make_view recurses into a struct, building a borrowed view per member.
	 * Confirm a kernel reached through it writes the member it was aimed at.
	 */
	lbm::device dev;
	auto& q = dev.get_handle();

	using Sch = sch::Struct<
		sch::Member<sch::FixedArray<sch::UInt64,4u>, "a">,
		sch::Member<sch::FixedArray<sch::UInt64,4u>, "b">
	>;

	data<Sch> host;
	for(uint64_t i = 0u; i < 4u; ++i){
		host.template get<"a">().at({i}).set(0u);
		host.template get<"b">().at({i}).set(9u);
	}

	data<Sch, Dev> device_data{q};
	SAW_EXPECT(dev.copy_to_device(host, device_data).is_value(), "copy to device failed");
	q.wait();

	auto view = lbm::make_view(device_data);

	q.submit([&](acpp::sycl::handler& h){
		h.parallel_for(acpp::sycl::range<1u>{4u}, [=](acpp::sycl::id<1u> idx){
			data<sch::FixedArray<sch::UInt64,1u>> index;
			index.at({{0u}}).set(idx[0]);
			view.template get<"a">().at(index).set(idx[0] + 50u);
		});
	}).wait();

	SAW_EXPECT(dev.copy_to_host(device_data, host).is_value(), "copy to host failed");
	q.wait();

	for(uint64_t i = 0u; i < 4u; ++i){
		SAW_EXPECT(host.template get<"a">().at({i}).get() == i + 50u,
			std::string{"member a not written at "} + std::to_string(i));
		// The untouched member must be exactly as it was.
		SAW_EXPECT(host.template get<"b">().at({i}).get() == 9u,
			std::string{"member b was clobbered at "} + std::to_string(i));
	}
}

SAW_TEST("Sycl / arithmetic on data inside a kernel"){
	using namespace kel;

	KEL_REQUIRE_SYCL();

	/**
	 * The data class carries operators; confirm they behave on device rather
	 * than only in host tests.
	 */
	lbm::device dev;
	auto& q = dev.get_handle();

	using Sch = sch::FixedArray<sch::Float64, 5u>;

	data<Sch> host;
	for(uint64_t i = 0u; i < 5u; ++i){
		host.at({i}).set(static_cast<double>(i));
	}

	data<Sch, Dev> device_data{q};
	SAW_EXPECT(dev.copy_to_device(host, device_data).is_value(), "copy to device failed");
	q.wait();

	data<sch::Ptr<Sch>, Dev> view{device_data};

	q.submit([&](acpp::sycl::handler& h){
		h.parallel_for(acpp::sycl::range<1u>{5u}, [=](acpp::sycl::id<1u> idx){
			data<sch::FixedArray<sch::UInt64,1u>> index;
			index.at({{0u}}).set(idx[0]);

			auto& v = view.at(index);
			data<sch::Float64> two;
			two.set(2.0);
			data<sch::Float64> one;
			one.set(1.0);

			v = v * two + one;
		});
	}).wait();

	SAW_EXPECT(dev.copy_to_host(device_data, host).is_value(), "copy to host failed");
	q.wait();

	for(uint64_t i = 0u; i < 5u; ++i){
		auto expect = static_cast<double>(i) * 2.0 + 1.0;
		SAW_EXPECT(host.at({i}).get() == expect,
			std::string{"arithmetic wrong at "} + std::to_string(i)
			+ ": " + std::to_string(host.at({i}).get())
			+ " expected " + std::to_string(expect));
	}
}
}