gpt4 book ai didi

c++ - 使用 OpenMP 通过 C++ 列表/迭代器并行化两个 for 循环

转载 作者:行者123 更新时间:2023-11-30 05:32:24 25 4
gpt4 key购买 nike

我想使用 OpenMP 并行化以下结构:

for (auto it = data.begin(); it != data.end(); ++it)
{
for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
{
// code here (it2->process)
}
}

我尝试了不同的方法,但无法使其正常工作。你能帮帮我吗?

我使用的是 gcc 5.3.0、OpenMP 4.0。到目前为止我尝试过但没有成功的解决方案是:

解决方案一:

#pragma omp parallel
#pragma omp single
{
for (auto it = data.begin(); it != data.end(); ++it)
{
#pragma omp task firstprivate(it)
for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
{
// code here (it2->process)
}
#pragma omp taskwait
}
}

解决方案 2:

#pragma omp parallel
{
for (auto it = data.begin(); it != data.end(); ++it)
{
#pragma omp single nowait
{
for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
{
// code here (it2->process)
}
}
}
}

最佳答案

我认为你不应该调用 omp taskwait

#pragma omp parallel
{
#pragma omp single
{
for (auto it = data.begin(); it != data.end(); ++it)
{
#pragma omp task firstprivate(it)
for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
{
// code here (it2->process)
}
}
}
}

关于c++ - 使用 OpenMP 通过 C++ 列表/迭代器并行化两个 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127370/

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