I'm trying to use a task construct for my C++/OpenMP program:
#pragma omp parallel
{
typename ClusterNormal<DIM>::VectorMean ResultMeanThread;
ResultMeanThread.setZero();
#pragma omp single
for(list<unsigned int>::const_iterator it=IDLeft.cbegin(); it!=IDLeft.cend(); it++)
{
#pragma omp task
{
ResultMeanThread += Data[*it];
}
}
}
This code is computing the sum of some VectorMean
(it doesn't mind what they are, but they have operator + defined) for the elements of data indicated in IDLeft.
Every thread initialize VectorMean
with all zeros. My problem is that after the for loop, ResultMeanThread
is still composted of all zeros.
When a task is executed, the sum is computed correctly, but after the task execution,ResultMeanThread
is always re-initialised to zeros.
How could I fix it? I'm using tasks because of the lists, but my code isn't working.