I've a C code which compute the distance between two sets of nodes (three coordinate each), even though my code has been fast enough yet, I want to boost it up a bit more using parallel computing. I've already found some info about openMP and I'm trying to use it right now, but there's something a bit weird. Without omp the code cpu time is 20s, adding the two pragma lines it takes 160s! How could it happen?
I append my code down here
float computedist(float **vG1, float **vG2, int ncft, int ntri2, int jump, float *dist){
int k = 0, i, j;
float min = 0;
float max = 0;
float avg = 0;
float *d = malloc(3*sizeof(float));
float diff;
#pragma omp parallel
for(i=0;i<ncft;i+=jump){
#pragma omp parallel
for(j=0;j<ntri2;j++){
d[0] = vG1[i][0] - vG2[j][0];
d[1] = vG1[i][1] - vG2[j][1];
d[2] = vG1[i][2] - vG2[j][2];
diff = sqrt(pow(d[0],2) + pow(d[1],2) + pow(d[2],2));
if(j==0)
dist[k] = diff;
else
if(diff<dist[k])
dist[k] = diff;
}
avg += dist[k];
if(dist[k]>max)
max = dist[k];
k++;
}
printf("max distance: %f\n",max);
printf("average distance: %f\n",avg/(int)(ncft/jump));
free(d);
return max;
}
Thank you so much for any help
omp_set_num_threads(1)before the parallel loop and it takes 23s usingomp_set_num_threads(2)it gives 50s! - Nicholascomputedistonly once, or in a loop (and how many times)? How big arentri1andntri2? - Alexey Kukanov