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
13 #include "cuda_common.h"
16 #include "gpu_print.h"
20 static __isl_give isl_printer
*print_cuda_macros(__isl_take isl_printer
*p
)
23 "#define cudaCheckReturn(ret) \\\n"
25 " cudaError_t cudaCheckReturn_e = (ret); \\\n"
26 " if (cudaCheckReturn_e != cudaSuccess) { \\\n"
27 " fprintf(stderr, \"CUDA error: %s\\n\", "
28 "cudaGetErrorString(cudaCheckReturn_e)); \\\n"
29 " fflush(stderr); \\\n"
31 " assert(cudaCheckReturn_e == cudaSuccess); \\\n"
33 "#define cudaCheckKernel() \\\n"
35 " cudaCheckReturn(cudaGetLastError()); \\\n"
38 p
= isl_printer_print_str(p
, macros
);
42 /* Print a declaration for the device array corresponding to "array" on "p".
44 static __isl_give isl_printer
*declare_device_array(__isl_take isl_printer
*p
,
45 struct gpu_array_info
*array
)
49 p
= isl_printer_start_line(p
);
50 p
= isl_printer_print_str(p
, array
->type
);
51 p
= isl_printer_print_str(p
, " ");
52 if (!array
->linearize
&& array
->n_index
> 1)
53 p
= isl_printer_print_str(p
, "(");
54 p
= isl_printer_print_str(p
, "*dev_");
55 p
= isl_printer_print_str(p
, array
->name
);
56 if (!array
->linearize
&& array
->n_index
> 1) {
57 p
= isl_printer_print_str(p
, ")");
58 for (i
= 1; i
< array
->n_index
; i
++) {
59 p
= isl_printer_print_str(p
, "[");
60 p
= isl_printer_print_pw_aff(p
, array
->bound
[i
]);
61 p
= isl_printer_print_str(p
, "]");
64 p
= isl_printer_print_str(p
, ";");
65 p
= isl_printer_end_line(p
);
70 static __isl_give isl_printer
*declare_device_arrays(__isl_take isl_printer
*p
,
71 struct gpu_prog
*prog
)
75 for (i
= 0; i
< prog
->n_array
; ++i
) {
76 if (!gpu_array_requires_device_allocation(&prog
->array
[i
]))
79 p
= declare_device_array(p
, &prog
->array
[i
]);
81 p
= isl_printer_start_line(p
);
82 p
= isl_printer_end_line(p
);
86 static __isl_give isl_printer
*allocate_device_arrays(
87 __isl_take isl_printer
*p
, struct gpu_prog
*prog
)
91 for (i
= 0; i
< prog
->n_array
; ++i
) {
92 if (!gpu_array_requires_device_allocation(&prog
->array
[i
]))
94 p
= isl_printer_start_line(p
);
95 p
= isl_printer_print_str(p
,
96 "cudaCheckReturn(cudaMalloc((void **) &dev_");
97 p
= isl_printer_print_str(p
, prog
->array
[i
].name
);
98 p
= isl_printer_print_str(p
, ", ");
99 p
= gpu_array_info_print_size(p
, &prog
->array
[i
]);
100 p
= isl_printer_print_str(p
, "));");
101 p
= isl_printer_end_line(p
);
103 p
= isl_printer_start_line(p
);
104 p
= isl_printer_end_line(p
);
108 /* Print code to "p" for copying "array" from the host to the device
109 * in its entirety. The bounds on the extent of "array" have
110 * been precomputed in extract_array_info and are used in
111 * gpu_array_info_print_size.
113 static __isl_give isl_printer
*copy_array_to_device(__isl_take isl_printer
*p
,
114 struct gpu_array_info
*array
)
116 p
= isl_printer_start_line(p
);
117 p
= isl_printer_print_str(p
, "cudaCheckReturn(cudaMemcpy(dev_");
118 p
= isl_printer_print_str(p
, array
->name
);
119 p
= isl_printer_print_str(p
, ", ");
121 if (gpu_array_is_scalar(array
))
122 p
= isl_printer_print_str(p
, "&");
123 p
= isl_printer_print_str(p
, array
->name
);
124 p
= isl_printer_print_str(p
, ", ");
126 p
= gpu_array_info_print_size(p
, array
);
127 p
= isl_printer_print_str(p
, ", cudaMemcpyHostToDevice));");
128 p
= isl_printer_end_line(p
);
133 /* Print code to "p" for copying "array" back from the device to the host
134 * in its entirety. The bounds on the extent of "array" have
135 * been precomputed in extract_array_info and are used in
136 * gpu_array_info_print_size.
138 static __isl_give isl_printer
*copy_array_from_device(
139 __isl_take isl_printer
*p
, struct gpu_array_info
*array
)
141 p
= isl_printer_start_line(p
);
142 p
= isl_printer_print_str(p
, "cudaCheckReturn(cudaMemcpy(");
143 if (gpu_array_is_scalar(array
))
144 p
= isl_printer_print_str(p
, "&");
145 p
= isl_printer_print_str(p
, array
->name
);
146 p
= isl_printer_print_str(p
, ", dev_");
147 p
= isl_printer_print_str(p
, array
->name
);
148 p
= isl_printer_print_str(p
, ", ");
149 p
= gpu_array_info_print_size(p
, array
);
150 p
= isl_printer_print_str(p
, ", cudaMemcpyDeviceToHost));");
151 p
= isl_printer_end_line(p
);
156 static void print_reverse_list(FILE *out
, int len
, int *list
)
164 for (i
= 0; i
< len
; ++i
) {
167 fprintf(out
, "%d", list
[len
- 1 - i
]);
172 /* Print the effective grid size as a list of the sizes in each
173 * dimension, from innermost to outermost.
175 static __isl_give isl_printer
*print_grid_size(__isl_take isl_printer
*p
,
176 struct ppcg_kernel
*kernel
)
181 dim
= isl_multi_pw_aff_dim(kernel
->grid_size
, isl_dim_set
);
185 p
= isl_printer_print_str(p
, "(");
186 for (i
= dim
- 1; i
>= 0; --i
) {
189 bound
= isl_multi_pw_aff_get_pw_aff(kernel
->grid_size
, i
);
190 p
= isl_printer_print_pw_aff(p
, bound
);
191 isl_pw_aff_free(bound
);
194 p
= isl_printer_print_str(p
, ", ");
197 p
= isl_printer_print_str(p
, ")");
202 /* Print the grid definition.
204 static __isl_give isl_printer
*print_grid(__isl_take isl_printer
*p
,
205 struct ppcg_kernel
*kernel
)
207 p
= isl_printer_start_line(p
);
208 p
= isl_printer_print_str(p
, "dim3 k");
209 p
= isl_printer_print_int(p
, kernel
->id
);
210 p
= isl_printer_print_str(p
, "_dimGrid");
211 p
= print_grid_size(p
, kernel
);
212 p
= isl_printer_print_str(p
, ";");
213 p
= isl_printer_end_line(p
);
218 /* Print the arguments to a kernel declaration or call. If "types" is set,
219 * then print a declaration (including the types of the arguments).
221 * The arguments are printed in the following order
222 * - the arrays accessed by the kernel
224 * - the host loop iterators
226 static __isl_give isl_printer
*print_kernel_arguments(__isl_take isl_printer
*p
,
227 struct gpu_prog
*prog
, struct ppcg_kernel
*kernel
, int types
)
235 for (i
= 0; i
< prog
->n_array
; ++i
) {
238 required
= ppcg_kernel_requires_array_argument(kernel
, i
);
240 return isl_printer_free(p
);
245 p
= isl_printer_print_str(p
, ", ");
248 p
= gpu_array_info_print_declaration_argument(p
,
249 &prog
->array
[i
], NULL
);
251 p
= gpu_array_info_print_call_argument(p
,
257 space
= isl_union_set_get_space(kernel
->arrays
);
258 nparam
= isl_space_dim(space
, isl_dim_param
);
259 for (i
= 0; i
< nparam
; ++i
) {
262 name
= isl_space_get_dim_name(space
, isl_dim_param
, i
);
265 p
= isl_printer_print_str(p
, ", ");
267 p
= isl_printer_print_str(p
, "int ");
268 p
= isl_printer_print_str(p
, name
);
272 isl_space_free(space
);
274 n
= isl_space_dim(kernel
->space
, isl_dim_set
);
275 type
= isl_options_get_ast_iterator_type(prog
->ctx
);
276 for (i
= 0; i
< n
; ++i
) {
280 p
= isl_printer_print_str(p
, ", ");
281 name
= isl_space_get_dim_name(kernel
->space
, isl_dim_set
, i
);
283 p
= isl_printer_print_str(p
, type
);
284 p
= isl_printer_print_str(p
, " ");
286 p
= isl_printer_print_str(p
, name
);
294 /* Print the header of the given kernel.
296 static __isl_give isl_printer
*print_kernel_header(__isl_take isl_printer
*p
,
297 struct gpu_prog
*prog
, struct ppcg_kernel
*kernel
)
299 p
= isl_printer_start_line(p
);
300 p
= isl_printer_print_str(p
, "__global__ void kernel");
301 p
= isl_printer_print_int(p
, kernel
->id
);
302 p
= isl_printer_print_str(p
, "(");
303 p
= print_kernel_arguments(p
, prog
, kernel
, 1);
304 p
= isl_printer_print_str(p
, ")");
309 /* Print the header of the given kernel to both gen->cuda.kernel_h
310 * and gen->cuda.kernel_c.
312 static void print_kernel_headers(struct gpu_prog
*prog
,
313 struct ppcg_kernel
*kernel
, struct cuda_info
*cuda
)
317 p
= isl_printer_to_file(prog
->ctx
, cuda
->kernel_h
);
318 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
319 p
= print_kernel_header(p
, prog
, kernel
);
320 p
= isl_printer_print_str(p
, ";");
321 p
= isl_printer_end_line(p
);
324 p
= isl_printer_to_file(prog
->ctx
, cuda
->kernel_c
);
325 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
326 p
= print_kernel_header(p
, prog
, kernel
);
327 p
= isl_printer_end_line(p
);
331 static void print_indent(FILE *dst
, int indent
)
333 fprintf(dst
, "%*s", indent
, "");
336 /* Print a list of iterators of type "type" with names "ids" to "out".
337 * Each iterator is assigned one of the cuda identifiers in cuda_dims.
338 * In particular, the last iterator is assigned the x identifier
339 * (the first in the list of cuda identifiers).
341 static void print_iterators(FILE *out
, const char *type
,
342 __isl_keep isl_id_list
*ids
, const char *cuda_dims
[])
346 n
= isl_id_list_n_id(ids
);
349 print_indent(out
, 4);
350 fprintf(out
, "%s ", type
);
351 for (i
= 0; i
< n
; ++i
) {
356 id
= isl_id_list_get_id(ids
, i
);
357 fprintf(out
, "%s = %s", isl_id_get_name(id
),
358 cuda_dims
[n
- 1 - i
]);
364 static void print_kernel_iterators(FILE *out
, struct ppcg_kernel
*kernel
)
366 isl_ctx
*ctx
= isl_ast_node_get_ctx(kernel
->tree
);
368 const char *block_dims
[] = { "blockIdx.x", "blockIdx.y" };
369 const char *thread_dims
[] = { "threadIdx.x", "threadIdx.y",
372 type
= isl_options_get_ast_iterator_type(ctx
);
374 print_iterators(out
, type
, kernel
->block_ids
, block_dims
);
375 print_iterators(out
, type
, kernel
->thread_ids
, thread_dims
);
378 static __isl_give isl_printer
*print_kernel_var(__isl_take isl_printer
*p
,
379 struct ppcg_kernel_var
*var
)
383 p
= isl_printer_start_line(p
);
384 if (var
->type
== ppcg_access_shared
)
385 p
= isl_printer_print_str(p
, "__shared__ ");
386 p
= isl_printer_print_str(p
, var
->array
->type
);
387 p
= isl_printer_print_str(p
, " ");
388 p
= isl_printer_print_str(p
, var
->name
);
389 for (j
= 0; j
< var
->array
->n_index
; ++j
) {
392 p
= isl_printer_print_str(p
, "[");
393 v
= isl_vec_get_element_val(var
->size
, j
);
394 p
= isl_printer_print_val(p
, v
);
396 p
= isl_printer_print_str(p
, "]");
398 p
= isl_printer_print_str(p
, ";");
399 p
= isl_printer_end_line(p
);
404 static __isl_give isl_printer
*print_kernel_vars(__isl_take isl_printer
*p
,
405 struct ppcg_kernel
*kernel
)
409 for (i
= 0; i
< kernel
->n_var
; ++i
)
410 p
= print_kernel_var(p
, &kernel
->var
[i
]);
415 /* Print a sync statement.
417 static __isl_give isl_printer
*print_sync(__isl_take isl_printer
*p
,
418 struct ppcg_kernel_stmt
*stmt
)
420 p
= isl_printer_start_line(p
);
421 p
= isl_printer_print_str(p
, "__syncthreads();");
422 p
= isl_printer_end_line(p
);
427 /* This function is called for each user statement in the AST,
428 * i.e., for each kernel body statement, copy statement or sync statement.
430 static __isl_give isl_printer
*print_kernel_stmt(__isl_take isl_printer
*p
,
431 __isl_take isl_ast_print_options
*print_options
,
432 __isl_keep isl_ast_node
*node
, void *user
)
435 struct ppcg_kernel_stmt
*stmt
;
437 id
= isl_ast_node_get_annotation(node
);
438 stmt
= isl_id_get_user(id
);
441 isl_ast_print_options_free(print_options
);
443 switch (stmt
->type
) {
444 case ppcg_kernel_copy
:
445 return ppcg_kernel_print_copy(p
, stmt
);
446 case ppcg_kernel_sync
:
447 return print_sync(p
, stmt
);
448 case ppcg_kernel_domain
:
449 return ppcg_kernel_print_domain(p
, stmt
);
455 static void print_kernel(struct gpu_prog
*prog
, struct ppcg_kernel
*kernel
,
456 struct cuda_info
*cuda
)
458 isl_ctx
*ctx
= isl_ast_node_get_ctx(kernel
->tree
);
459 isl_ast_print_options
*print_options
;
462 print_kernel_headers(prog
, kernel
, cuda
);
463 fprintf(cuda
->kernel_c
, "{\n");
464 print_kernel_iterators(cuda
->kernel_c
, kernel
);
466 p
= isl_printer_to_file(ctx
, cuda
->kernel_c
);
467 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
468 p
= isl_printer_indent(p
, 4);
470 p
= print_kernel_vars(p
, kernel
);
471 p
= isl_printer_end_line(p
);
472 p
= gpu_print_macros(p
, kernel
->tree
);
474 print_options
= isl_ast_print_options_alloc(ctx
);
475 print_options
= isl_ast_print_options_set_print_user(print_options
,
476 &print_kernel_stmt
, NULL
);
477 p
= isl_ast_node_print(kernel
->tree
, p
, print_options
);
480 fprintf(cuda
->kernel_c
, "}\n");
483 /* Print a statement for copying an array to or from the device.
484 * The statement identifier is called "to_device_<array name>" or
485 * "from_device_<array name>" and its user pointer points
486 * to the gpu_array_info of the array that needs to be copied.
488 * Extract the array from the identifier and call
489 * copy_array_to_device or copy_array_from_device.
491 static __isl_give isl_printer
*print_to_from_device(__isl_take isl_printer
*p
,
492 __isl_keep isl_ast_node
*node
, struct gpu_prog
*prog
)
494 isl_ast_expr
*expr
, *arg
;
497 struct gpu_array_info
*array
;
499 expr
= isl_ast_node_user_get_expr(node
);
500 arg
= isl_ast_expr_get_op_arg(expr
, 0);
501 id
= isl_ast_expr_get_id(arg
);
502 name
= isl_id_get_name(id
);
503 array
= isl_id_get_user(id
);
505 isl_ast_expr_free(arg
);
506 isl_ast_expr_free(expr
);
511 return isl_printer_free(p
);
513 if (!prefixcmp(name
, "to_device"))
514 return copy_array_to_device(p
, array
);
516 return copy_array_from_device(p
, array
);
519 struct print_host_user_data
{
520 struct cuda_info
*cuda
;
521 struct gpu_prog
*prog
;
524 /* Print the user statement of the host code to "p".
526 * The host code may contain original user statements, kernel launches and
527 * statements that copy data to/from the device.
528 * The original user statements and the kernel launches have
529 * an associated annotation, while the data copy statements do not.
530 * The latter are handled by print_to_from_device.
531 * The annotation on the user statements is called "user".
533 * In case of a kernel launch, print a block of statements that
534 * defines the grid and the block and then launches the kernel.
536 static __isl_give isl_printer
*print_host_user(__isl_take isl_printer
*p
,
537 __isl_take isl_ast_print_options
*print_options
,
538 __isl_keep isl_ast_node
*node
, void *user
)
542 struct ppcg_kernel
*kernel
;
543 struct ppcg_kernel_stmt
*stmt
;
544 struct print_host_user_data
*data
;
546 isl_ast_print_options_free(print_options
);
548 data
= (struct print_host_user_data
*) user
;
550 id
= isl_ast_node_get_annotation(node
);
552 return print_to_from_device(p
, node
, data
->prog
);
554 is_user
= !strcmp(isl_id_get_name(id
), "user");
555 kernel
= is_user
? NULL
: isl_id_get_user(id
);
556 stmt
= is_user
? isl_id_get_user(id
) : NULL
;
560 return ppcg_kernel_print_domain(p
, stmt
);
562 p
= isl_printer_start_line(p
);
563 p
= isl_printer_print_str(p
, "{");
564 p
= isl_printer_end_line(p
);
565 p
= isl_printer_indent(p
, 2);
567 p
= isl_printer_start_line(p
);
568 p
= isl_printer_print_str(p
, "dim3 k");
569 p
= isl_printer_print_int(p
, kernel
->id
);
570 p
= isl_printer_print_str(p
, "_dimBlock");
571 print_reverse_list(isl_printer_get_file(p
),
572 kernel
->n_block
, kernel
->block_dim
);
573 p
= isl_printer_print_str(p
, ";");
574 p
= isl_printer_end_line(p
);
576 p
= print_grid(p
, kernel
);
578 p
= isl_printer_start_line(p
);
579 p
= isl_printer_print_str(p
, "kernel");
580 p
= isl_printer_print_int(p
, kernel
->id
);
581 p
= isl_printer_print_str(p
, " <<<k");
582 p
= isl_printer_print_int(p
, kernel
->id
);
583 p
= isl_printer_print_str(p
, "_dimGrid, k");
584 p
= isl_printer_print_int(p
, kernel
->id
);
585 p
= isl_printer_print_str(p
, "_dimBlock>>> (");
586 p
= print_kernel_arguments(p
, data
->prog
, kernel
, 0);
587 p
= isl_printer_print_str(p
, ");");
588 p
= isl_printer_end_line(p
);
590 p
= isl_printer_start_line(p
);
591 p
= isl_printer_print_str(p
, "cudaCheckKernel();");
592 p
= isl_printer_end_line(p
);
594 p
= isl_printer_indent(p
, -2);
595 p
= isl_printer_start_line(p
);
596 p
= isl_printer_print_str(p
, "}");
597 p
= isl_printer_end_line(p
);
599 p
= isl_printer_start_line(p
);
600 p
= isl_printer_end_line(p
);
602 print_kernel(data
->prog
, kernel
, data
->cuda
);
607 static __isl_give isl_printer
*print_host_code(__isl_take isl_printer
*p
,
608 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
609 struct cuda_info
*cuda
)
611 isl_ast_print_options
*print_options
;
612 isl_ctx
*ctx
= isl_ast_node_get_ctx(tree
);
613 struct print_host_user_data data
= { cuda
, prog
};
615 print_options
= isl_ast_print_options_alloc(ctx
);
616 print_options
= isl_ast_print_options_set_print_user(print_options
,
617 &print_host_user
, &data
);
619 p
= gpu_print_macros(p
, tree
);
620 p
= isl_ast_node_print(tree
, p
, print_options
);
625 static __isl_give isl_printer
*free_device_arrays(__isl_take isl_printer
*p
,
626 struct gpu_prog
*prog
)
630 for (i
= 0; i
< prog
->n_array
; ++i
) {
631 if (!gpu_array_requires_device_allocation(&prog
->array
[i
]))
633 p
= isl_printer_start_line(p
);
634 p
= isl_printer_print_str(p
, "cudaCheckReturn(cudaFree(dev_");
635 p
= isl_printer_print_str(p
, prog
->array
[i
].name
);
636 p
= isl_printer_print_str(p
, "));");
637 p
= isl_printer_end_line(p
);
643 /* Given a gpu_prog "prog" and the corresponding transformed AST
644 * "tree", print the entire CUDA code to "p".
645 * "types" collects the types for which a definition has already
648 static __isl_give isl_printer
*print_cuda(__isl_take isl_printer
*p
,
649 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
650 struct gpu_types
*types
, void *user
)
652 struct cuda_info
*cuda
= user
;
655 kernel
= isl_printer_to_file(isl_printer_get_ctx(p
), cuda
->kernel_c
);
656 kernel
= isl_printer_set_output_format(kernel
, ISL_FORMAT_C
);
657 kernel
= gpu_print_types(kernel
, types
, prog
);
658 isl_printer_free(kernel
);
661 return isl_printer_free(p
);
663 p
= ppcg_start_block(p
);
665 p
= print_cuda_macros(p
);
667 p
= gpu_print_local_declarations(p
, prog
);
668 p
= declare_device_arrays(p
, prog
);
669 p
= allocate_device_arrays(p
, prog
);
671 p
= print_host_code(p
, prog
, tree
, cuda
);
673 p
= free_device_arrays(p
, prog
);
675 p
= ppcg_end_block(p
);
680 /* Transform the code in the file called "input" by replacing
681 * all scops by corresponding CUDA code.
682 * The names of the output files are derived from "input".
684 * We let generate_gpu do all the hard work and then let it call
685 * us back for printing the AST in print_cuda.
687 * To prepare for this printing, we first open the output files
688 * and we close them after generate_gpu has finished.
690 int generate_cuda(isl_ctx
*ctx
, struct ppcg_options
*options
,
693 struct cuda_info cuda
;
696 cuda_open_files(&cuda
, input
);
698 r
= generate_gpu(ctx
, input
, cuda
.host_c
, options
, &print_cuda
, &cuda
);
700 cuda_close_files(&cuda
);