简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM