I have few global variables which are being used by various functions in the C program. I am using OpenMP threads in parallel. Each thread will call these functions assigning different values to these global variables. Is there any other alternative to threadprivate? It's not clear to me how to use copyin clause.
The sample code is like this:
int main (void) {
int low=5,high=0;
----
func1(int *k) { do something with low,high and set value for k}
func2(int *k) { do something with low,high and set value for k}
func3(int *k) { do something with low,high and set value for k}
----
int i;
int *arr= malloc(CONSTANT1*sizeof(int));
#pragma omp parallel num_threads(numberOfThreads) threadprivate(low,high) private(i) shared(arr)
{
#pragma omp for
for(i=0;i<CONSTANT1;i++) {
low=low+CONSTANT2*i;
high=low+CONSTANT2;
func1(&arr[i]);
func2(&arr[i]);
func3(&arr[i]);
----
}
}
}
Or shall I use private(low,high) and pass them again and again to each function?
Please advise.
lowandhigharrays of sizenumberOfThreads. Outside of your parallel section, every element of the arrays should be set to what your current single values are. Inside the loop, you can make them shared and each thread will only access its single element of thelowandhighshared array. In general though, I'd suggest what @JensGustedt has suggested.lowandhighto use outside of the parallel region or in another parallel region later which you don't show. But then you say that you only need them in your parallel region in the comments. Why don't you show the scalar code of what you are trying to achieve?