I have a problem with a function for splitting a matrix into to matrix depending if it's bigger or smaller than a median_point. The functions work fine without openMP, but with it, I can get segmentation error after a while. The function is used in a three-ball algorithm, so it's being called multiple times. And after a couple of iterations, it can break down, because it gets wrong values in the pts matrix for some reason. Any Idea why?
/******************************************************************************
*
* create_L_and_R():
*
* - create two sets of points, L and R. The point smaller than the median
* point goes to the left (L) and bigger (and including the point) goes to
* the right (R).
*
* - input:
* -n_dims: dimension of the points
* -n_points: number of points in the pts
* - median_point
* -pts: pts of the line
* - Two empty arrays:
* L_split: empty array which will be filled with points
* R_split: empty array which will be filled with points, pluss the center point
*****************************************************************************/
void create_L_and_R(int n_dims, int n_ponits, double** pts, double** L_split, double** R_split, double *median_point, int *counter_L, int *counter_R){
int n = 0;
printf("\nMedian: %f\n", median_point[0]);
#pragma omp parallel for private(n) \
shared(L_split, R_split, pts, counter_L, counter_R, median_point)
for (n=0; n<n_ponits; n++){
printf("pts[n][dim]: %f\n", pts[n][0]);
if (pts[n][0] < median_point[0]){
L_split[*counter_L] =pts[n];
printf("L_split: %f\n", L_split[*counter_L][0]);
*counter_L = *counter_L + 1;
}
else{
R_split[*counter_R] = pts[n];
printf("R_split: %f\n", R_split[*counter_R][0]);
*counter_R = *counter_R + 1;
}
}
printf("\n");
return;
}
Output when running with 50 points: edian: 0.086492 pts[n][dim]: 0.697553 R_split: 0.697553 pts[n][dim]: 0.000000 L_split: 0.000000 pts[n][dim]: 1.372316 R_split: 1.372316 pts[n][dim]: 1.416026 R_split: 1.416026 pts[n][dim]: 0.000000 L_split: 0.000000 pts[n][dim]: 0.641713 R_split: 0.641713 pts[n][dim]: 1.297904 R_split: 1.297904
Median: -nan pts[n][dim]: 0.000000 R_split: 0.000000 pts[n][dim]: 0.000000 R_split: 0.000000 Thread 1 "ballAlg-omp.o" received signal SIGSEGV, Segmentation fault.