I have this code that I am trying to figure out how to pass differnt value to as thread parameter in openMp threads
so for example I have an simple decrement statement like --t where t is a variable so every time thread function called it gets decremented value of t first then is received as thread parameter. this is what I tried
int main(int argc, char* argv[])
{
int t=2;
#pragma omp parallel firstprivate(t=(--t))
{
printf("%d\n",t);
}
// Ending of parallel region
}
So I have absolute no clue how this above code is expended to since I assumed firstprivate(...)
part of #pragma omp derective is just a way of spawning a thread using open mp but I need different parameters received each thread. Is it doable in openMM C
CodePudding user response:
It is not possible to do that in a firstprivate
clause. However, you can change the clause to shared
and use the atomic
construct to update the value safely. Here is an example:
int main(int argc, char* argv[])
{
int t=2;
#pragma omp parallel shared(t)
{
int local_t_value;
#pragma omp atomic capture relaxed
local_t_value = --t;
printf("%d\n", local_t_value);
}
}
Note that capture
is used in this context because the atomic value is both fetched and updated at the same time. relaxed
is a memory-ordering specifying that this value can be updated without doing any flush. For more information please read this related part of the specification.