gpt4 book ai didi

c++ - OpenMP、C++ 和迭代器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:02 30 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c++ - OpenMP、C++ 和迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3151440/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com