Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
6 Views

about OpenMP Critical ,data race

why?
code1
#include "stdafx.h"
#include "omp.h"
#define N 100000
int _tmain(int argc, _TCHAR* argv[])
{
int arx,ary;
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i {
arx=i;
ary=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i {
//#pragma omp critical(max_arx)
if(arx>max_num_x)
max_num_x=arx;
//#pragma omp critical(max_ary)
if(ary>max_num_y)
max_num_y=ary;
}

printf("max_num_x=%d max_num_y=%d\n",max_num_x,max_num_y);
return 0;
}

and
code2
#include "stdafx.h"
#include "omp.h"
#define N 100000
int _tmain(int argc, _TCHAR* argv[])
{
int arx,ary;
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i {
arx=i;
ary=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i {
#pragma omp critical(max_arx)
if(arx>max_num_x)
max_num_x=arx;
#pragma omp critical(max_ary)
if(ary>max_num_y)
max_num_y=ary;
}

printf("max_num_x=%d max_num_y=%d\n",max_num_x,max_num_y);
return 0;
}

please tell me why the results of the two codes are identical? I don't know why no add #pragma omp critical ,no data race too,in code1.
0 Kudos
1 Reply
Highlighted
Employee
6 Views

Quoting - zhangzhe65
why?
code1
#include "stdafx.h"
#include "omp.h"
#define N 100000
int _tmain(int argc, _TCHAR* argv[])
{
int arx,ary;
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i{
arx=i;
ary=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i{
//#pragma omp critical(max_arx)
if(arx>max_num_x)
max_num_x=arx;
//#pragma omp critical(max_ary)
if(ary>max_num_y)
max_num_y=ary;
}

printf("max_num_x=%d max_num_y=%dn",max_num_x,max_num_y);
return 0;
}

and
code2
#include "stdafx.h"
#include "omp.h"
#define N 100000
int _tmain(int argc, _TCHAR* argv[])
{
int arx,ary;
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i{
arx=i;
ary=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i{
#pragma omp critical(max_arx)
if(arx>max_num_x)
max_num_x=arx;
#pragma omp critical(max_ary)
if(ary>max_num_y)
max_num_y=ary;
}

printf("max_num_x=%d max_num_y=%dn",max_num_x,max_num_y);
return 0;
}

please tell me why the results of the two codes are identical? I don't know why no add #pragma omp critical ,no data race too,in code1.


Hello,

It's obvious you'vea potential data race => you've a percentile of chance to have one
it's for that it's so difficult to detect and reproduce => that's the heart of parallelism

Statistically,it depends on size (in cache line) of potential data racable,number of caches and cores, cash coherency policy (inclusive,exclusive), and time needed to read/write from cache
=> not easy to calculate (I could perhapsif you pay me :=) :=) )

But if you want to increase the chances to "SEE" a cache race, I propose:

- increase hardware configuration (more cores and caches involved)
- diminish the size of your array to few elements
- randomize your data in array

And parallel graal will appear !


Hope this help.
0 Kudos