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"
19 static __isl_give isl_printer
*print_cuda_macros(__isl_take isl_printer
*p
)
22 "#define cudaCheckReturn(ret) \\\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"
30 " assert(cudaCheckReturn_e == cudaSuccess); \\\n"
32 "#define cudaCheckKernel() \\\n"
34 " cudaCheckReturn(cudaGetLastError()); \\\n"
37 p
= isl_printer_print_str(p
, macros
);
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
)
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
);
69 static __isl_give isl_printer
*declare_device_arrays(__isl_take isl_printer
*p
,
70 struct gpu_prog
*prog
)
74 for (i
= 0; i
< prog
->n_array
; ++i
) {
75 if (gpu_array_is_read_only_scalar(&prog
->array
[i
]))
78 p
= declare_device_array(p
, &prog
->array
[i
]);
80 p
= isl_printer_start_line(p
);
81 p
= isl_printer_end_line(p
);
85 static __isl_give isl_printer
*allocate_device_arrays(
86 __isl_take isl_printer
*p
, struct gpu_prog
*prog
)
90 for (i
= 0; i
< prog
->n_array
; ++i
) {
91 if (gpu_array_is_read_only_scalar(&prog
->array
[i
]))
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
);
107 static __isl_give isl_printer
*copy_arrays_to_device(__isl_take isl_printer
*p
,
108 struct gpu_prog
*prog
)
112 for (i
= 0; i
< prog
->n_array
; ++i
) {
117 if (gpu_array_is_read_only_scalar(&prog
->array
[i
]))
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
);
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
);
146 static void print_reverse_list(FILE *out
, int len
, int *list
)
154 for (i
= 0; i
< len
; ++i
) {
157 fprintf(out
, "%d", list
[len
- 1 - i
]);
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
)
171 dim
= isl_multi_pw_aff_dim(kernel
->grid_size
, isl_dim_set
);
175 p
= isl_printer_print_str(p
, "(");
176 for (i
= dim
- 1; i
>= 0; --i
) {
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
);
184 p
= isl_printer_print_str(p
, ", ");
187 p
= isl_printer_print_str(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
);
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
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
)
225 for (i
= 0; i
< prog
->n_array
; ++i
) {
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
);
237 p
= isl_printer_print_str(p
, ", ");
240 p
= gpu_array_info_print_declaration_argument(p
,
243 p
= gpu_array_info_print_call_argument(p
,
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
) {
254 name
= isl_space_get_dim_name(space
, isl_dim_param
, i
);
257 p
= isl_printer_print_str(p
, ", ");
259 p
= isl_printer_print_str(p
, "int ");
260 p
= isl_printer_print_str(p
, name
);
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
) {
273 p
= isl_printer_print_str(p
, ", ");
274 name
= isl_space_get_dim_name(kernel
->space
, isl_dim_set
, i
);
276 p
= isl_printer_print_str(p
, type
);
277 p
= isl_printer_print_str(p
, " ");
279 p
= isl_printer_print_str(p
, name
);
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
, ")");
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
)
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
);
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
);
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
)
332 isl_ctx
*ctx
= isl_ast_node_get_ctx(kernel
->tree
);
334 const char *block_dims
[] = { "blockIdx.x", "blockIdx.y" };
335 const char *thread_dims
[] = { "threadIdx.x", "threadIdx.y",
338 type
= isl_options_get_ast_iterator_type(ctx
);
340 n_grid
= isl_multi_pw_aff_dim(kernel
->grid_size
, isl_dim_set
);
342 print_indent(out
, 4);
343 fprintf(out
, "%s ", type
);
344 for (i
= 0; i
< n_grid
; ++i
) {
347 fprintf(out
, "b%d = %s",
348 i
, block_dims
[n_grid
- 1 - i
]);
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
) {
359 fprintf(out
, "t%d = %s",
360 i
, thread_dims
[kernel
->n_block
- 1 - i
]);
366 static __isl_give isl_printer
*print_kernel_var(__isl_take isl_printer
*p
,
367 struct ppcg_kernel_var
*var
)
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
) {
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
);
384 p
= isl_printer_print_str(p
, "]");
386 p
= isl_printer_print_str(p
, ";");
387 p
= isl_printer_end_line(p
);
392 static __isl_give isl_printer
*print_kernel_vars(__isl_take isl_printer
*p
,
393 struct ppcg_kernel
*kernel
)
397 for (i
= 0; i
< kernel
->n_var
; ++i
)
398 p
= print_kernel_var(p
, &kernel
->var
[i
]);
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
);
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
)
423 struct ppcg_kernel_stmt
*stmt
;
425 id
= isl_ast_node_get_annotation(node
);
426 stmt
= isl_id_get_user(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
);
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
;
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
);
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
)
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
);
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
);
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
);
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
)
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
) {
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
);
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
);
606 static __isl_give isl_printer
*free_device_arrays(__isl_take isl_printer
*p
,
607 struct gpu_prog
*prog
)
611 for (i
= 0; i
< prog
->n_array
; ++i
) {
612 if (gpu_array_is_read_only_scalar(&prog
->array
[i
]))
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
);
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
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
;
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
);
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
);
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
,
675 struct cuda_info cuda
;
678 cuda_open_files(&cuda
, input
);
680 r
= generate_gpu(ctx
, input
, cuda
.host_c
, options
, &print_cuda
, &cuda
);
682 cuda_close_files(&cuda
);