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_plain_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_plain_is_empty(arr
);
237 p
= isl_printer_print_str(p
, ", ");
240 p
= gpu_array_info_print_declaration_argument(p
,
241 &prog
->array
[i
], NULL
);
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
) {
272 p
= isl_printer_print_str(p
, ", ");
273 name
= isl_space_get_dim_name(kernel
->space
, isl_dim_set
, i
);
275 p
= isl_printer_print_str(p
, type
);
276 p
= isl_printer_print_str(p
, " ");
278 p
= isl_printer_print_str(p
, name
);
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
, ")");
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
)
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
);
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
);
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
)
331 isl_ctx
*ctx
= isl_ast_node_get_ctx(kernel
->tree
);
333 const char *block_dims
[] = { "blockIdx.x", "blockIdx.y" };
334 const char *thread_dims
[] = { "threadIdx.x", "threadIdx.y",
337 type
= isl_options_get_ast_iterator_type(ctx
);
339 n_grid
= isl_multi_pw_aff_dim(kernel
->grid_size
, isl_dim_set
);
341 print_indent(out
, 4);
342 fprintf(out
, "%s ", type
);
343 for (i
= 0; i
< n_grid
; ++i
) {
346 fprintf(out
, "b%d = %s",
347 i
, block_dims
[n_grid
- 1 - i
]);
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
) {
358 fprintf(out
, "t%d = %s",
359 i
, thread_dims
[kernel
->n_block
- 1 - i
]);
365 static __isl_give isl_printer
*print_kernel_var(__isl_take isl_printer
*p
,
366 struct ppcg_kernel_var
*var
)
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
) {
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
);
383 p
= isl_printer_print_str(p
, "]");
385 p
= isl_printer_print_str(p
, ";");
386 p
= isl_printer_end_line(p
);
391 static __isl_give isl_printer
*print_kernel_vars(__isl_take isl_printer
*p
,
392 struct ppcg_kernel
*kernel
)
396 for (i
= 0; i
< kernel
->n_var
; ++i
)
397 p
= print_kernel_var(p
, &kernel
->var
[i
]);
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
);
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
)
422 struct ppcg_kernel_stmt
*stmt
;
424 id
= isl_ast_node_get_annotation(node
);
425 stmt
= isl_id_get_user(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
);
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
;
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
);
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
)
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
);
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
);
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
);
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
)
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
) {
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
);
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
);
605 static __isl_give isl_printer
*free_device_arrays(__isl_take isl_printer
*p
,
606 struct gpu_prog
*prog
)
610 for (i
= 0; i
< prog
->n_array
; ++i
) {
611 if (gpu_array_is_read_only_scalar(&prog
->array
[i
]))
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
);
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
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
;
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
);
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
);
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
,
674 struct cuda_info cuda
;
677 cuda_open_files(&cuda
, input
);
679 r
= generate_gpu(ctx
, input
, cuda
.host_c
, options
, &print_cuda
, &cuda
);
681 cuda_close_files(&cuda
);