简体   繁体   English

OpenMP,C ++和迭代器

[英]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: 现在,如果我想使用OpenMP并行化循环,我可能会尝试类似:

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? 我想知道的是,是否可以在OpenMP的上下文中使用迭代器,或者我是否需要将迭代循环转换为索引循环?

Parallelization for STL iterators is allowed only in OpenMP 3.0. 仅在OpenMP 3.0中允许STL迭代器的并行化。 Which version of OpenMP does your compiler supports? 您的编译器支持哪个版本的OpenMP?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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