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
/
bench_rescue_prime.cpp
253 lines (220 loc) · 8.8 KB
/
bench_rescue_prime.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
#include "bench_rescue_prime.hpp"
#include "rescue_prime.hpp"
uint64_t
benchmark_hash_elements(sycl::queue& q,
const uint32_t dim,
const uint32_t wg_size,
const uint32_t itr_count)
{
sycl::ulong* elements =
static_cast<sycl::ulong*>(sycl::malloc_device(sizeof(sycl::ulong) * 8, q));
sycl::ulong* hashes = static_cast<sycl::ulong*>(
sycl::malloc_device(sizeof(sycl::ulong) * dim * dim * DIGEST_SIZE, q));
sycl::ulong4* mds_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* mds_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
prepare_mds(mds_h);
prepare_ark1(ark1_h);
prepare_ark2(ark2_h);
sycl::event evt_0 = q.single_task([=]() {
for (uint8_t i = 0; i < 8; i++) {
*(elements + i) = i;
}
});
sycl::event evt_1 =
q.memcpy(mds_d, mds_h, sizeof(sycl::ulong4) * STATE_WIDTH * 3);
sycl::event evt_2 =
q.memcpy(ark1_d, ark1_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_3 =
q.memcpy(ark2_d, ark2_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_4 = q.submit([&](sycl::handler& h) {
h.depends_on({ evt_0, evt_1, evt_2, evt_3 });
h.parallel_for<class kernelBenchmarkRescuePrimeHash>(
sycl::nd_range<2>{ sycl::range<2>{ dim, dim },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
const size_t idx = it.get_global_linear_id();
for (uint32_t i = 0; i < itr_count; i++) {
hash_elements(elements, 8, hashes + idx * 4, mds_d, ark1_d, ark2_d);
}
});
});
evt_4.wait();
uint64_t start =
evt_4.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_4.get_profiling_info<sycl::info::event_profiling::command_end>();
sycl::free(elements, q);
sycl::free(hashes, q);
sycl::free(mds_h, q);
sycl::free(ark1_h, q);
sycl::free(ark2_h, q);
sycl::free(mds_d, q);
sycl::free(ark1_d, q);
sycl::free(ark2_d, q);
return (end - start);
}
uint64_t
benchmark_merge(sycl::queue& q,
const uint32_t dim,
const uint32_t wg_size,
const uint32_t itr_count)
{
sycl::ulong* input_hashes =
static_cast<sycl::ulong*>(sycl::malloc_device(sizeof(sycl::ulong) * 8, q));
sycl::ulong* merged_hashes = static_cast<sycl::ulong*>(
sycl::malloc_device(sizeof(sycl::ulong) * dim * dim * DIGEST_SIZE, q));
sycl::ulong4* mds_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* mds_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
prepare_mds(mds_h);
prepare_ark1(ark1_h);
prepare_ark2(ark2_h);
sycl::event evt_0 = q.single_task([=]() {
for (uint8_t i = 0; i < 8; i++) {
*(input_hashes + i) = i;
}
});
sycl::event evt_1 =
q.memcpy(mds_d, mds_h, sizeof(sycl::ulong4) * STATE_WIDTH * 3);
sycl::event evt_2 =
q.memcpy(ark1_d, ark1_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_3 =
q.memcpy(ark2_d, ark2_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_4 = q.submit([&](sycl::handler& h) {
h.depends_on({ evt_0, evt_1, evt_2, evt_3 });
h.parallel_for<class kernelBenchmarkRescuePrimeMergeHashes>(
sycl::nd_range<2>{ sycl::range<2>{ dim, dim },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> it) {
const size_t idx = it.get_global_linear_id();
for (uint32_t i = 0; i < itr_count; i++) {
merge(input_hashes, merged_hashes + idx * 4, mds_d, ark1_d, ark2_d);
}
});
});
evt_4.wait();
uint64_t start =
evt_4.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_4.get_profiling_info<sycl::info::event_profiling::command_end>();
sycl::free(input_hashes, q);
sycl::free(merged_hashes, q);
sycl::free(mds_h, q);
sycl::free(ark1_h, q);
sycl::free(ark2_h, q);
sycl::free(mds_d, q);
sycl::free(ark1_d, q);
sycl::free(ark2_d, q);
return (end - start); // in nanoseconds
}
uint64_t
benchmark_merge_using_scratch_pad(sycl::queue& q,
const uint32_t dim,
const uint32_t wg_size,
const uint32_t itr_count)
{
assert(wg_size >= 12);
sycl::ulong* input_hashes =
static_cast<sycl::ulong*>(sycl::malloc_device(sizeof(sycl::ulong) * 8, q));
sycl::ulong* merged_hashes = static_cast<sycl::ulong*>(
sycl::malloc_device(sizeof(sycl::ulong) * dim * dim * DIGEST_SIZE, q));
sycl::ulong4* mds_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_h = static_cast<sycl::ulong4*>(
sycl::malloc_host(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* mds_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * STATE_WIDTH * 3, q));
sycl::ulong4* ark1_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
sycl::ulong4* ark2_d = static_cast<sycl::ulong4*>(
sycl::malloc_device(sizeof(sycl::ulong4) * NUM_ROUNDS * 3, q));
prepare_mds(mds_h);
prepare_ark1(ark1_h);
prepare_ark2(ark2_h);
sycl::event evt_0 = q.single_task([=]() {
for (uint8_t i = 0; i < 8; i++) {
*(input_hashes + i) = i;
}
});
sycl::event evt_1 =
q.memcpy(mds_d, mds_h, sizeof(sycl::ulong4) * STATE_WIDTH * 3);
sycl::event evt_2 =
q.memcpy(ark1_d, ark1_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_3 =
q.memcpy(ark2_d, ark2_h, sizeof(sycl::ulong4) * NUM_ROUNDS * 3);
sycl::event evt_4 = q.submit([&](sycl::handler& h) {
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.depends_on({ evt_0, evt_1, evt_2, evt_3 });
h.parallel_for<class kernelBenchmarkRescuePrimeMergeHashesUsingScratchPad>(
sycl::nd_range<2>{ sycl::range<2>{ dim, dim },
sycl::range<2>{ 1, wg_size } },
[=](sycl::nd_item<2> 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_d[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_d[k];
}
for (size_t j = 0; j < 3; j++) {
const size_t k = j * 7 + loc_idx;
ark2_loc[k] = ark2_d[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);
for (uint32_t i = 0; i < itr_count; i++) {
merge(
input_hashes, merged_hashes + idx * 4, mds_loc, ark1_loc, ark2_loc);
}
});
});
evt_4.wait();
uint64_t start =
evt_4.get_profiling_info<sycl::info::event_profiling::command_start>();
uint64_t end =
evt_4.get_profiling_info<sycl::info::event_profiling::command_end>();
sycl::free(input_hashes, q);
sycl::free(merged_hashes, q);
sycl::free(mds_h, q);
sycl::free(ark1_h, q);
sycl::free(ark2_h, q);
sycl::free(mds_d, q);
sycl::free(ark1_d, q);
sycl::free(ark2_d, q);
return (end - start); // in nanoseconds
}