Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP, C++ and Iterators

To loop over elements of a container, I would typically use an iterator, like so:

container<type> myContainer;
// fill up the container
container<type>::iterator it;
for(it=myContainer.begin(); it!=myContainer.end(); ++it) {
   //do stuff to the elements of the container
}

Now, if I want to parallelize the loop using OpenMP, I might try something like:

container<type> myContainer;
// fill up the container
container<type>::iterator it, it_begin=myContainer.begin(), it_end=myContainer.end();
#pragma omp parallel for default(none) private(it) shared(it_begin, it_end)
for(it=it_begin; it!=it_end; ++it) {
   //do stuff to the elements of the container
}

However, when I run said code, the changes are not made to the container. If, however, I use a typical indexing on the container, the parallel code works fine. What I am wondering is if it is possible to use iterators in the context of OpenMP, or if I need to convert the iterated loop to an indexed loop?

like image 813
MarkD Avatar asked Oct 14 '22 03:10

MarkD


1 Answers

Parallelization for STL iterators is allowed only in OpenMP 3.0. Which version of OpenMP does your compiler supports?

like image 88
Kirill V. Lyadvinsky Avatar answered Oct 18 '22 14:10

Kirill V. Lyadvinsky