#include #include #include #include #include #include "meanshift_utils.h" #include "meanshift_kernels.h" #define OUTPUT_PREFIX "../output/output_" #define BLOCK_SIZE 16 void get_args(int argc, char **argv){ if (argc != 6) { printf("Usage: %s h N D Pd Pl\nwhere:\n", argv[0]); printf("\th is the variance\n"); printf("\tN is the the number of points\n"); printf("\tD is the number of dimensions of each point\n"); printf("\tPd is the path of the dataset file\n"); printf("\tPl is the path of the labels file\n"); exit(1); } DEVIATION = atoi(argv[1]); NUMBER_OF_POINTS = atoi(argv[2]); DIMENSIONS = atoi(argv[3]); POINTS_FILENAME = argv[4]; LABELS_FILENAME = argv[5]; } void init(double ***vectors, char **labels, parameters *params){ int bytes_read = 0; // initializes vectors FILE *points_file; points_file = fopen(POINTS_FILENAME, "rb"); if (points_file != NULL){ // allocates memory for the array (*vectors) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); // reads vectors dataset from file for (int i=0; i // variables of type uint8 are stored as 1-byte (8-bit) unsigned integers // gets number of labels fseek(labels_file, 0L, SEEK_END); long int pos = ftell(labels_file); rewind(labels_file); int label_elements = pos/ sizeof(char); // allocates memory for the array *labels = (char*)malloc(label_elements* sizeof(char)); fseek(labels_file, 0L, SEEK_SET); bytes_read = fread((*labels), sizeof(char), label_elements, labels_file); if ( bytes_read != label_elements ){ if(feof(points_file)){ printf("Premature end of file reached.\n"); } else{ printf("Error reading points file."); } fclose(labels_file); exit(EXIT_FAILURE); } } fclose(labels_file); // MEAN SHIFT OPTIONS params->epsilon = 0.0001; params->verbose = false; params->display = false; } int meanshift(double **original_points, double ***shifted_points, int deviation , parameters *opt){ static int iteration = 0; static double **mean_shift_vector, **kernel_matrix, *denominator; // allocates memory and copies original points on first iteration if (iteration == 0 || (*shifted_points) == NULL){ (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); // allocates memory for mean shift vector mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); // initializes elements of mean_shift_vector to inf for (int i=0;i>>(d_kernel_matrix, d_original_points , d_new_shift); size = NUMBER_OF_POINTS * DIMENSIONS * sizeof(double); cudaMemcpy(&(new_shift[0][0]), d_new_shift.elements, size, cudaMemcpyDeviceToHost); cudaFree(d_kernel_matrix.elements); cudaFree(d_original_points.elements); cudaFree(d_new_shift.elements); //============================================================================== // divides element-wise for (int i=0; i opt->epsilon) { ++iteration; meanshift(original_points, shifted_points, deviation, opt); } if (iteration == 0){ // cleans up this iteration's allocations free(mean_shift_vector[0]); free(mean_shift_vector); free(kernel_matrix[0]); free(kernel_matrix); free(denominator); } return iteration; } // TODO check why there's is a difference in the norm calculate in matlab double norm(double **matrix, int rows, int cols){ double sum=0, temp_mul=0; for (int i=0; i