about OpenMP Critical ,data race

about OpenMP Critical ,data race

zhangzhe65's picture

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

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[N],ary[N];
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i {
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i {
#pragma omp critical(max_arx)
if(arx[i]>max_num_x)
max_num_x=arx[i];
#pragma omp critical(max_ary)
if(ary[i]>max_num_y)
max_num_y=ary[i];
}

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.

v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} p\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v\:textbox {display:none;}

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Alain Dominguez (Intel)'s picture
Quoting - zhangzhe65 why?
code1
#include "stdafx.h"
#include "omp.h"
#define N 100000
int _tmain(int argc, _TCHAR* argv[])
{
int arx[N],ary[N];
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i{
//#pragma omp critical(max_arx)
if(arx[i]>max_num_x)
max_num_x=arx[i];
//#pragma omp critical(max_ary)
if(ary[i]>max_num_y)
max_num_y=ary[i];
}

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[N],ary[N];
int i,max_num_x=-1,max_num_y=-1;
for(i=0;i{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i{
#pragma omp critical(max_arx)
if(arx[i]>max_num_x)
max_num_x=arx[i];
#pragma omp critical(max_ary)
if(ary[i]>max_num_y)
max_num_y=ary[i];
}

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.

Login to leave a comment.