LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2009, 10:57 AM   #1
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
OpenMP & C/C++: Parallel loop w/shared total result


Hello!
I am trying out OpenMP.
I think I understood that a simple loop like...
Code:
int i;
for (i = 0 ; i < 3 ; i++)
{
cout << "Hello " << i << endl;
}
...can be parallelized this way:
Code:
int i;
#pragma omp parallel for
for (i = 0 ; i < 3 ; i++)
{
cout << "Hello " << i << endl;
}
That's fine.

But what if I have e.g. this kind of loop:
Code:
int i;
int iTotal = 0;
for (i = 0 ; i < 3 ; i++)
{
iTotal += i;
}
?

Is it correct if I parallelize it this way...
Code:
int i;
int iTotal = 0;
#pragma omp parallel for reduction(+:iTotal)
for (i = 0 ; i < 3 ; i++)
{
iTotal += i;
}
...so that "iTotal" does not get overwritten by the results of other threads, but each thread computes its own subresult and at the end all subresults are added to get the real result of "iTotal"?
Thanks a lot
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Lost on parallel programming... (openMPI, openMP..) sylvaticus Programming 2 07-21-2008 02:49 PM
Grep result in a loop and linefeed issue romainp Programming 2 06-13-2008 10:11 AM
Unexpected result in while do done loop reading file steven.c.banks Linux - General 2 05-01-2008 12:32 PM
Testing parallel port loop device freeindy Linux - Hardware 2 01-21-2008 01:49 AM
openMP? distributed shared memory? Hano Linux - General 0 05-11-2002 11:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration