ppcg_scop: keep track of all variable names that are in use
[ppcg.git] / cuda.c
blob30f0a0c822a2c30305ea045e2dd2c22eab72dd84
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_plain_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_plain_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], NULL);
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;
271 if (!first)
272 p = isl_printer_print_str(p, ", ");
273 name = isl_space_get_dim_name(kernel->space, isl_dim_set, i);
274 if (types) {
275 p = isl_printer_print_str(p, type);
276 p = isl_printer_print_str(p, " ");
278 p = isl_printer_print_str(p, name);
280 first = 0;
283 return p;
286 /* Print the header of the given kernel.
288 static __isl_give isl_printer *print_kernel_header(__isl_take isl_printer *p,
289 struct gpu_prog *prog, struct ppcg_kernel *kernel)
291 p = isl_printer_start_line(p);
292 p = isl_printer_print_str(p, "__global__ void kernel");
293 p = isl_printer_print_int(p, kernel->id);
294 p = isl_printer_print_str(p, "(");
295 p = print_kernel_arguments(p, prog, kernel, 1);
296 p = isl_printer_print_str(p, ")");
298 return p;
301 /* Print the header of the given kernel to both gen->cuda.kernel_h
302 * and gen->cuda.kernel_c.
304 static void print_kernel_headers(struct gpu_prog *prog,
305 struct ppcg_kernel *kernel, struct cuda_info *cuda)
307 isl_printer *p;
309 p = isl_printer_to_file(prog->ctx, cuda->kernel_h);
310 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
311 p = print_kernel_header(p, prog, kernel);
312 p = isl_printer_print_str(p, ";");
313 p = isl_printer_end_line(p);
314 isl_printer_free(p);
316 p = isl_printer_to_file(prog->ctx, cuda->kernel_c);
317 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
318 p = print_kernel_header(p, prog, kernel);
319 p = isl_printer_end_line(p);
320 isl_printer_free(p);
323 static void print_indent(FILE *dst, int indent)
325 fprintf(dst, "%*s", indent, "");
328 static void print_kernel_iterators(FILE *out, struct ppcg_kernel *kernel)
330 int i, n_grid;
331 isl_ctx *ctx = isl_ast_node_get_ctx(kernel->tree);
332 const char *type;
333 const char *block_dims[] = { "blockIdx.x", "blockIdx.y" };
334 const char *thread_dims[] = { "threadIdx.x", "threadIdx.y",
335 "threadIdx.z" };
337 type = isl_options_get_ast_iterator_type(ctx);
339 n_grid = isl_multi_pw_aff_dim(kernel->grid_size, isl_dim_set);
340 if (n_grid > 0) {
341 print_indent(out, 4);
342 fprintf(out, "%s ", type);
343 for (i = 0; i < n_grid; ++i) {
344 if (i)
345 fprintf(out, ", ");
346 fprintf(out, "b%d = %s",
347 i, block_dims[n_grid - 1 - i]);
349 fprintf(out, ";\n");
352 if (kernel->n_block > 0) {
353 print_indent(out, 4);
354 fprintf(out, "%s ", type);
355 for (i = 0; i < kernel->n_block; ++i) {
356 if (i)
357 fprintf(out, ", ");
358 fprintf(out, "t%d = %s",
359 i, thread_dims[kernel->n_block - 1 - i]);
361 fprintf(out, ";\n");
365 static __isl_give isl_printer *print_kernel_var(__isl_take isl_printer *p,
366 struct ppcg_kernel_var *var)
368 int j;
370 p = isl_printer_start_line(p);
371 if (var->type == ppcg_access_shared)
372 p = isl_printer_print_str(p, "__shared__ ");
373 p = isl_printer_print_str(p, var->array->type);
374 p = isl_printer_print_str(p, " ");
375 p = isl_printer_print_str(p, var->name);
376 for (j = 0; j < var->array->n_index; ++j) {
377 isl_val *v;
379 p = isl_printer_print_str(p, "[");
380 v = isl_vec_get_element_val(var->size, j);
381 p = isl_printer_print_val(p, v);
382 isl_val_free(v);
383 p = isl_printer_print_str(p, "]");
385 p = isl_printer_print_str(p, ";");
386 p = isl_printer_end_line(p);
388 return p;
391 static __isl_give isl_printer *print_kernel_vars(__isl_take isl_printer *p,
392 struct ppcg_kernel *kernel)
394 int i;
396 for (i = 0; i < kernel->n_var; ++i)
397 p = print_kernel_var(p, &kernel->var[i]);
399 return p;
402 /* Print a sync statement.
404 static __isl_give isl_printer *print_sync(__isl_take isl_printer *p,
405 struct ppcg_kernel_stmt *stmt)
407 p = isl_printer_start_line(p);
408 p = isl_printer_print_str(p, "__syncthreads();");
409 p = isl_printer_end_line(p);
411 return p;
414 /* This function is called for each user statement in the AST,
415 * i.e., for each kernel body statement, copy statement or sync statement.
417 static __isl_give isl_printer *print_kernel_stmt(__isl_take isl_printer *p,
418 __isl_take isl_ast_print_options *print_options,
419 __isl_keep isl_ast_node *node, void *user)
421 isl_id *id;
422 struct ppcg_kernel_stmt *stmt;
424 id = isl_ast_node_get_annotation(node);
425 stmt = isl_id_get_user(id);
426 isl_id_free(id);
428 isl_ast_print_options_free(print_options);
430 switch (stmt->type) {
431 case ppcg_kernel_copy:
432 return ppcg_kernel_print_copy(p, stmt);
433 case ppcg_kernel_sync:
434 return print_sync(p, stmt);
435 case ppcg_kernel_domain:
436 return ppcg_kernel_print_domain(p, stmt);
439 return p;
442 static void print_kernel(struct gpu_prog *prog, struct ppcg_kernel *kernel,
443 struct cuda_info *cuda)
445 isl_ctx *ctx = isl_ast_node_get_ctx(kernel->tree);
446 isl_ast_print_options *print_options;
447 isl_printer *p;
449 print_kernel_headers(prog, kernel, cuda);
450 fprintf(cuda->kernel_c, "{\n");
451 print_kernel_iterators(cuda->kernel_c, kernel);
453 p = isl_printer_to_file(ctx, cuda->kernel_c);
454 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
455 p = isl_printer_indent(p, 4);
457 p = print_kernel_vars(p, kernel);
458 p = isl_printer_end_line(p);
459 p = gpu_print_macros(p, kernel->tree);
461 print_options = isl_ast_print_options_alloc(ctx);
462 print_options = isl_ast_print_options_set_print_user(print_options,
463 &print_kernel_stmt, NULL);
464 p = isl_ast_node_print(kernel->tree, p, print_options);
465 isl_printer_free(p);
467 fprintf(cuda->kernel_c, "}\n");
470 struct print_host_user_data {
471 struct cuda_info *cuda;
472 struct gpu_prog *prog;
475 /* Print the user statement of the host code to "p".
477 * In particular, print a block of statements that defines the grid
478 * and the block and then launches the kernel.
480 static __isl_give isl_printer *print_host_user(__isl_take isl_printer *p,
481 __isl_take isl_ast_print_options *print_options,
482 __isl_keep isl_ast_node *node, void *user)
484 isl_id *id;
485 struct ppcg_kernel *kernel;
486 struct print_host_user_data *data;
488 id = isl_ast_node_get_annotation(node);
489 kernel = isl_id_get_user(id);
490 isl_id_free(id);
492 data = (struct print_host_user_data *) user;
494 p = isl_printer_start_line(p);
495 p = isl_printer_print_str(p, "{");
496 p = isl_printer_end_line(p);
497 p = isl_printer_indent(p, 2);
499 p = isl_printer_start_line(p);
500 p = isl_printer_print_str(p, "dim3 k");
501 p = isl_printer_print_int(p, kernel->id);
502 p = isl_printer_print_str(p, "_dimBlock");
503 print_reverse_list(isl_printer_get_file(p),
504 kernel->n_block, kernel->block_dim);
505 p = isl_printer_print_str(p, ";");
506 p = isl_printer_end_line(p);
508 p = print_grid(p, kernel);
510 p = isl_printer_start_line(p);
511 p = isl_printer_print_str(p, "kernel");
512 p = isl_printer_print_int(p, kernel->id);
513 p = isl_printer_print_str(p, " <<<k");
514 p = isl_printer_print_int(p, kernel->id);
515 p = isl_printer_print_str(p, "_dimGrid, k");
516 p = isl_printer_print_int(p, kernel->id);
517 p = isl_printer_print_str(p, "_dimBlock>>> (");
518 p = print_kernel_arguments(p, data->prog, kernel, 0);
519 p = isl_printer_print_str(p, ");");
520 p = isl_printer_end_line(p);
522 p = isl_printer_start_line(p);
523 p = isl_printer_print_str(p, "cudaCheckKernel();");
524 p = isl_printer_end_line(p);
526 p = isl_printer_indent(p, -2);
527 p = isl_printer_start_line(p);
528 p = isl_printer_print_str(p, "}");
529 p = isl_printer_end_line(p);
531 p = isl_printer_start_line(p);
532 p = isl_printer_end_line(p);
534 print_kernel(data->prog, kernel, data->cuda);
536 isl_ast_print_options_free(print_options);
538 return p;
541 static __isl_give isl_printer *print_host_code(__isl_take isl_printer *p,
542 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
543 struct cuda_info *cuda)
545 isl_ast_print_options *print_options;
546 isl_ctx *ctx = isl_ast_node_get_ctx(tree);
547 struct print_host_user_data data = { cuda, prog };
549 print_options = isl_ast_print_options_alloc(ctx);
550 print_options = isl_ast_print_options_set_print_user(print_options,
551 &print_host_user, &data);
553 p = gpu_print_macros(p, tree);
554 p = isl_ast_node_print(tree, p, print_options);
556 return p;
559 /* For each array that needs to be copied out (based on prog->copy_out),
560 * copy the contents back from the GPU to the host.
562 * If any element of a given array appears in prog->copy_out, then its
563 * entire extent is in prog->copy_out. The bounds on this extent have
564 * been precomputed in extract_array_info and are used in
565 * gpu_array_info_print_size.
567 static __isl_give isl_printer *copy_arrays_from_device(
568 __isl_take isl_printer *p, struct gpu_prog *prog)
570 int i;
571 isl_union_set *copy_out;
572 copy_out = isl_union_set_copy(prog->copy_out);
574 for (i = 0; i < prog->n_array; ++i) {
575 isl_space *dim;
576 isl_set *copy_out_i;
577 int empty;
579 dim = isl_space_copy(prog->array[i].space);
580 copy_out_i = isl_union_set_extract_set(copy_out, dim);
581 empty = isl_set_plain_is_empty(copy_out_i);
582 isl_set_free(copy_out_i);
583 if (empty)
584 continue;
586 p = isl_printer_start_line(p);
587 p = isl_printer_print_str(p, "cudaCheckReturn(cudaMemcpy(");
588 if (gpu_array_is_scalar(&prog->array[i]))
589 p = isl_printer_print_str(p, "&");
590 p = isl_printer_print_str(p, prog->array[i].name);
591 p = isl_printer_print_str(p, ", dev_");
592 p = isl_printer_print_str(p, prog->array[i].name);
593 p = isl_printer_print_str(p, ", ");
594 p = gpu_array_info_print_size(p, &prog->array[i]);
595 p = isl_printer_print_str(p, ", cudaMemcpyDeviceToHost));");
596 p = isl_printer_end_line(p);
599 isl_union_set_free(copy_out);
600 p = isl_printer_start_line(p);
601 p = isl_printer_end_line(p);
602 return p;
605 static __isl_give isl_printer *free_device_arrays(__isl_take isl_printer *p,
606 struct gpu_prog *prog)
608 int i;
610 for (i = 0; i < prog->n_array; ++i) {
611 if (gpu_array_is_read_only_scalar(&prog->array[i]))
612 continue;
613 p = isl_printer_start_line(p);
614 p = isl_printer_print_str(p, "cudaCheckReturn(cudaFree(dev_");
615 p = isl_printer_print_str(p, prog->array[i].name);
616 p = isl_printer_print_str(p, "));");
617 p = isl_printer_end_line(p);
620 return p;
623 /* Given a gpu_prog "prog" and the corresponding transformed AST
624 * "tree", print the entire CUDA code to "p".
625 * "types" collects the types for which a definition has already
626 * been printed.
628 static __isl_give isl_printer *print_cuda(__isl_take isl_printer *p,
629 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
630 struct gpu_types *types, void *user)
632 struct cuda_info *cuda = user;
633 isl_printer *kernel;
635 kernel = isl_printer_to_file(isl_printer_get_ctx(p), cuda->kernel_c);
636 kernel = isl_printer_set_output_format(kernel, ISL_FORMAT_C);
637 kernel = gpu_print_types(kernel, types, prog);
638 isl_printer_free(kernel);
640 if (!kernel)
641 return isl_printer_free(p);
643 p = ppcg_start_block(p);
645 p = print_cuda_macros(p);
647 p = declare_device_arrays(p, prog);
648 p = allocate_device_arrays(p, prog);
649 p = copy_arrays_to_device(p, prog);
651 p = print_host_code(p, prog, tree, cuda);
653 p = copy_arrays_from_device(p, prog);
654 p = free_device_arrays(p, prog);
656 p = ppcg_end_block(p);
658 return p;
661 /* Transform the code in the file called "input" by replacing
662 * all scops by corresponding CUDA code.
663 * The names of the output files are derived from "input".
665 * We let generate_gpu do all the hard work and then let it call
666 * us back for printing the AST in print_cuda.
668 * To prepare for this printing, we first open the output files
669 * and we close them after generate_gpu has finished.
671 int generate_cuda(isl_ctx *ctx, struct ppcg_options *options,
672 const char *input)
674 struct cuda_info cuda;
675 int r;
677 cuda_open_files(&cuda, input);
679 r = generate_gpu(ctx, input, cuda.host_c, options, &print_cuda, &cuda);
681 cuda_close_files(&cuda);
683 return r;