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