What's the matter with my routine? help me !!

General OpenMP discussion

What's the matter with my routine? help me !!

Postby hjnln » Sun May 12, 2013 11:06 pm

Hi~! I want to compute the sum of "A",but it doesn't print the right result. I don't know how to modify it! Can you give me some suggestions? how about Nested ?

double x,y,A=0;
#pragma omp parallel for num_threads(4) private(x,y) reduction(+:A)
for(m=1;m<=num;m++)
{
x=m*1.0;
for(n=1;n<=num;n++)
{
y=n*1.0;
A=A+(x+y);
}
}
cout<<"A: "<<A<<endl;
hjnln
 
Posts: 5
Joined: Sun May 12, 2013 10:11 pm

Re: What's the matter with my routine? help me !!

Postby MarkB » Mon May 13, 2013 2:04 am

Hi there,

Try adding n to the list of variables in the private clause!

Hope that helps,
Mark.
MarkB
 
Posts: 447
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: What's the matter with my routine? help me !!

Postby hjnln » Mon May 13, 2013 4:23 am

It works ! Mark,thank you very much ! It is a long road for me !
hjnln
 
Posts: 5
Joined: Sun May 12, 2013 10:11 pm

Re: What's the matter with my routine? help me !!

Postby MarkB » Mon May 13, 2013 4:38 am

You're very welcome! I always recommend using the default(none) clause to help catch bugs like that.
MarkB
 
Posts: 447
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 10 guests