print more understandable error message in case of unbounded arrays
[ppcg.git] / cuda.c
bloba4f97b667128e699c36fc0f0bd657673c22e09f6
1 /*
2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <isl/aff.h>
11 #include <isl/ast.h>
13 #include "cuda_common.h"
14 #include "cuda.h"
15 #include "gpu.h"
16 #include "gpu_print.h"
17 #include "print.h"
19 static __isl_give isl_printer *print_cuda_macros(__isl_take isl_printer *p)
21 const char *macros =
22 "#define cudaCheckReturn(ret) \\\n"
23 " do { \\\n"
24 " cudaError_t cudaCheckReturn_e = (ret); \\\n"
25 " if (cudaCheckReturn_e != cudaSuccess) { \\\n"
26 " fprintf(stderr, \"CUDA error: %s\\n\", "
27 "cudaGetErrorString(cudaCheckReturn_e)); \\\n"
28 " fflush(stderr); \\\n"
29 " } \\\n"
30 " assert(cudaCheckReturn_e == cudaSuccess); \\\n"
31 " } while(0)\n"
32 "#define cudaCheckKernel() \\\n"
33 " do { \\\n"
34 " cudaCheckReturn(cudaGetLastError()); \\\n"
35 " } while(0)\n\n";
37 p = isl_printer_print_str(p, macros);
38 return p;
41 /* Print a declaration for the device array corresponding to "array" on "p".
43 static __isl_give isl_printer *declare_device_array(__isl_take isl_printer *p,
44 struct gpu_array_info *array)
46 int i;
48 p = isl_printer_start_line(p);
49 p = isl_printer_print_str(p, array->type);
50 p = isl_printer_print_str(p, " ");
51 if (!array->linearize && array->n_index > 1)
52 p = isl_printer_print_str(p, "(");
53 p = isl_printer_print_str(p, "*dev_");
54 p = isl_printer_print_str(p, array->name);
55 if (!array->linearize && array->n_index > 1) {
56 p = isl_printer_print_str(p, ")");
57 for (i = 1; i < array->n_index; i++) {
58 p = isl_printer_print_str(p, "[");
59 p = isl_printer_print_pw_aff(p, array->bound[i]);
60 p = isl_printer_print_str(p, "]");
63 p = isl_printer_print_str(p, ";");
64 p = isl_printer_end_line(p);
66 return p;
69 static __isl_give isl_printer *declare_device_arrays(__isl_take isl_printer *p,
70 struct gpu_prog *prog)
72 int i;
74 for (i = 0; i < prog->n_array; ++i) {
75 if (gpu_array_is_read_only_scalar(&prog->array[i]))
76 continue;
78 p = declare_device_array(p, &prog->array[i]);
80 p = isl_printer_start_line(p);
81 p = isl_printer_end_line(p);
82 return p;
85 static __isl_give isl_printer *allocate_device_arrays(
86 __isl_take isl_printer *p, struct gpu_prog *prog)
88 int i;
90 for (i = 0; i < prog->n_array; ++i) {
91 if (gpu_array_is_read_only_scalar(&prog->array[i]))
92 continue;
93 p = isl_printer_start_line(p);
94 p = isl_printer_print_str(p,
95 "cudaCheckReturn(cudaMalloc((void **) &dev_");
96 p = isl_printer_print_str(p, prog->array[i].name);
97 p = isl_printer_print_str(p, ", ");
98 p = gpu_array_info_print_size(p, &prog->array[i]);
99 p = isl_printer_print_str(p, "));");
100 p = isl_printer_end_line(p);
102 p = isl_printer_start_line(p);
103 p = isl_printer_end_line(p);
104 return p;
107 static __isl_give isl_printer *copy_arrays_to_device(__isl_take isl_printer *p,
108 struct gpu_prog *prog)
110 int i;
112 for (i = 0; i < prog->n_array; ++i) {
113 isl_space *dim;
114 isl_set *read_i;
115 int empty;
117 if (gpu_array_is_read_only_scalar(&prog->array[i]))
118 continue;
120 dim = isl_space_copy(prog->array[i].space);
121 read_i = isl_union_set_extract_set(prog->copy_in, dim);
122 empty = isl_set_fast_is_empty(read_i);
123 isl_set_free(read_i);
124 if (empty)
125 continue;
127 p = isl_printer_start_line(p);
128 p = isl_printer_print_str(p, "cudaCheckReturn(cudaMemcpy(dev_");
129 p = isl_printer_print_str(p, prog->array[i].name);
130 p = isl_printer_print_str(p, ", ");
132 if (gpu_array_is_scalar(&prog->array[i]))
133 p = isl_printer_print_str(p, "&");
134 p = isl_printer_print_str(p, prog->array[i].name);
135 p = isl_printer_print_str(p, ", ");
137 p = gpu_array_info_print_size(p, &prog->array[i]);
138 p = isl_printer_print_str(p, ", cudaMemcpyHostToDevice));");
139 p = isl_printer_end_line(p);
141 p = isl_printer_start_line(p);
142 p = isl_printer_end_line(p);
143 return p;
146 static void print_reverse_list(FILE *out, int len, int *list)
148 int i;
150 if (len == 0)
151 return;
153 fprintf(out, "(");
154 for (i = 0; i < len; ++i) {
155 if (i)
156 fprintf(out, ", ");
157 fprintf(out, "%d", list[len - 1 - i]);
159 fprintf(out, ")");
162 /* Print the effective grid size as a list of the sizes in each
163 * dimension, from innermost to outermost.
165 static __isl_give isl_printer *print_grid_size(__isl_take isl_printer *p,
166 struct ppcg_kernel *kernel)
168 int i;
169 int dim;
171 dim = isl_multi_pw_aff_dim(kernel->grid_size, isl_dim_set);
172 if (dim == 0)
173 return p;
175 p = isl_printer_print_str(p, "(");
176 for (i = dim - 1; i >= 0; --i) {
177 isl_pw_aff *bound;
179 bound = isl_multi_pw_aff_get_pw_aff(kernel->grid_size, i);
180 p = isl_printer_print_pw_aff(p, bound);
181 isl_pw_aff_free(bound);
183 if (i > 0)
184 p = isl_printer_print_str(p, ", ");
187 p = isl_printer_print_str(p, ")");
189 return p;
192 /* Print the grid definition.
194 static __isl_give isl_printer *print_grid(__isl_take isl_printer *p,
195 struct ppcg_kernel *kernel)
197 p = isl_printer_start_line(p);
198 p = isl_printer_print_str(p, "dim3 k");
199 p = isl_printer_print_int(p, kernel->id);
200 p = isl_printer_print_str(p, "_dimGrid");
201 p = print_grid_size(p, kernel);
202 p = isl_printer_print_str(p, ";");
203 p = isl_printer_end_line(p);
205 return p;
208 /* Print the arguments to a kernel declaration or call. If "types" is set,
209 * then print a declaration (including the types of the arguments).
211 * The arguments are printed in the following order
212 * - the arrays accessed by the kernel
213 * - the parameters
214 * - the host loop iterators
216 static __isl_give isl_printer *print_kernel_arguments(__isl_take isl_printer *p,
217 struct gpu_prog *prog, struct ppcg_kernel *kernel, int types)
219 int i, n;
220 int first = 1;
221 unsigned nparam;
222 isl_space *space;
223 const char *type;
225 for (i = 0; i < prog->n_array; ++i) {
226 isl_set *arr;
227 int empty;
229 space = isl_space_copy(prog->array[i].space);
230 arr = isl_union_set_extract_set(kernel->arrays, space);
231 empty = isl_set_fast_is_empty(arr);
232 isl_set_free(arr);
233 if (empty)
234 continue;
236 if (!first)
237 p = isl_printer_print_str(p, ", ");
239 if (types)
240 p = gpu_array_info_print_declaration_argument(p,
241 &prog->array[i]);
242 else
243 p = gpu_array_info_print_call_argument(p,
244 &prog->array[i]);
246 first = 0;
249 space = isl_union_set_get_space(kernel->arrays);
250 nparam = isl_space_dim(space, isl_dim_param);
251 for (i = 0; i < nparam; ++i) {
252 const char *name;
254 name = isl_space_get_dim_name(space, isl_dim_param, i);
256 if (!first)
257 p = isl_printer_print_str(p, ", ");
258 if (types)
259 p = isl_printer_print_str(p, "int ");
260 p = isl_printer_print_str(p, name);
262 first = 0;
264 isl_space_free(space);
266 n = isl_space_dim(kernel->space, isl_dim_set);
267 type = isl_options_get_ast_iterator_type(prog->ctx);
268 for (i = 0; i < n; ++i) {
269 const char *name;
270 isl_id *id;
272 if (!first)
273 p = isl_printer_print_str(p, ", ");
274 name = isl_space_get_dim_name(kernel->space, isl_dim_set, i);
275 if (types) {
276 p = isl_printer_print_str(p, type);
277 p = isl_printer_print_str(p, " ");
279 p = isl_printer_print_str(p, name);
281 first = 0;
284 return p;
287 /* Print the header of the given kernel.
289 static __isl_give isl_printer *print_kernel_header(__isl_take isl_printer *p,
290 struct gpu_prog *prog, struct ppcg_kernel *kernel)
292 p = isl_printer_start_line(p);
293 p = isl_printer_print_str(p, "__global__ void kernel");
294 p = isl_printer_print_int(p, kernel->id);
295 p = isl_printer_print_str(p, "(");
296 p = print_kernel_arguments(p, prog, kernel, 1);
297 p = isl_printer_print_str(p, ")");
299 return p;
302 /* Print the header of the given kernel to both gen->cuda.kernel_h
303 * and gen->cuda.kernel_c.
305 static void print_kernel_headers(struct gpu_prog *prog,
306 struct ppcg_kernel *kernel, struct cuda_info *cuda)
308 isl_printer *p;
310 p = isl_printer_to_file(prog->ctx, cuda->kernel_h);
311 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
312 p = print_kernel_header(p, prog, kernel);
313 p = isl_printer_print_str(p, ";");
314 p = isl_printer_end_line(p);
315 isl_printer_free(p);
317 p = isl_printer_to_file(prog->ctx, cuda->kernel_c);
318 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
319 p = print_kernel_header(p, prog, kernel);
320 p = isl_printer_end_line(p);
321 isl_printer_free(p);
324 static void print_indent(FILE *dst, int indent)
326 fprintf(dst, "%*s", indent, "");
329 static void print_kernel_iterators(FILE *out, struct ppcg_kernel *kernel)
331 int i, n_grid;
332 isl_ctx *ctx = isl_ast_node_get_ctx(kernel->tree);
333 const char *type;
334 const char *block_dims[] = { "blockIdx.x", "blockIdx.y" };
335 const char *thread_dims[] = { "threadIdx.x", "threadIdx.y",
336 "threadIdx.z" };
338 type = isl_options_get_ast_iterator_type(ctx);
340 n_grid = isl_multi_pw_aff_dim(kernel->grid_size, isl_dim_set);
341 if (n_grid > 0) {
342 print_indent(out, 4);
343 fprintf(out, "%s ", type);
344 for (i = 0; i < n_grid; ++i) {
345 if (i)
346 fprintf(out, ", ");
347 fprintf(out, "b%d = %s",
348 i, block_dims[n_grid - 1 - i]);
350 fprintf(out, ";\n");
353 if (kernel->n_block > 0) {
354 print_indent(out, 4);
355 fprintf(out, "%s ", type);
356 for (i = 0; i < kernel->n_block; ++i) {
357 if (i)
358 fprintf(out, ", ");
359 fprintf(out, "t%d = %s",
360 i, thread_dims[kernel->n_block - 1 - i]);
362 fprintf(out, ";\n");
366 static __isl_give isl_printer *print_kernel_var(__isl_take isl_printer *p,
367 struct ppcg_kernel_var *var)
369 int j;
371 p = isl_printer_start_line(p);
372 if (var->type == ppcg_access_shared)
373 p = isl_printer_print_str(p, "__shared__ ");
374 p = isl_printer_print_str(p, var->array->type);
375 p = isl_printer_print_str(p, " ");
376 p = isl_printer_print_str(p, var->name);
377 for (j = 0; j < var->array->n_index; ++j) {
378 isl_val *v;
380 p = isl_printer_print_str(p, "[");
381 v = isl_vec_get_element_val(var->size, j);
382 p = isl_printer_print_val(p, v);
383 isl_val_free(v);
384 p = isl_printer_print_str(p, "]");
386 p = isl_printer_print_str(p, ";");
387 p = isl_printer_end_line(p);
389 return p;
392 static __isl_give isl_printer *print_kernel_vars(__isl_take isl_printer *p,
393 struct ppcg_kernel *kernel)
395 int i;
397 for (i = 0; i < kernel->n_var; ++i)
398 p = print_kernel_var(p, &kernel->var[i]);
400 return p;
403 /* Print a sync statement.
405 static __isl_give isl_printer *print_sync(__isl_take isl_printer *p,
406 struct ppcg_kernel_stmt *stmt)
408 p = isl_printer_start_line(p);
409 p = isl_printer_print_str(p, "__syncthreads();");
410 p = isl_printer_end_line(p);
412 return p;
415 /* This function is called for each user statement in the AST,
416 * i.e., for each kernel body statement, copy statement or sync statement.
418 static __isl_give isl_printer *print_kernel_stmt(__isl_take isl_printer *p,
419 __isl_take isl_ast_print_options *print_options,
420 __isl_keep isl_ast_node *node, void *user)
422 isl_id *id;
423 struct ppcg_kernel_stmt *stmt;
425 id = isl_ast_node_get_annotation(node);
426 stmt = isl_id_get_user(id);
427 isl_id_free(id);
429 isl_ast_print_options_free(print_options);
431 switch (stmt->type) {
432 case ppcg_kernel_copy:
433 return ppcg_kernel_print_copy(p, stmt);
434 case ppcg_kernel_sync:
435 return print_sync(p, stmt);
436 case ppcg_kernel_domain:
437 return ppcg_kernel_print_domain(p, stmt);
440 return p;
443 static void print_kernel(struct gpu_prog *prog, struct ppcg_kernel *kernel,
444 struct cuda_info *cuda)
446 isl_ctx *ctx = isl_ast_node_get_ctx(kernel->tree);
447 isl_ast_print_options *print_options;
448 isl_printer *p;
450 print_kernel_headers(prog, kernel, cuda);
451 fprintf(cuda->kernel_c, "{\n");
452 print_kernel_iterators(cuda->kernel_c, kernel);
454 p = isl_printer_to_file(ctx, cuda->kernel_c);
455 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
456 p = isl_printer_indent(p, 4);
458 p = print_kernel_vars(p, kernel);
459 p = isl_printer_end_line(p);
460 p = gpu_print_macros(p, kernel->tree);
462 print_options = isl_ast_print_options_alloc(ctx);
463 print_options = isl_ast_print_options_set_print_user(print_options,
464 &print_kernel_stmt, NULL);
465 p = isl_ast_node_print(kernel->tree, p, print_options);
466 isl_printer_free(p);
468 fprintf(cuda->kernel_c, "}\n");
471 struct print_host_user_data {
472 struct cuda_info *cuda;
473 struct gpu_prog *prog;
476 /* Print the user statement of the host code to "p".
478 * In particular, print a block of statements that defines the grid
479 * and the block and then launches the kernel.
481 static __isl_give isl_printer *print_host_user(__isl_take isl_printer *p,
482 __isl_take isl_ast_print_options *print_options,
483 __isl_keep isl_ast_node *node, void *user)
485 isl_id *id;
486 struct ppcg_kernel *kernel;
487 struct print_host_user_data *data;
489 id = isl_ast_node_get_annotation(node);
490 kernel = isl_id_get_user(id);
491 isl_id_free(id);
493 data = (struct print_host_user_data *) user;
495 p = isl_printer_start_line(p);
496 p = isl_printer_print_str(p, "{");
497 p = isl_printer_end_line(p);
498 p = isl_printer_indent(p, 2);
500 p = isl_printer_start_line(p);
501 p = isl_printer_print_str(p, "dim3 k");
502 p = isl_printer_print_int(p, kernel->id);
503 p = isl_printer_print_str(p, "_dimBlock");
504 print_reverse_list(isl_printer_get_file(p),
505 kernel->n_block, kernel->block_dim);
506 p = isl_printer_print_str(p, ";");
507 p = isl_printer_end_line(p);
509 p = print_grid(p, kernel);
511 p = isl_printer_start_line(p);
512 p = isl_printer_print_str(p, "kernel");
513 p = isl_printer_print_int(p, kernel->id);
514 p = isl_printer_print_str(p, " <<<k");
515 p = isl_printer_print_int(p, kernel->id);
516 p = isl_printer_print_str(p, "_dimGrid, k");
517 p = isl_printer_print_int(p, kernel->id);
518 p = isl_printer_print_str(p, "_dimBlock>>> (");
519 p = print_kernel_arguments(p, data->prog, kernel, 0);
520 p = isl_printer_print_str(p, ");");
521 p = isl_printer_end_line(p);
523 p = isl_printer_start_line(p);
524 p = isl_printer_print_str(p, "cudaCheckKernel();");
525 p = isl_printer_end_line(p);
527 p = isl_printer_indent(p, -2);
528 p = isl_printer_start_line(p);
529 p = isl_printer_print_str(p, "}");
530 p = isl_printer_end_line(p);
532 p = isl_printer_start_line(p);
533 p = isl_printer_end_line(p);
535 print_kernel(data->prog, kernel, data->cuda);
537 isl_ast_print_options_free(print_options);
539 return p;
542 static __isl_give isl_printer *print_host_code(__isl_take isl_printer *p,
543 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
544 struct cuda_info *cuda)
546 isl_ast_print_options *print_options;
547 isl_ctx *ctx = isl_ast_node_get_ctx(tree);
548 struct print_host_user_data data = { cuda, prog };
550 print_options = isl_ast_print_options_alloc(ctx);
551 print_options = isl_ast_print_options_set_print_user(print_options,
552 &print_host_user, &data);
554 p = gpu_print_macros(p, tree);
555 p = isl_ast_node_print(tree, p, print_options);
557 return p;
560 /* For each array that needs to be copied out (based on prog->copy_out),
561 * copy the contents back from the GPU to the host.
563 * If any element of a given array appears in prog->copy_out, then its
564 * entire extent is in prog->copy_out. The bounds on this extent have
565 * been precomputed in extract_array_info and are used in
566 * gpu_array_info_print_size.
568 static __isl_give isl_printer *copy_arrays_from_device(
569 __isl_take isl_printer *p, struct gpu_prog *prog)
571 int i;
572 isl_union_set *copy_out;
573 copy_out = isl_union_set_copy(prog->copy_out);
575 for (i = 0; i < prog->n_array; ++i) {
576 isl_space *dim;
577 isl_set *copy_out_i;
578 int empty;
580 dim = isl_space_copy(prog->array[i].space);
581 copy_out_i = isl_union_set_extract_set(copy_out, dim);
582 empty = isl_set_fast_is_empty(copy_out_i);
583 isl_set_free(copy_out_i);
584 if (empty)
585 continue;
587 p = isl_printer_start_line(p);
588 p = isl_printer_print_str(p, "cudaCheckReturn(cudaMemcpy(");
589 if (gpu_array_is_scalar(&prog->array[i]))
590 p = isl_printer_print_str(p, "&");
591 p = isl_printer_print_str(p, prog->array[i].name);
592 p = isl_printer_print_str(p, ", dev_");
593 p = isl_printer_print_str(p, prog->array[i].name);
594 p = isl_printer_print_str(p, ", ");
595 p = gpu_array_info_print_size(p, &prog->array[i]);
596 p = isl_printer_print_str(p, ", cudaMemcpyDeviceToHost));");
597 p = isl_printer_end_line(p);
600 isl_union_set_free(copy_out);
601 p = isl_printer_start_line(p);
602 p = isl_printer_end_line(p);
603 return p;
606 static __isl_give isl_printer *free_device_arrays(__isl_take isl_printer *p,
607 struct gpu_prog *prog)
609 int i;
611 for (i = 0; i < prog->n_array; ++i) {
612 if (gpu_array_is_read_only_scalar(&prog->array[i]))
613 continue;
614 p = isl_printer_start_line(p);
615 p = isl_printer_print_str(p, "cudaCheckReturn(cudaFree(dev_");
616 p = isl_printer_print_str(p, prog->array[i].name);
617 p = isl_printer_print_str(p, "));");
618 p = isl_printer_end_line(p);
621 return p;
624 /* Given a gpu_prog "prog" and the corresponding transformed AST
625 * "tree", print the entire CUDA code to "p".
626 * "types" collects the types for which a definition has already
627 * been printed.
629 static __isl_give isl_printer *print_cuda(__isl_take isl_printer *p,
630 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
631 struct gpu_types *types, void *user)
633 struct cuda_info *cuda = user;
634 isl_printer *kernel;
636 kernel = isl_printer_to_file(isl_printer_get_ctx(p), cuda->kernel_c);
637 kernel = isl_printer_set_output_format(kernel, ISL_FORMAT_C);
638 kernel = gpu_print_types(kernel, types, prog);
639 isl_printer_free(kernel);
641 if (!kernel)
642 return isl_printer_free(p);
644 p = ppcg_start_block(p);
646 p = print_cuda_macros(p);
648 p = declare_device_arrays(p, prog);
649 p = allocate_device_arrays(p, prog);
650 p = copy_arrays_to_device(p, prog);
652 p = print_host_code(p, prog, tree, cuda);
654 p = copy_arrays_from_device(p, prog);
655 p = free_device_arrays(p, prog);
657 p = ppcg_end_block(p);
659 return p;
662 /* Transform the code in the file called "input" by replacing
663 * all scops by corresponding CUDA code.
664 * The names of the output files are derived from "input".
666 * We let generate_gpu do all the hard work and then let it call
667 * us back for printing the AST in print_cuda.
669 * To prepare for this printing, we first open the output files
670 * and we close them after generate_gpu has finished.
672 int generate_cuda(isl_ctx *ctx, struct ppcg_options *options,
673 const char *input)
675 struct cuda_info cuda;
676 int r;
678 cuda_open_files(&cuda, input);
680 r = generate_gpu(ctx, input, cuda.host_c, options, &print_cuda, &cuda);
682 cuda_close_files(&cuda);
684 return r;