Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
3d_caching_implementations.h
Go to the documentation of this file.
1
2#pragma once
3
4static void
5prefetch_stencil_elems_to_smem_and_compute_stencil_ops(const int curr_kernel)
6{
7 printf("extern __shared__ AcReal smem[];");
8 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
9 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
10 printf("const int sid = threadIdx.x + "
11 "threadIdx.y * blockDim.x;");
12
13 printf("const int3 baseIdx = (int3){"
14 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
15 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
16 "threadIdx.z + blockIdx.z * blockDim.z + start.z - "
17 "(STENCIL_DEPTH-1)/2};");
18
19 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
20 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
21 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
22
23 const bool rolling_cache = true;
24 if (rolling_cache) {
25 // 2.5D blocking with smem
26
27 // Fetch from gmem
28 printf("if (%d == 0 || threadIdx.z == blockDim.z - 1) {", depth);
29 printf("for (int curr = sid; curr < sx * sy;"
30 "curr += blockDim.x * blockDim.y) {");
31 printf("const int i = curr %% sx;");
32 printf("const int j = curr / sx;");
33 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
34 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
35 printf("if (baseIdx.z + (%d) >= end.z + (STENCIL_DEPTH-1)/2){ break; }",
36 depth);
37 printf("smem[i + j * sx + ((threadIdx.z+%d)%%blockDim.z) * sx * sy] = ",
38 depth);
39 printf("vba.in[%d]"
40 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
41 field, depth);
42 printf("}");
43 printf("}");
44 printf("__syncthreads();");
45 }
46 else {
47 // 2D blocking with smem
48 printf("for (int curr = sid; curr < sx * sy;"
49 "curr += blockDim.x * blockDim.y) {");
50 printf("const int i = curr %% sx;");
51 printf("const int j = curr / sx;");
52 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
53 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
54 printf("if (baseIdx.z + (%d) >= end.z + (STENCIL_DEPTH-1)/2){ break; }",
55 depth);
56 printf("smem[i + j * sx + ((threadIdx.z+%d)%%blockDim.z) * sx * sy] = ",
57 depth);
58 printf("vba.in[%d]"
59 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
60 field, depth);
61 printf("}");
62 printf("__syncthreads();");
63 }
64
65 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
66 for (int width = 0; width < STENCIL_WIDTH; ++width) {
67 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
68
69 // Skip if the stencil is not used
70 if (!stencils_accessed[curr_kernel][field][stencil])
71 continue;
72
73 if (stencils[stencil][depth][height][width]) {
74 if (!stencil_initialized[field][stencil]) {
75 printf("auto f%d_s%d = ", field, stencil);
76 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
77 width);
78 printf("%s(smem[(threadIdx.x + %d) + "
79 "(threadIdx.y + %d) * sx + "
80 "((threadIdx.z+%d)%%blockDim.z) * sx * sy]);",
81 stencil_unary_ops[stencil], width, height, depth);
82
83 stencil_initialized[field][stencil] = 1;
84 }
85 else {
86 printf("f%d_s%d = ", field, stencil);
87 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
88 stencil);
89 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
90 width);
91 printf("%s(smem[(threadIdx.x + %d) + "
92 "(threadIdx.y + %d) * sx + "
93 "((threadIdx.z+%d)%%blockDim.z) * sx * sy])",
94 stencil_unary_ops[stencil], width, height, depth);
95 printf(");");
96 }
97 }
98 }
99 }
100 }
101 printf("__syncthreads();");
102 }
103 }
104}
105
107static void
108prefetch_stencil_elems_to_smem_3d_and_compute_stencil_ops(const int curr_kernel)
109{
110 printf("extern __shared__ AcReal smem[];");
111 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
112 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
113 printf("const int sz = blockDim.z + STENCIL_DEPTH - 1;");
114 printf("const int sid = threadIdx.x + "
115 "threadIdx.y * blockDim.x + "
116 "threadIdx.z * blockDim.x * blockDim.y;");
117
118 printf("const int3 baseIdx = (int3){"
119 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
120 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
121 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
122 printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
123
124 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
125 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
126
127 printf("for (int curr = sid; curr < sx * sy * sz; curr += tpb) {");
128 printf("const int i = curr %% sx;");
129 printf("const int j = (curr %% (sx * sy)) / sx;");
130 printf("const int k = curr / (sx * sy);");
131 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
132 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
133 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
134 printf("smem[i + j * sx + k * sx * sy] = ");
135 printf("__ldg(&");
136 printf("vba.in[%d]", field);
137 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
138 printf(")");
139 printf(";");
140 printf("}");
141 printf("__syncthreads();");
142
143 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
144 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
145 for (int width = 0; width < STENCIL_WIDTH; ++width) {
146 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
147
148 // Skip if the stencil is not used
149 if (!stencils_accessed[curr_kernel][field][stencil])
150 continue;
151
152 if (stencils[stencil][depth][height][width]) {
153 if (!stencil_initialized[field][stencil]) {
154 printf("auto f%d_s%d = ", field, stencil);
155 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
156 width);
157 printf("%s(smem[(threadIdx.x + %d) + "
158 "(threadIdx.y + %d) * sx + "
159 "(threadIdx.z + %d) * sx * sy]);",
160 stencil_unary_ops[stencil], width, height, depth);
161
162 stencil_initialized[field][stencil] = 1;
163 }
164 else {
165 printf("f%d_s%d = ", field, stencil);
166 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
167 stencil);
168 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
169 width);
170 printf("%s(smem[(threadIdx.x + %d) + "
171 "(threadIdx.y + %d) * sx + "
172 "(threadIdx.z + %d) * sx * sy])",
173 stencil_unary_ops[stencil], width, height, depth);
174 printf(");");
175 }
176 }
177 }
178 }
179 }
180 }
181 printf("__syncthreads();");
182 }
183}
184
186static void
187prefetch_stencil_elems_to_smem_4d_and_compute_stencil_ops(const int curr_kernel)
188{
189 printf("extern __shared__ AcReal smem[];");
190 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
191 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
192 printf("const int sz = NUM_ALL_FIELDS;");
193 printf("const int sid = threadIdx.x + "
194 "threadIdx.y * blockDim.x;");
195
196 printf("const int3 baseIdx = (int3){"
197 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
198 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
199 "threadIdx.z + blockIdx.z * blockDim.z + start.z - "
200 "(STENCIL_DEPTH-1)/2};");
201
202 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
203 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
204
205 const bool rolling_cache = true;
206 if (rolling_cache) {
207 // 2.5D blocking with smem
208
209 // Fetch from gmem
210 printf("if (%d == 0 || threadIdx.z == blockDim.z - 1) {", depth);
211 printf("for (int curr = sid; curr < sx * sy * sz;"
212 "curr += blockDim.x * blockDim.y) {");
213 printf("const int i = curr %% sx;");
214 printf("const int j = (curr %% sy) / sx;");
215 printf("const int field = curr / (sx * sy);");
216 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
217 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
218 printf("if (field >= NUM_ALL_FIELDS){ break; }");
219 printf("smem[i + j * sx + (field) * sx * sy] = ");
220 printf("vba.in[field]"
221 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
222 depth);
223 printf("}");
224 printf("}");
225 printf("__syncthreads();");
226 }
227 else {
228 // 2D blocking with smem
229 printf("for (int curr = sid; curr < sx * sy * sz;"
230 "curr += blockDim.x * blockDim.y * blockDim.z) {");
231 printf("const int i = curr %% sx;");
232 printf("const int j = (curr %% sy) / sx;");
233 printf("const int field = curr / (sx * sy);");
234 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
235 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
236 printf("if (field >= NUM_ALL_FIELDS){ break; }");
237 printf("smem[i + j * sx + field * sx * sy] = ");
238 printf("vba.in[field]"
239 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
240 depth);
241 printf("}");
242 printf("__syncthreads();");
243 }
244
245 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
246 for (int width = 0; width < STENCIL_WIDTH; ++width) {
247 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
248 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
249
250 // Skip if the stencil is not used
251 if (!stencils_accessed[curr_kernel][field][stencil])
252 continue;
253
254 if (stencils[stencil][depth][height][width]) {
255 if (!stencil_initialized[field][stencil]) {
256 printf("auto f%d_s%d = ", field, stencil);
257 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
258 width);
259 printf("%s(smem[(threadIdx.x + %d) + "
260 "(threadIdx.y + %d) * sx + "
261 "(%d) * sx * sy]);",
262 stencil_unary_ops[stencil], width, height, field);
263
264 stencil_initialized[field][stencil] = 1;
265 }
266 else {
267 printf("f%d_s%d = ", field, stencil);
268 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
269 stencil);
270 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
271 width);
272 printf("%s(smem[(threadIdx.x + %d) + "
273 "(threadIdx.y + %d) * sx + "
274 "(%d) * sx * sy])",
275 stencil_unary_ops[stencil], width, height, field);
276 printf(");");
277 }
278 }
279 }
280 }
281 }
282 }
283 printf("__syncthreads();");
284 }
285}
287#if IMPLEMENTATION == EXPLICIT_PINGPONG_txw
288 static void
289 refetch_stencil_elems_to_smem_pingpong_txw_and_compute_stencil_ops(
290 const int curr_kernel)
291{
292 printf("extern __shared__ AcReal smem[];");
293 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
294 printf("const int sw = NUM_ALL_FIELDS;");
295 printf("const int sb = 2;");
296 printf("const int sid = threadIdx.x;");
297 printf(
298 "const int3 baseIdx = (int3){"
299 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
300 "threadIdx.y + blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
301 "threadIdx.z + blockIdx.z * blockDim.z + start.z - "
302 "(STENCIL_DEPTH-1)/2};");
303 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
304 printf("for (int curr=sid; curr < sx*sw; curr += blockDim.x) {");
305 printf("const int i = curr %% sx;");
306 printf("const int w = curr / sx;");
307 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
308 printf("if (w >= NUM_ALL_FIELDS) {break;}");
309 printf("smem[i + w * sx + 0 * sx*sw] = ");
310 printf("vba.in[w]"
311 "[IDX(baseIdx.x + i, baseIdx.y + 0, baseIdx.z + 0)];");
312 printf("}");
313 printf("__syncthreads();");
314 for (int fiber = 0; fiber < STENCIL_HEIGHT * STENCIL_DEPTH; ++fiber) {
315 const int height = fiber % STENCIL_HEIGHT;
316 const int depth = fiber / STENCIL_HEIGHT;
317 if (fiber < STENCIL_HEIGHT * STENCIL_DEPTH - 1) {
318 // NOTE THIS IS WRONG, SHOULD USE next_height = (fiber+1) %... and
319 // next_depth!
320 printf("for (int curr=sid; curr < sx*sw; curr += blockDim.x) {");
321 printf("const int i = curr %% sx;");
322 printf("const int w = curr / sx;");
323 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
324 printf("if (w >= NUM_ALL_FIELDS) {break;}");
325 printf("smem[i + w * sx + (%d) * sx*sw] = ", (fiber + 1) % 2);
326 printf("vba.in[w]"
327 "[IDX(baseIdx.x + i, baseIdx.y + (%d), baseIdx.z + (%d))];",
328 height, depth);
329 printf("}");
330 }
331 for (int width = 0; width < STENCIL_WIDTH; ++width) {
332 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
333 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
334 // Skip if the stencil is not used
335 if (!stencils_accessed[curr_kernel][field][stencil])
336 continue;
337 if (stencils[stencil][depth][height][width]) {
338 if (!stencil_initialized[field][stencil]) {
339 printf("auto f%d_s%d = ", field, stencil);
340 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
341 width);
342 printf("%s(smem[(threadIdx.x + %d) + "
343 "(%d) * sx + "
344 "(%d) * sx * sw]);",
345 stencil_unary_ops[stencil], width, field, fiber % 2);
346 stencil_initialized[field][stencil] = 1;
347 }
348 else {
349 printf("f%d_s%d = ", field, stencil);
350 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
351 stencil);
352 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
353 width);
354 printf("%s(smem[(threadIdx.x + %d) + "
355 "(%d) * sx + "
356 "(%d) * sx * sw])",
357 stencil_unary_ops[stencil], width, field, fiber % 2);
358 printf(");");
359 }
360 }
361 }
362 }
363 }
364 printf("__syncthreads();");
365 }
366}
367#endif
369#if IMPLEMENTATION == EXPLICIT_PINGPONG_txy
370static void
371prefetch_stencil_elems_to_smem_pingpong_txy_and_compute_stencil_ops(
372 const int curr_kernel)
373{
374 printf("extern __shared__ AcReal smem[];");
375 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
376 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
377 printf("const int sb = 2;");
378 printf("const int sid = threadIdx.x + threadIdx.y * blockDim.x;");
379 printf("const int3 baseIdx = (int3){"
380 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
381 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
382 "threadIdx.z + blockIdx.z * blockDim.z + start.z - "
383 "(STENCIL_DEPTH-1)/2};");
384 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
385 printf("for (int curr=sid; curr < sx*sy; curr += blockDim.x * blockDim.y) {");
386 printf("const int i = curr %% sx;");
387 printf("const int j = curr / sx;");
388 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
389 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
390 printf("smem[i + j * sx + 0 * sx*sy] = ");
391 printf("vba.in[0]"
392 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + 0)];");
393 printf("}");
394 printf("__syncthreads();");
395 for (int slab = 0; slab < STENCIL_DEPTH * NUM_ALL_FIELDS; ++slab) {
396 const int depth = slab % STENCIL_DEPTH;
397 const int field = slab / STENCIL_DEPTH;
398 const int next_slab = slab + 1;
399 const int next_depth = next_slab % STENCIL_DEPTH;
400 const int next_field = next_slab / STENCIL_DEPTH;
401 if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS) {
402 printf("for (int curr=sid; curr < sx*sy; "
403 "curr += blockDim.x * blockDim.y) {");
404 printf("const int i = curr %% sx;");
405 printf("const int j = curr / sx;");
406 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
407 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
408 printf("smem[i + j * sx + (%d) * sx*sy] = ", next_slab % 2);
409 printf("vba.in[(%d)]"
410 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
411 next_field, next_depth);
412 printf("}");
413 }
414 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
415 for (int width = 0; width < STENCIL_WIDTH; ++width) {
416 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
417 // Skip if the stencil is not used
418 if (!stencils_accessed[curr_kernel][field][stencil])
419 continue;
420 if (stencils[stencil][depth][height][width]) {
421 if (!stencil_initialized[field][stencil]) {
422 printf("auto f%d_s%d = ", field, stencil);
423 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
424 width);
425 printf("%s(smem[(threadIdx.x + %d) + "
426 "(threadIdx.y + %d) * sx + "
427 "(%d) * sx * sy]);",
428 stencil_unary_ops[stencil], width, height, slab % 2);
429 stencil_initialized[field][stencil] = 1;
430 }
431 else {
432 printf("f%d_s%d = ", field, stencil);
433 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
434 stencil);
435 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
436 width);
437 printf("%s(smem[(threadIdx.x + %d) + "
438 "(threadIdx.y + %d) * sx + "
439 "(%d) * sx * sy])",
440 stencil_unary_ops[stencil], width, height, slab % 2);
441 printf(");");
442 }
443 }
444 }
445 }
446 }
447 if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS - 1)
448 printf("__syncthreads();");
449 }
450}
451#endif
452// /** Ping-pong 2D txy blocked (TODO need to rewrite)*/
453// static void
454// prefetch_stencil_elems_to_smem_pingpong_txyblocked_and_compute_stencil_ops(
455// const int curr_kernel)
456// {
457// const size_t num_blocks = 7;
458// printf("extern __shared__ AcReal smem[];");
459// printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
460// printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
461// printf("const int sz = blockDim.y + STENCIL_HEIGHT - 1;");
462// printf("const int sb = 2;");
463// printf("const int sid = threadIdx.x + threadIdx.y * blockDim.x;");
464// printf("const int3 baseIdx = (int3){"
465// "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
466// "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
467// "threadIdx.z + blockIdx.z * blockDim.z + start.z - "
468// "(STENCIL_DEPTH-1)/2};");
469// int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
470// printf(
471// "for (int curr=sid; curr < sx*sy*sz; curr += blockDim.x * blockDim.y)
472// {");
473// printf("const int i = curr %% sx;");
474// printf("const int j = (curr %% (sx*sy)) / sx;");
475// printf("const int k = curr / (sx*sy);");
476// printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
477// printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
478// printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2) {break;}");
479// printf("smem[i + j * sx + k * sx*sy + (%d)*sx*sy*sz] = ", 0);
480// printf("vba.in[(%d)]", 0);
481// printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)];");
482// printf("}");
483// printf("__syncthreads();");
484// for (int block = 0; block < STENCIL_DEPTH / num_blocks; ++slab) {
485// const int depth = slab % STENCIL_DEPTH;
486// const int field = slab / STENCIL_DEPTH;
487// const int next_slab = slab + 1;
488// const int next_depth = next_slab % STENCIL_DEPTH;
489// const int next_field = next_slab / STENCIL_DEPTH;
490// if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS) {
491// printf("for (int curr=sid; curr < sx*sy; "
492// "curr += blockDim.x * blockDim.y) {");
493// printf("const int i = curr %% sx;");
494// printf("const int j = curr / sx;");
495// printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
496// printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
497// printf("smem[i + j * sx + (%d) * sx*sy] = ", next_slab % 2);
498// printf("vba.in[(%d)]"
499// "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
500// next_field, next_depth);
501// printf("}");
502// }
503// for (int height = 0; height < STENCIL_HEIGHT; ++height) {
504// for (int width = 0; width < STENCIL_WIDTH; ++width) {
505// for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
506// // Skip if the stencil is not used
507// if (!stencils_accessed[curr_kernel][field][stencil])
508// continue;
509// if (stencils[stencil][depth][height][width]) {
510// if (!stencil_initialized[field][stencil]) {
511// printf("auto f%d_s%d = ", field, stencil);
512// printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
513// width);
514// printf("%s(smem[(threadIdx.x + %d) + "
515// "(threadIdx.y + %d) * sx + "
516// "(%d) * sx * sy]);",
517// stencil_unary_ops[stencil], width, height, slab % 2);
518// stencil_initialized[field][stencil] = 1;
519// }
520// else {
521// printf("f%d_s%d = ", field, stencil);
522// printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
523// stencil);
524// printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
525// width);
526// printf("%s(smem[(threadIdx.x + %d) + "
527// "(threadIdx.y + %d) * sx + "
528// "(%d) * sx * sy])",
529// stencil_unary_ops[stencil], width, height, slab % 2);
530// printf(");");
531// }
532// }
533// }
534// }
535// }
536// if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS - 1)
537// printf("__syncthreads();");
538// }
539// }
540// /** Ping-pong 3D txyz (TODO)*/
541// static void
542// prefetch_stencil_elems_to_smem_pingpong_txyz_and_compute_stencil_ops(
543// const int curr_kernel)
544// {
545// printf("extern __shared__ AcReal smem[];");
546// printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
547// printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
548// printf("const int sz = blockDim.z + STENCIL_HEIGHT - 1;");
549// printf("const int sb = 2;");
550// printf("const int sid = threadIdx.x + threadIdx.y * blockDim.x +
551// threadIdx.z "
552// "* blockDim.x * blockDim.y;");
553// printf("const int3 baseIdx = (int3){"
554// "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
555// "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
556// "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
557// int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
558// printf("for (int curr=sid; curr < sx*sy*sz; curr += blockDim.x * blockDim.y
559// "
560// "* blockDim.z) {");
561// printf("const int i = curr %% sx;");
562// printf("const int j = (curr %% ) / sx;");
563// printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
564// printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
565// printf("smem[i + j * sx + 0 * sx*sy] = ");
566// printf("vba.in[0]"
567// "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + 0)];");
568// printf("}");
569// printf("__syncthreads();");
570// for (int slab = 0; slab < STENCIL_DEPTH * NUM_ALL_FIELDS; ++slab) {
571// const int depth = slab % STENCIL_DEPTH;
572// const int field = slab / STENCIL_DEPTH;
573// const int next_slab = slab + 1;
574// const int next_depth = next_slab % STENCIL_DEPTH;
575// const int next_field = next_slab / STENCIL_DEPTH;
576// if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS) {
577// printf("for (int curr=sid; curr < sx*sy; "
578// "curr += blockDim.x * blockDim.y) {");
579// printf("const int i = curr %% sx;");
580// printf("const int j = curr / sx;");
581// printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2) {break;}");
582// printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2) {break;}");
583// printf("smem[i + j * sx + (%d) * sx*sy] = ", next_slab % 2);
584// printf("vba.in[(%d)]"
585// "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
586// next_field, next_depth);
587// printf("}");
588// }
589// for (int height = 0; height < STENCIL_HEIGHT; ++height) {
590// for (int width = 0; width < STENCIL_WIDTH; ++width) {
591// for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
592// // Skip if the stencil is not used
593// if (!stencils_accessed[curr_kernel][field][stencil])
594// continue;
595// if (stencils[stencil][depth][height][width]) {
596// if (!stencil_initialized[field][stencil]) {
597// printf("auto f%d_s%d = ", field, stencil);
598// printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
599// width);
600// printf("%s(smem[(threadIdx.x + %d) + "
601// "(threadIdx.y + %d) * sx + "
602// "(%d) * sx * sy]);",
603// stencil_unary_ops[stencil], width, height, slab % 2);
604// stencil_initialized[field][stencil] = 1;
605// }
606// else {
607// printf("f%d_s%d = ", field, stencil);
608// printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
609// stencil);
610// printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
611// width);
612// printf("%s(smem[(threadIdx.x + %d) + "
613// "(threadIdx.y + %d) * sx + "
614// "(%d) * sx * sy])",
615// stencil_unary_ops[stencil], width, height, slab % 2);
616// printf(");");
617// }
618// }
619// }
620// }
621// }
622// if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS - 1)
623// printf("__syncthreads();");
624// }
625// }
627#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
628static void
629prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_original(
630 const int curr_kernel)
631{
632 printf("extern __shared__ AcReal smem[];");
633 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
634 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
635 printf("const int sz = blockDim.z + 1;");
636 printf("const int sid = threadIdx.x + "
637 "threadIdx.y * blockDim.x + threadIdx.z * blockDim.x * blockDim.y;");
638 printf("const int3 baseIdx = (int3){"
639 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
640 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
641 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
642 printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
643 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
644 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
645 printf("__syncthreads();");
646 // Load the main block
647 printf("for (int curr = sid; curr < sx * sy * blockDim.z; curr += tpb) {");
648 printf("const int i = curr %% sx;");
649 printf("const int j = (curr %% (sx * sy)) / sx;");
650 printf("const int k = curr / (sx * sy);");
651 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
652 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
653 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
654 printf("smem[i + j * sx + k * sx * sy] = ");
655 printf("__ldg(&");
656 printf("vba.in[%d]", field);
657 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
658 printf(")");
659 printf(";");
660 printf("}");
661 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
662 printf("__syncthreads();");
663 if (depth + 1 < STENCIL_DEPTH) {
664 // Load the rolling block
665 printf("for (int curr = sid; curr < sx * sy; curr += tpb) {");
666 printf("const int i = curr %% sx;");
667 printf("const int j = (curr %% (sx * sy)) / sx;");
668 printf("const int k = blockDim.z + %d;", depth);
669 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
670 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
671 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
672 printf("smem[i + j * sx + (k%%sz) * sx * sy] = ");
673 printf("__ldg(&");
674 printf("vba.in[%d]", field);
675 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
676 printf(")");
677 printf(";");
678 printf("}");
679 }
680 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
681 for (int width = 0; width < STENCIL_WIDTH; ++width) {
682 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
683 // Skip if the stencil is not used
684 if (!stencils_accessed[curr_kernel][field][stencil])
685 continue;
686 if (stencils[stencil][depth][height][width]) {
687 if (!stencil_initialized[field][stencil]) {
688 printf("auto f%d_s%d = ", field, stencil);
689 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
690 width);
691 printf("%s(smem[(threadIdx.x + %d) + "
692 "(threadIdx.y + %d) * sx + "
693 "((threadIdx.z + %d)%%sz) * sx * sy]);",
694 stencil_unary_ops[stencil], width, height, depth);
695 stencil_initialized[field][stencil] = 1;
696 }
697 else {
698 printf("f%d_s%d = ", field, stencil);
699 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
700 stencil);
701 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
702 width);
703 printf("%s(smem[(threadIdx.x + %d) + "
704 "(threadIdx.y + %d) * sx + "
705 "((threadIdx.z + %d)%%sz) * sx * sy])",
706 stencil_unary_ops[stencil], width, height, depth);
707 printf(");");
708 }
709 }
710 }
711 }
712 }
713 }
714 }
715}
716#endif
718#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
719static void
720prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v2(
721 const int curr_kernel)
722{
723 const int BLOCK_SIZE = EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE;
724 const int NUM_BLOCKS = (NUM_ALL_FIELDS + BLOCK_SIZE - 1) / BLOCK_SIZE;
725 if (BLOCK_SIZE * NUM_BLOCKS < NUM_ALL_FIELDS)
726 raise_error(
727 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
728 if (BLOCK_SIZE > NUM_ALL_FIELDS)
729 raise_error(
730 "Invalid EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE. Must be <= NUM_ALL_FIELDS");
731 printf("extern __shared__ AcReal smem[];");
732 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
733 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
734 printf("const int sz = blockDim.z + 1;");
735 // printf("const int sw = %d;", BLOCK_SIZE);
736 printf("const int sid = threadIdx.x + "
737 "threadIdx.y * blockDim.x + threadIdx.z * blockDim.x * blockDim.y;");
738 printf("const int3 baseIdx = (int3){"
739 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
740 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
741 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
742 printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
743 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
744 for (int block = 0; block < NUM_BLOCKS; ++block) {
745 printf("__syncthreads();");
746 for (int block_offset = 0; block_offset < BLOCK_SIZE; ++block_offset) {
747 const int field = block_offset + block * BLOCK_SIZE;
748 if (field >= NUM_ALL_FIELDS)
749 break;
750 // Load the main block
751 printf(
752 "for (int curr = sid; curr < sx * sy * blockDim.z; curr += tpb) {");
753 printf("const int i = curr %% sx;");
754 printf("const int j = (curr %% (sx * sy)) / sx;");
755 printf("const int k = curr / (sx * sy);");
756 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ continue;}");
757 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){continue; }");
758 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ continue; }");
759 printf("smem[i + j * sx + k * sx * sy + (%d) * sx * sy * sz] = ",
760 field % BLOCK_SIZE);
761 printf("__ldg(&");
762 printf("vba.in[%d]", field);
763 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
764 printf(")");
765 printf(";");
766 printf("}");
767 }
768 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
769 printf("__syncthreads();");
770 for (int block_offset = 0; block_offset < BLOCK_SIZE; ++block_offset) {
771 const int field = block_offset + block * BLOCK_SIZE;
772 if (field >= NUM_ALL_FIELDS)
773 break;
774 if (depth + 1 < STENCIL_DEPTH) {
775 // Load the rolling block
776 printf("for (int curr = sid; curr < sx * sy; curr += tpb) {");
777 printf("const int i = curr %% sx;");
778 printf("const int j = (curr %% (sx * sy)) / sx;");
779 printf("const int k = blockDim.z + %d;", depth);
780 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ "
781 "continue; }");
782 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ "
783 "continue; }");
784 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ "
785 "continue; }");
786 printf("smem[i + j * sx + (k%%sz) * sx * sy + "
787 "(%d) * sx * sy * sz] =",
788 field % BLOCK_SIZE);
789 printf("__ldg(&");
790 printf("vba.in[%d]", field);
791 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
792 printf(")");
793 printf(";");
794 printf("}");
795 }
796 }
797 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
798 for (int width = 0; width < STENCIL_WIDTH; ++width) {
799 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
800 for (int block_offset = 0; block_offset < BLOCK_SIZE;
801 ++block_offset) {
802 const int field = block_offset + block * BLOCK_SIZE;
803 if (field >= NUM_ALL_FIELDS)
804 break;
805 // Skip if the stencil is not used
806 if (!stencils_accessed[curr_kernel][field][stencil])
807 continue;
808 if (stencils[stencil][depth][height][width]) {
809 if (!stencil_initialized[field][stencil]) {
810 printf("auto f%d_s%d = ", field, stencil);
811 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
812 width);
813 printf("%s(smem[(threadIdx.x + %d) + "
814 "(threadIdx.y + %d) * sx + "
815 "((threadIdx.z + %d)%%sz) * sx * sy + "
816 "(%d) * sx * sy * sz]);",
817 stencil_unary_ops[stencil], width, height, depth,
818 field % BLOCK_SIZE);
819 stencil_initialized[field][stencil] = 1;
820 }
821 else {
822 printf("f%d_s%d = ", field, stencil);
823 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
824 stencil);
825 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
826 width);
827 printf("%s(smem[(threadIdx.x + %d) + "
828 "(threadIdx.y + %d) * sx + "
829 "((threadIdx.z + %d)%%sz) * sx * sy + "
830 "(%d) * sx * sy * sz])",
831 stencil_unary_ops[stencil], width, height, depth,
832 field % BLOCK_SIZE);
833 printf(");");
834 }
835 }
836 }
837 }
838 }
839 }
840 }
841 }
842}
843#endif
847#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
848static void
849prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v3(
850 const int curr_kernel)
851{
852 printf("extern __shared__ AcReal smem[];");
853 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
854 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
855 printf("const int sz = blockDim.z + 1;");
856 printf("const int sid = threadIdx.x + "
857 "threadIdx.y * blockDim.x + threadIdx.z * blockDim.x * blockDim.y;");
858 printf("const int3 baseIdx = (int3){"
859 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
860 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
861 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
862 printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
863 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
864 printf("int curr_slab = 0;");
865 for (int field = 0; field < NUM_ALL_FIELDS; ++field) {
866 printf("__syncthreads();");
867 // Load the main block
868 printf("for (int curr = sid; curr < sx * sy * blockDim.z; curr += tpb) {");
869 printf("const int i = curr %% sx;");
870 printf("const int j = (curr %% (sx * sy)) / sx;");
871 printf("const int k = curr / (sx * sy);");
872 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
873 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
874 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
875 printf("smem[i + j * sx + ((curr_slab + k)%%sz) * sx * sy] = ");
876 printf("__ldg(&");
877 printf("vba.in[%d]", field);
878 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
879 printf(")");
880 printf(";");
881 printf("}");
882 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
883 printf("__syncthreads();");
884 if (depth + 1 < STENCIL_DEPTH) {
885 // Load the rolling block
886 printf("for (int curr = sid; curr < sx * sy; curr += tpb) {");
887 printf("const int i = curr %% sx;");
888 printf("const int j = (curr %% (sx * sy)) / sx;");
889 printf("const int k = blockDim.z + %d;", depth);
890 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
891 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
892 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
893 printf("smem[i + j * sx + ((curr_slab+blockDim.z)%%sz) * sx * sy] = ");
894 printf("__ldg(&");
895 printf("vba.in[%d]", field);
896 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
897 printf(")");
898 printf(";");
899 printf("}");
900 }
901 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
902 for (int width = 0; width < STENCIL_WIDTH; ++width) {
903 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
904 // Skip if the stencil is not used
905 if (!stencils_accessed[curr_kernel][field][stencil])
906 continue;
907 if (stencils[stencil][depth][height][width]) {
908 if (!stencil_initialized[field][stencil]) {
909 printf("auto f%d_s%d = ", field, stencil);
910 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
911 width);
912 printf("%s(smem[(threadIdx.x + %d) + "
913 "(threadIdx.y + %d) * sx + "
914 "((curr_slab+threadIdx.z)%%sz) * sx * sy]);",
915 stencil_unary_ops[stencil], width, height);
916 stencil_initialized[field][stencil] = 1;
917 }
918 else {
919 printf("f%d_s%d = ", field, stencil);
920 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
921 stencil);
922 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
923 width);
924 printf("%s(smem[(threadIdx.x + %d) + "
925 "(threadIdx.y + %d) * sx + "
926 "((curr_slab + threadIdx.z)%%sz) * sx * sy])",
927 stencil_unary_ops[stencil], width, height);
928 printf(");");
929 }
930 }
931 }
932 }
933 }
934 printf("++curr_slab;");
935 }
936 }
937}
938#endif
942#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
943static void
944prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v4(
945 const int curr_kernel)
946{
947 const int BLOCK_SIZE = EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE;
948 const int NUM_BLOCKS = (NUM_ALL_FIELDS + BLOCK_SIZE - 1) / BLOCK_SIZE;
949 if (BLOCK_SIZE * NUM_BLOCKS < NUM_ALL_FIELDS)
950 raise_error(
951 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
952 if (BLOCK_SIZE > NUM_ALL_FIELDS)
953 raise_error(
954 "Invalid EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE. Must be <= NUM_ALL_FIELDS");
955 printf("extern __shared__ AcReal smem[];");
956 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
957 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
958 printf("const int sz = blockDim.z + 1;");
959 // printf("const int sw = %d;", BLOCK_SIZE);
960 printf("const int sid = threadIdx.x + "
961 "threadIdx.y * blockDim.x + threadIdx.z * blockDim.x * blockDim.y;");
962 printf("const int3 baseIdx = (int3){"
963 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
964 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
965 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
966 printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
967 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
968 printf("int curr_slab = 0;");
969 for (int block = 0; block < NUM_BLOCKS; ++block) {
970 printf("__syncthreads();");
971 for (int block_offset = 0; block_offset < BLOCK_SIZE; ++block_offset) {
972 const int field = block_offset + block * BLOCK_SIZE;
973 if (field >= NUM_ALL_FIELDS)
974 break;
975 // Load the main block
976 printf(
977 "for (int curr = sid; curr < sx * sy * blockDim.z; curr += tpb) {");
978 printf("const int i = curr %% sx;");
979 printf("const int j = (curr %% (sx * sy)) / sx;");
980 printf("const int k = curr / (sx * sy);");
981 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
982 printf("if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
983 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
984 printf("smem[i + j * sx + ((curr_slab+k)%%sz) * sx * sy + (%d) * sx * sy "
985 "* sz] = ",
986 field % BLOCK_SIZE);
987 printf("__ldg(&");
988 printf("vba.in[%d]", field);
989 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
990 printf(")");
991 printf(";");
992 printf("}");
993 }
994 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
995 printf("__syncthreads();");
996 for (int block_offset = 0; block_offset < BLOCK_SIZE; ++block_offset) {
997 const int field = block_offset + block * BLOCK_SIZE;
998 if (field >= NUM_ALL_FIELDS)
999 break;
1000 if (depth + 1 < STENCIL_DEPTH) {
1001 // Load the rolling block
1002 printf("for (int curr = sid; curr < sx * sy; curr += tpb) {");
1003 printf("const int i = curr %% sx;");
1004 printf("const int j = (curr %% (sx * sy)) / sx;");
1005 printf("const int k = blockDim.z + %d;", depth);
1006 printf("if (baseIdx.x + i >= end.x + (STENCIL_WIDTH-1)/2){ break; }");
1007 printf(
1008 "if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ break; }");
1009 printf("if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ break; }");
1010 printf("smem[i + j * sx + ((curr_slab+blockDim.z)%%sz) * sx * sy + "
1011 "(%d) * sx "
1012 "* sy * sz] = ",
1013 field % BLOCK_SIZE);
1014 printf("__ldg(&");
1015 printf("vba.in[%d]", field);
1016 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
1017 printf(")");
1018 printf(";");
1019 printf("}");
1020 }
1021 }
1022 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
1023 for (int width = 0; width < STENCIL_WIDTH; ++width) {
1024 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
1025 for (int block_offset = 0; block_offset < BLOCK_SIZE;
1026 ++block_offset) {
1027 const int field = block_offset + block * BLOCK_SIZE;
1028 if (field >= NUM_ALL_FIELDS)
1029 break;
1030 // Skip if the stencil is not used
1031 if (!stencils_accessed[curr_kernel][field][stencil])
1032 continue;
1033 if (stencils[stencil][depth][height][width]) {
1034 if (!stencil_initialized[field][stencil]) {
1035 printf("auto f%d_s%d = ", field, stencil);
1036 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
1037 width);
1038 printf("%s(smem[(threadIdx.x + %d) + "
1039 "(threadIdx.y + %d) * sx + "
1040 "((curr_slab + threadIdx.z)%%sz) * sx * sy + "
1041 "(%d) * sx * sy * sz]);",
1042 stencil_unary_ops[stencil], width, height,
1043 field % BLOCK_SIZE);
1044 stencil_initialized[field][stencil] = 1;
1045 }
1046 else {
1047 printf("f%d_s%d = ", field, stencil);
1048 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
1049 stencil);
1050 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
1051 width);
1052 printf("%s(smem[(threadIdx.x + %d) + "
1053 "(threadIdx.y + %d) * sx + "
1054 "((curr_slab + threadIdx.z)%%sz) * sx * sy + "
1055 "(%d) * sx * sy * sz])",
1056 stencil_unary_ops[stencil], width, height,
1057 field % BLOCK_SIZE);
1058 printf(");");
1059 }
1060 }
1061 }
1062 }
1063 }
1064 }
1065 printf("++curr_slab;");
1066 }
1067 }
1068}
1069#endif
1073#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
1074static void
1075prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops(
1076 const int curr_kernel)
1077{
1078 const int BLOCK_SIZE = EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE;
1079 const int NUM_BLOCKS = (NUM_ALL_FIELDS + BLOCK_SIZE - 1) / BLOCK_SIZE;
1080 printf("// BLOCK_SIZE = %d\n", BLOCK_SIZE);
1081 printf("// NUM_BLOCKS = %d\n", NUM_BLOCKS);
1082 if (BLOCK_SIZE * NUM_BLOCKS < NUM_ALL_FIELDS)
1083 raise_error(
1084 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
1085 if (BLOCK_SIZE > NUM_ALL_FIELDS)
1086 raise_error(
1087 "Invalid EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE. Must be <= NUM_ALL_FIELDS");
1088 printf("extern __shared__ AcReal smem[];");
1089 printf("const int sx = blockDim.x + STENCIL_WIDTH - 1;");
1090 printf("const int sy = blockDim.y + STENCIL_HEIGHT - 1;");
1091 printf("const int sz = blockDim.z + 1;");
1092 // printf("const int sw = %d;", BLOCK_SIZE);
1093 // printf("const int sid = threadIdx.x + "
1094 // "threadIdx.y * blockDim.x + threadIdx.z * blockDim.x *
1095 // blockDim.y;");
1096 printf("const int3 baseIdx = (int3){"
1097 "blockIdx.x * blockDim.x + start.x - (STENCIL_WIDTH-1)/2,"
1098 "blockIdx.y * blockDim.y + start.y - (STENCIL_HEIGHT-1)/2,"
1099 "blockIdx.z * blockDim.z + start.z - (STENCIL_DEPTH-1)/2};");
1100 // printf("const int tpb = blockDim.x * blockDim.y * blockDim.z;");
1101 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
1102 for (int block = 0; block < NUM_BLOCKS; ++block) {
1103 printf("__syncthreads();");
1104 // Unrolled (note: need to do a minimum of STENCIL_WIDTH or _HEIGHT
1105 // iterations to be sure all stencil points are covered with if the tbdim
1106 // is 1
1107 printf("{");
1108 printf("const int k = threadIdx.z;");
1109 printf("if (baseIdx.z + k < end.z + (STENCIL_DEPTH-1)/2){ ");
1110 // printf("if (k < blockDim.z){ ");
1111 for (int by = 0; by < 2; ++by) {
1112 printf("{");
1113 printf("const int j = threadIdx.y + (%d) * blockDim.y;", by);
1114 printf("if (baseIdx.y + j < end.y + (STENCIL_HEIGHT-1)/2){ ");
1115 printf("if (j < sy){ ");
1116 for (int bx = 0; bx < 2; ++bx) {
1117 printf("{");
1118 printf("const int i = threadIdx.x + (%d) * blockDim.x;", bx);
1119 printf("if (baseIdx.x + i < end.x + (STENCIL_WIDTH-1)/2){ ");
1120 printf("if (i < sx){ ");
1121 for (int block_offset = 0; block_offset < BLOCK_SIZE; ++block_offset) {
1122 const int field = block_offset + block * BLOCK_SIZE;
1123 if (field >= NUM_ALL_FIELDS)
1124 break;
1125 printf("smem[i + j * sx + k * sx * sy + (%d) * sx * sy * sz] = ",
1126 field % BLOCK_SIZE);
1127 printf("__ldg(&");
1128 printf("vba.in[%d]", field);
1129 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
1130 printf(")");
1131 printf(";");
1132 }
1133 printf("}");
1134 printf("}");
1135 printf("}");
1136 }
1137 printf("}");
1138 printf("}");
1139 printf("}");
1140 }
1141 // printf("}");
1142 printf("}");
1143 printf("}");
1144 for (int depth = 0; depth < STENCIL_DEPTH; ++depth) {
1145 printf("__syncthreads();");
1146 if (depth + 1 < STENCIL_DEPTH) {
1147 printf("{");
1148 printf("if (threadIdx.z == 0){ ");
1149 printf("const int k = blockDim.z + %d;", depth);
1150 printf("if (baseIdx.z + k < end.z + (STENCIL_DEPTH-1)/2){ ");
1151 for (int by = 0; by < 2; ++by) {
1152 printf("{");
1153 printf("const int j = threadIdx.y + (%d) * blockDim.y;", by);
1154 printf("if (baseIdx.y + j < end.y + (STENCIL_HEIGHT-1)/2){ ");
1155 printf("if (j < sy){ ");
1156 for (int bx = 0; bx < 2; ++bx) {
1157 printf("{");
1158 printf("const int i = threadIdx.x + (%d) * blockDim.x;", bx);
1159 printf("if (baseIdx.x + i < end.x + (STENCIL_WIDTH-1)/2){ ");
1160 printf("if (i < sx){ ");
1161 for (int block_offset = 0; block_offset < BLOCK_SIZE;
1162 ++block_offset) {
1163 const int field = block_offset + block * BLOCK_SIZE;
1164 if (field >= NUM_ALL_FIELDS)
1165 break;
1166 // Load the rolling block
1167 printf("smem[i + j * sx + (k%%sz) * sx * sy + "
1168 "(%d) * sx * sy * sz] =",
1169 field % BLOCK_SIZE);
1170 printf("__ldg(&");
1171 printf("vba.in[%d]", field);
1172 printf("[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
1173 printf(")");
1174 printf(";");
1175 }
1176 printf("}");
1177 printf("}");
1178 printf("}");
1179 }
1180 printf("}");
1181 printf("}");
1182 printf("}");
1183 }
1184 printf("}");
1185 printf("}");
1186 printf("}");
1187 }
1188 for (int height = 0; height < STENCIL_HEIGHT; ++height) {
1189 for (int width = 0; width < STENCIL_WIDTH; ++width) {
1190 for (int stencil = 0; stencil < NUM_STENCILS; ++stencil) {
1191 for (int block_offset = 0; block_offset < BLOCK_SIZE;
1192 ++block_offset) {
1193 const int field = block_offset + block * BLOCK_SIZE;
1194 if (field >= NUM_ALL_FIELDS)
1195 break;
1196 // Skip if the stencil is not used
1197 if (!stencils_accessed[curr_kernel][field][stencil])
1198 continue;
1199 if (stencils[stencil][depth][height][width]) {
1200 if (!stencil_initialized[field][stencil]) {
1201 printf("auto f%d_s%d = ", field, stencil);
1202 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
1203 width);
1204 printf("%s(smem[(threadIdx.x + %d) + "
1205 "(threadIdx.y + %d) * sx + "
1206 "((threadIdx.z + %d)%%sz) * sx * sy + "
1207 "(%d) * sx * sy * sz]);",
1208 stencil_unary_ops[stencil], width, height, depth,
1209 field % BLOCK_SIZE);
1210 stencil_initialized[field][stencil] = 1;
1211 }
1212 else {
1213 printf("f%d_s%d = ", field, stencil);
1214 printf("%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
1215 stencil);
1216 printf("stencils[%d][%d][%d][%d] * ", stencil, depth, height,
1217 width);
1218 printf("%s(smem[(threadIdx.x + %d) + "
1219 "(threadIdx.y + %d) * sx + "
1220 "((threadIdx.z + %d)%%sz) * sx * sy + "
1221 "(%d) * sx * sy * sz])",
1222 stencil_unary_ops[stencil], width, height, depth,
1223 field % BLOCK_SIZE);
1224 printf(");");
1225 }
1226 }
1227 }
1228 }
1229 }
1230 }
1231 }
1232 }
1233}
1234#endif
#define EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE
Definition implementation.h:18
__device__ AcReal stencils[NUM_STENCILS][STENCIL_DEPTH][STENCIL_HEIGHT][STENCIL_WIDTH]