From e16dc2ac725619ba854386ccfe4f4c404b8ff060 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Tue, 23 Jan 2018 22:31:58 +0200 Subject: [PATCH] Add separate implementation folders, minimize serial's main --- mean_shift_cuda/Makefile | 36 ++++++ meanshift.c => mean_shift_cuda/meanshift.c | 4 +- .../meanshift_declarations.c | 0 .../meanshift_declarations.h | 0 Makefile => mean_shift_serial/Makefile | 2 +- mean_shift_serial/serial.c | 39 ++++++ .../serial_declarations.c | 114 ++++++++++++++---- .../serial_declarations.h | 11 +- serial.c | 72 ----------- 9 files changed, 177 insertions(+), 101 deletions(-) create mode 100644 mean_shift_cuda/Makefile rename meanshift.c => mean_shift_cuda/meanshift.c (99%) rename meanshift_declarations.c => mean_shift_cuda/meanshift_declarations.c (100%) rename meanshift_declarations.h => mean_shift_cuda/meanshift_declarations.h (100%) rename Makefile => mean_shift_serial/Makefile (94%) mode change 100755 => 100644 create mode 100644 mean_shift_serial/serial.c rename serial_declarations.c => mean_shift_serial/serial_declarations.c (58%) rename serial_declarations.h => mean_shift_serial/serial_declarations.h (84%) delete mode 100755 serial.c diff --git a/mean_shift_cuda/Makefile b/mean_shift_cuda/Makefile new file mode 100644 index 0000000..d7c9a56 --- /dev/null +++ b/mean_shift_cuda/Makefile @@ -0,0 +1,36 @@ +SHELL := /bin/bash + +# ============================================ +# COMMANDS + +CC = nvcc +RM = rm -f +CFLAGS=-lm -O3 -Wall -I. +OBJ=meanshift.o meanshift_declarations.o +DEPS=meanshift_declarations.h + +# ========================================== +# TARGETS + +EXECUTABLES = meanshift + +.PHONY: all clean + +all: $(EXECUTABLES) + +# ========================================== +# DEPENDENCIES (HEADERS) + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +.PRECIOUS: $(EXECUTABLES) $(OBJ) + +# ========================================== +# EXECUTABLE (MAIN) + +$(EXECUTABLES): $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) + +clean: + $(RM) *.o *~ $(EXECUTABLES) diff --git a/meanshift.c b/mean_shift_cuda/meanshift.c similarity index 99% rename from meanshift.c rename to mean_shift_cuda/meanshift.c index 6c168b3..2826a03 100644 --- a/meanshift.c +++ b/mean_shift_cuda/meanshift.c @@ -4,7 +4,9 @@ #include #include -#include "serial_declarations.h" +#include + +#include "meanshift_declarations.h" #define N 512 int NUMBER_OF_POINTS = 600; diff --git a/meanshift_declarations.c b/mean_shift_cuda/meanshift_declarations.c similarity index 100% rename from meanshift_declarations.c rename to mean_shift_cuda/meanshift_declarations.c diff --git a/meanshift_declarations.h b/mean_shift_cuda/meanshift_declarations.h similarity index 100% rename from meanshift_declarations.h rename to mean_shift_cuda/meanshift_declarations.h diff --git a/Makefile b/mean_shift_serial/Makefile old mode 100755 new mode 100644 similarity index 94% rename from Makefile rename to mean_shift_serial/Makefile index 90aa13f..2be8678 --- a/Makefile +++ b/mean_shift_serial/Makefile @@ -12,7 +12,7 @@ DEPS=serial_declarations.h # ========================================== # TARGETS -EXECUTABLES = serial mean-shift +EXECUTABLES = serial .PHONY: all clean diff --git a/mean_shift_serial/serial.c b/mean_shift_serial/serial.c new file mode 100644 index 0000000..7447c85 --- /dev/null +++ b/mean_shift_serial/serial.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "serial_declarations.h" + +int DEVIATION = 1; +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 main(int argc, char **argv){ + double **vectors, **shifted_points; + char *labels; + parameters params; + + //get_args(argc, argv); commented out while in development + init(&vectors, &labels, ¶ms); + + //save_matrix(vectors, 0); + + // tic + gettimeofday (&startwtime, NULL); + + int iterations = meanshift(vectors, &shifted_points, DEVIATION, ¶ms); + printf("Total iterations = %d\n", iterations); + + // 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); +} \ No newline at end of file diff --git a/serial_declarations.c b/mean_shift_serial/serial_declarations.c similarity index 58% rename from serial_declarations.c rename to mean_shift_serial/serial_declarations.c index 9665c9f..5e68d60 100644 --- a/serial_declarations.c +++ b/mean_shift_serial/serial_declarations.c @@ -6,7 +6,9 @@ #include "serial_declarations.h" -void get_args(int argc, char **argv, int *h){ +#define OUTPUT_PREFIX "../output/output_" + +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"); @@ -17,47 +19,111 @@ void get_args(int argc, char **argv, int *h){ exit(1); } - *h = atoi(argv[1]); + DEVIATION = atoi(argv[1]); NUMBER_OF_POINTS = atoi(argv[2]); DIMENSIONS = atoi(argv[3]); POINTS_FILENAME = argv[4]; LABELS_FILENAME = argv[5]; } -int meanshift(double **original_points, double ***shifted_points, int h - , parameters *opt, int iteration){ +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; - // allocates space and copies original points on first iteration - if (iteration == 1){ + // allocates memory and copies original points on first iteration + if (iteration == 0 || (*shifted_points) == NULL){ + iteration = 0; (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); } - // mean shift vector + // allocates memory for 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 + // initializes elements of mean_shift_vector to inf for (int i=0;i opt->epsilon) { - return meanshift(original_points, shifted_points, h, opt, ++iteration); + ++iteration; + return meanshift(original_points, shifted_points, deviation, opt); } return iteration; @@ -171,8 +239,8 @@ void print_matrix(double **array, int rows, int cols){ } void save_matrix(double **matrix, int iteration){ - char filename[18]; - snprintf(filename, sizeof(filename), "%s%d", "output/output_", iteration); + char filename[50]; + snprintf(filename, sizeof(filename), "%s%d", "../output/output_", iteration); FILE *file; file = fopen(filename, "w"); for (int rows=0; rows +extern int DEVIATION; extern int NUMBER_OF_POINTS; extern int DIMENSIONS; extern char* POINTS_FILENAME; @@ -15,14 +16,16 @@ typedef struct parameters { } parameters; //Function get_args parses command line arguments. -void get_args(int argc, char **argv, int *h); +void get_args(int argc, char **argv); + +//Function init reads the dataset and label arrays from the corresponding files. +void init(double ***vectors, char **labels, parameters *params); //Function meanshift recursively shifts original points according to th //mean-shift algorithm saving the result to shiftedPoints. Struct opt has user -//options, h is the desirable deviation, iteration is this call's iteration -//number. +//options, h is the desirable deviation. int meanshift(double **original_points, double ***shifted_points, int h - , parameters *opt, int iteration); + , parameters *opt); //Function norm returns the second norm of matrix of dimensions rowsXcols. double norm(double **matrix, int rows, int cols); diff --git a/serial.c b/serial.c deleted file mode 100755 index 329a20f..0000000 --- a/serial.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include - -#include "serial_declarations.h" - -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 main(int argc, char **argv){ - int h = 1; - - //get_args(argc, argv, &h); commented out while in development - - 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/DIMENSIONS; -// //printf("points : %d \n", points); - f = fopen(POINTS_FILENAME, "rb"); - double **vectors; - vectors = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); - 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); -} \ No newline at end of file