#include #include #include #include #include #include #include "meanshift_declarations.h" #define N 512 int NUMBER_OF_POINTS = 600; int DIMENSIONS = 2; char* POINTS_FILENAME = "data/X.bin"; char* LABELS_FILENAME = "data/L.bin"; struct timeval startwtime, endwtime; double seq_time; int meanshift(double **original_points, double ***shifted_points, int h , parameters *opt, int iteration); __device__ double norm(double **matrix, int rows, int cols){ double sum=0, temp_mul=0; for (int i=0; i // variables of type uint8 are stored as 1-byte (8-bit) unsigned integers fseek(f, 0L, SEEK_END); long int pos = ftell(f); rewind(f); //printf("position : %ld \n", pos); int label_elements = pos/ sizeof(char); char *labels = (char*)malloc(label_elements* sizeof(char)); fseek(f, 0L, SEEK_SET); int out = fread(labels, sizeof(char), label_elements, f); fclose(f); // MEAN SHIFT OPTIONS parameters params; params.epsilon = 0.0001; params.verbose = false; params.display = false; parameters *opt; opt = ¶ms; double **shifted_points; // tic gettimeofday (&startwtime, NULL); int iterations = meanshift(vectors, &shifted_points, h, opt, 1); // toc gettimeofday (&endwtime, NULL); seq_time = (double)((endwtime.tv_usec - startwtime.tv_usec)/1.0e6 + endwtime.tv_sec - startwtime.tv_sec); printf("%s wall clock time = %f\n","Mean Shift", seq_time); //TODO write output points to file -> plot later //save_matrix(shifted_points, iterations); } int meanshift(double **original_points, double ***shifted_points, int h , parameters *opt, int iteration){ // allocates space and copies original points on first iteration if (iteration == 1){ (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); } // mean shift vector double **mean_shift_vector; mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); // initialize elements of mean_shift_vector to inf for (int i=0;i opt->epsilon) { return meanshift(original_points, shifted_points, h, opt, ++iteration); } return iteration; } /** __global__ int iteration(double * kernel_matrix, double * denominator, double * new_shift, double *shifted_points, double mean_shift_vector, int NUMBER_OF_POINTS, int DIMENSIONS, int h){ int i = threadIdx.x + blockIdx.x * blockDim.x; for (i = 0; i < NUMBER_OF_POINTS; i++) { double sum = 0; for (int j = 0; j < NUMBER_OF_POINTS; j++) { double dist_sum = 0; for (int p = 0; p < DIMENSIONS; p++) { double dif = ((*shifted_points)[i])[p] - (original_points[j])[p]; dist_sum += dif * dif; } double dist = sqrt(dist_sum); if (dist < h * h) { kernel_matrix[i][j] = dist * dist; // compute kernel matrix double pow = ((-1) * (kernel_matrix[i][j])) / (2 * (h * h)); kernel_matrix[i][j] = exp(pow); } else { kernel_matrix[i][j] = 0; } if (i == j) { kernel_matrix[i][j] += 1; } sum = sum + kernel_matrix[i][j]; } denominator[i] = sum; // build nominator for (int j = 0; j < DIMENSIONS; j++) { new_shift[i][j] = 0; for (int k = 0; k < NUMBER_OF_POINTS; k++) { new_shift[i][j] += kernel_matrix[i][k] * original_points[k][j]; } // divide element-wise new_shift[i][j] = new_shift[i][j] / denominator[i]; // calculate mean-shift vector at the same time mean_shift_vector[i][j] = new_shift[i][j] - (*shifted_points)[i][j]; } } // frees previously shifted points, they're now garbage free((*shifted_points)[0]); // updates shifted points pointer to the new array address shifted_points = &new_shift; } */