From bfb029a4162177cd72a7e7524d09338be96450a9 Mon Sep 17 00:00:00 2001 From: anapt Date: Thu, 18 Jan 2018 20:40:34 +0200 Subject: [PATCH] serial implementation of mean shift --- serial.c | 269 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100755 serial.c diff --git a/serial.c b/serial.c new file mode 100755 index 0000000..852c954 --- /dev/null +++ b/serial.c @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include + +#define X "data/X.bin" +#define L "data/L.bin" +#define COLUMNS 2 +#define ROWS 600 + +struct parameters { + double epsilom; + bool verbose; + bool display; +}; +struct timeval startwtime, endwtime; +double seq_time; + +double **alloc_2d_double(int rows, int cols); +double **duplicate(double **a, double **b, int rows, int cols); +void meanshift(double **x, int h, struct parameters *opt); +double norm(double ** m, int rows, int cols); +void multiply(double ** matrix1, double ** matrix2, double ** output); +double calculateDistance(double *, double *); +void print_matrix(double ** array, int rows, int cols); + +int main(int argc, char **argv){ + +// if (argc<2){ +// printf("%s\n", "Specify the k"); +// return 1; +// } +// = atoi(argv[1]); // the k-parameter + + + FILE *f; + f = fopen(X, "rb"); + fseek(f, 0L, SEEK_END); + long int pos = ftell(f); + fclose(f); + int elements = pos / sizeof(double); // number of total elements (points*dimension) + int points = elements/COLUMNS; + //printf("points : %d \n", points); + f = fopen(X, "rb"); + double ** vectors; + vectors = alloc_2d_double(ROWS, COLUMNS); + for (int i=0; i + // variables of type uint8 are stored as 1-byte (8-bit) unsigned integers + fseek(f, 0L, SEEK_END); + 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 + int h = 1; + struct parameters params; + params.epsilom = 0.0001; + params.verbose = false; + params.display = false; + struct parameters *opt; + opt = ¶ms; + + // tic + gettimeofday (&startwtime, NULL); + + meanshift(vectors, h, opt); + + // 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 + +} + + +void meanshift(double **x, int h, struct parameters *opt){ + + double **y; + y = alloc_2d_double(ROWS, COLUMNS); + y = duplicate(x, y, ROWS, COLUMNS); + + // mean shift vectors + double **m; + m = alloc_2d_double(ROWS, COLUMNS); + // initialize elements of m to inf + for (int i=0;iepsilom); + // TODO ITERATION + /** iterate until convergence **/ + // printf("norm : %f \n", norm(m, ROWS, COLUMNS)); + + while (norm(m, ROWS, COLUMNS) > opt->epsilom) { +// while (iter < 1){ + iter = iter +1; + // find pairwise distance matrix (inside radius) + // TODO write in C + /** allocate memory for inside iteration arrays **/ + double ** W = alloc_2d_double(ROWS, ROWS); + double * l = malloc(ROWS * sizeof(double)); + // [I, D] = rangesearch(x,y,h); + for (int i=0; i apply to non-zero elements + for (int i=0; i