void Stencil::normalize() { std::cout << "Source PGM kernel:\n"; print_kernel(); double kernel_sum = shift_kernel_range(); normalize_values(kernel_sum); std::cout << "Normalized kernel:\n"; print_kernel(); }
/* 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; }
/* resulting groups are sorted WRT size */ Volume *group_kernel(Kernel * K, Volume * vol, double bg) { int x, y, z; int sizes[MAX_VAR_DIMS]; progress_struct progress; Volume tmp_vol; Kernel *k1, *k2; unsigned int *equiv; unsigned int *counts; unsigned int *trans; unsigned int neighbours[K->nelems]; /* counters */ unsigned int c; unsigned int value; unsigned int group_idx; /* label for the next group */ unsigned int num_groups; unsigned int min_label; unsigned int curr_label; unsigned int prev_label; unsigned int num_matches; /* structure for group data */ Group_info *group_data; /* split the Kernel into forward and backwards kernels */ k1 = new_kernel(K->nelems); k2 = new_kernel(K->nelems); split_kernel(K, k1, k2); setup_pad_values(k1); setup_pad_values(k2); if(verbose){ fprintf(stdout, "Group kernel - background %g\n", bg); fprintf(stdout, "forward direction kernel:\n"); print_kernel(k1); fprintf(stdout, "\nreverse direction kernel:\n"); print_kernel(k2); } get_volume_sizes(*vol, sizes); initialize_progress_report(&progress, FALSE, sizes[2], "Groups"); /* copy and then zero out the original volume */ tmp_vol = copy_volume(*vol); for(z = sizes[0]; z--;){ for(y = sizes[1]; y--;){ for(x = sizes[2]; x--;){ set_volume_voxel_value(*vol, z, y, x, 0, 0, 0); } } } /* pass 1 - forward direction (we assume a symmetric kernel) */ /* our first group is given the label 1 */ group_idx = 1; /* initialise the equiv and counts arrays */ SET_ARRAY_SIZE(equiv, 0, group_idx, 500); equiv[0] = 0; SET_ARRAY_SIZE(counts, 0, group_idx, 500); counts[0] = 0; for(z = -k1->pre_pad[2]; z < sizes[0] - k1->post_pad[2]; z++){ for(y = -k1->pre_pad[1]; y < sizes[1] - k1->post_pad[1]; y++){ for(x = -k1->pre_pad[0]; x < sizes[2] - k1->post_pad[0]; x++){ if(get_volume_voxel_value(tmp_vol, z, y, x, 0, 0) != bg){ /* search this voxels neighbours */ num_matches = 0; min_label = INT_MAX; for(c = 0; c < k1->nelems; c++){ value = (unsigned int)get_volume_voxel_value(*vol, z + k1->K[c][2], y + k1->K[c][1], x + k1->K[c][0], 0 + k1->K[c][3], 0 + k1->K[c][4]); if(value != 0){ if(value < min_label){ min_label = value; } neighbours[num_matches] = value; num_matches++; } } switch (num_matches){ case 0: /* no neighbours, make a new label and increment */ set_volume_voxel_value(*vol, z, y, x, 0, 0, (Real) group_idx); SET_ARRAY_SIZE(equiv, group_idx, group_idx + 1, 500); equiv[group_idx] = group_idx; SET_ARRAY_SIZE(counts, group_idx, group_idx + 1, 500); counts[group_idx] = 1; group_idx++; break; case 1: /* only one neighbour, no equivalences needed */ set_volume_voxel_value(*vol, z, y, x, 0, 0, (Real) min_label); counts[min_label]++; break; default: /* more than one neighbour */ /* first sort the neighbours array */ qsort(&neighbours[0], (size_t) num_matches, sizeof(unsigned int), &compare_ints); /* find the minimum possible label for this voxel, */ /* this is done by descending through each neighbours */ /* equivalences until an equivalence equal to itself */ /* is found */ prev_label = -1; for(c = 0; c < num_matches; c++){ curr_label = neighbours[c]; /* recurse this label if we haven't yet */ if(curr_label != prev_label){ while(equiv[curr_label] != equiv[equiv[curr_label]]){ curr_label = equiv[curr_label]; } /* check against the current minimum value */ if(equiv[curr_label] < min_label){ min_label = equiv[curr_label]; } } prev_label = neighbours[c]; } /* repeat, setting equivalences to the min_label */ prev_label = -1; for(c = 0; c < num_matches; c++){ curr_label = neighbours[c]; if(curr_label != prev_label){ while(equiv[curr_label] != equiv[equiv[curr_label]]){ curr_label = equiv[curr_label]; equiv[curr_label] = min_label; } /* set the label itself */ if(equiv[neighbours[c]] != min_label){ equiv[neighbours[c]] = min_label; } } prev_label = neighbours[c]; } /* finally set the voxel in question to the minimum value */ set_volume_voxel_value(*vol, z, y, x, 0, 0, (Real) min_label); counts[min_label]++; break; } /* end case */ } } } update_progress_report(&progress, z + 1); } terminate_progress_report(&progress); /* reduce the equiv and counts array */ num_groups = 0; for(c = 0; c < group_idx; c++){ /* if this equivalence is not resolved yet */ if(c != equiv[c]){ /* find the min label value */ min_label = equiv[c]; while(min_label != equiv[min_label]){ min_label = equiv[min_label]; } /* update the label and its counters */ equiv[c] = min_label; counts[min_label] += counts[c]; counts[c] = 0; } else { num_groups++; } } /* Allocate space for the array of groups */ group_data = (Group_info *) malloc(num_groups * sizeof(Group_info)); num_groups = 0; for(c = 0; c < group_idx; c++){ if(counts[c] > 0){ /* allocate space for this element */ group_data[num_groups] = malloc(sizeof(group_info_struct)); group_data[num_groups]->orig_label = equiv[c]; group_data[num_groups]->count = counts[c]; num_groups++; } } /* sort the groups by the count size */ if(verbose){ fprintf(stdout, "Found %d unique groups from %d, sorting...\n", num_groups, group_idx); } qsort(group_data, num_groups, sizeof(Group_info), &compare_groups); /* set up the transpose array */ trans = (unsigned int *)malloc(sizeof(unsigned int) * group_idx); for(c = 0; c < num_groups; c++){ trans[group_data[c]->orig_label] = c + 1; /* +1 to bump past 0 */ } /* pass 2 - resolve equivalences in the output data */ if(verbose){ fprintf(stdout, "Resolving equivalences...\n"); } for(z = sizes[0]; z--;){ for(y = sizes[1]; y--;){ for(x = sizes[2]; x--;){ value = (unsigned int)get_volume_voxel_value(*vol, z, y, x, 0, 0); if(value != 0){ value = trans[equiv[value]]; set_volume_voxel_value(*vol, z, y, x, 0, 0, (Real) value); } } } } /* tidy up */ delete_volume(tmp_vol); for(c = 0; c < num_groups; c++){ free(group_data[c]); } free(group_data); free(trans); free(k1); free(k2); return (vol); }
/* from the original 2 pass Borgefors alg */ Volume *distance_kernel(Kernel * K, Volume * vol, double bg) { int x, y, z, c; double value, min; int sizes[MAX_VAR_DIMS]; progress_struct progress; Kernel *k1, *k2; /* split the Kernel */ k1 = new_kernel(K->nelems); k2 = new_kernel(K->nelems); split_kernel(K, k1, k2); setup_pad_values(k1); setup_pad_values(k2); if(verbose){ fprintf(stdout, "Distance kernel - background %g\n", bg); fprintf(stdout, "forward direction kernel:\n"); print_kernel(k1); fprintf(stdout, "\nreverse direction kernel:\n"); print_kernel(k2); } get_volume_sizes(*vol, sizes); initialize_progress_report(&progress, FALSE, sizes[2] * 2, "Distance"); /* forward raster direction */ for(z = -K->pre_pad[2]; z < sizes[0] - K->post_pad[2]; z++){ for(y = -K->pre_pad[1]; y < sizes[1] - K->post_pad[1]; y++){ for(x = -K->pre_pad[0]; x < sizes[2] - K->post_pad[0]; x++){ if(get_volume_real_value(*vol, z, y, x, 0, 0) != bg){ /* find the minimum */ min = DBL_MAX; for(c = 0; c < k1->nelems; c++){ value = get_volume_real_value(*vol, z + k1->K[c][2], y + k1->K[c][1], x + k1->K[c][0], 0 + k1->K[c][3], 0 + k1->K[c][4]) + 1; if(value < min){ min = value; } } set_volume_real_value(*vol, z, y, x, 0, 0, min); } } } update_progress_report(&progress, z + 1); } /* reverse raster direction */ for(z = sizes[0] - k2->post_pad[2] - 1; z >= -k2->pre_pad[2]; z--){ for(y = sizes[1] - k2->post_pad[1] - 1; y >= -k2->pre_pad[1]; y--){ for(x = sizes[2] - k2->post_pad[0] - 1; x >= -k2->pre_pad[0]; x--){ min = get_volume_real_value(*vol, z, y, x, 0, 0); if(min != bg){ /* find the minimum distance to bg in the neighbouring vectors */ for(c = 0; c < k2->nelems; c++){ value = get_volume_real_value(*vol, z + k2->K[c][2], y + k2->K[c][1], x + k2->K[c][0], 0 + k2->K[c][3], 0 + k2->K[c][4]) + 1; if(value < min){ min = value; } } set_volume_real_value(*vol, z, y, x, 0, 0, min); } } } update_progress_report(&progress, sizes[2] + z + 1); } free(k1); free(k2); terminate_progress_report(&progress); return (vol); }