5

要遍历容器的元素,我通常会使用迭代器,如下所示:

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
}

现在,如果我想使用 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
}

但是,当我运行所述代码时,不会对容器进行更改。但是,如果我在容器上使用典型的索引,则并行代码可以正常工作。我想知道是否可以在 OpenMP 的上下文中使用迭代器,或者我是否需要将迭代循环转换为索引循环?

4

1 回答 1

4

STL 迭代器的并行化仅在 OpenMP 3.0 中允许。您的编译器支持哪个版本的 OpenMP?

于 2010-06-30T16:55:02.893 回答