Im trying to parallelize my code with openmp.
I have a global vector, so i can excess it with my functions. Is there a way that i can asign a copy of the vector to every thread so they can do stuff with it? Here is some pseudocode to describe my problem:
double var = 1;
std::vector<double> vec;
void function()
{
vec.push_back(var);
return;
}
int main()
{
omp_set_num_threads(2);
#pragma omp parallel
{
#pragma omp for private(vec)
for (int i = 0; i < 4; i++)
{
function();
}
}
return 0;
}
Notes:
- i want each tread to have an own vector, to safe specific values, which later only the same thread needs to excess
- each thread calls a function (sometimes its the same) which then does some work on the vector (changing specific values)
- (in my original code there are many vectors and functions, ive just tried to break the problem down)
Ive tried #pragma omp threadprivate(), but that only works for varibles and not for vectors. Also redeclaring the vector inside the parallel region doesnt help, as my function always works with the global vector, which then leads to problems when different treads call it at the same time.
vecby value tofunction?