Forum Jump

Select Group :
Select Forum :
Sorted By :
Sort Order :
From The :
 
Thread Tools  Search this thread 
zhangzhe65
Total Points:
240
Status Points:
190
Green Belt
March 31, 2009 10:19 PM PDT
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<N;i++)
{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i<N;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<N;i++)
{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i<N;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 code1Slide 173 .O {color:white; font-size:149%;} a:link {color:#0860A8 !important;} a:active {color:#567EB9 !important;} a:visited {color:#AA014C !important;} <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} -->
Critical
Om Sachan (Intel)
Total Points:
7,270
Status Points:
6,770
Brown Belt
April 5, 2009 11:48 PM PDT
Rate
 
#1
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<N;i++)
{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i<N;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<N;i++)
{
arx[i]=i;
ary[i]=N-i;
}
omp_set_num_threads(10);
#pragma omp parallel for
for(i=0;i<N;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 Slide 173<!--[if !ppt]--> .O {color:white; font-size:149%;} a:link {color:#0860A8 !important;} a:active {color:#567EB9 !important;} a:visited {color:#AA014C !important;} <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} --> <!--[endif]-->
Critical

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

connorisaac
Total Points:
60
Status Points:
10
Green Belt
April 9, 2009 3:19 AM PDT
Rate
 
#2 Reply to #1

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


Nice One no doubt. Really cool coding.




Intel Software Network Forums Statistics

8470 users have contributed to 31601 threads and 100646 posts to date.
In the past 24 hours, we have 30 new thread(s) 113 new posts(s), and 159 new user(s).

In the past 3 days, the most popular thread for everyone has been gemm(A,A,A) like possible? The most posts were made to gemm(A,A,A) like possible? The post with the most views is Dear Steve, excuse me for a d

Please welcome our newest member kopernikus