我有一个递归函数,它调用自己两次。我尝试并行化该函数最终会奏效,但在此期间会进行大量冗余计算,从而消除了并行性的所有收益。
主程序试图计算一个辅助图,它是计算图的所有 k 边连通分量所需的中间数据结构。
几个月来我一直在努力解决这个问题,我只是决定在这里寻求帮助作为最后的手段。我将不胜感激任何指出我正确方向的意见或建议;我不一定要在盘子上寻找解决方案。
我尝试使用#pragma omp single nowait,但这只会导致代码的顺序执行。
我曾尝试使用 cilk_spawn 另一次,但这只会导致我的计算机内存不足。我想产生了太多的进程。
我将问题的精神提取到我粘贴在下面的最小工作示例中。
下面发布的代码将每个计算重复大约八次。我猜八个不同的进程运行程序的单独副本,而不是同时处理部分问题。
#include <iostream>
#include <omp.h>
#include <numeric>
#include <vector>
#include <random>
#include <algorithm>
using namespace std;
int foo(std::vector<int> V, int s){
int n = V.size();
if (n>1){
std::cout<<n<<" ";
std::random_device rd; // obtain a random number from hardware
std::mt19937 eng(rd()); // seed the generator
std::uniform_int_distribution<int> distr(0, n-1); // define the range
int t = 1;
auto first = V.begin();
auto mid = V.begin() + (t);
auto mid_1 = V.begin() + (t);
std::vector<int> S(first, mid);
std::vector<int> T(mid_1, V.end());
#pragma omp parallel
{
#pragma omp task
foo(S, s);
#pragma omp task
foo(T, t);
}
}
return 0;
}
int main(){
std::vector<int> N(100);
iota(N.begin(), N.end(), 0);
int p = foo(N,0);
return (0);
}
我的目标是让所有进程/线程一起工作以完成递归。