update pet for compatibility with recent clangs
[ppcg.git] / cuda.c
blob495616cf908983bb843c2597bfbba46f21791673
1 /*
2 * Copyright 2010-2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <assert.h>
12 #include <stdlib.h>
14 #include <isl/polynomial.h>
15 #include <isl/union_set.h>
16 #include <isl/aff.h>
17 #include <isl/ilp.h>
18 #include <isl/flow.h>
19 #include <isl/band.h>
20 #include <isl/schedule.h>
21 #include <isl/options.h>
22 #include <cloog/isl/cloog.h>
24 #include "cuda.h"
25 #include "cuda_common.h"
26 #include "gpucode.h"
27 #include "schedule.h"
28 #include "ppcg_options.h"
30 /* The fields stride, shift and shift_map only contain valid information
31 * if shift != NULL.
32 * If so, they express that current index is such that if you add shift,
33 * then the result is always a multiple of stride.
34 * shift_map contains the mapping
36 * i -> (i + shift)/stride
38 struct cuda_array_bound {
39 isl_int size;
40 isl_aff *lb;
42 isl_int stride;
43 isl_qpolynomial *shift;
44 isl_basic_map *shift_map;
47 struct cuda_array_info;
49 /* A group of array references in a kernel that should be handled together.
50 * If private_bound is not NULL, then it is mapped to registers.
51 * Otherwise, if shared_bound is not NULL, it is mapped to shared memory.
52 * Otherwise, it is accesses from global memory.
54 struct cuda_array_ref_group {
55 /* The references in this group access this array. */
56 struct cuda_array_info *array;
57 /* Position of this group in the list of reference groups of array. */
58 int nr;
60 /* The following fields are use during the construction of the groups.
61 * access is the combined access relation relative to the shared
62 * memory tiling.
63 * write is set if any access in the group is a write.
65 isl_map *access;
66 int write;
68 /* For each index, size and offset of piece in shared memory. */
69 struct cuda_array_bound *shared_bound;
71 /* For each index, size and offset of piece in private memory. */
72 struct cuda_array_bound *private_bound;
74 /* References in this group; point to elements of a linked list. */
75 int n_ref;
76 struct cuda_stmt_access **refs;
79 struct cuda_array_info {
80 isl_space *dim;
81 /* Element type. */
82 char *type;
83 /* Name of the array. */
84 char *name;
85 /* Number of indices. */
86 unsigned n_index;
87 /* For each index, a bound on the array in that direction. */
88 isl_pw_aff **bound;
89 /* For each index, bound[i] specialized to the current kernel. */
90 isl_pw_aff **local_bound;
92 /* All references to this array; point to elements of a linked list. */
93 int n_ref;
94 struct cuda_stmt_access **refs;
96 /* The reference groups associated to this array. */
97 int n_group;
98 struct cuda_array_ref_group **groups;
100 /* Last shared memory tile dimension that affects tile of this array. */
101 int last_shared;
102 /* Dimension at which copying to/from shared memory is printed.
103 * if >= 0, then the value is >= last_shared
104 * if -1, then the copying is done at the leaf level.
106 int print_shared_level;
109 /* Print the name of the local copy of a given group of array references.
111 static void print_array_name(FILE *out, struct cuda_array_ref_group *group)
113 int global = 0;
115 if (group->private_bound)
116 fprintf(out, "private_");
117 else if (group->shared_bound)
118 fprintf(out, "shared_");
119 else
120 global = 1;
121 fprintf(out, "%s", group->array->name);
122 if (!global && group->array->n_group > 1)
123 fprintf(out, "_%d", group->nr);
126 /* Collect all references to the given array and store pointers to them
127 * in array->refs.
129 static void collect_references(struct cuda_gen *gen,
130 struct cuda_array_info *array)
132 int i;
133 int n;
135 n = 0;
136 for (i = 0; i < gen->n_stmts; ++i) {
137 struct cuda_stmt *stmt = &gen->stmts[i];
138 struct cuda_stmt_access *access;
140 for (access = stmt->accesses; access; access = access->next) {
141 const char *name;
142 name = isl_map_get_tuple_name(access->access,
143 isl_dim_out);
144 if (name && !strcmp(array->name, name))
145 n++;
149 array->n_ref = n;
150 array->refs = isl_alloc_array(gen->ctx, struct cuda_stmt_access *, n);
151 assert(array->refs);
153 n = 0;
154 for (i = 0; i < gen->n_stmts; ++i) {
155 struct cuda_stmt *stmt = &gen->stmts[i];
156 struct cuda_stmt_access *access;
158 for (access = stmt->accesses; access; access = access->next) {
159 const char *name;
160 name = isl_map_get_tuple_name(access->access,
161 isl_dim_out);
162 if (!name || strcmp(array->name, name))
163 continue;
165 array->refs[n++] = access;
170 static struct cuda_array_bound *create_bound_list(isl_ctx *ctx, int n_index)
172 int i;
173 struct cuda_array_bound *bound;
175 bound = isl_alloc_array(ctx, struct cuda_array_bound, n_index);
176 assert(bound);
178 for (i = 0; i < n_index; ++i) {
179 isl_int_init(bound[i].size);
180 bound[i].lb = NULL;
181 isl_int_init(bound[i].stride);
182 bound[i].shift = NULL;
183 bound[i].shift_map = NULL;
186 return bound;
189 static void free_bound_list(struct cuda_array_bound *bound, int n_index)
191 int j;
193 if (!bound)
194 return;
196 for (j = 0; j < n_index; ++j) {
197 isl_int_clear(bound[j].size);
198 isl_int_clear(bound[j].stride);
199 isl_aff_free(bound[j].lb);
200 isl_qpolynomial_free(bound[j].shift);
201 isl_basic_map_free(bound[j].shift_map);
203 free(bound);
206 static struct pet_array *find_array(struct pet_scop *scop,
207 __isl_keep isl_set *accessed)
209 int i;
210 isl_id *id;
212 id = isl_set_get_tuple_id(accessed);
214 for (i = 0; i < scop->n_array; ++i) {
215 isl_id *id_i;
217 id_i = isl_set_get_tuple_id(scop->arrays[i]->extent);
218 isl_id_free(id_i);
219 if (id == id_i)
220 break;
222 isl_id_free(id);
224 return i < scop->n_array ? scop->arrays[i] : NULL;
227 /* Compute bounds on the host arrays based on the accessed elements
228 * and collect all references to the array.
230 static int extract_array_info(__isl_take isl_set *array, void *user)
232 int i;
233 struct cuda_gen *gen = (struct cuda_gen *)user;
234 const char *name;
235 int n_index;
236 isl_pw_aff **bounds;
237 isl_pw_aff **local_bounds;
238 struct pet_array *pa;
240 n_index = isl_set_dim(array, isl_dim_set);
241 name = isl_set_get_tuple_name(array);
242 bounds = isl_alloc_array(isl_set_get_ctx(array),
243 isl_pw_aff *, n_index);
244 assert(bounds);
245 local_bounds = isl_calloc_array(isl_set_get_ctx(array),
246 isl_pw_aff *, n_index);
247 assert(local_bounds);
248 gen->array[gen->n_array].dim = isl_set_get_space(array);
249 gen->array[gen->n_array].name = strdup(name);
250 gen->array[gen->n_array].n_index = n_index;
251 gen->array[gen->n_array].bound = bounds;
252 gen->array[gen->n_array].local_bound = local_bounds;
254 pa = find_array(gen->scop, array);
255 assert(pa);
257 gen->array[gen->n_array].type = strdup(pa->element_type);
259 for (i = 0; i < n_index; ++i) {
260 isl_set *dom;
261 isl_local_space *ls;
262 isl_aff *one;
263 isl_pw_aff *bound;
264 isl_set *size = i == 0 ? array : pa->extent;
266 bound = isl_set_dim_max(isl_set_copy(size), i);
267 assert(bound);
268 dom = isl_pw_aff_domain(isl_pw_aff_copy(bound));
269 ls = isl_local_space_from_space(isl_set_get_space(dom));
270 one = isl_aff_zero_on_domain(ls);
271 one = isl_aff_add_constant_si(one, 1);
272 bound = isl_pw_aff_add(bound, isl_pw_aff_alloc(dom, one));
273 bound = isl_pw_aff_gist(bound, isl_set_copy(gen->context));
275 bounds[i] = bound;
278 collect_references(gen, &gen->array[gen->n_array]);
280 gen->n_array++;
282 isl_set_free(array);
283 return 0;
286 void collect_array_info(struct cuda_gen *gen)
288 isl_union_set *arrays;
290 arrays = isl_union_map_range(isl_union_map_copy(gen->read));
291 arrays = isl_union_set_union(arrays,
292 isl_union_map_range(isl_union_map_copy(gen->write)));
293 arrays = isl_union_set_coalesce(arrays);
295 gen->n_array = isl_union_set_n_set(arrays);
296 gen->array = isl_alloc_array(gen->ctx,
297 struct cuda_array_info, gen->n_array);
298 assert(gen->array);
299 gen->n_array = 0;
300 isl_union_set_foreach_set(arrays, &extract_array_info, gen);
301 isl_union_set_free(arrays);
304 static void free_array_info(struct cuda_gen *gen)
306 int i, j;
308 for (i = 0; i < gen->n_array; ++i) {
309 int n_index = gen->array[i].n_index;
310 free(gen->array[i].type);
311 free(gen->array[i].name);
312 for (j = 0; j < n_index; ++j) {
313 isl_pw_aff_free(gen->array[i].bound[j]);
314 isl_pw_aff_free(gen->array[i].local_bound[j]);
316 isl_space_free(gen->array[i].dim);
317 free(gen->array[i].bound);
318 free(gen->array[i].local_bound);
319 free(gen->array[i].refs);
321 free(gen->array);
324 static void declare_device_arrays(struct cuda_gen *gen)
326 int i;
328 for (i = 0; i < gen->n_array; ++i)
329 fprintf(gen->cuda.host_c, "%s *dev_%s;\n",
330 gen->array[i].type, gen->array[i].name);
331 fprintf(gen->cuda.host_c, "\n");
334 static void print_array_size(struct cuda_gen *gen, FILE *out,
335 struct cuda_array_info *array)
337 int i;
338 isl_printer *prn;
340 prn = isl_printer_to_file(gen->ctx, out);
341 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
342 for (i = 0; i < array->n_index; ++i) {
343 prn = isl_printer_print_str(prn, "(");
344 prn = isl_printer_print_pw_aff(prn, array->bound[i]);
345 prn = isl_printer_print_str(prn, ") * ");
347 prn = isl_printer_print_str(prn, "sizeof(");
348 prn = isl_printer_print_str(prn, array->type);
349 prn = isl_printer_print_str(prn, ")");
350 isl_printer_free(prn);
353 static void allocate_device_arrays(struct cuda_gen *gen)
355 int i;
357 for (i = 0; i < gen->n_array; ++i) {
358 fprintf(gen->cuda.host_c,
359 "cudaCheckReturn(cudaMalloc((void **) &dev_%s, ",
360 gen->array[i].name);
361 print_array_size(gen, gen->cuda.host_c, &gen->array[i]);
362 fprintf(gen->cuda.host_c, "));\n");
364 fprintf(gen->cuda.host_c, "\n");
367 static void free_device_arrays(struct cuda_gen *gen)
369 int i;
371 for (i = 0; i < gen->n_array; ++i)
372 fprintf(gen->cuda.host_c, "cudaCheckReturn(cudaFree(dev_%s));\n",
373 gen->array[i].name);
376 /* Check if a cuda array is a scalar. A scalar is a value that is not stored
377 * as an array or through a pointer reference, but as single data element. At
378 * the moment, scalars are represented as zero dimensional arrays.
380 static int cuda_array_is_scalar(struct cuda_array_info *array)
382 return (array->n_index == 0);
385 static void copy_arrays_to_device(struct cuda_gen *gen)
387 int i;
389 for (i = 0; i < gen->n_array; ++i) {
390 isl_space *dim;
391 isl_set *read_i;
392 int empty;
394 dim = isl_space_copy(gen->array[i].dim);
395 read_i = isl_union_set_extract_set(gen->copy_in, dim);
396 empty = isl_set_fast_is_empty(read_i);
397 isl_set_free(read_i);
398 if (empty)
399 continue;
401 fprintf(gen->cuda.host_c, "cudaCheckReturn(cudaMemcpy(dev_%s,",
402 gen->array[i].name);
404 if (cuda_array_is_scalar(&(gen->array[i])))
405 fprintf(gen->cuda.host_c, " &%s, ",
406 gen->array[i].name);
407 else
408 fprintf(gen->cuda.host_c, " %s, ", gen->array[i].name);
410 print_array_size(gen, gen->cuda.host_c, &gen->array[i]);
411 fprintf(gen->cuda.host_c, ", cudaMemcpyHostToDevice));\n");
413 fprintf(gen->cuda.host_c, "\n");
416 static void copy_arrays_from_device(struct cuda_gen *gen)
418 int i;
419 isl_union_set *write;
420 write = isl_union_map_range(isl_union_map_copy(gen->write));
422 for (i = 0; i < gen->n_array; ++i) {
423 isl_space *dim;
424 isl_set *write_i;
425 int empty;
427 dim = isl_space_copy(gen->array[i].dim);
428 write_i = isl_union_set_extract_set(write, dim);
429 empty = isl_set_fast_is_empty(write_i);
430 isl_set_free(write_i);
431 if (empty)
432 continue;
434 fprintf(gen->cuda.host_c,
435 "cudaCheckReturn(cudaMemcpy(%s, dev_%s, ",
436 gen->array[i].name, gen->array[i].name);
437 print_array_size(gen, gen->cuda.host_c, &gen->array[i]);
438 fprintf(gen->cuda.host_c, ", cudaMemcpyDeviceToHost));\n");
441 isl_union_set_free(write);
442 fprintf(gen->cuda.host_c, "\n");
445 static void read_sizes_from_file(struct cuda_gen *gen, const char *filename,
446 int *sizes, int len)
448 int i;
449 FILE *file;
451 file = fopen(filename, "r");
452 if (!file)
453 return;
455 for (i = 0; i < len; ++i)
456 if (fscanf(file, "%d", &sizes[i]) < 1)
457 break;
459 fclose(file);
462 static void reverse_list(int *list, int len)
464 int i;
465 int t;
467 for (i = 0; 2 * i < len; ++i) {
468 t = list[i];
469 list[i] = list[len - 1 - i];
470 list[len - 1 - i] = t;
474 /* Read user specified sizes from "tile.sizes", "block.sizes" and "grid.sizes"
475 * after filling in some potentially useful defaults.
477 static void read_sizes(struct cuda_gen *gen)
479 int n;
481 gen->tile_size = isl_alloc_array(gen->ctx, int, gen->tile_len);
482 assert(gen->tile_size);
483 for (n = 0; n < gen->tile_len; ++n)
484 gen->tile_size[n] = gen->options->tile_size;
485 read_sizes_from_file(gen, "tile.sizes", gen->tile_size, gen->tile_len);
487 n = gen->n_parallel;
488 gen->n_block = (n <= 3) ? n : 3;
489 switch (gen->n_block) {
490 case 1:
491 gen->block_dim[0] = 512;
492 break;
493 case 2:
494 gen->block_dim[0] = 32;
495 gen->block_dim[1] = 16;
496 break;
497 default:
498 gen->block_dim[0] = 32;
499 gen->block_dim[1] = 4;
500 gen->block_dim[2] = 4;
501 break;
503 read_sizes_from_file(gen, "block.sizes", gen->block_dim, gen->n_block);
504 reverse_list(gen->block_dim, gen->n_block);
506 gen->n_grid = (n <= 2) ? n : 2;
507 switch (gen->n_grid) {
508 case 1:
509 gen->grid_dim[0] = 32768;
510 break;
511 default:
512 gen->grid_dim[0] = 256;
513 gen->grid_dim[1] = 256;
514 break;
516 read_sizes_from_file(gen, "grid.sizes", gen->grid_dim, gen->n_grid);
517 reverse_list(gen->grid_dim, gen->n_grid);
520 static void free_stmts(struct cuda_stmt *stmts, int n)
522 int i;
524 for (i = 0; i < n; ++i) {
525 struct cuda_stmt_access *access, *next;
527 for (access = stmts[i].accesses; access; access = next) {
528 next = access->next;
529 isl_map_free(access->access);
530 free(access);
533 isl_set_free(stmts[i].domain);
535 free(stmts);
538 void clear_cuda_gen(struct cuda_gen *gen)
540 free_stmts(gen->stmts, gen->n_stmts);
541 free_array_info(gen);
542 isl_set_free(gen->context);
543 isl_union_set_free(gen->copy_in);
544 isl_union_map_free(gen->sched);
545 isl_union_map_free(gen->read);
546 isl_union_map_free(gen->write);
549 static void print_reverse_list(FILE *out, int len, int *list)
551 int i;
553 for (i = 0; i < len; ++i) {
554 if (i)
555 fprintf(out, ", ");
556 fprintf(out, "%d", list[len - 1 - i]);
560 static void print_kernel_launch(struct cuda_gen *gen,
561 __isl_keep isl_union_set *arrays)
563 int i;
564 int first = 1;
565 unsigned nparam;
566 isl_space *dim;
568 print_indent(gen->code.dst, gen->code.indent);
569 fprintf(gen->code.dst, "kernel%d <<<k%d_dimGrid, k%d_dimBlock>>> (",
570 gen->kernel_id, gen->kernel_id, gen->kernel_id);
571 fprintf(gen->cuda.kernel_c, "__global__ void kernel%d(",
572 gen->kernel_id);
573 fprintf(gen->cuda.kernel_h, "__global__ void kernel%d(",
574 gen->kernel_id);
576 for (i = 0; i < gen->n_array; ++i) {
577 isl_space *dim;
578 isl_set *arr;
579 int empty;
581 dim = isl_space_copy(gen->array[i].dim);
582 arr = isl_union_set_extract_set(arrays, dim);
583 empty = isl_set_fast_is_empty(arr);
584 isl_set_free(arr);
585 if (empty)
586 continue;
588 if (!first) {
589 fprintf(gen->code.dst, ", ");
590 fprintf(gen->cuda.kernel_c, ", ");
591 fprintf(gen->cuda.kernel_h, ", ");
594 fprintf(gen->code.dst, "dev_%s", gen->array[i].name);
595 fprintf(gen->cuda.kernel_c, "%s *%s",
596 gen->array[i].type, gen->array[i].name);
597 fprintf(gen->cuda.kernel_h, "%s *%s",
598 gen->array[i].type, gen->array[i].name);
600 first = 0;
603 dim = isl_union_set_get_space(arrays);
604 nparam = isl_space_dim(dim, isl_dim_param);
605 for (i = 0; i < nparam; ++i) {
606 const char *name = isl_space_get_dim_name(dim, isl_dim_param, i);
607 if (!first) {
608 fprintf(gen->code.dst, ", ");
609 fprintf(gen->cuda.kernel_c, ", ");
610 fprintf(gen->cuda.kernel_h, ", ");
612 fprintf(gen->code.dst, "%s", name);
613 fprintf(gen->cuda.kernel_c, "int %s", name);
614 fprintf(gen->cuda.kernel_h, "int %s", name);
615 first = 0;
617 isl_space_free(dim);
619 for (i = 0; i < gen->tile_first; ++i) {
620 if (!first) {
621 fprintf(gen->code.dst, ", ");
622 fprintf(gen->cuda.kernel_c, ", ");
623 fprintf(gen->cuda.kernel_h, ", ");
625 fprintf(gen->code.dst, "h%d", i);
626 fprintf(gen->cuda.kernel_c, "int h%d", i);
627 fprintf(gen->cuda.kernel_h, "int h%d", i);
628 first = 0;
631 fprintf(gen->code.dst, ");\n");
632 fprintf(gen->cuda.kernel_c, ")\n");
633 fprintf(gen->cuda.kernel_h, ");\n");
635 fprintf(gen->code.dst, "cudaCheckKernel();\n");
638 /* Construct a map from a domain of dimensionality "len"
639 * to a domain of dimensionality "len" + "tile_len" that tiles
640 * the "tile_len" coordinates starting at "first".
641 * In particular, [s_i] -> [s_i / tile_size[i], s_i % tile_size[i]].
642 * "dim" prescribes the parameters.
644 static __isl_give isl_map *tile(__isl_take isl_space *dim, int len,
645 int first, int tile_len, int *tile_size)
647 int i;
648 isl_int v;
649 isl_basic_map *bmap;
650 isl_constraint *c;
652 isl_int_init(v);
654 dim = isl_space_add_dims(dim, isl_dim_in, len);
655 dim = isl_space_add_dims(dim, isl_dim_out, len + tile_len);
656 bmap = isl_basic_map_universe(isl_space_copy(dim));
658 for (i = 0; i < len - tile_len; ++i) {
659 int j = i < first ? i : i + tile_len;
660 int k = i < first ? i : i + 2 * tile_len;
662 c = isl_equality_alloc(isl_space_copy(dim));
663 isl_int_set_si(v, -1);
664 isl_constraint_set_coefficient(c, isl_dim_in, j, v);
665 isl_int_set_si(v, 1);
666 isl_constraint_set_coefficient(c, isl_dim_out, k, v);
667 bmap = isl_basic_map_add_constraint(bmap, c);
670 for (i = 0; i < tile_len; ++i) {
671 c = isl_equality_alloc(isl_space_copy(dim));
672 isl_int_set_si(v, -1);
673 isl_constraint_set_coefficient(c, isl_dim_in, first + i, v);
674 isl_int_set_si(v, tile_size[i]);
675 isl_constraint_set_coefficient(c, isl_dim_out, first + i, v);
676 isl_int_set_si(v, 1);
677 isl_constraint_set_coefficient(c, isl_dim_out,
678 first + i + tile_len, v);
679 bmap = isl_basic_map_add_constraint(bmap, c);
681 c = isl_inequality_alloc(isl_space_copy(dim));
682 isl_int_set_si(v, 1);
683 isl_constraint_set_coefficient(c, isl_dim_out,
684 first + i + tile_len, v);
685 bmap = isl_basic_map_add_constraint(bmap, c);
687 c = isl_inequality_alloc(isl_space_copy(dim));
688 isl_int_set_si(v, -1);
689 isl_constraint_set_coefficient(c, isl_dim_out,
690 first + i + tile_len, v);
691 isl_int_set_si(v, tile_size[i] - 1);
692 isl_constraint_set_constant(c, v);
693 bmap = isl_basic_map_add_constraint(bmap, c);
696 isl_space_free(dim);
697 isl_int_clear(v);
699 return isl_map_from_basic_map(bmap);
702 /* Construct a map from a domain of dimensionality "len"
703 * to a domain of dimensionality "len" + "wrap_len" that "wraps"
704 * the "wrap_len" coordinates starting at "first" according to "wrap_size".
705 * In particular, [s_i] -> [s_i, s_i % wrap_size[i]].
706 * To do so, we need extra variables corresponding to [s_i / wrap_size[i]],
707 * that are projected out at the end.
708 * "dim" prescribes the parameters.
710 static __isl_give isl_map *wrap(__isl_take isl_space *dim, int len,
711 int first, int wrap_len, int *wrap_size)
713 int i;
714 isl_basic_map *bmap;
715 isl_constraint *c;
717 dim = isl_space_add_dims(dim, isl_dim_in, len);
718 dim = isl_space_add_dims(dim, isl_dim_out, len + 2 * wrap_len);
719 bmap = isl_basic_map_universe(isl_space_copy(dim));
721 for (i = 0; i < len; ++i) {
722 int k = i < first + wrap_len ? i : i + 2 * wrap_len;
724 c = isl_equality_alloc(isl_space_copy(dim));
725 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
726 isl_constraint_set_coefficient_si(c, isl_dim_out, k, 1);
727 bmap = isl_basic_map_add_constraint(bmap, c);
730 for (i = 0; i < wrap_len; ++i) {
731 c = isl_equality_alloc(isl_space_copy(dim));
732 isl_constraint_set_coefficient_si(c, isl_dim_out,
733 first + i, -1);
734 isl_constraint_set_coefficient_si(c, isl_dim_out,
735 first + wrap_len + i, 1);
736 isl_constraint_set_coefficient_si(c, isl_dim_out,
737 first + 2 * wrap_len + i, wrap_size[i]);
738 bmap = isl_basic_map_add_constraint(bmap, c);
740 c = isl_inequality_alloc(isl_space_copy(dim));
741 isl_constraint_set_coefficient_si(c, isl_dim_out,
742 first + wrap_len + i, 1);
743 bmap = isl_basic_map_add_constraint(bmap, c);
745 c = isl_inequality_alloc(isl_space_copy(dim));
746 isl_constraint_set_coefficient_si(c, isl_dim_out,
747 first + wrap_len + i, -1);
748 isl_constraint_set_constant_si(c, wrap_size[i] - 1);
749 bmap = isl_basic_map_add_constraint(bmap, c);
752 isl_space_free(dim);
754 bmap = isl_basic_map_project_out(bmap, isl_dim_out,
755 first + 2 * wrap_len, wrap_len);
757 return isl_map_from_basic_map(bmap);
760 /* Add "n" parameters named prefix%d.
762 static __isl_give isl_set *add_params( __isl_take isl_set *set,
763 int n, const char *prefix)
765 int i;
766 unsigned nparam;
767 char name[20];
769 nparam = isl_set_dim(set, isl_dim_param);
770 set = isl_set_add_dims(set, isl_dim_param, n);
772 for (i = 0; i < n; ++i) {
773 snprintf(name, sizeof(name), "%s%d", prefix, i);
774 set = isl_set_set_dim_name(set, isl_dim_param,
775 nparam + i, name);
778 return set;
781 /* Equate the "n" dimensions of "set" starting at "first" to
782 * freshly created parameters named prefix%d.
784 static __isl_give isl_set *parametrize(__isl_take isl_set *set,
785 int first, int n, const char *prefix)
787 int i;
788 unsigned nparam;
789 isl_int v;
790 isl_space *dim;
791 isl_basic_set *bset;
792 isl_constraint *c;
794 nparam = isl_set_dim(set, isl_dim_param);
796 set = add_params(set, n, prefix);
798 dim = isl_set_get_space(set);
799 bset = isl_basic_set_universe(isl_space_copy(dim));
801 isl_int_init(v);
803 for (i = 0; i < n; ++i) {
804 c = isl_equality_alloc(isl_space_copy(dim));
805 isl_int_set_si(v, -1);
806 isl_constraint_set_coefficient(c, isl_dim_param, nparam + i, v);
807 isl_int_set_si(v, 1);
808 isl_constraint_set_coefficient(c, isl_dim_set, first + i, v);
809 bset = isl_basic_set_add_constraint(bset, c);
812 isl_int_clear(v);
813 isl_space_free(dim);
815 return isl_set_intersect(set, isl_set_from_basic_set(bset));
818 static __isl_give isl_set *parametrization(__isl_take isl_space *dim,
819 int len, int first, int n, const char *prefix)
821 isl_set *set;
823 dim = isl_space_add_dims(dim, isl_dim_set, len);
824 set = isl_set_universe(dim);
826 return parametrize(set, first, n, prefix);
829 /* Tile the B loops over the tile sizes and then tile/wrap
830 * the T1 loops over the blocks.
832 static __isl_give isl_union_map *tile_schedule(struct cuda_gen *gen,
833 __isl_take isl_union_map *sched)
835 isl_space *dim;
836 isl_map *tiling, *block_tiling;
838 dim = isl_union_map_get_space(sched);
839 tiling = tile(isl_space_copy(dim), gen->untiled_len,
840 gen->tile_first, gen->tile_len, gen->tile_size);
842 if (gen->options->wrap)
843 block_tiling = wrap(dim, gen->untiled_len + gen->tile_len,
844 gen->tile_first, gen->n_grid, gen->grid_dim);
845 else
846 block_tiling = tile(dim, gen->untiled_len + gen->tile_len,
847 gen->tile_first, gen->n_grid, gen->grid_dim);
849 gen->tiled_len = gen->untiled_len + gen->tile_len + gen->n_grid;
851 tiling = isl_map_apply_range(tiling, block_tiling);
853 sched = isl_union_map_apply_range(sched,
854 isl_union_map_from_map(tiling));
856 gen->shared_len = gen->tile_first + gen->tile_len + gen->n_grid;
858 return sched;
861 static __isl_give isl_union_map *parametrize_tiled_schedule(
862 struct cuda_gen *gen, __isl_take isl_union_map *sched)
864 isl_space *dim;
865 isl_set *par;
867 dim = isl_union_map_get_space(sched);
868 par = parametrization(dim, gen->tiled_len, 0, gen->tile_first, "h");
869 sched = isl_union_map_intersect_range(sched,
870 isl_union_set_from_set(par));
872 dim = isl_union_map_get_space(sched);
873 par = parametrization(dim, gen->tiled_len,
874 gen->tile_first + gen->n_grid, gen->n_grid, "b");
875 sched = isl_union_map_intersect_range(sched,
876 isl_union_set_from_set(par));
878 return sched;
881 /* Tile/wrap the P1 loops over the threads.
883 static __isl_give isl_union_map *thread_tile_schedule(struct cuda_gen *gen,
884 __isl_take isl_union_map *sched)
886 isl_space *dim;
887 isl_map *tiling;
888 isl_set *par;
890 dim = isl_union_map_get_space(sched);
892 if (gen->options->wrap)
893 tiling = wrap(isl_space_copy(dim), gen->tiled_len,
894 gen->shared_len, gen->n_block, gen->block_dim);
895 else
896 tiling = tile(isl_space_copy(dim), gen->tiled_len,
897 gen->shared_len, gen->n_block, gen->block_dim);
898 gen->thread_tiled_len = gen->tiled_len + gen->n_block;
900 sched = isl_union_map_apply_range(sched,
901 isl_union_map_from_map(tiling));
903 par = parametrization(dim, gen->thread_tiled_len,
904 gen->tile_first + gen->tile_len + gen->n_grid + gen->n_block,
905 gen->n_block, "t");
906 sched = isl_union_map_intersect_range(sched,
907 isl_union_set_from_set(par));
909 gen->shared_len = gen->tile_first + gen->tile_len + gen->n_grid;
911 return sched;
914 /* If the user asked for it, scale the shared memory tile loops
915 * (T1P and T2) of "sched" by gen->tile_size[i].
916 * If we are not performing "wrapping", then additionally scale the T1P
917 * loops by gen->grid_dim[i].
919 static __isl_give isl_union_map *scale_tile_loops(struct cuda_gen *gen,
920 __isl_take isl_union_map *sched)
922 int i;
923 isl_space *dim;
924 isl_basic_map *scale;
925 isl_constraint *c;
927 if (!gen->options->scale_tile_loops)
928 return sched;
930 dim = isl_union_map_get_space(sched);
931 dim = isl_space_add_dims(dim, isl_dim_in, gen->tiled_len);
932 dim = isl_space_add_dims(dim, isl_dim_out, gen->tiled_len);
933 scale = isl_basic_map_universe(isl_space_copy(dim));
935 for (i = 0; i < gen->tiled_len; ++i) {
936 int f = 1;
938 if (i >= gen->tile_first && i < gen->tile_first + gen->n_grid) {
939 f = gen->tile_size[i - gen->tile_first];
940 if (!gen->options->wrap)
941 f *= gen->grid_dim[i - gen->tile_first];
942 } else if (i >= gen->tile_first + gen->n_grid &&
943 i < gen->tile_first + gen->n_grid + gen->tile_len) {
944 f = gen->tile_size[i - (gen->tile_first + gen->n_grid)];
947 c = isl_equality_alloc(isl_space_copy(dim));
948 isl_constraint_set_coefficient_si(c, isl_dim_in, i, f);
949 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
950 scale = isl_basic_map_add_constraint(scale, c);
953 isl_space_free(dim);
955 sched = isl_union_map_apply_range(sched,
956 isl_union_map_from_map(isl_map_from_basic_map(scale)));
958 return sched;
961 /* If we are not performing "wrapping" and if the user asked for it,
962 * scale the thread tile loops (P1T) of "sched" by gen->block_dim[i].
964 static __isl_give isl_union_map *scale_thread_tile_loops(struct cuda_gen *gen,
965 __isl_take isl_union_map *sched)
967 int i;
968 isl_space *dim;
969 isl_basic_map *scale;
970 isl_constraint *c;
972 if (gen->options->wrap)
973 return sched;
974 if (!gen->options->scale_tile_loops)
975 return sched;
977 dim = isl_union_map_get_space(sched);
978 dim = isl_space_add_dims(dim, isl_dim_in, gen->thread_tiled_len);
979 dim = isl_space_add_dims(dim, isl_dim_out, gen->thread_tiled_len);
980 scale = isl_basic_map_universe(isl_space_copy(dim));
982 for (i = 0; i < gen->thread_tiled_len; ++i) {
983 int f = 1;
985 if (i >= gen->shared_len &&
986 i < gen->shared_len + gen->n_block)
987 f = gen->block_dim[i - gen->shared_len];
989 c = isl_equality_alloc(isl_space_copy(dim));
990 isl_constraint_set_coefficient_si(c, isl_dim_in, i, f);
991 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
992 scale = isl_basic_map_add_constraint(scale, c);
995 isl_space_free(dim);
997 sched = isl_union_map_apply_range(sched,
998 isl_union_map_from_map(isl_map_from_basic_map(scale)));
1000 return sched;
1003 /* If we are not performing "wrapping" and if the user asked for it,
1004 * scale the "n_tile" loops starting at "first" of "sched" by gen->block_dim[i].
1006 static __isl_give isl_union_map *scale_access_tile_loops(struct cuda_gen *gen,
1007 __isl_take isl_union_map *sched, int len, int first, int n_tile)
1009 int i;
1010 isl_space *dim;
1011 isl_basic_map *scale;
1012 isl_constraint *c;
1014 if (gen->options->wrap)
1015 return sched;
1016 if (!gen->options->scale_tile_loops)
1017 return sched;
1019 dim = isl_union_map_get_space(sched);
1020 dim = isl_space_add_dims(dim, isl_dim_in, len);
1021 dim = isl_space_add_dims(dim, isl_dim_out, len);
1022 scale = isl_basic_map_universe(isl_space_copy(dim));
1024 for (i = 0; i < len; ++i) {
1025 int f = 1;
1027 if (i >= first && i < first + n_tile)
1028 f = gen->block_dim[i - first];
1030 c = isl_equality_alloc(isl_space_copy(dim));
1031 isl_constraint_set_coefficient_si(c, isl_dim_in, i, f);
1032 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
1033 scale = isl_basic_map_add_constraint(scale, c);
1036 isl_space_free(dim);
1038 sched = isl_union_map_apply_range(sched,
1039 isl_union_map_from_map(isl_map_from_basic_map(scale)));
1041 return sched;
1044 /* If print_user_stmt is set, we want to print the statements ourselves,
1045 * instead of relying on the C preprocessor. If so, we need to use
1046 * the stop option so that the domains will be saved on the statement
1047 * nodes.
1049 static void print_cloog_shared_body(struct cuda_gen *gen,
1050 __isl_keep isl_set *context, __isl_keep isl_union_map *sched, int len,
1051 void (*print_user_stmt)(struct gpucode_info *info,
1052 struct clast_user_stmt *s),
1053 int first_unroll)
1055 int i;
1056 CloogOptions *options;
1057 CloogDomain *cloog_context;
1058 CloogUnionDomain *ud;
1059 CloogInput *input;
1060 struct clast_stmt *stmt;
1061 char name[20];
1063 sched = isl_union_map_copy(sched);
1064 sched = isl_union_map_align_params(sched, isl_set_get_space(context));
1066 options = cloog_options_malloc(gen->state);
1067 options->language = LANGUAGE_C;
1068 options->strides = 1;
1069 options->sh = 1;
1070 options->f = len;
1071 options->l = -1;
1072 options->override = 1;
1073 options->save_domains = 1;
1074 options->noscalars = 1;
1075 options->first_unroll = first_unroll;
1077 ud = cloog_union_domain_from_isl_union_map(sched);
1078 for (i = 0; i < len; ++i) {
1079 snprintf(name, sizeof(name), "c%d", i);
1080 ud = cloog_union_domain_set_name(ud, CLOOG_SCAT, i, name);
1082 cloog_context = cloog_domain_from_isl_set(isl_set_copy(context));
1083 input = cloog_input_alloc(cloog_context, ud);
1085 stmt = cloog_clast_create_from_input(input, options);
1087 gen->stmt_code.indent = gen->kernel_code.indent;
1088 gen->stmt_code.dst = gen->cuda.kernel_c;
1089 gen->stmt_code.print_user_stmt = print_user_stmt;
1090 gen->stmt_code.print_user_stmt_list = NULL;
1091 gen->stmt_code.print_for_head = NULL;
1092 gen->stmt_code.print_for_foot = NULL;
1093 gen->stmt_code.user = gen;
1094 gpu_print_host_stmt(&gen->stmt_code, stmt);
1096 cloog_clast_free(stmt);
1097 cloog_options_free(options);
1100 /* Add "len" parameters p[i] called prefix%d,
1101 * with bounds to 0 <= p[i] < size[i].
1103 __isl_give isl_set *add_bounded_parameters(__isl_take isl_set *set,
1104 int len, int *size, const char *prefix)
1106 int i;
1107 unsigned nparam;
1108 isl_int v;
1109 isl_space *dim;
1110 isl_basic_set *bset;
1111 isl_constraint *c;
1112 char name[20];
1114 nparam = isl_set_dim(set, isl_dim_param);
1115 set = isl_set_add_dims(set, isl_dim_param, len);
1117 for (i = 0; i < len; ++i) {
1118 snprintf(name, sizeof(name), "%s%d", prefix, i);
1119 set = isl_set_set_dim_name(set, isl_dim_param,
1120 nparam + i, name);
1123 dim = isl_set_get_space(set);
1124 bset = isl_basic_set_universe(isl_space_copy(dim));
1126 isl_int_init(v);
1128 for (i = 0; i < len; ++i) {
1129 c = isl_inequality_alloc(isl_space_copy(dim));
1130 isl_int_set_si(v, 1);
1131 isl_constraint_set_coefficient(c, isl_dim_param, nparam + i, v);
1132 bset = isl_basic_set_add_constraint(bset, c);
1134 c = isl_inequality_alloc(isl_space_copy(dim));
1135 isl_int_set_si(v, -1);
1136 isl_constraint_set_coefficient(c, isl_dim_param, nparam + i, v);
1137 isl_int_set_si(v, size[i] - 1);
1138 isl_constraint_set_constant(c, v);
1139 bset = isl_basic_set_add_constraint(bset, c);
1142 isl_int_clear(v);
1143 isl_space_free(dim);
1145 return isl_set_intersect(set, isl_set_from_basic_set(bset));
1148 static void print_shared_body(struct cuda_gen *gen,
1149 __isl_keep isl_set *shared_domain, __isl_keep isl_union_map *sched,
1150 int len, void (*print_user_stmt)(struct gpucode_info *info,
1151 struct clast_user_stmt *s),
1152 int first_unroll)
1154 isl_set *context;
1156 context = isl_set_copy(shared_domain);
1157 context = parametrize(context, 0, gen->shared_len, "g");
1158 context = isl_set_project_out(context, isl_dim_set, 0, gen->shared_len);
1159 context = add_bounded_parameters(context,
1160 gen->n_block, gen->block_dim, "t");
1162 print_cloog_shared_body(gen, context, sched, len, print_user_stmt,
1163 first_unroll);
1165 isl_set_free(context);
1168 /* Given a tile of an array, construct a map that maps each element
1169 * of the tile to a copy of the tile shifted to the origin
1170 * (based on the lower bounds in group->private_bound or group->shared_bound).
1171 * If any of the indices is strided, then {private,shared}_bound[i].shift_map
1172 * is applied to the index first.
1173 * The domain of the resulting map is "access",
1174 * while the range space is anonymous.
1176 static __isl_give isl_map *shift_access(__isl_take isl_set *access,
1177 struct cuda_array_ref_group *group)
1179 int i;
1180 isl_space *dim;
1181 isl_basic_set *bset;
1182 isl_basic_map *bmap;
1183 isl_aff *lb;
1184 isl_basic_set *offset;
1185 isl_basic_map *shift;
1186 isl_basic_map *pre_shift;
1187 isl_map *sched;
1188 const char *name;
1189 struct cuda_array_bound *bounds;
1190 int n_index = group->array->n_index;
1192 bounds = group->private_bound;
1193 if (!bounds)
1194 bounds = group->shared_bound;
1196 dim = isl_set_get_space(access);
1197 dim = isl_space_drop_dims(dim, isl_dim_set, 0, n_index);
1198 offset = isl_basic_set_universe(dim);
1199 for (i = 0; i < n_index; ++i) {
1200 lb = isl_aff_copy(bounds[i].lb);
1201 bmap = isl_basic_map_from_aff(lb);
1202 bset = isl_basic_map_range(bmap);
1203 offset = isl_basic_set_flat_product(offset, bset);
1205 offset = isl_basic_set_neg(offset);
1207 dim = isl_space_map_from_set(isl_set_get_space(access));
1208 shift = isl_basic_map_identity(dim);
1209 shift = isl_basic_map_set_tuple_name(shift, isl_dim_out, NULL);
1211 bset = isl_basic_set_universe(isl_set_get_space(access));
1212 bmap = isl_basic_map_from_domain_and_range(bset, offset);
1214 shift = isl_basic_map_sum(shift, bmap);
1216 dim = isl_set_get_space(access);
1217 dim = isl_space_drop_dims(dim, isl_dim_set, 0, n_index);
1218 dim = isl_space_map_from_set(dim);
1219 pre_shift = isl_basic_map_universe(isl_space_copy(dim));
1220 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1221 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1222 for (i = 0; i < n_index; ++i) {
1223 if (!bounds[i].shift_map)
1224 bmap = isl_basic_map_identity(isl_space_copy(dim));
1225 else
1226 bmap = isl_basic_map_copy(bounds[i].shift_map);
1227 pre_shift = isl_basic_map_flat_product(pre_shift, bmap);
1229 isl_space_free(dim);
1230 name = isl_basic_map_get_tuple_name(shift, isl_dim_in);
1231 pre_shift = isl_basic_map_set_tuple_name(pre_shift, isl_dim_in, name);
1232 pre_shift = isl_basic_map_set_tuple_name(pre_shift, isl_dim_out, name);
1233 shift = isl_basic_map_apply_range(pre_shift, shift);
1235 sched = isl_map_from_basic_map(shift);
1236 sched = isl_map_intersect_domain(sched, access);
1238 return sched;
1241 /* Construct a schedule for iterating over all elements in the given
1242 * piece of an array. The schedule iterates over a copy of the piece
1243 * that is shifted to the origin.
1244 * We subsequently also perform the tiling/wrapping over the threads.
1246 * In particular, we tile the final iterators so that the final thread
1247 * dimension runs over the final array dimension.
1248 * However, if those final iterators have only a single iteration,
1249 * we try to tile earlier iterators instead.
1251 static __isl_give isl_union_map *access_schedule(struct cuda_gen *gen,
1252 __isl_take isl_set *access, struct cuda_array_ref_group *group)
1254 isl_space *dim;
1255 isl_map *sched;
1256 isl_union_map *usched;
1257 isl_map *tiling;
1258 isl_set *par;
1259 unsigned nvar = isl_set_dim(access, isl_dim_set);
1260 int n_tile;
1261 int first;
1263 sched = shift_access(access, group);
1265 n_tile = gen->n_block;
1266 if (n_tile > nvar) {
1267 int i;
1268 sched = isl_map_insert_dims(sched,
1269 isl_dim_out, 0, n_tile - nvar);
1270 for (i = 0; i < n_tile - nvar; ++i)
1271 sched = isl_map_fix_si(sched, isl_dim_out, i, 0);
1272 nvar = n_tile;
1275 first = nvar - n_tile;
1277 for (; first > 0; first --)
1278 if (!isl_map_plain_is_fixed(sched, isl_dim_out,
1279 first + n_tile - 1, NULL))
1280 break;
1282 dim = isl_map_get_space(sched);
1283 dim = isl_space_params(dim);
1284 if (gen->options->wrap)
1285 tiling = wrap(isl_space_copy(dim), nvar, first,
1286 n_tile, gen->block_dim);
1287 else
1288 tiling = tile(isl_space_copy(dim), nvar, first,
1289 n_tile, gen->block_dim);
1290 sched = isl_map_apply_range(sched, tiling);
1292 par = parametrization(dim, nvar + n_tile, first + n_tile, n_tile, "t");
1293 usched = isl_union_map_from_map(sched);
1294 usched = isl_union_map_intersect_range(usched,
1295 isl_union_set_from_set(par));
1297 usched = scale_access_tile_loops(gen, usched, nvar + n_tile,
1298 first, n_tile);
1300 return usched;
1303 static void print_shared_access(struct cuda_gen *gen,
1304 __isl_keep isl_set *shared_domain, __isl_take isl_set *access,
1305 const char *type, struct cuda_array_ref_group *group)
1307 const char *array_name;
1308 char *name;
1309 isl_ctx *ctx;
1310 isl_union_map *sched;
1311 unsigned nvar = isl_set_dim(access, isl_dim_set);
1312 int n_tile;
1314 ctx = isl_set_get_ctx(access);
1315 array_name = isl_set_get_tuple_name(access);
1316 name = isl_alloc_array(ctx, char,
1317 strlen(type) + sizeof("_shared_") + strlen(array_name) + 20);
1318 if (group->array->n_group > 1)
1319 sprintf(name, "%s_shared_%s_%d", type, array_name, group->nr);
1320 else
1321 sprintf(name, "%s_shared_%s", type, array_name);
1322 access = isl_set_set_tuple_name(access, name);
1323 free(name);
1325 sched = access_schedule(gen, access, group);
1327 n_tile = gen->n_block;
1328 if (n_tile > nvar)
1329 n_tile = nvar;
1331 print_shared_body(gen, shared_domain, sched, nvar + n_tile, NULL, -1);
1333 isl_union_map_free(sched);
1336 /* Return the union of all read (read = 1) and/or write (write = 1)
1337 * access relations in the group.
1339 static __isl_give isl_union_map *group_access_relation(
1340 struct cuda_array_ref_group *group, int read, int write)
1342 int i;
1343 isl_union_map *access;
1345 access = isl_union_map_empty(isl_map_get_space(group->access));
1346 for (i = 0; i < group->n_ref; ++i) {
1347 isl_map *map_i;
1349 if (!((read && group->refs[i]->read) ||
1350 (write && group->refs[i]->write)))
1351 continue;
1352 map_i = isl_map_copy(group->refs[i]->access);
1353 access = isl_union_map_union(access,
1354 isl_union_map_from_map(map_i));
1357 return access;
1360 /* Check that none of the shared memory tiles involve any strides.
1362 static int no_strides(struct cuda_array_ref_group *group)
1364 int i;
1365 int n_index = group->array->n_index;
1367 for (i = 0; i < n_index; ++i)
1368 if (group->shared_bound[i].shift)
1369 return 0;
1371 return 1;
1374 /* Return a set containing the values of the given index i
1375 * of the elements in the array tile in global memory that corresponds
1376 * to the shared memory copy.
1377 * In particular, if a is the index, we return a set with constraints
1379 * tile_offset <= a <= tile_offset + tile_size - 1
1381 * and
1383 * 0 <= a <= array_size - 1
1386 static __isl_give isl_set *group_tile_dim(struct cuda_array_ref_group *group,
1387 int i)
1389 isl_basic_set *tile;
1390 isl_aff *aff;
1391 isl_constraint *c;
1392 isl_local_space *ls;
1393 isl_pw_aff *bound;
1394 isl_set *dom;
1395 isl_set *tile_set;
1397 aff = isl_aff_copy(group->shared_bound[i].lb);
1398 aff = isl_aff_add_dims(aff, isl_dim_in, 1);
1399 ls = isl_aff_get_domain_local_space(aff);
1400 aff = isl_aff_neg(aff);
1401 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1402 c = isl_inequality_from_aff(isl_aff_copy(aff));
1403 tile = isl_basic_set_from_constraint(c);
1405 aff = isl_aff_neg(aff);
1406 aff = isl_aff_add_constant(aff, group->shared_bound[i].size);
1407 aff = isl_aff_add_constant_si(aff, -1);
1408 c = isl_inequality_from_aff(aff);
1409 tile = isl_basic_set_add_constraint(tile, c);
1411 aff = isl_aff_zero_on_domain(ls);
1412 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1413 c = isl_inequality_from_aff(aff);
1414 tile = isl_basic_set_add_constraint(tile, c);
1416 bound = isl_pw_aff_copy(group->array->bound[i]);
1417 bound = isl_pw_aff_add_dims(bound, isl_dim_in, 1);
1418 ls = isl_local_space_from_space(isl_pw_aff_get_domain_space(bound));
1419 aff = isl_aff_zero_on_domain(ls);
1420 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1421 aff = isl_aff_add_constant_si(aff, 1);
1422 dom = isl_pw_aff_domain(isl_pw_aff_copy(bound));
1424 tile_set = isl_pw_aff_ge_set(bound, isl_pw_aff_alloc(dom, aff));
1425 tile_set = isl_set_align_params(tile_set, isl_basic_set_get_space(tile));
1426 tile_set = isl_set_intersect(tile_set, isl_set_from_basic_set(tile));
1428 return tile_set;
1431 /* Return a set containing the elements in the array tile in
1432 * global memory that corresponds to the shared memory copy.
1434 static __isl_give isl_set *group_tile(struct cuda_array_ref_group *group)
1436 int i;
1437 int n_index = group->array->n_index;
1438 isl_set *tile;
1440 tile = group_tile_dim(group, 0);
1441 for (i = 1; i < n_index; ++i) {
1442 isl_set *tile_i;
1444 tile_i = group_tile_dim(group, i);
1445 tile = isl_set_flat_product(tile, tile_i);
1448 tile = isl_set_set_tuple_name(tile, group->array->name);
1450 return tile;
1453 /* Print code for reading into or writing from shared memory
1454 * the given array reference group.
1456 * sched maps the original iteration domains to the shared memory tile loops.
1458 * If we are performing a read from global memory to shared memory,
1459 * if the array involved is not a scalar and if the definition of the
1460 * shared memory tiles does not involve any strides, then we copy
1461 * the entire tile to shared memory. This may result in some extra
1462 * elements getting copied, but it should lead to simpler code
1463 * (which means that fewer registers may be needed) and less divergence.
1465 * Otherwise, we only copy the elements that will be read or have been written
1466 * in the kernel.
1468 * Note that the absence of stride requirement can easily be lifted.
1469 * We would just need to add constraints of the form
1471 * shift + a = stride * alpha
1473 static int print_group_shared_accesses(struct cuda_gen *gen,
1474 struct cuda_array_ref_group *group, const char *type,
1475 __isl_keep isl_set *shared_domain, __isl_keep isl_union_map *sched)
1477 int read;
1478 isl_union_map *access;
1479 isl_union_set *uset;
1480 isl_set *access_set;
1482 if (group->private_bound)
1483 return 0;
1484 if (!group->shared_bound)
1485 return 0;
1487 read = !strcmp(type, "read");
1489 access = group_access_relation(group, read, !read);
1490 access = isl_union_map_apply_domain(access, isl_union_map_copy(sched));
1491 uset = isl_union_map_range(access);
1493 if (isl_union_set_is_empty(uset)) {
1494 isl_union_set_free(uset);
1495 return 0;
1498 if (read && group->array->n_index > 0 && no_strides(group)) {
1499 isl_union_set_free(uset);
1500 access_set = group_tile(group);
1501 print_shared_access(gen, shared_domain, access_set,
1502 type, group);
1503 return 1;
1506 access_set = isl_set_from_union_set(uset);
1507 access_set = isl_set_coalesce(access_set);
1509 print_shared_access(gen, shared_domain, access_set, type, group);
1511 return 1;
1514 /* Print code for reading into or writing from shared memory at
1515 * the given level (-1 for innermost).
1517 * If we are not printing at the innermost level, then the dimensionality
1518 * of shared_domain may be smaller than gen->shared_len.
1519 * As the rest of the code assumes that the domain of access has
1520 * gen->shared_len dimensions, we therefore may need to embed this domain
1521 * in a higher dimensional space after intersection with shared_domain.
1523 static void print_shared_accesses(struct cuda_gen *gen,
1524 __isl_keep isl_set *shared_domain, __isl_keep isl_union_map *access,
1525 const char *type, int level)
1527 int i, j;
1528 isl_space *dim;
1529 isl_map *proj;
1530 isl_set *par;
1531 int shared_len = isl_set_dim(shared_domain, isl_dim_set);
1532 int sync = 0;
1533 isl_union_map *sched;
1535 shared_domain = isl_set_copy(shared_domain);
1536 sched = isl_union_map_copy(gen->tiled_sched);
1537 dim = isl_union_map_get_space(sched);
1538 proj = projection(dim, gen->tiled_len, shared_len);
1539 sched = isl_union_map_apply_range(sched, isl_union_map_from_map(proj));
1540 sched = isl_union_map_intersect_range(sched,
1541 isl_union_set_from_set(isl_set_copy(shared_domain)));
1542 if (shared_len != gen->shared_len) {
1543 dim = isl_union_map_get_space(sched);
1544 proj = projection(dim, gen->shared_len, shared_len);
1545 proj = isl_map_reverse(proj);
1546 shared_domain = isl_set_apply(shared_domain,
1547 isl_map_copy(proj));
1548 sched = isl_union_map_apply_range(sched,
1549 isl_union_map_from_map(proj));
1552 dim = isl_union_map_get_space(sched);
1553 par = parametrization(dim, gen->shared_len, 0, gen->shared_len, "g");
1554 sched = isl_union_map_intersect_range(sched,
1555 isl_union_set_from_set(par));
1557 for (i = 0; i < gen->n_array; ++i) {
1558 struct cuda_array_info *array = &gen->array[i];
1560 if (gen->array[i].print_shared_level != level)
1561 continue;
1563 for (j = 0; j < array->n_group; ++j) {
1564 if (print_group_shared_accesses(gen, array->groups[j],
1565 type, shared_domain, sched))
1566 sync = 1;
1570 isl_union_map_free(sched);
1571 isl_set_free(shared_domain);
1573 if (sync) {
1574 print_indent(gen->cuda.kernel_c, gen->kernel_code.indent);
1575 fprintf(gen->cuda.kernel_c, "__syncthreads();\n");
1579 /* Given an index expression into a tile of an array, adjust the expression
1580 * to a shift of the tile to the origin
1581 * (based on the lower bounds in array->shared_bound).
1582 * If the index is strided, then we first add
1583 * bound->shift and divide by bound->stride.
1585 static __isl_give isl_qpolynomial *shift_index(__isl_take isl_qpolynomial *qp,
1586 struct cuda_array_info *array,
1587 struct cuda_array_bound *bound, __isl_take isl_set *domain)
1589 isl_qpolynomial *lb;
1591 if (bound->shift) {
1592 isl_qpolynomial *shift, *t;
1593 isl_int one;
1594 isl_space *dim;
1595 shift = bound->shift;
1596 shift = isl_qpolynomial_copy(shift);
1597 shift = isl_qpolynomial_project_domain_on_params(shift);
1598 shift = isl_qpolynomial_align_params(shift,
1599 isl_qpolynomial_get_space(qp));
1600 qp = isl_qpolynomial_add(qp, shift);
1601 dim = isl_qpolynomial_get_domain_space(qp);
1602 isl_int_init(one);
1603 isl_int_set_si(one, 1);
1604 t = isl_qpolynomial_rat_cst_on_domain(dim, one, bound->stride);
1605 isl_int_clear(one);
1606 qp = isl_qpolynomial_mul(qp, t);
1609 lb = isl_qpolynomial_from_aff(isl_aff_copy(bound->lb));
1610 lb = isl_qpolynomial_project_domain_on_params(lb);
1612 lb = isl_qpolynomial_align_params(lb, isl_qpolynomial_get_space(qp));
1614 qp = isl_qpolynomial_sub(qp, lb);
1615 qp = isl_qpolynomial_gist(qp, domain);
1617 return qp;
1620 /* This function is called for each access to an array in some statement
1621 * in the original code.
1622 * Replace that access by an access to shared or (linearized) global memory.
1623 * Since the array in shared memory is just
1624 * a shifted copy of part of the original array, we simply need
1625 * to subtract the lower bound, which was computed
1626 * in can_tile_for_shared_memory.
1627 * If any of the indices is strided, then we first add
1628 * shared_bound[i].shift and divide by shared_bound[i].stride.
1630 * If the given array is accessed directly from global memory,
1631 * we don't need to perform any shifting and simply simplify
1632 * expression in the context of the domain instead.
1634 * If the array space (range of access) has no name, then we are
1635 * accessing an iterator in the original program.
1637 static void print_access(struct cuda_gen *gen, __isl_take isl_map *access,
1638 int group_nr)
1640 int i;
1641 const char *name;
1642 unsigned n_index;
1643 struct cuda_array_info *array = NULL;
1644 isl_printer *prn;
1645 isl_basic_set *aff;
1646 isl_set *data_set;
1647 isl_set *domain;
1648 struct cuda_array_bound *bounds = NULL;
1650 access = isl_map_align_params(access,
1651 isl_set_get_space(gen->stmt_domain));
1653 data_set = isl_set_apply(isl_set_copy(gen->stmt_domain), access);
1655 name = isl_set_get_tuple_name(data_set);
1657 if (!name)
1658 fprintf(gen->cuda.kernel_c, "(");
1659 else {
1660 struct cuda_array_ref_group *group;
1662 for (i = 0; i < gen->n_array; ++i) {
1663 if (strcmp(name, gen->array[i].name))
1664 continue;
1665 array = &gen->array[i];
1667 assert(array);
1668 group = array->groups[group_nr];
1669 bounds = group->private_bound;
1670 if (!bounds)
1671 bounds = group->shared_bound;
1673 print_array_name(gen->cuda.kernel_c, group);
1675 if (cuda_array_is_scalar(array)) {
1676 isl_set_free(data_set);
1677 return;
1680 fprintf(gen->cuda.kernel_c, "[");
1684 n_index = isl_set_dim(data_set, isl_dim_set);
1685 aff = isl_set_affine_hull(data_set);
1687 prn = isl_printer_to_file(gen->ctx, gen->cuda.kernel_c);
1688 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
1690 if (!bounds)
1691 for (i = 0; i + 1 < n_index; ++i)
1692 prn = isl_printer_print_str(prn, "(");
1694 for (i = 0; i < n_index; ++i) {
1695 isl_constraint *c;
1696 isl_qpolynomial *qp;
1697 int ok;
1699 ok = isl_basic_set_has_defining_equality(aff,
1700 isl_dim_out, i, &c);
1701 assert(ok);
1702 qp = isl_qpolynomial_from_constraint(c, isl_dim_out, i);
1703 qp = isl_qpolynomial_project_domain_on_params(qp);
1705 if (!array) {
1706 prn = isl_printer_print_qpolynomial(prn, qp);
1707 isl_qpolynomial_free(qp);
1708 continue;
1711 domain = isl_set_copy(gen->stmt_domain);
1712 domain = isl_set_project_out(domain, isl_dim_set, 0,
1713 isl_set_dim(domain, isl_dim_set));
1714 if (!bounds)
1715 qp = isl_qpolynomial_gist(qp, domain);
1716 else
1717 qp = shift_index(qp, array, &bounds[i], domain);
1719 if (i) {
1720 if (!bounds) {
1721 prn = isl_printer_print_str(prn, ") * (");
1722 prn = isl_printer_print_pw_aff(prn,
1723 array->local_bound[i]);
1724 prn = isl_printer_print_str(prn, ") + ");
1725 } else
1726 prn = isl_printer_print_str(prn, "][");
1728 prn = isl_printer_print_qpolynomial(prn, qp);
1729 isl_qpolynomial_free(qp);
1731 if (!name)
1732 prn = isl_printer_print_str(prn, ")");
1733 else
1734 prn = isl_printer_print_str(prn, "]");
1735 isl_printer_free(prn);
1737 isl_basic_set_free(aff);
1740 static struct cuda_stmt_access *print_expr(struct cuda_gen *gen, FILE *out,
1741 struct pet_expr *expr, struct cuda_stmt_access *access, int outer)
1743 int i;
1745 switch (expr->type) {
1746 case pet_expr_double:
1747 fprintf(out, "%g", expr->d);
1748 break;
1749 case pet_expr_access:
1750 print_access(gen, isl_map_copy(access->access), access->group);
1751 access = access->next;
1752 break;
1753 case pet_expr_unary:
1754 if (!outer)
1755 fprintf(out, "(");
1756 fprintf(out, " %s ", pet_op_str(expr->op));
1757 access = print_expr(gen, out, expr->args[pet_un_arg],
1758 access, 0);
1759 if (!outer)
1760 fprintf(out, ")");
1761 break;
1762 case pet_expr_binary:
1763 if (!outer)
1764 fprintf(out, "(");
1765 access = print_expr(gen, out, expr->args[pet_bin_lhs],
1766 access, 0);
1767 fprintf(out, " %s ", pet_op_str(expr->op));
1768 access = print_expr(gen, out, expr->args[pet_bin_rhs],
1769 access, 0);
1770 if (!outer)
1771 fprintf(out, ")");
1772 break;
1773 case pet_expr_ternary:
1774 if (!outer)
1775 fprintf(out, "(");
1776 access = print_expr(gen, out, expr->args[pet_ter_cond],
1777 access, 0);
1778 fprintf(out, " ? ");
1779 access = print_expr(gen, out, expr->args[pet_ter_true],
1780 access, 0);
1781 fprintf(out, " : ");
1782 access = print_expr(gen, out, expr->args[pet_ter_false],
1783 access, 0);
1784 if (!outer)
1785 fprintf(out, ")");
1786 break;
1787 case pet_expr_call:
1788 fprintf(out, "%s(", expr->name);
1789 for (i = 0; i < expr->n_arg; ++i) {
1790 if (i)
1791 fprintf(out, ", ");
1792 access = print_expr(gen, out, expr->args[i],
1793 access, 1);
1795 fprintf(out, ")");
1797 return access;
1800 static void print_stmt_body(struct cuda_gen *gen,
1801 FILE *out, struct cuda_stmt *stmt)
1803 print_expr(gen, out, stmt->body, stmt->accesses, 1);
1804 fprintf(out, ";\n");
1807 /* This function is called for each leaf in the innermost clast,
1808 * i.e., for each statemetn.
1809 * We print the statement body, simplifying the accesses based
1810 * on the schedule.
1812 static void print_statement(struct gpucode_info *code,
1813 struct clast_user_stmt *u)
1815 struct cuda_gen *gen = code->user;
1816 isl_space *dim;
1817 isl_set *par;
1818 isl_set *stmt_domain;
1819 isl_union_map *stmt_sched;
1820 isl_union_set *uset;
1821 int nr;
1822 struct cuda_stmt *stmt;
1824 nr = atoi(u->statement->name + 2);
1825 stmt = &gen->stmts[nr];
1827 stmt_domain = extract_host_domain(u);
1829 stmt_sched = isl_union_map_intersect_range(
1830 isl_union_map_copy(gen->local_sched),
1831 isl_union_set_from_set(extend(stmt_domain,
1832 gen->thread_tiled_len)));
1833 dim = isl_union_map_get_space(stmt_sched);
1834 par = parametrization(dim, gen->thread_tiled_len, 0,
1835 gen->thread_tiled_len, "c");
1836 stmt_sched = isl_union_map_intersect_range(stmt_sched,
1837 isl_union_set_from_set(par));
1839 uset = isl_union_map_domain(stmt_sched);
1840 dim = isl_union_set_get_space(uset);
1841 dim = isl_space_add_dims(dim, isl_dim_set,
1842 isl_set_dim(stmt->domain, isl_dim_set));
1843 dim = isl_space_set_tuple_name(dim, isl_dim_set, u->statement->name);
1844 gen->stmt_domain = isl_union_set_extract_set(uset, dim);
1845 isl_union_set_free(uset);
1847 print_indent(code->dst, code->indent);
1848 print_stmt_body(gen, code->dst, stmt);
1850 isl_set_free(gen->stmt_domain);
1853 /* Print an access to the element in the global memory copy of the
1854 * given array that corresponds to element [qp[0]][qp[1]]...
1855 * of the original array.
1856 * The copy in global memory has been linearized, so we need to take
1857 * the array size into account.
1859 static void print_private_global_index(isl_ctx *ctx, FILE *out,
1860 struct cuda_array_info *array, __isl_keep isl_qpolynomial **qp)
1862 int i;
1863 isl_printer *prn;
1865 fprintf(out, "%s[", array->name);
1866 prn = isl_printer_to_file(ctx, out);
1867 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
1868 for (i = 0; i + 1 < array->n_index; ++i)
1869 prn = isl_printer_print_str(prn, "(");
1870 for (i = 0; i < array->n_index; ++i) {
1871 if (i) {
1872 prn = isl_printer_print_str(prn, ") * (");
1873 prn = isl_printer_print_pw_aff(prn,
1874 array->local_bound[i]);
1875 prn = isl_printer_print_str(prn, ") + ");
1877 prn = isl_printer_print_qpolynomial(prn, qp[i]);
1879 isl_printer_free(prn);
1880 fprintf(out, "]");
1883 /* Print an access to the element in the shared memory copy of the
1884 * given array reference group that corresponds to element [qps[0]][qps[1]]...
1885 * of the original array.
1886 * Since the array in shared memory is just a shifted copy of part
1887 * of the original array, we simply need to subtract the lower bound,
1888 * which was computed in can_tile_for_shared_memory.
1889 * If any of the indices is strided, then we first add
1890 * shared_bound[i].shift and divide by shared_bound[i].stride.
1892 static void print_private_local_index(isl_ctx *ctx, FILE *out,
1893 struct cuda_array_ref_group *group,
1894 __isl_keep isl_qpolynomial **qps, __isl_keep isl_set *domain)
1896 int i;
1897 isl_printer *prn;
1898 struct cuda_array_info *array = group->array;
1899 struct cuda_array_bound *bounds = group->private_bound;
1901 print_array_name(out, group);
1902 for (i = 0; i < array->n_index; ++i) {
1903 isl_qpolynomial *qp = isl_qpolynomial_copy(qps[i]);
1905 qp = shift_index(qp, array, &bounds[i], isl_set_copy(domain));
1907 fprintf(out, "[");
1908 prn = isl_printer_to_file(ctx, out);
1909 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
1910 prn = isl_printer_print_qpolynomial(prn, qp);
1911 isl_printer_free(prn);
1912 fprintf(out, "]");
1913 isl_qpolynomial_free(qp);
1917 /* This function is called for each leaf in the clast of the code
1918 * for copying to or from private memory.
1919 * The statement name is read_private_<array> or write_private_<array>.
1921 * The schedule iterates over the array elements, so we can use
1922 * the domain of private_sched at the current scheduling position
1923 * as the index of the array.
1925 static void print_private_copy_statement(struct gpucode_info *code,
1926 struct clast_user_stmt *u)
1928 struct cuda_gen *gen = code->user;
1929 isl_set *domain;
1930 isl_map *sched;
1931 struct cuda_array_ref_group *group = gen->private_group;
1932 int i;
1933 unsigned n_in;
1934 unsigned n_out;
1935 isl_space *dim;
1936 isl_set *param;
1937 isl_set *index;
1938 isl_basic_set *aff;
1939 isl_ctx *ctx;
1940 isl_qpolynomial **qp;
1941 int read;
1943 read = !strncmp(u->statement->name, "read", 4);
1945 domain = extract_host_domain(u);
1946 assert(domain);
1948 sched = isl_map_copy(gen->private_sched);
1949 sched = isl_map_reverse(sched);
1950 sched = isl_map_intersect_domain(sched, domain);
1951 n_in = isl_map_dim(sched, isl_dim_in);
1952 n_out = isl_map_dim(sched, isl_dim_out);
1953 dim = isl_map_get_space(sched);
1954 dim = isl_space_drop_dims(dim, isl_dim_in, 0, n_in);
1955 dim = isl_space_drop_dims(dim, isl_dim_out, 0, n_out);
1956 param = parametrization(dim, n_in, 0, n_in, "c");
1957 sched = isl_map_align_params(sched, isl_set_get_space(param));
1958 sched = isl_map_intersect_domain(sched, param);
1959 index = isl_map_range(sched);
1960 domain = isl_set_copy(index);
1961 aff = isl_set_affine_hull(index);
1962 domain = isl_set_project_out(domain, isl_dim_set, 0, n_out);
1964 ctx = isl_basic_set_get_ctx(aff);
1965 qp = isl_alloc_array(ctx, isl_qpolynomial *, n_out);
1966 assert(qp);
1968 for (i = 0; i < n_out; ++i) {
1969 isl_constraint *c;
1970 int ok;
1972 ok = isl_basic_set_has_defining_equality(aff,
1973 isl_dim_set, i, &c);
1974 assert(ok);
1975 qp[i] = isl_qpolynomial_from_constraint(c, isl_dim_set, i);
1976 qp[i] = isl_qpolynomial_project_domain_on_params(qp[i]);
1979 print_indent(code->dst, code->indent);
1980 if (read) {
1981 print_private_local_index(ctx, code->dst, group, qp, domain);
1982 fprintf(code->dst, " = ");
1983 print_private_global_index(ctx, code->dst, group->array, qp);
1984 } else {
1985 print_private_global_index(ctx, code->dst, group->array, qp);
1986 fprintf(code->dst, " = ");
1987 print_private_local_index(ctx, code->dst, group, qp, domain);
1989 fprintf(code->dst, ";\n");
1991 for (i = 0; i < n_out; ++i)
1992 isl_qpolynomial_free(qp[i]);
1993 free(qp);
1995 isl_basic_set_free(aff);
1996 isl_set_free(domain);
1999 static void print_private_access(struct cuda_gen *gen,
2000 __isl_keep isl_set *shared_domain, __isl_take isl_set *access,
2001 const char *type, struct cuda_array_ref_group *group)
2003 const char *array_name;
2004 char *name;
2005 isl_ctx *ctx;
2006 unsigned nvar = isl_set_dim(access, isl_dim_set);
2007 isl_union_map *usched;
2009 if (isl_set_fast_is_empty(access)) {
2010 isl_set_free(access);
2011 return;
2014 ctx = isl_set_get_ctx(access);
2015 array_name = isl_set_get_tuple_name(access);
2016 name = isl_alloc_array(ctx, char,
2017 strlen(type) + sizeof("_private_") + strlen(array_name) + 20);
2018 if (group->array->n_group > 1)
2019 sprintf(name, "%s_private_%s_%d", type, array_name, group->nr);
2020 else
2021 sprintf(name, "%s_private_%s", type, array_name);
2022 access = isl_set_set_tuple_name(access, name);
2023 free(name);
2025 gen->private_sched = shift_access(access, group);
2026 gen->private_group = group;
2028 usched = isl_union_map_from_map(isl_map_copy(gen->private_sched));
2029 print_shared_body(gen, shared_domain, usched, nvar,
2030 &print_private_copy_statement, 1);
2031 isl_union_map_free(usched);
2033 isl_map_free(gen->private_sched);
2036 /* Print code for reading into or writing from private memory
2037 * the given array reference group.
2039 * sched maps the original iteration domains to the shared memory tile loops.
2041 static void print_group_private_accesses(struct cuda_gen *gen,
2042 struct cuda_array_ref_group *group,
2043 const char *type, __isl_keep isl_set *shared_domain,
2044 unsigned first_shared, int shared_len, __isl_keep isl_union_map *sched)
2046 int read;
2047 isl_union_map *access;
2048 isl_union_set *uset;
2049 isl_set *access_set;
2051 if (!group->private_bound)
2052 return;
2054 read = !strcmp(type, "read");
2056 access = group_access_relation(group, read, !read);
2057 access = isl_union_map_apply_domain(access, isl_union_map_copy(sched));
2058 access = isl_union_map_intersect(access,
2059 isl_union_map_copy(gen->private_access));
2060 uset = isl_union_map_range(access);
2062 if (isl_union_set_is_empty(uset)) {
2063 isl_union_set_free(uset);
2064 return;
2067 access_set = isl_set_from_union_set(uset);
2068 access_set = isl_set_coalesce(access_set);
2069 access_set = isl_set_eliminate(access_set, isl_dim_param,
2070 first_shared + shared_len,
2071 gen->shared_len - shared_len);
2073 print_private_access(gen, shared_domain, access_set, type, group);
2076 /* Print code for reading into or writing from private memory at
2077 * the given level (-1 for innermost).
2079 * If we are not printing at the innermost level, then the dimensionality
2080 * of shared_domain may be smaller than gen->shared_len.
2081 * As the rest of the code assumes that the domain of access has
2082 * gen->shared_len dimensions, we therefore may need to embed this domain
2083 * in a higher dimensional space after intersection with shared_domain.
2085 * This code is very similar to print_shared_accesses.
2086 * The main difference is that we to take into account gen->private_access.
2088 static void print_private_accesses(struct cuda_gen *gen,
2089 __isl_keep isl_set *shared_domain, __isl_keep isl_union_map *access,
2090 const char *type, int level)
2092 int i, j;
2093 isl_space *dim;
2094 isl_map *proj;
2095 int shared_len = isl_set_dim(shared_domain, isl_dim_set);
2096 unsigned first_shared;
2097 isl_union_map *sched;
2099 shared_domain = isl_set_copy(shared_domain);
2100 sched = isl_union_map_copy(gen->tiled_sched);
2101 dim = isl_union_map_get_space(sched);
2102 first_shared = isl_space_dim(dim, isl_dim_param);
2103 proj = projection(dim, gen->tiled_len, shared_len);
2104 sched = isl_union_map_apply_range(sched, isl_union_map_from_map(proj));
2105 sched = isl_union_map_intersect_range(sched,
2106 isl_union_set_from_set(isl_set_copy(shared_domain)));
2107 if (shared_len != gen->shared_len) {
2108 dim = isl_union_map_get_space(sched);
2109 proj = projection(dim, gen->shared_len, shared_len);
2110 proj = isl_map_reverse(proj);
2111 shared_domain = isl_set_apply(shared_domain,
2112 isl_map_copy(proj));
2113 sched = isl_union_map_apply_range(sched,
2114 isl_union_map_from_map(proj));
2117 for (i = 0; i < gen->n_array; ++i) {
2118 struct cuda_array_info *array = &gen->array[i];
2120 if (gen->array[i].print_shared_level != level)
2121 continue;
2123 for (j = 0; j < array->n_group; ++j)
2124 print_group_private_accesses(gen, array->groups[j],
2125 type, shared_domain,
2126 first_shared, shared_len, sched);
2129 isl_union_map_free(sched);
2130 isl_set_free(shared_domain);
2133 /* Set unroll[j] if the input dimension j is involved in
2134 * the index expression represented by bmap.
2136 static int check_unroll(__isl_take isl_basic_map *bmap, void *user)
2138 int i, j;
2139 int n_in = isl_basic_map_dim(bmap, isl_dim_in);
2140 int n_out = isl_basic_map_dim(bmap, isl_dim_out);
2141 int *unroll = user;
2143 for (i = 0; i < n_out; ++i) {
2144 isl_constraint *c;
2145 int ok;
2147 ok = isl_basic_map_has_defining_equality(bmap,
2148 isl_dim_out, i, &c);
2149 assert(ok);
2150 for (j = 0; j < n_in; ++j)
2151 if (isl_constraint_involves_dims(c, isl_dim_in, j, 1))
2152 unroll[j] = 1;
2153 isl_constraint_free(c);
2156 isl_basic_map_free(bmap);
2157 return 0;
2160 /* Given an array pos mapping input dimensions to the corresponding
2161 * output dimension, construct the corresponding map.
2163 static __isl_give isl_map *permutation(__isl_take isl_space *dim,
2164 int *pos, int len)
2166 int i;
2167 isl_constraint *c;
2168 isl_basic_map *bmap;
2170 dim = isl_space_add_dims(dim, isl_dim_in, len);
2171 dim = isl_space_add_dims(dim, isl_dim_out, len);
2172 bmap = isl_basic_map_universe(isl_space_copy(dim));
2174 for (i = 0; i < len; ++i) {
2175 c = isl_equality_alloc(isl_space_copy(dim));
2176 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
2177 isl_constraint_set_coefficient_si(c, isl_dim_out, pos[i], 1);
2178 bmap = isl_basic_map_add_constraint(bmap, c);
2180 isl_space_free(dim);
2182 return isl_map_from_basic_map(bmap);
2185 /* Find all loops involved in any of the index expressions for any of
2186 * the private accesses, move them innermost and then mark them as
2187 * requiring unrolling by setting gen->first_unroll.
2188 * The loops involved should all be parallel because of the checks
2189 * we performed in check_private_group_access. Moving them innermost
2190 * is therefore a valid transformation.
2192 static __isl_give isl_union_map *interchange_for_unroll(struct cuda_gen *gen,
2193 __isl_take isl_union_map *sched)
2195 int i, j;
2196 int unroll[gen->thread_tiled_len];
2197 int perm[gen->thread_tiled_len];
2198 isl_space *dim;
2199 isl_map *permute;
2200 int len = gen->shared_len + gen->n_parallel + gen->n_block;
2202 gen->first_unroll = -1;
2204 for (i = 0; i < gen->thread_tiled_len; ++i)
2205 unroll[i] = 0;
2206 for (i = 0; i < gen->n_array; ++i) {
2207 struct cuda_array_info *array = &gen->array[i];
2209 for (j = 0; j < array->n_group; ++j) {
2210 isl_union_map *access;
2211 isl_map *acc;
2213 if (!array->groups[j]->private_bound)
2214 continue;
2216 access = group_access_relation(array->groups[j], 1, 1);
2217 access = isl_union_map_apply_domain(access,
2218 isl_union_map_copy(sched));
2220 acc = isl_map_from_union_map(access);
2221 isl_map_foreach_basic_map(acc, &check_unroll, unroll);
2223 isl_map_free(acc);
2227 for (i = 0; i < gen->shared_len; ++i)
2228 if (unroll[i])
2229 return sched;
2231 for (i = gen->shared_len; i < len; ++i)
2232 if (unroll[i])
2233 break;
2235 if (i >= len)
2236 return sched;
2238 for (i = len; i < gen->thread_tiled_len; ++i)
2239 if (unroll[i])
2240 return sched;
2242 j = 0;
2243 for (i = 0; i < gen->thread_tiled_len; ++i)
2244 if (!unroll[i])
2245 perm[i] = j++;
2246 gen->first_unroll = 1 + j;
2247 for (i = 0; i < len; ++i)
2248 if (unroll[i])
2249 perm[i] = j++;
2251 dim = isl_union_map_get_space(sched);
2252 permute = permutation(dim, perm, gen->thread_tiled_len);
2253 sched = isl_union_map_apply_range(sched,
2254 isl_union_map_from_map(permute));
2256 return sched;
2259 /* This function is called for each leaf in the clast of the kernel code.
2260 * We first specialize the schedule to the site of the leaf and
2261 * print code for reading into shared memory, performing the actual
2262 * computations and writing from shared memory, with the required
2263 * synchronizations.
2265 static void print_kernel_user(struct gpucode_info *code,
2266 struct clast_user_stmt *u)
2268 struct cuda_gen *gen = code->user;
2269 isl_set *shared_domain;
2271 shared_domain = extract_entire_host_domain(u);
2273 print_shared_accesses(gen, shared_domain, gen->read, "read", -1);
2275 print_private_accesses(gen, shared_domain, gen->read, "read", -1);
2277 print_shared_body(gen, shared_domain, gen->local_sched,
2278 gen->thread_tiled_len, &print_statement,
2279 gen->first_unroll);
2281 print_private_accesses(gen, shared_domain, gen->write, "write", -1);
2283 print_indent(gen->cuda.kernel_c, gen->kernel_code.indent);
2284 fprintf(gen->cuda.kernel_c, "__syncthreads();\n");
2286 print_shared_accesses(gen, shared_domain, gen->write, "write", -1);
2288 isl_set_free(shared_domain);
2291 /* Check if we need to perform any copying to shared memory at this level
2292 * and if so, print the copying instructions.
2293 * Any array for which we are allowed to print copying instructions at
2294 * this level, but haven't done so already, is printed.
2296 static void print_kernel_for_head(struct gpucode_info *code,
2297 struct clast_for *f)
2299 int i;
2300 struct cuda_gen *gen = code->user;
2301 isl_set *domain;
2302 int level;
2303 int print = 0;
2305 domain = isl_set_from_cloog_domain(cloog_domain_copy(f->domain));
2306 level = isl_set_dim(domain, isl_dim_set) - 1;
2308 for (i = 0; i < gen->n_array; ++i) {
2309 if (gen->array[i].print_shared_level >= 0)
2310 continue;
2311 if (gen->array[i].last_shared > level)
2312 continue;
2313 gen->array[i].print_shared_level = level;
2314 print = 1;
2317 if (print) {
2318 print_shared_accesses(gen, domain, gen->read, "read", level);
2319 print_private_accesses(gen, domain, gen->read, "read", level);
2322 isl_set_free(domain);
2325 /* Print instructions for copying from shared memory for each array
2326 * for which print_kernel_for_head has added copying instructions
2327 * to shared memory.
2329 static void print_kernel_for_foot(struct gpucode_info *code,
2330 struct clast_for *f)
2332 int i;
2333 struct cuda_gen *gen = code->user;
2334 isl_set *domain;
2335 int level;
2336 int print = 0;
2338 domain = isl_set_from_cloog_domain(cloog_domain_copy(f->domain));
2339 level = isl_set_dim(domain, isl_dim_set) - 1;
2341 for (i = 0; i < gen->n_array; ++i) {
2342 if (gen->array[i].print_shared_level != level)
2343 continue;
2344 print = 1;
2345 break;
2348 if (print) {
2349 print_private_accesses(gen, domain, gen->write, "write", level);
2350 print_shared_accesses(gen, domain, gen->write, "write", level);
2353 isl_set_free(domain);
2356 /* Use CLooG to generate code for the outer gen->shared_first loops
2357 * of the local schedule "sched".
2358 * The pretty printing of this code is handled by gpu_print_host_stmt,
2359 * which calls print_kernel_user for each iteration of the shared tile loops.
2361 static void print_cloog_kernel_body(struct cuda_gen *gen,
2362 __isl_keep isl_set *context, __isl_keep isl_union_map *sched)
2364 int i;
2365 CloogOptions *options;
2366 CloogDomain *cloog_context;
2367 CloogUnionDomain *ud;
2368 CloogInput *input;
2369 struct clast_stmt *stmt;
2370 char name[20];
2372 sched = isl_union_map_copy(sched);
2373 sched = isl_union_map_align_params(sched, isl_set_get_space(context));
2375 options = cloog_options_malloc(gen->state);
2376 options->language = LANGUAGE_C;
2377 options->strides = 1;
2378 options->sh = 1;
2379 options->stop = gen->shared_len;
2380 options->f = gen->tiled_len;
2381 options->l = gen->tiled_len;
2382 options->save_domains = 1;
2383 options->noscalars = 1;
2385 ud = cloog_union_domain_from_isl_union_map(sched);
2386 for (i = 0; i < gen->shared_len; ++i) {
2387 snprintf(name, sizeof(name), "g%d", i);
2388 ud = cloog_union_domain_set_name(ud, CLOOG_SCAT, i, name);
2390 cloog_context = cloog_domain_from_isl_set(isl_set_copy(context));
2391 input = cloog_input_alloc(cloog_context, ud);
2393 stmt = cloog_clast_create_from_input(input, options);
2395 gen->kernel_code.indent = 4;
2396 gen->kernel_code.dst = gen->cuda.kernel_c;
2397 gen->kernel_code.print_user_stmt = NULL;
2398 gen->kernel_code.print_user_stmt_list = &print_kernel_user;
2399 gen->kernel_code.print_for_head = &print_kernel_for_head;
2400 gen->kernel_code.print_for_foot = &print_kernel_for_foot;
2401 gen->kernel_code.user = gen;
2402 gpu_print_host_stmt(&gen->kernel_code, stmt);
2404 cloog_clast_free(stmt);
2405 cloog_options_free(options);
2408 static void print_kernel_iterators(struct cuda_gen *gen)
2410 int i;
2411 const char *block_dims[] = { "blockIdx.x", "blockIdx.y" };
2412 const char *thread_dims[] = { "threadIdx.x", "threadIdx.y",
2413 "threadIdx.z" };
2415 if (gen->n_grid > 0) {
2416 print_indent(gen->cuda.kernel_c, 4);
2417 fprintf(gen->cuda.kernel_c, "int ");
2418 for (i = 0; i < gen->n_grid; ++i) {
2419 if (i)
2420 fprintf(gen->cuda.kernel_c, ", ");
2421 fprintf(gen->cuda.kernel_c, "b%d = %s",
2422 i, block_dims[gen->n_grid - 1 - i]);
2424 fprintf(gen->cuda.kernel_c, ";\n");
2427 if (gen->n_block > 0) {
2428 print_indent(gen->cuda.kernel_c, 4);
2429 fprintf(gen->cuda.kernel_c, "int ");
2430 for (i = 0; i < gen->n_block; ++i) {
2431 if (i)
2432 fprintf(gen->cuda.kernel_c, ", ");
2433 fprintf(gen->cuda.kernel_c, "t%d = %s",
2434 i, thread_dims[gen->n_block - 1 - i]);
2436 fprintf(gen->cuda.kernel_c, ";\n");
2440 static void print_group_shared_array(struct cuda_gen *gen,
2441 struct cuda_array_ref_group *group)
2443 int j;
2444 struct cuda_array_bound *bounds;
2446 bounds = group->private_bound;
2447 if (!bounds)
2448 bounds = group->shared_bound;
2449 if (!bounds)
2450 return;
2452 print_indent(gen->cuda.kernel_c, 4);
2453 fprintf(gen->cuda.kernel_c, "%s%s ",
2454 group->private_bound ? "" : "__shared__ ", group->array->type);
2455 print_array_name(gen->cuda.kernel_c, group);
2456 for (j = 0; j < group->array->n_index; ++j) {
2457 fprintf(gen->cuda.kernel_c, "[");
2458 isl_int_print(gen->cuda.kernel_c, bounds[j].size, 0);
2459 fprintf(gen->cuda.kernel_c, "]");
2461 fprintf(gen->cuda.kernel_c, ";\n");
2464 static void print_shared_arrays(struct cuda_gen *gen)
2466 int i, j;
2468 for (i = 0; i < gen->n_array; ++i) {
2469 struct cuda_array_info *array = &gen->array[i];
2471 for (j = 0; j < array->n_group; ++j)
2472 print_group_shared_array(gen, array->groups[j]);
2476 static void print_kernel_body(struct cuda_gen *gen,
2477 __isl_keep isl_set *host_domain, __isl_keep isl_union_map *sched)
2479 isl_set *context;
2481 context = isl_set_copy(host_domain);
2482 context = parametrize(context, 0, gen->tile_first, "h");
2483 context = isl_set_project_out(context, isl_dim_set, 0, gen->tile_first);
2484 context = add_bounded_parameters(context,
2485 gen->n_grid, gen->grid_dim, "b");
2487 print_kernel_iterators(gen);
2488 print_shared_arrays(gen);
2490 fprintf(gen->cuda.kernel_c, "\n");
2492 print_cloog_kernel_body(gen, context, sched);
2494 isl_set_free(context);
2497 /* Given a constraint
2499 * a(p,i) + j = g f(e)
2501 * or -a(p,i) - j = g f(e) if sign < 0,
2502 * store a(p,i) in bound->shift and g (stride) in bound->stride.
2503 * a(p,i) is assumed to be an expression in only the parameters.
2505 static void extract_stride(__isl_keep isl_constraint *c,
2506 struct cuda_array_bound *bound, isl_int stride, int sign)
2508 int i;
2509 isl_int v;
2510 isl_int one;
2511 isl_space *dim;
2512 unsigned nparam;
2513 isl_qpolynomial *qp;
2515 isl_int_set(bound->stride, stride);
2517 dim = isl_constraint_get_space(c);
2518 dim = isl_space_params(dim);
2520 nparam = isl_space_dim(dim, isl_dim_param);
2522 isl_int_init(v);
2523 isl_int_init(one);
2524 isl_int_set_si(one, 1);
2526 isl_constraint_get_constant(c, &v);
2527 if (sign < 0)
2528 isl_int_neg(v, v);
2529 qp = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim), v, one);
2531 for (i = 0; i < nparam; ++i) {
2532 isl_qpolynomial *t, *p;
2534 isl_constraint_get_coefficient(c, isl_dim_param, i, &v);
2535 if (isl_int_is_zero(v))
2536 continue;
2537 if (sign < 0)
2538 isl_int_neg(v, v);
2539 t = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim), v, one);
2540 p = isl_qpolynomial_var_on_domain(isl_space_copy(dim), isl_dim_param, i);
2541 t = isl_qpolynomial_mul(t, p);
2542 qp = isl_qpolynomial_add(qp, t);
2545 isl_space_free(dim);
2546 isl_int_clear(one);
2547 isl_int_clear(v);
2549 bound->shift = qp;
2552 /* Given an equality constraint of a map with a single output dimension j,
2553 * check if the constraint is of the form
2555 * a(p,i) + j = g f(e)
2557 * with a(p,i) an expression in the parameters and input dimensions
2558 * and f(e) an expression in the existentially quantified variables.
2559 * If so, and if g is larger than any such g from a previously considered
2560 * constraint, then call extract_stride. to record the stride information
2561 * in bound.
2563 static int check_stride_constraint(__isl_take isl_constraint *c, void *user)
2565 int i;
2566 isl_int v, stride;
2567 unsigned n_div;
2568 struct cuda_array_bound *bound = user;
2570 isl_int_init(v);
2571 isl_int_init(stride);
2573 n_div = isl_constraint_dim(c, isl_dim_div);
2574 isl_constraint_get_coefficient(c, isl_dim_out, 0, &v);
2576 if (n_div && (isl_int_is_one(v) || isl_int_is_negone(v))) {
2577 int s = isl_int_sgn(v);
2578 isl_int_set_si(stride, 0);
2579 for (i = 0; i < n_div; ++i) {
2580 isl_constraint_get_coefficient(c, isl_dim_div, i, &v);
2581 isl_int_gcd(stride, stride, v);
2583 if (!isl_int_is_zero(stride) &&
2584 isl_int_gt(stride, bound->stride))
2585 extract_stride(c, bound, stride, s);
2588 isl_int_clear(stride);
2589 isl_int_clear(v);
2591 isl_constraint_free(c);
2592 return 0;
2595 /* Given contraints on an array index i, check if we can find
2596 * a shift a(p) and a stride g such that
2598 * a(p) + i = 0 mod g
2600 * If so, record the information in bound and apply the mapping
2601 * i -> (i + a(p))/g to the array index in bounds and return
2602 * the new constraints.
2603 * If not, simply return the original constraints.
2605 static __isl_give isl_basic_map *check_stride(struct cuda_gen *gen,
2606 struct cuda_array_bound *bound, __isl_take isl_basic_map *bounds)
2608 isl_space *dim;
2609 isl_basic_map *aff;
2610 isl_basic_map *shift;
2611 isl_qpolynomial *qp, *t;
2612 isl_int one;
2614 isl_int_set_si(bound->stride, -1);
2616 aff = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
2618 isl_basic_map_foreach_constraint(aff, &check_stride_constraint, bound);
2620 isl_basic_map_free(aff);
2622 if (isl_int_is_neg(bound->stride))
2623 return bounds;
2625 qp = isl_qpolynomial_copy(bound->shift);
2626 qp = isl_qpolynomial_add_dims(qp, isl_dim_in, 1);
2627 dim = isl_qpolynomial_get_domain_space(qp);
2628 t = isl_qpolynomial_var_on_domain(isl_space_copy(dim), isl_dim_set, 0);
2629 qp = isl_qpolynomial_add(qp, t);
2630 isl_int_init(one);
2631 isl_int_set_si(one, 1);
2632 t = isl_qpolynomial_rat_cst_on_domain(dim, one, bound->stride);
2633 isl_int_clear(one);
2634 qp = isl_qpolynomial_mul(qp, t);
2635 shift = isl_basic_map_from_qpolynomial(qp);
2637 bound->shift_map = isl_basic_map_copy(shift);
2638 bounds = isl_basic_map_apply_range(bounds, shift);
2640 return bounds;
2643 struct cuda_size_info {
2644 isl_basic_set *bset;
2645 struct cuda_array_bound *bound;
2646 int pos;
2649 /* Given a constraint from the basic set describing the bounds on
2650 * an array index, check if it is a lower bound, say m i >= b(x), and,
2651 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
2652 * upper bound. If so, and if this bound is smaller than any bound
2653 * derived from earlier constraints, set the size to this bound on
2654 * the expression and the lower bound to ceil(b(x)/m).
2656 static int compute_size_in_direction(__isl_take isl_constraint *c, void *user)
2658 struct cuda_size_info *size = user;
2659 unsigned nparam;
2660 unsigned n_div;
2661 isl_int v;
2663 nparam = isl_basic_set_dim(size->bset, isl_dim_param);
2664 n_div = isl_constraint_dim(c, isl_dim_div);
2666 if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div)) {
2667 isl_constraint_free(c);
2668 return 0;
2671 isl_int_init(v);
2673 isl_constraint_get_coefficient(c, isl_dim_set, size->pos, &v);
2675 if (isl_int_is_pos(v)) {
2676 isl_aff *aff;
2677 isl_aff *lb;
2678 enum isl_lp_result res;
2680 aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
2681 aff = isl_aff_ceil(aff);
2683 lb = isl_aff_copy(aff);
2685 aff = isl_aff_neg(aff);
2686 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
2688 res = isl_basic_set_max(size->bset, aff, &v);
2689 isl_aff_free(aff);
2691 if (res == isl_lp_ok) {
2692 isl_int_add_ui(v, v, 1);
2693 if (isl_int_is_neg(size->bound->size) ||
2694 isl_int_lt(v, size->bound->size)) {
2695 isl_int_set(size->bound->size, v);
2696 lb = isl_aff_drop_dims(lb, isl_dim_in,
2697 0, size->pos + 1);
2698 isl_aff_free(size->bound->lb);
2699 size->bound->lb = isl_aff_copy(lb);
2702 isl_aff_free(lb);
2705 isl_int_clear(v);
2706 isl_constraint_free(c);
2708 return 0;
2711 /* Given a basic map "bounds" that maps parameters and input dimensions
2712 * to a single output dimension, look for an expression in the parameters
2713 * and input dimensions such that the range of the output dimension shifted
2714 * by this expression is a constant.
2716 * In particular, we currently only consider lower bounds on the output
2717 * dimension as candidate expressions.
2719 static int compute_array_dim_size(struct cuda_gen *gen,
2720 struct cuda_array_bound *bound, __isl_take isl_basic_map *bounds)
2722 struct cuda_size_info size;
2724 bounds = check_stride(gen, bound, bounds);
2726 isl_int_set_si(bound->size, -1);
2727 bound->lb = NULL;
2729 size.bound = bound;
2730 size.pos = isl_basic_map_dim(bounds, isl_dim_in);
2731 size.bset = isl_basic_map_wrap(bounds);
2732 size.bset = isl_basic_set_flatten(size.bset);
2733 isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
2734 &size);
2735 isl_basic_set_free(size.bset);
2737 return isl_int_is_nonneg(bound->size) ? 0 : -1;
2740 /* Check if we can find a shared memory tile for the given array
2741 * based on the given accesses, and if so, put the results
2742 * in array->shared_bound.
2744 * We project the accesses on each index in turn and look for a parametric
2745 * offset such that the size is constant.
2747 static int can_tile_for_shared_memory(struct cuda_gen *gen,
2748 struct cuda_array_info *array, __isl_keep isl_map *access,
2749 struct cuda_array_bound *bounds)
2751 int i;
2753 for (i = 0; i < array->n_index; ++i) {
2754 isl_map *access_i;
2755 isl_basic_map *hull;
2757 access_i = isl_map_copy(access);
2758 access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
2759 access_i = isl_map_project_out(access_i, isl_dim_out,
2760 1, array->n_index - (i + 1));
2761 access_i = isl_map_compute_divs(access_i);
2762 hull = isl_map_simple_hull(access_i);
2763 if (compute_array_dim_size(gen, &bounds[i], hull) < 0)
2764 return 0;
2767 return 1;
2770 /* Construct a map with input the shared tile loops and the loops that
2771 * will be wrapped around the threads that relates these later loops
2772 * to the thread indices and the projects them out.
2774 static __isl_give isl_map *compute_privatization(struct cuda_gen *gen)
2776 isl_map *priv;
2777 isl_map *tiling;
2778 isl_map *proj;
2779 isl_set *par;
2780 isl_space *dim;
2782 dim = isl_union_map_get_space(gen->shared_sched);
2784 if (gen->options->wrap)
2785 tiling = wrap(isl_space_copy(dim), gen->shared_len + gen->n_block,
2786 gen->shared_len, gen->n_block, gen->block_dim);
2787 else
2788 tiling = tile(isl_space_copy(dim), gen->shared_len + gen->n_block,
2789 gen->shared_len, gen->n_block, gen->block_dim);
2791 priv = tiling;
2793 par = parametrization(dim, gen->shared_len + 2 * gen->n_block,
2794 gen->tile_first + gen->tile_len + gen->n_grid + gen->n_block,
2795 gen->n_block, "t");
2797 priv = isl_map_align_params(priv, isl_set_get_space(par));
2798 priv = isl_map_intersect_range(priv, par);
2800 dim = isl_map_get_space(priv);
2801 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
2802 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
2803 proj = projection(dim, gen->shared_len + 2 * gen->n_block,
2804 gen->shared_len);
2806 priv = isl_map_apply_range(priv, proj);
2808 return priv;
2811 /* Construct a map from domain_dim to domain_dim that increments
2812 * the dimension at position "pos" and leaves all other dimensions
2813 * constant.
2815 static __isl_give isl_map *next(__isl_take isl_space *domain_dim, int pos)
2817 int i;
2818 int len = isl_space_dim(domain_dim, isl_dim_set);
2819 isl_space *dim;
2820 isl_basic_map *next;
2822 dim = isl_space_map_from_set(domain_dim);
2823 next = isl_basic_map_universe(isl_space_copy(dim));
2825 for (i = 0; i < len; ++i) {
2826 isl_constraint *c;
2828 c = isl_equality_alloc(isl_space_copy(dim));
2829 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
2830 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
2831 if (i == pos)
2832 isl_constraint_set_constant_si(c, 1);
2833 next = isl_basic_map_add_constraint(next, c);
2836 isl_space_free(dim);
2838 return isl_map_from_basic_map(next);
2841 /* Check if the given access is coalesced.
2842 * That is, check whether incrementing the dimension that will get
2843 * wrapped over the last thread index results in incrementing
2844 * the last array index.
2846 * This function is only called for access relations without reuse.
2848 static int access_is_coalesced(struct cuda_gen *gen,
2849 __isl_keep isl_union_map *access)
2851 isl_space *dim;
2852 isl_map *access_map;
2853 isl_map *next_thread_x;
2854 isl_map *next_element;
2855 isl_map *map;
2856 int coalesced;
2858 access = isl_union_map_copy(access);
2859 access = isl_union_map_apply_domain(access,
2860 isl_union_map_copy(gen->tiled_sched));
2861 access_map = isl_map_from_union_map(access);
2863 dim = isl_map_get_space(access_map);
2864 dim = isl_space_domain(dim);
2865 next_thread_x = next(dim, gen->shared_len + gen->n_block - 1);
2867 dim = isl_map_get_space(access_map);
2868 dim = isl_space_range(dim);
2869 next_element = next(dim, isl_space_dim(dim, isl_dim_set) - 1);
2871 map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
2872 map = isl_map_apply_range(map, access_map);
2874 coalesced = isl_map_is_subset(map, next_element);
2876 isl_map_free(next_element);
2877 isl_map_free(map);
2879 return coalesced;
2882 /* For the given array reference group, check whether the access is private
2883 * to the thread. That is, check that any given array element
2884 * is only accessed by a single thread.
2885 * We compute an access relation that maps the shared tile loop iterators
2886 * and the shared point loop iterators that will be wrapped over the
2887 * threads to the array elements.
2888 * We actually check that those iterators that will be wrapped
2889 * partition the array space. This check is stricter than necessary
2890 * since several iterations may be mapped onto the same thread
2891 * and then they could be allowed to access the same memory elements,
2892 * but our check does not allow this situation.
2894 * We also check that the index expression only depends on parallel
2895 * loops. That way, we can move those loops innermost and unroll them.
2896 * Again, we use a test that is stricter than necessary.
2897 * We actually check whether the index expression only depends
2898 * on the iterators that are wrapped over the threads.
2899 * These are necessarily parallel, but there may be more parallel loops.
2901 * Combining the injectivity of the first test with the single-valuedness
2902 * of the second test, we simply test for bijectivity.
2904 * If it turns out we can use registers, we compute the private memory
2905 * tile size using can_tile_for_shared_memory, after introducing a dependence
2906 * on the thread indices.
2908 * Before performing any of the above computations, we first check
2909 * if there is any reuse on the reference group. If not, we simply
2910 * return. If, moreover, the access is coalesced then we also remove
2911 * the shared memory tiling since we should just use global memory instead.
2913 static void check_private_group_access(struct cuda_gen *gen,
2914 struct cuda_array_ref_group *group)
2916 isl_map *acc;
2917 isl_union_map *access;
2918 int n_index = group->array->n_index;
2920 access = group_access_relation(group, 1, 1);
2921 if (isl_union_map_is_injective(access)) {
2922 if (group->shared_bound && access_is_coalesced(gen, access)) {
2923 free_bound_list(group->shared_bound, n_index);
2924 group->shared_bound = NULL;
2926 isl_union_map_free(access);
2927 return;
2929 access = isl_union_map_apply_domain(access,
2930 isl_union_map_copy(gen->shared_sched));
2932 acc = isl_map_from_union_map(access);
2934 if (!isl_map_is_bijective(acc)) {
2935 isl_map_free(acc);
2936 return;
2939 group->private_bound = create_bound_list(gen->ctx, n_index);
2940 acc = isl_map_align_params(acc, isl_map_get_space(gen->privatization));
2941 acc = isl_map_apply_domain(acc, isl_map_copy(gen->privatization));
2942 if (!can_tile_for_shared_memory(gen, group->array, acc,
2943 group->private_bound)) {
2944 free_bound_list(group->private_bound, n_index);
2945 group->private_bound = NULL;
2948 isl_map_free(acc);
2951 /* Look for the last shared tile loop that affects the offset of the
2952 * shared or private tile and store the result in array->last_shared.
2954 static void set_last_shared(struct cuda_gen *gen,
2955 struct cuda_array_ref_group *group)
2957 int i, j;
2958 struct cuda_array_bound *bounds;
2959 unsigned first_shared = gen->first_shared;
2960 int n_index = group->array->n_index;
2962 bounds = group->private_bound;
2963 if (!bounds)
2964 bounds = group->shared_bound;
2965 if (!bounds)
2966 return;
2968 for (j = gen->shared_len - 1; j >= 0; --j) {
2969 for (i = 0; i < n_index; ++i) {
2970 isl_aff *lb;
2971 isl_qpolynomial *shift;
2973 lb = bounds[i].lb;
2974 if (isl_aff_involves_dims(lb, isl_dim_param,
2975 first_shared + j, 1))
2976 break;
2978 shift = bounds[i].shift;
2979 if (!shift)
2980 continue;
2981 if (isl_qpolynomial_involves_dims(shift, isl_dim_param,
2982 first_shared + j, 1))
2983 break;
2985 if (i < n_index)
2986 break;
2988 group->array->last_shared = j;
2991 /* Compute the sizes of all private arrays for the current kernel,
2992 * as well as the offsets of the private pieces in the original arrays.
2993 * If we cannot or don't want to privatize a given array group,
2994 * we use the shared memory tile sizes computed in
2995 * compute_group_shared_bound instead.
2997 * If a given Array only has a single reference group and if we have
2998 * been able to find a privated or shared tile,
2999 * we also look for the last shared tile loop that affects the offset
3000 * (and therefore the array tile) and store the result in array->last_shared.
3002 * A privatized copy of all access relations from reference groups that
3003 * are mapped to private memory is stored in gen->privatization.
3005 static void compute_private_size(struct cuda_gen *gen)
3007 int i, j;
3008 isl_union_map *private;
3010 if (!gen->options->use_private_memory)
3011 return;
3013 private = isl_union_map_empty(isl_union_map_get_space(gen->shared_sched));
3015 for (i = 0; i < gen->n_array; ++i) {
3016 struct cuda_array_info *array = &gen->array[i];
3018 for (j = 0; j < array->n_group; ++j) {
3019 check_private_group_access(gen, array->groups[j]);
3021 if (!array->groups[j]->private_bound)
3022 continue;
3024 private = isl_union_map_union(private,
3025 group_access_relation(array->groups[j], 1, 1));
3028 array->last_shared = gen->shared_len - 1;
3029 array->print_shared_level = -1;
3031 if (array->n_group != 1)
3032 continue;
3033 set_last_shared(gen, array->groups[0]);
3036 if (isl_union_map_is_empty(private))
3037 isl_union_map_free(private);
3038 else {
3039 isl_union_map *priv;
3041 private = isl_union_map_apply_domain(private,
3042 isl_union_map_copy(gen->shared_sched));
3043 priv = isl_union_map_from_map(isl_map_copy(gen->privatization));
3044 private = isl_union_map_apply_domain(private, priv);
3045 gen->private_access = private;
3049 /* Fill up the groups array with singleton groups, i.e., one group
3050 * per reference, initializing the array, access, write and refs fields.
3051 * In particular the access field is initialized to the scheduled
3052 * access relation of the array reference.
3054 * Return the number of elements initialized, i.e., the number of
3055 * active references in the current kernel.
3057 static int populate_array_references(struct cuda_gen *gen,
3058 struct cuda_array_info *array, __isl_keep isl_union_map *sched,
3059 struct cuda_array_ref_group **groups)
3061 int i;
3062 int n;
3063 isl_ctx *ctx = isl_union_map_get_ctx(sched);
3065 n = 0;
3066 for (i = 0; i < array->n_ref; ++i) {
3067 isl_union_map *umap;
3068 isl_map *map;
3069 struct cuda_array_ref_group *group;
3070 struct cuda_stmt_access *access = array->refs[i];
3072 map = isl_map_copy(access->access);
3073 umap = isl_union_map_from_map(map);
3074 umap = isl_union_map_apply_domain(umap,
3075 isl_union_map_copy(sched));
3077 if (isl_union_map_is_empty(umap)) {
3078 isl_union_map_free(umap);
3079 continue;
3082 map = isl_map_from_union_map(umap);
3084 group = isl_calloc_type(ctx, struct cuda_array_ref_group);
3085 assert(group);
3086 group->array = array;
3087 group->access = map;
3088 group->write = access->write;
3089 group->refs = &array->refs[i];
3091 groups[n++] = group;
3094 return n;
3097 static void free_array_ref_group(struct cuda_array_ref_group *group,
3098 int n_index)
3100 if (!group)
3101 return;
3102 free_bound_list(group->shared_bound, n_index);
3103 free_bound_list(group->private_bound, n_index);
3104 isl_map_free(group->access);
3105 free(group->refs);
3106 free(group);
3109 /* If two groups have overlapping access relations and if one of them
3110 * involves a write, then merge the two groups into one.
3112 * We keep track of the grouping in "leader". leader[j] points to
3113 * an earlier group array element that belongs to the same group,
3114 * or the array element j itself if this element is the first in the group.
3116 * Return the number of group leaders.
3118 static int group_overlapping_writes(int n,
3119 struct cuda_array_ref_group **groups, int *leader)
3121 int i, j;
3122 int n_group = n;
3124 for (i = 0; i < n; ++i) {
3125 int l = i;
3126 groups[l]->n_ref = 1;
3127 for (j = i - 1; j >= 0; --j) {
3128 isl_map *map;
3129 int empty;
3131 if (leader[j] != j)
3132 continue;
3133 if (!groups[l]->write && !groups[j]->write)
3134 continue;
3136 map = isl_map_intersect(isl_map_copy(groups[l]->access),
3137 isl_map_copy(groups[j]->access));
3138 empty = isl_map_is_empty(map);
3139 isl_map_free(map);
3141 if (empty)
3142 continue;
3144 groups[j]->access = isl_map_union(groups[j]->access,
3145 groups[l]->access);
3146 groups[j]->write = 1;
3147 groups[l]->access = NULL;
3148 groups[j]->n_ref += groups[l]->n_ref;
3149 l = leader[l] = j;
3150 n_group--;
3152 leader[i] = l;
3155 return n_group;
3158 /* Compute the size of the shared array corresponding to the given array
3159 * array refrence group, based on the accesses from the current kernel,
3160 * as well as the offset of the shared piece in the original array.
3162 static void compute_group_shared_bound(struct cuda_gen *gen,
3163 struct cuda_array_info *array, struct cuda_array_ref_group *group)
3165 isl_ctx *ctx = isl_space_get_ctx(array->dim);
3167 if (!gen->options->use_shared_memory)
3168 return;
3170 group->shared_bound = create_bound_list(ctx, array->n_index);
3171 if (!can_tile_for_shared_memory(gen, array, group->access,
3172 group->shared_bound)) {
3173 free_bound_list(group->shared_bound, array->n_index);
3174 group->shared_bound = NULL;
3178 /* Given an initial grouping of array references and shared memory tiles
3179 * for each group that allows for a shared memory tile, merge two groups
3180 * if both have a shared memory tile and if the merged group also has
3181 * a shared memory tile.
3183 * Return the number of group leaders after merging.
3185 static int group_common_shared_memory_tile(struct cuda_gen *gen,
3186 struct cuda_array_info *array, int n,
3187 struct cuda_array_ref_group **groups, int *leader, int n_group)
3189 int i, j;
3190 isl_ctx *ctx = isl_space_get_ctx(array->dim);
3192 for (i = 0; n_group > 1 && i < n; ++i) {
3193 int l = i;
3194 if (leader[i] != i)
3195 continue;
3196 if (!groups[i]->shared_bound)
3197 continue;
3198 for (j = i - 1; j >= 0; --j) {
3199 isl_map *map;
3200 int empty;
3201 struct cuda_array_bound *shared_bound;
3203 if (leader[j] != j)
3204 continue;
3205 if (!groups[j]->shared_bound)
3206 continue;
3208 map = isl_map_intersect(isl_map_copy(groups[l]->access),
3209 isl_map_copy(groups[j]->access));
3210 empty = isl_map_is_empty(map);
3211 isl_map_free(map);
3213 if (empty)
3214 continue;
3216 map = isl_map_union(isl_map_copy(groups[l]->access),
3217 isl_map_copy(groups[j]->access));
3218 shared_bound = create_bound_list(ctx, array->n_index);
3219 if (!can_tile_for_shared_memory(gen, array, map,
3220 shared_bound)) {
3221 isl_map_free(map);
3222 free_bound_list(shared_bound, array->n_index);
3223 continue;
3226 free_bound_list(groups[j]->shared_bound,
3227 array->n_index);
3228 groups[j]->shared_bound = shared_bound;
3229 isl_map_free(groups[j]->access);
3230 groups[j]->access = map;
3231 groups[j]->n_ref += groups[l]->n_ref;
3232 l = leader[l] = j;
3233 n_group--;
3237 return n_group;
3240 /* Extract an array of array reference groups from the array of references
3241 * and the grouping information in "leader".
3243 * Store the results in array->n_group and array->groups.
3245 static void extract_array_groups(isl_ctx *ctx, struct cuda_array_info *array,
3246 int n, struct cuda_array_ref_group **groups, int *leader, int n_group)
3248 int i, j;
3250 for (i = 2; i < n; ++i)
3251 leader[i] = leader[leader[i]];
3253 array->n_group = n_group;
3254 array->groups = isl_alloc_array(ctx, struct cuda_array_ref_group *,
3255 n_group);
3256 assert(array->groups);
3258 j = 0;
3259 for (i = 0; i < n; ++i) {
3260 int k, l;
3261 struct cuda_stmt_access **refs;
3263 if (leader[i] != i) {
3264 groups[i]->refs = NULL;
3265 free_array_ref_group(groups[i], array->n_index);
3266 continue;
3269 refs = isl_alloc_array(ctx, struct cuda_stmt_access *,
3270 groups[i]->n_ref);
3271 assert(refs);
3272 l = 0;
3273 for (k = i; k < n; ++k)
3274 if (leader[k] == i) {
3275 refs[l++] = *groups[k]->refs;
3276 (*groups[k]->refs)->group = j;
3279 groups[i]->refs = refs;
3280 groups[i]->nr = j;
3281 array->groups[j++] = groups[i];
3285 /* Group array references that should be considered together when
3286 * deciding whether to access them from private, shared or global memory.
3288 * In particular, if two array references overlap and if one of them
3289 * is a write, then the two references are grouped together.
3290 * Furthermore, if two groups admit a shared memory tile and if the
3291 * combination of the two also admits a shared memory tile, we merge
3292 * the two groups.
3294 * During the construction the group->refs field points to a single
3295 * array reference inside the array of array references, while
3296 * group->n_ref contains the number of element in leader that
3297 * (directly or indirectly) point to this group, provided the group
3298 * is a leader.
3300 static void group_array_references(struct cuda_gen *gen,
3301 struct cuda_array_info *array, __isl_keep isl_union_map *sched)
3303 int i;
3304 int n, n_group;
3305 isl_ctx *ctx = isl_union_map_get_ctx(sched);
3306 struct cuda_array_ref_group **groups;
3307 int *leader;
3309 groups = isl_calloc_array(ctx, struct cuda_array_ref_group *,
3310 array->n_ref);
3311 assert(groups);
3313 n = populate_array_references(gen, array, sched, groups);
3315 leader = isl_alloc_array(ctx, int, n);
3316 assert(leader);
3318 n_group = group_overlapping_writes(n, groups, leader);
3320 for (i = 0; i < n; ++i)
3321 if (leader[i] == i)
3322 compute_group_shared_bound(gen, array, groups[i]);
3324 n_group = group_common_shared_memory_tile(gen, array, n, groups,
3325 leader, n_group);
3327 extract_array_groups(ctx, array, n, groups, leader, n_group);
3329 free(leader);
3330 free(groups);
3333 /* Take tiled_sched, project it onto the shared tile loops and
3334 * the loops that will be wrapped over the threads,
3335 * parametrize the shared tile loops and store the result in gen->shared_sched.
3336 * The position of the first of these parameters is stored in gen->first_shared.
3337 * Also compute a projection that projects out the loops that will be
3338 * wrapped over the threads and store this projection in gen->shared_proj.
3340 static void compute_shared_sched(struct cuda_gen *gen)
3342 isl_space *dim;
3343 isl_map *proj;
3344 isl_set *par;
3345 isl_union_map *sched;
3347 sched = isl_union_map_copy(gen->tiled_sched);
3349 dim = isl_union_map_get_space(sched);
3350 gen->first_shared = isl_space_dim(dim, isl_dim_param);
3351 proj = projection(dim, gen->tiled_len, gen->shared_len + gen->n_block);
3352 sched = isl_union_map_apply_range(sched, isl_union_map_from_map(proj));
3354 dim = isl_union_map_get_space(sched);
3355 par = parametrization(dim, gen->shared_len + gen->n_block,
3356 0, gen->shared_len, "g");
3357 sched = isl_union_map_intersect_range(sched,
3358 isl_union_set_from_set(par));
3360 dim = isl_union_map_get_space(sched);
3361 proj = projection(dim, gen->shared_len + gen->n_block, gen->shared_len);
3363 gen->shared_sched = sched;
3364 gen->shared_proj = isl_union_map_from_map(proj);
3367 /* Group references of all arrays in the program.
3369 static void group_references(struct cuda_gen *gen)
3371 int i;
3372 isl_union_map *sched;
3374 sched = isl_union_map_apply_range(isl_union_map_copy(gen->shared_sched),
3375 isl_union_map_copy(gen->shared_proj));
3377 for (i = 0; i < gen->n_array; ++i)
3378 group_array_references(gen, &gen->array[i], sched);
3380 isl_union_map_free(sched);
3383 /* Free all array information that is local to the current kernel.
3385 static void free_local_array_info(struct cuda_gen *gen)
3387 int i, j;
3389 for (i = 0; i < gen->n_array; ++i) {
3390 struct cuda_array_info *array = &gen->array[i];
3392 for (j = 0; j < array->n_group; ++j)
3393 free_array_ref_group(array->groups[j], array->n_index);
3394 free(array->groups);
3396 if (array->n_group == 0)
3397 continue;
3398 for (j = 0; j < gen->array[i].n_index; ++j) {
3399 isl_pw_aff_free(gen->array[i].local_bound[j]);
3400 gen->array[i].local_bound[j] = NULL;
3405 static void print_iterator_list(FILE *out, int len, const char *prefix,
3406 int parens)
3408 int i;
3410 fprintf(out, "(");
3411 for (i = 0; i < len; ++i) {
3412 if (i)
3413 fprintf(out, ", ");
3414 if (parens)
3415 fprintf(out, "(%s%d)", prefix, i);
3416 else
3417 fprintf(out, "%s%d", prefix, i);
3419 fprintf(out, ")");
3422 /* Print an access to the element in the global memory copy of the
3423 * given array that corresponds to element [a0][a1]... of the original array.
3424 * The copy in global memory has been linearized, so we need to take
3425 * the array size into account.
3427 static void print_global_index(isl_ctx *ctx, FILE *out,
3428 struct cuda_array_info *array)
3430 int i;
3431 isl_printer *prn;
3433 if (cuda_array_is_scalar(array)) {
3434 fprintf(out, "*%s", array->name);
3435 return;
3438 fprintf(out, "%s[", array->name);
3439 for (i = 0; i + 1 < array->n_index; ++i)
3440 fprintf(out, "(");
3441 for (i = 0; i < array->n_index; ++i) {
3442 if (i) {
3443 prn = isl_printer_to_file(ctx, out);
3444 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
3445 prn = isl_printer_print_str(prn, ") * (");
3446 prn = isl_printer_print_pw_aff(prn,
3447 array->local_bound[i]);
3448 prn = isl_printer_print_str(prn, ") + ");
3449 isl_printer_free(prn);
3451 fprintf(out, "a%d", i);
3453 fprintf(out, "]");
3456 /* Print an access to the element in the shared memory copy of the
3457 * given array that corresponds to element [a0][a1]... of the original array.
3458 * Since the array in shared memory is just a shifted copy of part
3459 * of the original array, we simply need to subtract the lower bound,
3460 * which was computed in can_tile_for_shared_memory.
3461 * If any of the indices is strided, then we first add
3462 * shared_bound[i].shift and divide by shared_bound[i].stride.
3464 static void print_local_index(FILE *out, struct cuda_array_ref_group *group)
3466 int i;
3467 isl_ctx *ctx;
3468 isl_printer *prn;
3469 struct cuda_array_bound *bounds = group->shared_bound;
3471 ctx = isl_space_get_ctx(group->array->dim);
3472 print_array_name(out, group);
3473 for (i = 0; i < group->array->n_index; ++i) {
3474 fprintf(out, "[(a%d", i);
3475 if (bounds[i].shift) {
3476 fprintf(out, " + (");
3477 prn = isl_printer_to_file(ctx, out);
3478 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
3479 prn = isl_printer_print_qpolynomial(prn,
3480 bounds[i].shift);
3481 prn = isl_printer_print_str(prn, "))/");
3482 prn = isl_printer_print_isl_int(prn,
3483 bounds[i].stride);
3484 isl_printer_free(prn);
3485 } else
3486 fprintf(out, ")");
3487 fprintf(out, " - (");
3488 prn = isl_printer_to_file(ctx, out);
3489 prn = isl_printer_set_output_format(prn, ISL_FORMAT_C);
3490 prn = isl_printer_print_aff(prn, bounds[i].lb);
3491 isl_printer_free(prn);
3492 fprintf(out, ")]");
3496 /* Print '#define's for copying data from global memory to shared
3497 * memory and back for the given array.
3499 static void print_array_copy_defines(struct cuda_gen *gen,
3500 struct cuda_array_ref_group *group)
3502 int i;
3503 const char *type[] = { "read", "write" };
3504 struct cuda_array_info *array = group->array;
3505 int n_index = array->n_index;
3507 for (i = 0; i < 2; ++i) {
3508 fprintf(gen->cuda.kernel_c, "#define %s_", type[i]);
3509 print_array_name(gen->cuda.kernel_c, group);
3510 print_iterator_list(gen->cuda.kernel_c, n_index, "a", 0);
3511 fprintf(gen->cuda.kernel_c, " %s_", type[i]);
3512 print_array_name(gen->cuda.kernel_c, group);
3513 fprintf(gen->cuda.kernel_c, "_");
3514 print_iterator_list(gen->cuda.kernel_c, n_index, "a", 1);
3515 fprintf(gen->cuda.kernel_c, "\n");
3517 fprintf(gen->cuda.kernel_c, "#define %s_", type[i]);
3518 print_array_name(gen->cuda.kernel_c, group);
3519 fprintf(gen->cuda.kernel_c, "_");
3520 print_iterator_list(gen->cuda.kernel_c, n_index, "a", 0);
3521 if (i) {
3522 fprintf(gen->cuda.kernel_c, " ");
3523 print_global_index(gen->ctx, gen->cuda.kernel_c, array);
3524 fprintf(gen->cuda.kernel_c, " = ");
3525 print_local_index(gen->cuda.kernel_c, group);
3526 } else {
3527 fprintf(gen->cuda.kernel_c, " ");
3528 print_local_index(gen->cuda.kernel_c, group);
3529 fprintf(gen->cuda.kernel_c, " = ");
3530 print_global_index(gen->ctx, gen->cuda.kernel_c, array);
3532 fprintf(gen->cuda.kernel_c, "\n");
3536 static void print_copy_defines(struct cuda_gen *gen)
3538 int i, j;
3540 for (i = 0; i < gen->n_array; ++i) {
3541 struct cuda_array_info *array = &gen->array[i];
3543 for (j = 0; j < array->n_group; ++j) {
3544 if (array->groups[j]->private_bound)
3545 continue;
3546 if (!array->groups[j]->shared_bound)
3547 continue;
3548 print_array_copy_defines(gen, array->groups[j]);
3553 /* The sizes of the arrays on the host that have been computed by
3554 * extract_array_info may depend on the parameters. Use the extra
3555 * constraints on the parameters that are valid at "host_domain"
3556 * to simplify these expressions.
3558 static void localize_bounds(struct cuda_gen *gen,
3559 __isl_keep isl_set *host_domain)
3561 int i, j;
3562 isl_set *context;
3564 context = isl_set_copy(host_domain);
3565 context = isl_set_params(host_domain);
3567 for (i = 0; i < gen->n_array; ++i) {
3568 struct cuda_array_info *array = &gen->array[i];
3570 if (array->n_group == 0)
3571 continue;
3573 for (j = 0; j < array->n_index; ++j) {
3574 isl_pw_aff *pwaff;
3576 pwaff = isl_pw_aff_copy(array->bound[j]);
3577 pwaff = isl_pw_aff_gist(pwaff, isl_set_copy(context));
3578 array->local_bound[j] = pwaff;
3581 isl_set_free(context);
3584 /* Set gen->tile_len and gen->n_parallel to those of the first statement
3585 * in the statement list u.
3586 * Because of the way the schedule is constructed, the other statements
3587 * in the list, if any, should have the same values for these properties.
3589 static void set_tile_len(struct cuda_gen *gen, struct clast_user_stmt *u)
3591 int nr;
3592 struct cuda_stmt *stmt;
3594 nr = atoi(u->statement->name + 2);
3595 stmt = &gen->stmts[nr];
3597 gen->tile_len = stmt->tile_len;
3598 gen->n_parallel = stmt->n_parallel;
3601 /* This function is called for each leaf in the clast of the host code.
3602 * We first specialize the schedule to the site of the leaf, compute
3603 * the size of shared memory and then print the body of host code
3604 * and the associated kernel (through a call to print_kernel_body).
3606 static void print_host_user(struct gpucode_info *code,
3607 struct clast_user_stmt *u)
3609 struct cuda_gen *gen = code->user;
3610 isl_space *dim;
3611 isl_set *par;
3612 isl_set *host_domain;
3613 isl_union_map *access;
3614 isl_union_map *local_sched;
3615 isl_union_set *arrays;
3617 set_tile_len(gen, u);
3618 read_sizes(gen);
3620 host_domain = extract_entire_host_domain(u);
3622 local_sched = isl_union_map_intersect_range(
3623 isl_union_map_copy(gen->sched),
3624 isl_union_set_from_set(extend(isl_set_copy(host_domain),
3625 gen->untiled_len)));
3626 access = isl_union_map_union(isl_union_map_copy(gen->read),
3627 isl_union_map_copy(gen->write));
3628 access = isl_union_map_apply_domain(access,
3629 isl_union_map_copy(local_sched));
3630 arrays = isl_union_map_range(access);
3632 print_indent(code->dst, code->indent);
3633 fprintf(code->dst, "dim3 k%d_dimBlock(", gen->kernel_id);
3634 print_reverse_list(code->dst, gen->n_block, gen->block_dim);
3635 fprintf(code->dst, ");\n");
3637 print_indent(code->dst, code->indent);
3638 fprintf(code->dst, "dim3 k%d_dimGrid(", gen->kernel_id);
3639 print_reverse_list(code->dst, gen->n_grid, gen->grid_dim);
3640 fprintf(code->dst, ");\n");
3642 gen->tiled_sched = tile_schedule(gen, local_sched);
3643 gen->tiled_sched = parametrize_tiled_schedule(gen, gen->tiled_sched);
3644 gen->tiled_sched = scale_tile_loops(gen, gen->tiled_sched);
3646 gen->local_sched = isl_union_map_copy(gen->tiled_sched);
3648 dim = isl_union_map_get_space(gen->local_sched);
3649 par = parametrization(dim, gen->tiled_len, 0, gen->shared_len, "g");
3650 gen->local_sched = isl_union_map_intersect_range(gen->local_sched,
3651 isl_union_set_from_set(par));
3653 gen->local_sched = thread_tile_schedule(gen, gen->local_sched);
3654 gen->local_sched = scale_thread_tile_loops(gen, gen->local_sched);
3656 gen->private_access = NULL;
3657 compute_shared_sched(gen);
3658 gen->privatization = compute_privatization(gen);
3659 group_references(gen);
3660 compute_private_size(gen);
3661 localize_bounds(gen, host_domain);
3663 gen->local_sched = interchange_for_unroll(gen, gen->local_sched);
3665 print_copy_defines(gen);
3666 print_kernel_launch(gen, arrays);
3668 fprintf(gen->cuda.kernel_c, "{\n");
3670 print_kernel_body(gen, host_domain, gen->tiled_sched);
3672 fprintf(gen->cuda.kernel_c, "}\n");
3674 free_local_array_info(gen);
3675 isl_map_free(gen->privatization);
3676 isl_union_map_free(gen->private_access);
3677 isl_union_map_free(gen->local_sched);
3678 isl_union_map_free(gen->tiled_sched);
3679 isl_union_map_free(gen->shared_sched);
3680 isl_union_map_free(gen->shared_proj);
3681 isl_union_set_free(arrays);
3682 isl_set_free(host_domain);
3684 free(gen->tile_size);
3685 gen->kernel_id++;
3688 /* Use CLooG to generate code for the outer gen->tile_first loops
3689 * of the global schedule in gen->sched.
3690 * The pretty printing of this code is handled by gpu_print_host_stmt,
3691 * which calls print_host_user for each kernel invocation location.
3693 static void print_cloog_host_code(struct cuda_gen *gen)
3695 int i;
3696 isl_set *context;
3697 isl_union_map *sched;
3698 CloogOptions *options;
3699 CloogDomain *cloog_context;
3700 CloogUnionDomain *ud;
3701 CloogInput *input;
3702 struct clast_stmt *stmt;
3703 char name[20];
3705 options = cloog_options_malloc(gen->state);
3706 options->language = LANGUAGE_C;
3707 options->otl = 0;
3708 options->strides = 1;
3709 options->stop = gen->tile_first;
3710 options->f = gen->untiled_len;
3711 options->l = gen->untiled_len;
3712 options->save_domains = 1;
3713 options->noscalars = 1;
3715 sched = isl_union_map_copy(gen->sched);
3716 ud = cloog_union_domain_from_isl_union_map(sched);
3717 for (i = 0; i < options->stop; ++i) {
3718 snprintf(name, sizeof(name), "h%d", i);
3719 ud = cloog_union_domain_set_name(ud, CLOOG_SCAT, i, name);
3721 context = isl_set_copy(gen->context);
3722 cloog_context = cloog_domain_from_isl_set(context);
3723 input = cloog_input_alloc(cloog_context, ud);
3725 stmt = cloog_clast_create_from_input(input, options);
3727 gen->code.indent = 0;
3728 gen->code.dst = gen->cuda.host_c;
3729 gen->code.print_user_stmt = NULL;
3730 gen->code.print_user_stmt_list = &print_host_user;
3731 gen->code.print_for_head = NULL;
3732 gen->code.print_for_foot = NULL;
3733 gen->code.user = gen;
3734 gpu_print_host_stmt(&gen->code, stmt);
3736 cloog_clast_free(stmt);
3737 cloog_options_free(options);
3738 fprintf(gen->cuda.host_c, "\n");
3741 void print_cuda_macros(struct cuda_gen *gen)
3743 const char *macros =
3744 "#define cudaCheckReturn(ret) assert((ret) == cudaSuccess)\n"
3745 "#define cudaCheckKernel()"
3746 " assert(cudaGetLastError() == cudaSuccess)\n\n";
3747 fputs(macros, gen->cuda.host_c);
3750 void print_host_code(struct cuda_gen *gen)
3752 fprintf(gen->cuda.host_c, "{\n");
3753 print_cloog_macros(gen->cuda.host_c);
3754 print_cloog_macros(gen->cuda.kernel_c);
3756 print_cuda_macros(gen);
3758 declare_device_arrays(gen);
3760 allocate_device_arrays(gen);
3761 copy_arrays_to_device(gen);
3763 gen->kernel_id = 0;
3764 print_cloog_host_code(gen);
3766 copy_arrays_from_device(gen);
3767 free_device_arrays(gen);
3769 fprintf(gen->cuda.host_c, "}\n");
3772 __isl_give isl_set *add_context_from_str(__isl_take isl_set *set,
3773 const char *str)
3775 isl_ctx *ctx;
3776 isl_set *context;
3778 if (!str)
3779 return set;
3781 ctx = isl_set_get_ctx(set);
3782 context = isl_set_read_from_str(ctx, str, -1);
3783 context = isl_set_align_params(context, isl_set_get_space(set));
3784 set = isl_set_intersect(set, context);
3786 return set;
3789 /* Return the union of all iteration domains of the gen->stmts[i].
3791 static __isl_give isl_union_set *extract_domain(struct cuda_gen *gen)
3793 int i;
3794 isl_union_set *domain;
3796 domain = isl_union_set_empty(isl_set_get_space(gen->context));
3797 for (i = 0; i < gen->n_stmts; ++i) {
3798 isl_set *domain_i;
3800 domain_i = isl_set_copy(gen->stmts[i].domain);
3801 domain = isl_union_set_union(domain,
3802 isl_union_set_from_set(domain_i));
3805 return domain;
3808 /* Information about the outermost tilable bands in the forest of bands.
3810 * tile_len and n_parallel are only sets on band_info structures
3811 * that correspond to outermost bands. For other bands (in particular,
3812 * ancestors of the outermost bands), n_parallal is set to 0.
3814 * prefix is the (padded) schedule leading up to the outermost tilable bands.
3816 * tile_first is the number of schedule dimensions in prefix.
3818 * suffix is the schedule of the outermost tilable bands and their descendants.
3820 struct band_info {
3821 struct cuda_gen *gen;
3822 int tile_first;
3823 int tile_len;
3824 int n_parallel;
3825 isl_union_map *prefix;
3826 isl_union_map *suffix;
3829 /* Set tile_len and n_parallel of the statement to that of
3830 * their outermost band, recorded in the band_info.
3832 static int set_stmt_tile_len(__isl_take isl_map *map, void *user)
3834 struct band_info *info = user;
3835 int nr;
3836 struct cuda_stmt *stmt;
3838 nr = atoi(isl_map_get_tuple_name(map, isl_dim_in) + 2);
3839 stmt = &info->gen->stmts[nr];
3841 stmt->tile_len = info->tile_len;
3842 stmt->n_parallel = info->n_parallel;
3844 isl_map_free(map);
3846 return 0;
3849 static void list_select_outer_band(struct cuda_gen *gen,
3850 __isl_take isl_band_list *list, int pos, struct band_info *list_info);
3852 /* Check if this band has any parallel loops. If so, take it as
3853 * the outermost tilable band. If not, continue looking for the
3854 * outermost tilable band in the children of the current band.
3856 static void band_select_outer_band(struct cuda_gen *gen,
3857 __isl_take isl_band *band, int pos, struct band_info *info)
3859 int n = isl_band_n_member(band);
3860 int n_parallel;
3862 for (n_parallel = 0; n_parallel < n; ++n_parallel)
3863 if (!isl_band_member_is_zero_distance(band, n_parallel))
3864 break;
3866 info->n_parallel = n_parallel;
3867 if (n_parallel) {
3868 info->gen = gen;
3869 info->tile_first = pos;
3870 info->tile_len = n;
3871 info->prefix = isl_band_get_prefix_schedule(band);
3872 info->suffix = isl_union_map_flat_range_product(
3873 isl_band_get_partial_schedule(band),
3874 isl_band_get_suffix_schedule(band));
3875 isl_union_map_foreach_map(info->prefix,
3876 &set_stmt_tile_len, info);
3877 } else {
3878 isl_band_list *children;
3879 if (!isl_band_has_children(band))
3880 isl_die(isl_band_get_ctx(band), isl_error_unknown,
3881 "unable to detect any parallelism", abort());
3882 children = isl_band_get_children(band);
3883 list_select_outer_band(gen, children, pos + n, info);
3886 isl_band_free(band);
3889 /* Comparison function that returns a non-zero value for band_infos
3890 * with different tile_len fields or different n_parallel fields.
3892 static int cmp_band(const void *p1, const void *p2)
3894 const struct band_info *info1 = p1;
3895 const struct band_info *info2 = p2;
3897 if (info1->tile_len != info2->tile_len)
3898 return info1->tile_len - info2->tile_len;
3900 return info1->n_parallel - info2->n_parallel;
3903 /* Extend "umap" with coordinates with fixed value "val"
3904 * to a total length of "dst_len", assuming the original dimension is "src_len".
3906 static __isl_give isl_union_map *extend_range(__isl_take isl_union_map *umap,
3907 int src_len, int dst_len, int val)
3909 isl_space *dim;
3910 isl_map *map;
3911 int i;
3913 dim = isl_union_map_get_space(umap);
3914 map = isl_map_reverse(projection(dim, dst_len, src_len));
3915 for (i = src_len; i < dst_len; ++i)
3916 map = isl_map_fix_si(map, isl_dim_out, i, val);
3918 umap = isl_union_map_apply_range(umap, isl_union_map_from_map(map));
3920 return umap;
3923 /* Group bands with the same values for tile_len and n_parallel.
3924 * The prefix schedule is then extended with a fixed coordinate that
3925 * is different for each such group.
3926 * Note that the actual values for this coordinate are not important.
3927 * The bands have already been effectively separated at a higher level
3928 * or they are independent and may be executed in parallel.
3929 * The list of band_info has been sorted before this functions is called.
3931 static void separate_bands(struct band_info *info, int n)
3933 int i;
3934 int j = 0;
3936 for (i = 0; i < n; ++i) {
3937 int l = info[i].tile_first;
3939 if (i &&
3940 (info[i].tile_len != info[i - 1].tile_len ||
3941 info[i].n_parallel != info[i - 1].n_parallel))
3942 j++;
3944 info[i].prefix = extend_range(info[i].prefix,
3945 l, l + 1, j);
3946 info[i].tile_first = l + 1;
3950 /* Select the outermost bands in the elements of the list, align
3951 * their prefix schedules, separate bands with different values
3952 * for tile_len and/or n_parallel and then combine the resulting
3953 * prefix and suffix schedules into a single pair of prefix and
3954 * suffix schedules for the entire list.
3956 static void list_select_outer_band(struct cuda_gen *gen,
3957 __isl_take isl_band_list *list, int pos, struct band_info *list_info)
3959 isl_band *band;
3960 int i;
3961 int n = isl_band_list_n_band(list);
3962 isl_ctx *ctx = isl_band_list_get_ctx(list);
3963 struct band_info *info;
3964 int max_tile_first;
3965 isl_union_map *prefix;
3966 isl_union_map *suffix;
3968 assert(n >= 1);
3969 info = isl_calloc_array(ctx, struct band_info, n);
3970 assert(info);
3972 max_tile_first = 0;
3973 for (i = 0; i < n; ++i) {
3974 band = isl_band_list_get_band(list, i);
3975 band_select_outer_band(gen, band, pos, &info[i]);
3976 if (info[i].tile_first > max_tile_first)
3977 max_tile_first = info[i].tile_first;
3980 for (i = 0; i < n; ++i) {
3981 if (info[i].tile_first == max_tile_first)
3982 continue;
3983 info[i].prefix = extend_range(info[i].prefix,
3984 info[i].tile_first, max_tile_first, 0);
3987 qsort(info, n, sizeof(struct band_info), &cmp_band);
3989 for (i = 0; i < n - 1; ++i)
3990 if (info[i].tile_len != info[i + 1].tile_len ||
3991 info[i].n_parallel != info[i + 1].n_parallel)
3992 break;
3994 if (i < n -1)
3995 separate_bands(info, n);
3997 prefix = info[0].prefix;
3998 suffix = info[0].suffix;
4000 for (i = 1; i < n; ++i) {
4001 prefix = isl_union_map_union(prefix, info[i].prefix);
4002 suffix = isl_union_map_union(suffix, info[i].suffix);
4005 list_info->tile_first = info[0].tile_first;
4006 list_info->tile_len = -1;
4007 list_info->prefix = prefix;
4008 list_info->suffix = suffix;
4010 isl_band_list_free(list);
4011 free(info);
4014 /* Set max_out to the maximal number of output dimensions over
4015 * all maps.
4017 static int update_max_out(__isl_take isl_map *map, void *user)
4019 int *max_out = user;
4020 int n_out = isl_map_dim(map, isl_dim_out);
4022 if (n_out > *max_out)
4023 *max_out = n_out;
4025 isl_map_free(map);
4026 return 0;
4029 struct align_range_data {
4030 int max_out;
4031 isl_union_map *res;
4034 /* Extend the dimension of the range of the given map to data->max_out and
4035 * then add the result to data->res.
4037 static int map_align_range(__isl_take isl_map *map, void *user)
4039 struct align_range_data *data = user;
4040 int i;
4041 isl_space *dim;
4042 isl_map *proj;
4043 int n_out = isl_map_dim(map, isl_dim_out);
4045 dim = isl_union_map_get_space(data->res);
4046 proj = isl_map_reverse(projection(dim, data->max_out, n_out));
4047 for (i = n_out; i < data->max_out; ++i)
4048 proj = isl_map_fix_si(proj, isl_dim_out, i, 0);
4050 map = isl_map_apply_range(map, proj);
4052 data->res = isl_union_map_add_map(data->res, map);
4054 return 0;
4057 /* Extend the ranges of the maps in the union map such they all have
4058 * the same dimension.
4060 static __isl_give isl_union_map *align_range(__isl_take isl_union_map *umap)
4062 struct align_range_data data;
4064 data.max_out = 0;
4065 isl_union_map_foreach_map(umap, &update_max_out, &data.max_out);
4067 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4068 isl_union_map_foreach_map(umap, &map_align_range, &data);
4070 isl_union_map_free(umap);
4071 return data.res;
4074 /* Select the outermost tilable band that (by construction)
4075 * has at least one parallel loop.
4076 * The starting position of the aligned band is stored in the pair
4077 * gen->tile_first.
4078 * The sizes and number of parallel loops may be different in different
4079 * parts of the band forest and are therefore stored in the cuda_stmts.
4081 * Return the complete schedule, with the tilable bands aligned
4082 * at gen->tile_first and padded with zero, if needed.
4084 static __isl_give isl_union_map *select_outer_tilable_band(struct cuda_gen *gen,
4085 __isl_keep isl_schedule *schedule)
4087 isl_band_list *list;
4088 struct band_info info;
4090 gen->n_parallel = 0;
4091 gen->tile_len = -1;
4093 list = isl_schedule_get_band_forest(schedule);
4095 list_select_outer_band(gen, list, 0, &info);
4097 gen->tile_first = info.tile_first;
4098 info.suffix = align_range(info.suffix);
4100 return isl_union_map_flat_range_product(info.prefix, info.suffix);
4103 /* Set gen->untiled_len to the number of scheduling dimensions
4104 * for the schedule of the first domain.
4105 * We assume here that this number is the same for all domains.
4107 static int set_untiled_len(__isl_take isl_map *map, void *user)
4109 unsigned *untiled_len = user;
4111 *untiled_len = isl_map_dim(map, isl_dim_out);
4113 isl_map_free(map);
4114 return -1;
4117 /* Compute an appropriate schedule based on the accesses in
4118 * gen->read and gen->write.
4120 * We first compute dependences and then use those to compute
4121 * a schedule that has a parallel loop in each tilable band.
4122 * Finally, we select the outermost tilable band.
4124 static void compute_schedule(struct cuda_gen *gen,
4125 __isl_take isl_union_map *sched)
4127 isl_ctx *ctx = isl_union_map_get_ctx(sched);
4128 isl_union_set *domain;
4129 isl_union_map *empty;
4130 isl_union_map *dep_raw, *dep2, *dep3, *dep;
4131 isl_union_map *uninitialized;
4132 isl_schedule *schedule;
4133 struct isl_options *options;
4135 empty = isl_union_map_empty(isl_union_map_get_space(sched));
4137 isl_union_map_compute_flow(isl_union_map_copy(gen->read),
4138 isl_union_map_copy(gen->write), empty,
4139 isl_union_map_copy(sched),
4140 &dep_raw, NULL, &uninitialized, NULL);
4141 isl_union_map_compute_flow(isl_union_map_copy(gen->write),
4142 isl_union_map_copy(gen->write),
4143 isl_union_map_copy(gen->read),
4144 isl_union_map_copy(sched),
4145 &dep2, &dep3, NULL, NULL);
4146 isl_union_map_free(sched);
4148 gen->copy_in = isl_union_map_range(uninitialized);
4150 dep = isl_union_map_union(dep2, dep3);
4151 dep = isl_union_map_union(dep, dep_raw);
4152 dep = isl_union_map_coalesce(dep);
4154 domain = extract_domain(gen);
4155 options = isl_ctx_peek_options(ctx, isl_options_arg);
4156 options->schedule_outer_zero_distance = 1;
4157 schedule = isl_union_set_compute_schedule(isl_union_set_copy(domain),
4158 isl_union_map_copy(dep), dep);
4160 sched = select_outer_tilable_band(gen, schedule);
4162 isl_union_map_foreach_map(sched, &set_untiled_len, &gen->untiled_len);
4163 sched = isl_union_map_intersect_domain(sched, domain);
4164 gen->sched = sched;
4166 isl_schedule_free(schedule);
4169 static struct cuda_stmt_access **expr_extract_access(struct pet_expr *expr,
4170 struct cuda_stmt_access **next_access)
4172 struct cuda_stmt_access *access;
4173 isl_ctx *ctx = isl_map_get_ctx(expr->acc.access);
4175 access = isl_alloc_type(ctx, struct cuda_stmt_access);
4176 assert(access);
4177 access->next = NULL;
4178 access->read = expr->acc.read;
4179 access->write = expr->acc.write;
4180 access->access = isl_map_copy(expr->acc.access);
4182 *next_access = access;
4183 next_access = &(*next_access)->next;
4184 return next_access;
4187 static struct cuda_stmt_access **expr_extract_accesses(struct pet_expr *expr,
4188 struct cuda_stmt_access **next_access)
4190 int i;
4192 for (i = 0; i < expr->n_arg; ++i)
4193 next_access = expr_extract_accesses(expr->args[i],
4194 next_access);
4196 if (expr->type == pet_expr_access)
4197 next_access = expr_extract_access(expr, next_access);
4199 return next_access;
4202 static void pet_stmt_extract_accesses(struct cuda_stmt *stmt)
4204 struct cuda_stmt_access **next_access = &stmt->accesses;
4206 stmt->accesses = NULL;
4207 expr_extract_accesses(stmt->body, next_access);
4210 /* Return an array of cuda_stmt representing the statements in "scop".
4212 static struct cuda_stmt *extract_stmts(isl_ctx *ctx, struct pet_scop *scop,
4213 __isl_keep isl_set *context)
4215 int i;
4216 struct cuda_stmt *stmts;
4218 stmts = isl_calloc_array(ctx, struct cuda_stmt, scop->n_stmt);
4219 assert(stmts);
4221 for (i = 0; i < scop->n_stmt; ++i) {
4222 struct cuda_stmt *s = &stmts[i];
4224 s->domain = isl_set_copy(scop->stmts[i]->domain);
4225 s->domain = isl_set_intersect(s->domain, isl_set_copy(context));
4226 s->body = scop->stmts[i]->body;
4227 pet_stmt_extract_accesses(s);
4230 return stmts;
4233 /* Replace the scop in the "input" file by equivalent code
4234 * that uses the GPU. "scop" is assumed to correspond to this scop.
4236 * We first compute a schedule that respects the dependences
4237 * of the original program and select the outermost band
4238 * of tilable dimensions that has at least one parallel loop.
4239 * We then have three blocks of dimensions
4241 * H B G
4243 * The tilable band "B" is first tiled according to "tile.sizes", resulting
4244 * in
4246 * H T P G
4248 * For each iteration of the T loop and for each array, we compute
4249 * the array elements accessed by that iteration, construct a rectangular
4250 * box around it and shift it to the origin. The result is used
4251 * as shared memory for the array.
4253 * We then split off at most 2 parallel loops from the T loops and
4254 * at most 3 parallel loops from the P loops
4256 * H T1 T2 P1 P2 G
4258 * The T1/P1 loops are then tiled or "wrapped" over the blocks/threads,
4259 * according to "grid.sizes"/"block.sizes".
4261 * H T1T T1P T2 P1T P1P P2 G
4263 * Finally, the T1P and P1P iterators are equated to the block and
4264 * thread dimensions respectively and so are effectively removed.
4265 * The H loops are run on the host. The T1T, T2, P1T, P2 and G loops
4266 * are run on the GPU.
4268 * Code is generated in three stages. We first generate code for the
4269 * host (the H loops), with iterators h%d. Then, for each leaf node
4270 * of the resulting AST, we generate code for the shared loops (up to
4271 * and including T2), with iterators g%d and after equating the H loops
4272 * to h%d parameters and the T1P loops to the block dimensions.
4273 * Finally, we generate code for the remaining loops in a similar fashion.
4275 int cuda_pet(isl_ctx *ctx, struct pet_scop *scop, struct ppcg_options *options,
4276 const char *input)
4278 isl_union_map *sched;
4279 struct cuda_gen gen;
4281 if (!scop)
4282 return -1;
4284 scop = pet_scop_align_params(scop);
4286 gen.ctx = ctx;
4287 gen.context = isl_set_copy(scop->context);
4288 gen.context = add_context_from_str(gen.context, options->ctx);
4289 gen.n_stmts = scop->n_stmt;
4290 gen.stmts = extract_stmts(ctx, scop, gen.context);
4291 gen.read = pet_scop_collect_reads(scop);
4292 gen.write = pet_scop_collect_writes(scop);
4293 gen.options = options;
4294 gen.state = cloog_isl_state_malloc(gen.ctx);
4295 gen.scop = scop;
4297 cuda_open_files(&gen.cuda, input);
4299 collect_array_info(&gen);
4301 sched = pet_scop_collect_schedule(scop);
4303 compute_schedule(&gen, sched);
4305 print_host_code(&gen);
4307 cloog_state_free(gen.state);
4308 clear_cuda_gen(&gen);
4310 cuda_close_files(&gen.cuda);
4312 return 0;