/* Allocate a device array for "array'. * * Emit a max-expression to ensure the device array can contain at least one * element if the array's positive size guard expression is not trivial. */ static __isl_give isl_printer *allocate_device_array(__isl_take isl_printer *p, struct gpu_array_info *array) { int need_lower_bound; p = ppcg_start_block(p); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "dev_"); p = isl_printer_print_str(p, array->name); p = isl_printer_print_str(p, " = clCreateBuffer(context, "); p = isl_printer_print_str(p, "CL_MEM_READ_WRITE, "); need_lower_bound = !is_array_positive_size_guard_trivial(array); if (need_lower_bound) { p = isl_printer_print_str(p, "max(sizeof("); p = isl_printer_print_str(p, array->type); p = isl_printer_print_str(p, "), "); } p = gpu_array_info_print_size(p, array); if (need_lower_bound) p = isl_printer_print_str(p, ")"); p = isl_printer_print_str(p, ", NULL, &err);"); p = isl_printer_end_line(p); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "openclCheckReturn(err);"); p = isl_printer_end_line(p); p = ppcg_end_block(p); return p; }
/* Given a gpu_prog "prog" and the corresponding transformed AST * "tree", print the entire OpenCL code to "p". */ static __isl_give isl_printer *print_opencl(__isl_take isl_printer *p, struct gpu_prog *prog, __isl_keep isl_ast_node *tree, struct gpu_types *types, void *user) { struct opencl_info *opencl = user; opencl->kprinter = isl_printer_set_output_format(opencl->kprinter, ISL_FORMAT_C); if (any_double_elements(prog)) opencl->kprinter = opencl_enable_double_support( opencl->kprinter); if (opencl->options->opencl_print_kernel_types) opencl->kprinter = gpu_print_types(opencl->kprinter, types, prog); if (!opencl->kprinter) return isl_printer_free(p); p = ppcg_start_block(p); p = opencl_print_host_macros(p); p = gpu_print_local_declarations(p, prog); p = opencl_declare_device_arrays(p, prog); p = opencl_setup(p, opencl->input, opencl); p = opencl_allocate_device_arrays(p, prog); p = opencl_print_host_code(p, prog, tree, opencl); p = opencl_release_device_arrays(p, prog); p = opencl_release_cl_objects(p, opencl); p = ppcg_end_block(p); return p; }
/* Print the user statement of the host code to "p". * * The host code may contain original user statements, kernel launches, * statements that copy data to/from the device and statements * the initialize or clear the device. * The original user statements and the kernel launches have * an associated annotation, while the other statements do not. * The latter are handled by print_device_node. * The annotation on the user statements is called "user". * * In case of a kernel launch, print a block of statements that * defines the grid and the block and then launches the kernel. */ static __isl_give isl_printer *print_host_user(__isl_take isl_printer *p, __isl_take isl_ast_print_options *print_options, __isl_keep isl_ast_node *node, void *user) { isl_id *id; int is_user; struct ppcg_kernel *kernel; struct ppcg_kernel_stmt *stmt; struct print_host_user_data *data; isl_ast_print_options_free(print_options); data = (struct print_host_user_data *) user; id = isl_ast_node_get_annotation(node); if (!id) { //p = isl_printer_print_str(p,"marker_NO_ID_CASE"); return print_device_node(p, node, data->prog); } is_user = !strcmp(isl_id_get_name(id), "user"); kernel = is_user ? NULL : isl_id_get_user(id); stmt = is_user ? isl_id_get_user(id) : NULL; isl_id_free(id); if (is_user) return ppcg_kernel_print_domain(p, stmt); p = ppcg_start_block(p); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "dim3 k"); p = isl_printer_print_int(p, kernel->id); p = isl_printer_print_str(p, "_dimBlock"); print_reverse_list(isl_printer_get_file(p), kernel->n_block, kernel->block_dim); p = isl_printer_print_str(p, ";"); p = isl_printer_end_line(p); p = print_grid(p, kernel); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "kernel"); p = isl_printer_print_int(p, kernel->id); p = isl_printer_print_str(p, " <<<k"); p = isl_printer_print_int(p, kernel->id); p = isl_printer_print_str(p, "_dimGrid, k"); p = isl_printer_print_int(p, kernel->id); p = isl_printer_print_str(p, "_dimBlock>>> ("); p = print_kernel_arguments(p, data->prog, kernel, 0); p = isl_printer_print_str(p, ");"); p = isl_printer_end_line(p); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "cudaCheckKernel();"); p = isl_printer_end_line(p); p = ppcg_end_block(p); p = isl_printer_start_line(p); p = isl_printer_end_line(p); p = copy_data_from_device_to_device(p,kernel); printf("printing kernel"); print_kernel(data->prog, kernel, data->cuda); printf("printing kernel done"); return p; }