Browse Source

Fix memory allocations

master
Apostolos Fanakis 7 years ago
parent
commit
79c9c5f826
  1. 12
      data/run_helper.txt
  2. 21
      mean_shift_serial/serial_declarations.c

12
data/run_helper.txt

@ -1,9 +1,9 @@
Dataset X { Dataset X {
int NUMBER_OF_POINTS = 600; int NUMBER_OF_POINTS = 600;
int DIMENSIONS = 2; int DIMENSIONS = 2;
char* POINTS_FILENAME = "data/X.bin"; char* POINTS_FILENAME = "../data/X.bin";
A good h is 1 A good deviation is 1
For Processing script: For Processing script:
float maxX = 17.124000; float maxX = 17.124000;
float minX = 3.402000; float minX = 3.402000;
@ -16,9 +16,9 @@ Dataset X {
Dataset s1 { Dataset s1 {
int NUMBER_OF_POINTS = 5000; int NUMBER_OF_POINTS = 5000;
int DIMENSIONS = 2; int DIMENSIONS = 2;
char* POINTS_FILENAME = "data/s1"; char* POINTS_FILENAME = "../data/s1";
A good h is 30000 A good deviation is 30000
For Processing script: For Processing script:
float maxX = 961951; float maxX = 961951;
float minX = 19835; float minX = 19835;
@ -31,9 +31,9 @@ Dataset s1 {
Dataset s4 { Dataset s4 {
int NUMBER_OF_POINTS = 5000; int NUMBER_OF_POINTS = 5000;
int DIMENSIONS = 2; int DIMENSIONS = 2;
char* POINTS_FILENAME = "data/s4"; char* POINTS_FILENAME = "../data/s4";
A good h is 30000-35000 A good deviation is 30000-35000
For Processing script: For Processing script:
float maxX = 932954; float maxX = 932954;
float minX = 89604; float minX = 89604;

21
mean_shift_serial/serial_declarations.c

@ -90,16 +90,14 @@ void init(double ***vectors, char **labels, parameters *params){
int meanshift(double **original_points, double ***shifted_points, int deviation int meanshift(double **original_points, double ***shifted_points, int deviation
, parameters *opt){ , parameters *opt){
static int iteration = 0; static int iteration = 0;
static double **mean_shift_vector, **kernel_matrix, *denominator;
// allocates memory and copies original points on first iteration // allocates memory and copies original points on first iteration
if (iteration == 0 || (*shifted_points) == NULL){ if (iteration == 0 || (*shifted_points) == NULL){
iteration = 0;
(*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points);
}
// allocates memory for mean shift vector // allocates memory for mean shift vector
double **mean_shift_vector;
mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
// initializes elements of mean_shift_vector to inf // initializes elements of mean_shift_vector to inf
for (int i=0;i<NUMBER_OF_POINTS;i++){ for (int i=0;i<NUMBER_OF_POINTS;i++){
@ -109,8 +107,9 @@ int meanshift(double **original_points, double ***shifted_points, int deviation
} }
// allocates memory for other arrays needed // allocates memory for other arrays needed
double **kernel_matrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS); kernel_matrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS);
double *denominator = malloc(NUMBER_OF_POINTS * sizeof(double)); denominator = malloc(NUMBER_OF_POINTS * sizeof(double));
}
// finds pairwise distance matrix (inside radius) // finds pairwise distance matrix (inside radius)
// [I, D] = rangesearch(x,y,h); // [I, D] = rangesearch(x,y,h);
@ -160,17 +159,19 @@ int meanshift(double **original_points, double ***shifted_points, int deviation
double current_norm = norm(mean_shift_vector, NUMBER_OF_POINTS, DIMENSIONS); double current_norm = norm(mean_shift_vector, NUMBER_OF_POINTS, DIMENSIONS);
printf("Iteration n. %d, error %f \n", iteration, current_norm); printf("Iteration n. %d, error %f \n", iteration, current_norm);
/** iterates until convergence **/
if (current_norm > opt->epsilon) {
++iteration;
meanshift(original_points, shifted_points, deviation, opt);
}
if (iteration == 0){
// cleans up this iteration's allocations // cleans up this iteration's allocations
free(mean_shift_vector[0]); free(mean_shift_vector[0]);
free(mean_shift_vector); free(mean_shift_vector);
free(kernel_matrix[0]); free(kernel_matrix[0]);
free(kernel_matrix); free(kernel_matrix);
free(denominator); free(denominator);
/** iterates until convergence **/
if (current_norm > opt->epsilon) {
++iteration;
return meanshift(original_points, shifted_points, deviation, opt);
} }
return iteration; return iteration;

Loading…
Cancel
Save