I am trying to develop some recursive algorithm which I would like to run in parallel using open mp. I need to run the algorithm for multiple input so I want each of the Thread to run 1 input. Each Thread is independent but the results are stored in the same global variable (summing them) as the code shows:
#include <stdio.h>
#include <omp>
double * resultA;
double * resultB;
void recursiveAlgorithm(double a)
{
double b = someFunction(a);
uint c = anotherFunction(a);
if (b < 0)
{
#pragma omp critical
{
resultA[c] = resultA[c] + b;
}
return;
}
if (b > 100)
{
#pragma omp critical
{
resultB[c] = resultB[c] + b;
}
return;
}
recursiveAlgorithm(b);
}
int main( int argc, const char* argv[] )
{
double input[5] = {0, 1, 2, 3, 4};
resultA = malloc(1000*1000*3, sizeof(double));
resultB = malloc(1000*1000*3, sizeof(double));
#pragma omp parallel for
for (uint i; i < 5; i++){
recursiveAlgorithm(input[i]);
}
}
I have been using critical section to ensure that the variables resultA and resultB are not accessed simultaneously but I am not sure if it is the best for my case. The speed improvement is much less that I would expect. Is there any better approach for a code like this?