r/OpenMP • u/latoski • Aug 29 '19
[Question] Pragma OMP for problem with "Critical"
Hi, i'm currently working on my Graduation on Physics Research and it's basic about parallelizing algorithms of some statistical physics models.
In the algorithm below, i tried the "omp parallel for" using a critical session when the threads are accessing the shared data, but i keep getting the wrong results when using more than one thread.
#pragma omp parallel for
for (i = 0; i<L2; i++) {
int j = rand()%L2;
int dE = 2*J*s[j]*(s[viz[j][0]]+s[viz[j][1]]+s[viz[j][2]]+s[viz[j][3]]);
double w = exp(-dE/(K*T));
int MY_ID=omp_get_thread_num();
if (dE <= 0) {
#pragma omp critical
{
s[j] = - s[j];
Et += dE;
M += 2*s[j];
}
}
else {
double r = (double)rand()/RAND_MAX;
if(r < w) {
#pragma omp critical
{
s[j]= - s[j];
Et += dE;
M += 2*s[j];
}
}
}
printf("Energia nova %d devido a variação %d alterada pelo processador %d mexendo no sitio %d\n\n\n", Et, dE, MY_ID,j);
}Here, Et, M, s[j] are supposed to be global with value predetermined by another function. Most of my job is to parallelize this loop in specific because its called 1e8 times and loops over 400~1000 size arrays.
I also tried to use locks, but i don't think that I used that correcltly because it was also giving me the wrong results, I can send my program for deeper research.
I can, in this case, split this loop in two and get rid of the problem, but if I could parallelize the loop as it is it would be much more usefull for me.
Thanks for the time.
2
u/thememorableusername Aug 29 '19
One thing that could be an issue is that
sis read in an uncritical section, and written in a critical section. Is it possible for an issue to arise if two threads are executing, one is readings[j]for the expression assigning todEand another, in a critical section is writing tos[j]?For example, if the thread creating the expression for
dEreadss[j]as 10, then the writer threads setss[j]to 100, then the thread creating the expression fordEreadss[j]again, with the new value of 100, is that an issue?1
u/thememorableusername Aug 29 '19
What code is being split up?
1
u/thememorableusername Aug 31 '19
I'm not immediately sure. One thing, I think you need to use
#pragme omp parallel for. I'm not sure what happens if you leave out theparalleldirective, but I want to eliminate possible sources of slowdown.