I've been experimenting with openmp and some math functions in C. If I try to declare and initialize some variables outside a parallel construct, then use them within a math function inside the parallel, when I compile using gcc -fopenmp practice.c -o practice
I get the following error:
/usr/bin/ld: /tmp/ccQj4iIQ.o: in function `main._omp_fn.0':
practice.c:(.text+0xb3): undefined reference to `fmax'
collect2: error: ld returned 1 exit status
This issue happens with fmax
, fmin
, sqrt
, pow
, cos
, etc. Some sample code that illustrates this is:
#include <omp.h>
#include <math.h>
void main(void){
double m=1;
double a=12;
#pragma omp parallel
{
m = fmax(m,a);
}
}
I've found that the issue goes away if I 1) move the fmax
outside the parallel, or 2) re-initialize the variables inside the parallel, or 3) use fmax
on 1 and 12 directly instead of m and a. This issue also does NOT occur if I simply try to use printf
to print m and a inside the parallel, so I know each thread can "see" the values correctly.
Why is this happening, and is there a way to fix it other than the 3 things I've already tried? So far it seems like 2) is my best bet, but it seems silly to have to do initialization immediately inside the parallel when it would make more sense to do it beforehand.