c++ - Beginner in OpenMP - Problems in cicle -


i beginner in openmp , trying parallelize following function:

void calc(double *x, int *l[n], int d[n], double *z){      #pragma omp parallel     for(int i=0; i<n; i++){          double tmp = d[i]>0 ? ((double) z[i] / d[i]) : ((double) z[i] / n);          for(int j=0; j<d[i]; j++)             x[l[i][j]] += tmp;      }  } 

but n=100000 sequential time 50 seconds , 2 or more threads goes several minutes.

the l array of pointers has randomly between 1 , 30 elements (given corresponding position in d array) , elements varies between 0 , n, know have load-balance problem if had guided or dynamic scheduling (even auto) times worse.

i know problem in accesses x array because not being contiguously acceded there way fix problem , have kind of speedups in function?

thanks in advance!

assuming can afford use space it, can speed up.

the basic idea create separate array of sums each thread, when they're done add corresponding elements in separate copies, , add each element of result corresponding element in original x.

as long x small that's pretty reasonable. if x might huge, may less practical in hurry. given l apparently 30 elements, sounds x limited around 30 elements (that can used while running code, anyway) well. if that's correct, having separate copy each thread shouldn't cause major problem.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -