about OpenMP Critical

about OpenMP Critical

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;}

Slide 173 .O {color:white; font-size:149%;} a:link {color:#0860A8 !important;} a:active {color:#567EB9 !important;} a:visited {color:#AA014C !important;}
Critical

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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 Slide 173 .O {color:white; font-size:149%;} a:link {color:#0860A8 !important;} a:active {color:#567EB9 !important;} a:visited {color:#AA014C !important;}
Critical

There is a data race in code1. You may use Intel Thread Checker tool to confirm this.

Quoting - Om Sachan (Intel)

There is a data race in code1. You may use Intel Thread Checker tool to confirm this.

Nice One no doubt. Really cool coding.

Leave a Comment

Please sign in to add a comment. Not a member? Join today