5prefetch_stencil_elems_to_smem_and_compute_stencil_ops(
const int curr_kernel)
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;");
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};");
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) {
23 const bool rolling_cache =
true;
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; }",
37 printf(
"smem[i + j * sx + ((threadIdx.z+%d)%%blockDim.z) * sx * sy] = ",
40 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
44 printf(
"__syncthreads();");
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; }",
56 printf(
"smem[i + j * sx + ((threadIdx.z+%d)%%blockDim.z) * sx * sy] = ",
59 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + (%d))];",
62 printf(
"__syncthreads();");
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) {
70 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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);
83 stencil_initialized[field][stencil] = 1;
86 printf(
"f%d_s%d = ", field, stencil);
87 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
89 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
101 printf(
"__syncthreads();");
108prefetch_stencil_elems_to_smem_3d_and_compute_stencil_ops(
const int curr_kernel)
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;");
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;");
124 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
125 for (
int field = 0; field < NUM_ALL_FIELDS; ++field) {
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] = ");
136 printf(
"vba.in[%d]", field);
137 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
141 printf(
"__syncthreads();");
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) {
149 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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);
162 stencil_initialized[field][stencil] = 1;
165 printf(
"f%d_s%d = ", field, stencil);
166 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
168 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
181 printf(
"__syncthreads();");
187prefetch_stencil_elems_to_smem_4d_and_compute_stencil_ops(
const int curr_kernel)
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;");
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};");
202 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
203 for (
int depth = 0; depth < STENCIL_DEPTH; ++depth) {
205 const bool rolling_cache =
true;
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))];",
225 printf(
"__syncthreads();");
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))];",
242 printf(
"__syncthreads();");
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) {
251 if (!stencils_accessed[curr_kernel][field][stencil])
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,
259 printf(
"%s(smem[(threadIdx.x + %d) + "
260 "(threadIdx.y + %d) * sx + "
262 stencil_unary_ops[stencil], width, height, field);
264 stencil_initialized[field][stencil] = 1;
267 printf(
"f%d_s%d = ", field, stencil);
268 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
270 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
272 printf(
"%s(smem[(threadIdx.x + %d) + "
273 "(threadIdx.y + %d) * sx + "
275 stencil_unary_ops[stencil], width, height, field);
283 printf(
"__syncthreads();");
287#if IMPLEMENTATION == EXPLICIT_PINGPONG_txw
289 refetch_stencil_elems_to_smem_pingpong_txw_and_compute_stencil_ops(
290 const int curr_kernel)
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;");
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] = ");
311 "[IDX(baseIdx.x + i, baseIdx.y + 0, baseIdx.z + 0)];");
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) {
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);
327 "[IDX(baseIdx.x + i, baseIdx.y + (%d), baseIdx.z + (%d))];",
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) {
335 if (!stencils_accessed[curr_kernel][field][stencil])
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,
342 printf(
"%s(smem[(threadIdx.x + %d) + "
345 stencil_unary_ops[stencil], width, field, fiber % 2);
346 stencil_initialized[field][stencil] = 1;
349 printf(
"f%d_s%d = ", field, stencil);
350 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
352 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
354 printf(
"%s(smem[(threadIdx.x + %d) + "
357 stencil_unary_ops[stencil], width, field, fiber % 2);
364 printf(
"__syncthreads();");
369#if IMPLEMENTATION == EXPLICIT_PINGPONG_txy
371prefetch_stencil_elems_to_smem_pingpong_txy_and_compute_stencil_ops(
372 const int curr_kernel)
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] = ");
392 "[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + 0)];");
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);
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) {
418 if (!stencils_accessed[curr_kernel][field][stencil])
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,
425 printf(
"%s(smem[(threadIdx.x + %d) + "
426 "(threadIdx.y + %d) * sx + "
428 stencil_unary_ops[stencil], width, height, slab % 2);
429 stencil_initialized[field][stencil] = 1;
432 printf(
"f%d_s%d = ", field, stencil);
433 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
435 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
437 printf(
"%s(smem[(threadIdx.x + %d) + "
438 "(threadIdx.y + %d) * sx + "
440 stencil_unary_ops[stencil], width, height, slab % 2);
447 if (next_slab < STENCIL_DEPTH * NUM_ALL_FIELDS - 1)
448 printf(
"__syncthreads();");
627#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
629prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_original(
630 const int curr_kernel)
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();");
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] = ");
656 printf(
"vba.in[%d]", field);
657 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
661 for (
int depth = 0; depth < STENCIL_DEPTH; ++depth) {
662 printf(
"__syncthreads();");
663 if (depth + 1 < STENCIL_DEPTH) {
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] = ");
674 printf(
"vba.in[%d]", field);
675 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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) {
684 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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;
698 printf(
"f%d_s%d = ", field, stencil);
699 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
701 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
718#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
720prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v2(
721 const int curr_kernel)
724 const int NUM_BLOCKS = (NUM_ALL_FIELDS + BLOCK_SIZE - 1) / BLOCK_SIZE;
725 if (BLOCK_SIZE * NUM_BLOCKS < NUM_ALL_FIELDS)
727 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
728 if (BLOCK_SIZE > NUM_ALL_FIELDS)
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;");
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)
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] = ",
762 printf(
"vba.in[%d]", field);
763 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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)
774 if (depth + 1 < STENCIL_DEPTH) {
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){ "
782 printf(
"if (baseIdx.y + j >= end.y + (STENCIL_HEIGHT-1)/2){ "
784 printf(
"if (baseIdx.z + k >= end.z + (STENCIL_DEPTH-1)/2){ "
786 printf(
"smem[i + j * sx + (k%%sz) * sx * sy + "
787 "(%d) * sx * sy * sz] =",
790 printf(
"vba.in[%d]", field);
791 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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;
802 const int field = block_offset + block * BLOCK_SIZE;
803 if (field >= NUM_ALL_FIELDS)
806 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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,
819 stencil_initialized[field][stencil] = 1;
822 printf(
"f%d_s%d = ", field, stencil);
823 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
825 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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,
847#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
849prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v3(
850 const int curr_kernel)
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();");
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] = ");
877 printf(
"vba.in[%d]", field);
878 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
882 for (
int depth = 0; depth < STENCIL_DEPTH; ++depth) {
883 printf(
"__syncthreads();");
884 if (depth + 1 < STENCIL_DEPTH) {
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] = ");
895 printf(
"vba.in[%d]", field);
896 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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) {
905 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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;
919 printf(
"f%d_s%d = ", field, stencil);
920 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
922 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
934 printf(
"++curr_slab;");
942#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
944prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops_v4(
945 const int curr_kernel)
948 const int NUM_BLOCKS = (NUM_ALL_FIELDS + BLOCK_SIZE - 1) / BLOCK_SIZE;
949 if (BLOCK_SIZE * NUM_BLOCKS < NUM_ALL_FIELDS)
951 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
952 if (BLOCK_SIZE > NUM_ALL_FIELDS)
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;");
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)
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 "
988 printf(
"vba.in[%d]", field);
989 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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)
1000 if (depth + 1 < STENCIL_DEPTH) {
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; }");
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 + "
1013 field % BLOCK_SIZE);
1015 printf(
"vba.in[%d]", field);
1016 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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;
1027 const int field = block_offset + block * BLOCK_SIZE;
1028 if (field >= NUM_ALL_FIELDS)
1031 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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;
1047 printf(
"f%d_s%d = ", field, stencil);
1048 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
1050 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
1065 printf(
"++curr_slab;");
1073#if IMPLEMENTATION == EXPLICIT_ROLLING_PINGPONG
1075prefetch_stencil_elems_to_smem_rolling_pingpong_and_compute_stencil_ops(
1076 const int curr_kernel)
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)
1084 "Invalid NUM_BLOCKS computed in stencilgen.c (rolling pingpong)");
1085 if (BLOCK_SIZE > NUM_ALL_FIELDS)
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;");
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};");
1101 int stencil_initialized[NUM_ALL_FIELDS][NUM_STENCILS] = {0};
1102 for (
int block = 0; block < NUM_BLOCKS; ++block) {
1103 printf(
"__syncthreads();");
1108 printf(
"const int k = threadIdx.z;");
1109 printf(
"if (baseIdx.z + k < end.z + (STENCIL_DEPTH-1)/2){ ");
1111 for (
int by = 0; by < 2; ++by) {
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) {
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)
1125 printf(
"smem[i + j * sx + k * sx * sy + (%d) * sx * sy * sz] = ",
1126 field % BLOCK_SIZE);
1128 printf(
"vba.in[%d]", field);
1129 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
1144 for (
int depth = 0; depth < STENCIL_DEPTH; ++depth) {
1145 printf(
"__syncthreads();");
1146 if (depth + 1 < STENCIL_DEPTH) {
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) {
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) {
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;
1163 const int field = block_offset + block * BLOCK_SIZE;
1164 if (field >= NUM_ALL_FIELDS)
1167 printf(
"smem[i + j * sx + (k%%sz) * sx * sy + "
1168 "(%d) * sx * sy * sz] =",
1169 field % BLOCK_SIZE);
1171 printf(
"vba.in[%d]", field);
1172 printf(
"[IDX(baseIdx.x + i, baseIdx.y + j, baseIdx.z + k)]");
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;
1193 const int field = block_offset + block * BLOCK_SIZE;
1194 if (field >= NUM_ALL_FIELDS)
1197 if (!stencils_accessed[curr_kernel][field][stencil])
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,
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;
1213 printf(
"f%d_s%d = ", field, stencil);
1214 printf(
"%s(f%d_s%d, ", stencil_binary_ops[stencil], field,
1216 printf(
"stencils[%d][%d][%d][%d] * ", stencil, depth, height,
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);
#define EXPLICIT_ROLLING_PINGPONG_BLOCKSIZE
Definition implementation.h:18
__device__ AcReal stencils[NUM_STENCILS][STENCIL_DEPTH][STENCIL_HEIGHT][STENCIL_WIDTH]