This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerkle_tree.cpp
425 lines (358 loc) · 13.8 KB
/
merkle_tree.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <merkle_tree.hpp>
uint64_t
merklize_approach_1(sycl::queue& q,
const sycl::ulong* leaves,
sycl::ulong* const intermediates,
const size_t leaf_count,
const size_t wg_size,
const sycl::ulong4* mds,
const sycl::ulong4* ark1,
const sycl::ulong4* ark2)
{
// ensure only working with powers of 2 -many leaves
assert((leaf_count & (leaf_count - 1)) == 0);
// checking that requested work group size for first
// phase of kernel dispatch is valid
//
// for next rounds of kernel dispatches, work group
// size will be adapted when required !
assert(wg_size <= (leaf_count >> 1));
const size_t output_offset = leaf_count >> 1;
// this is first phase of kernel dispatch, where I compute
// ( in parallel ) all intermediate nodes just above leaves of tree
sycl::event evt_0 = q.submit([&](sycl::handler& h) {
h.parallel_for<class kernelMerklizeRescuePrimeApproach1Phase0>(
sycl::nd_range<1>{ sycl::range<1>{ output_offset },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(leaves + idx * (DIGEST_SIZE >> 1),
intermediates + (output_offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
});
});
// for computing all remaining intermediate nodes, we'll need to
// dispatch `rounds` -many kernels, where each round is data dependent
// on just previous one
const size_t rounds =
static_cast<size_t>(sycl::log2(static_cast<double>(leaf_count >> 1)));
std::vector<sycl::event> evts;
evts.reserve(rounds);
for (size_t r = 0; r < rounds; r++) {
sycl::event evt = q.submit([&](sycl::handler& h) {
if (r == 0) {
h.depends_on(evt_0);
} else {
h.depends_on(evts.at(r - 1));
}
// these many intermediate nodes to be computed during this
// kernel dispatch round
const size_t offset = leaf_count >> (r + 2);
h.parallel_for<class kernelMerklizeRescuePrimeApproach1Phase1>(
sycl::nd_range<1>{
sycl::range<1>{ offset },
sycl::range<1>{ offset < wg_size ? offset : wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(intermediates + (offset << 1) * DIGEST_SIZE +
idx * (DIGEST_SIZE >> 1),
intermediates + (offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
});
});
evts.push_back(evt);
}
evts.at(rounds - 1).wait();
// calculate sum of dispatched kernel execution times
uint64_t ts = 0;
uint64_t start =
evt_0.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_0.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
for (sycl::event evt : evts) {
uint64_t start =
evt.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
}
return ts;
}
uint64_t
merklize_approach_2(sycl::queue& q,
const sycl::ulong* leaves,
sycl::ulong* const intermediates,
const size_t leaf_count,
const size_t wg_size,
const sycl::ulong4* mds,
const sycl::ulong4* ark1,
const sycl::ulong4* ark2)
{
// ensure only working with powers of 2 -many leaves
assert((leaf_count & (leaf_count - 1)) == 0);
assert(wg_size <= (leaf_count >> 2));
// so that only last half of tree is touched, where
// intermediate nodes just above leaves are stored
const size_t output_offset = leaf_count >> 1;
sycl::event evt_0 = q.submit([&](sycl::handler& h) {
h.parallel_for<class kernelMerklizeRescuePrimeApproach2Phase0>(
sycl::nd_range<1>{ sycl::range<1>{ leaf_count >> 1 },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(leaves + idx * (DIGEST_SIZE >> 1),
intermediates + (output_offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
});
});
const size_t log_wg_size =
static_cast<size_t>(sycl::log2<float>(static_cast<float>(wg_size)));
const size_t rounds =
(leaf_count >> 2) == wg_size
? 1
: (leaf_count >> 3) == wg_size
? (static_cast<size_t>(sycl::log2<float>(
static_cast<float>((leaf_count >> 2) >> log_wg_size))) +
1)
: static_cast<size_t>(sycl::log2<float>(
static_cast<float>((leaf_count >> 2) >> log_wg_size)));
std::vector<sycl::event> evts;
evts.reserve(rounds);
size_t offset = (leaf_count >> 2);
for (size_t r = 0; r < rounds; r++) {
sycl::event evt = q.submit([&](sycl::handler& h) {
if (r == 0) {
h.depends_on(evt_0);
} else {
h.depends_on(evts.at(r - 1));
}
h.parallel_for<class kernelMerklizeRescuePrimeApproach2Phase1>(
sycl::nd_range<1>{
sycl::range<1>{ offset },
sycl::range<1>{ offset < wg_size ? offset : wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(intermediates + (offset << 1) * DIGEST_SIZE +
idx * (DIGEST_SIZE >> 1),
intermediates + (offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
size_t round = 1;
const size_t loc_size = it.get_local_range(0);
sycl::group<1> grp = it.get_group();
sycl::group_barrier(grp, sycl::memory_scope_device);
while ((1 << (round - 1)) < loc_size) {
if (idx % (1 << round) == 0) {
merge(intermediates + (offset >> (round - 1)) * DIGEST_SIZE +
(idx >> round) * (DIGEST_SIZE >> 1),
intermediates +
((offset >> round) + (idx >> round)) * DIGEST_SIZE,
mds,
ark1,
ark2);
}
sycl::group_barrier(grp, sycl::memory_scope_device);
round++;
}
});
});
evts.push_back(evt);
offset >>= (log_wg_size + 1);
offset = offset == 0 ? 1 : offset;
}
evts.at(rounds - 1).wait();
// calculate sum of dispatched kernel execution times
uint64_t ts = 0;
uint64_t start =
evt_0.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_0.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
for (sycl::event evt : evts) {
uint64_t start =
evt.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
}
return ts;
}
uint64_t
merklize_approach_3(sycl::queue& q,
const sycl::ulong* leaves,
sycl::ulong* const intermediates,
const size_t leaf_count,
const size_t wg_size,
const sycl::ulong4* mds,
const sycl::ulong4* ark1,
const sycl::ulong4* ark2)
{
// ensure only working with powers of 2 -many leaves
assert((leaf_count & (leaf_count - 1)) == 0);
// checking that requested work group size for first
// phase of kernel dispatch is valid
//
// for next rounds of kernel dispatches, work group
// size will be adapted when required !
assert(wg_size <= (leaf_count >> 1));
const size_t output_offset = leaf_count >> 1;
// this is first phase of kernel dispatch, where I compute
// ( in parallel ) all intermediate nodes just above leaves of tree
sycl::event evt_0 = q.submit([&](sycl::handler& h) {
if (wg_size >= 12) {
scratch_mem_1d_t mds_loc =
scratch_mem_1d_t{ sycl::range<1>{ STATE_WIDTH * 3 }, h };
scratch_mem_1d_t ark1_loc =
scratch_mem_1d_t{ sycl::range<1>{ NUM_ROUNDS * 3 }, h };
scratch_mem_1d_t ark2_loc =
scratch_mem_1d_t{ sycl::range<1>{ NUM_ROUNDS * 3 }, h };
h.parallel_for<
class kernelMerklizeRescuePrimeApproach3Phase0UsingScratchPad>(
sycl::nd_range<1>{ sycl::range<1>{ output_offset },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
const size_t loc_idx = it.get_local_linear_id();
sycl::group grp = it.get_group();
if (loc_idx % 12 == loc_idx) {
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 12 + loc_idx;
mds_loc[k] = mds[k];
}
}
if (loc_idx % 7 == loc_idx) {
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 7 + loc_idx;
ark1_loc[k] = ark1[k];
}
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 7 + loc_idx;
ark2_loc[k] = ark2[k];
}
}
// ensure all rescue prime constants written into local memory
// and visible to all work-items in work-group
sycl::group_barrier(grp, sycl::memory_scope::work_group);
merge(leaves + idx * (DIGEST_SIZE >> 1),
intermediates + (output_offset + idx) * DIGEST_SIZE,
mds_loc,
ark1_loc,
ark2_loc);
});
} else {
h.parallel_for<
class kernelMerklizeRescuePrimeApproach3Phase0UsingGlobalMem>(
sycl::nd_range<1>{ sycl::range<1>{ output_offset },
sycl::range<1>{ wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(leaves + idx * (DIGEST_SIZE >> 1),
intermediates + (output_offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
});
}
});
// for computing all remaining intermediate nodes, we'll need to
// dispatch `rounds` -many kernels, where each round is data dependent
// on just previous one
const size_t rounds =
static_cast<size_t>(sycl::log2(static_cast<double>(leaf_count >> 1)));
std::vector<sycl::event> evts;
evts.reserve(rounds);
for (size_t r = 0; r < rounds; r++) {
sycl::event evt = q.submit([&](sycl::handler& h) {
if (r == 0) {
h.depends_on(evt_0);
} else {
h.depends_on(evts.at(r - 1));
}
// these many intermediate nodes to be computed during this
// kernel dispatch round
const size_t offset = leaf_count >> (r + 2);
const size_t new_wg_size = offset < wg_size ? offset : wg_size;
if (new_wg_size >= 12) {
scratch_mem_1d_t mds_loc =
scratch_mem_1d_t{ sycl::range<1>{ STATE_WIDTH * 3 }, h };
scratch_mem_1d_t ark1_loc =
scratch_mem_1d_t{ sycl::range<1>{ NUM_ROUNDS * 3 }, h };
scratch_mem_1d_t ark2_loc =
scratch_mem_1d_t{ sycl::range<1>{ NUM_ROUNDS * 3 }, h };
h.parallel_for<
class kernelMerklizeRescuePrimeApproach3Phase1UsingScratchPad>(
sycl::nd_range<1>{ sycl::range<1>{ offset },
sycl::range<1>{ new_wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
const size_t loc_idx = it.get_local_linear_id();
sycl::group grp = it.get_group();
if (loc_idx % 12 == loc_idx) {
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 12 + loc_idx;
mds_loc[k] = mds[k];
}
}
if (loc_idx % 7 == loc_idx) {
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 7 + loc_idx;
ark1_loc[k] = ark1[k];
}
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 7 + loc_idx;
ark2_loc[k] = ark2[k];
}
}
// ensure all rescue prime constants written into local memory
// and visible to all work-items in work-group
sycl::group_barrier(grp, sycl::memory_scope::work_group);
merge(intermediates + (offset << 1) * DIGEST_SIZE +
idx * (DIGEST_SIZE >> 1),
intermediates + (offset + idx) * DIGEST_SIZE,
mds_loc,
ark1_loc,
ark2_loc);
});
} else {
h.parallel_for<
class kernelMerklizeRescuePrimeApproach3Phase1UsingGlobalMem>(
sycl::nd_range<1>{ sycl::range<1>{ offset },
sycl::range<1>{ new_wg_size } },
[=](sycl::nd_item<1> it) {
const size_t idx = it.get_global_linear_id();
merge(intermediates + (offset << 1) * DIGEST_SIZE +
idx * (DIGEST_SIZE >> 1),
intermediates + (offset + idx) * DIGEST_SIZE,
mds,
ark1,
ark2);
});
}
});
evts.push_back(evt);
}
evts.at(rounds - 1).wait();
// calculate sum of dispatched kernel execution times
uint64_t ts = 0;
uint64_t start =
evt_0.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_0.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
for (sycl::event evt : evts) {
uint64_t start =
evt.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt.get_profiling_info<sycl::info::event_profiling::command_end>();
ts += (end - start);
}
return ts;
}