Call pgfortran routines with OpenMP from g++

Hello,

I’d like to call some fortran code with OpenMP parallel regions from c++ code, which will need to be compiled with g++ rather than the pgc++ compiler. I was trying to follow http://www.pgroup.com/support/interlan.htm#pgf77-f90-g%2B%2B. Things are fine without using omp, but I get segfaults if the omp part is present. Can someone please take a look? Here is the code:

t1m.for

      subroutine t1m_fort(n, a, b, c)
        integer i,n
        real a(*),b(*),c(*)
!$OMP PARALLEL DO DEFAULT(SHARED)
        do i=1,n
          c(i) = a(i)+b(i)
        end do
!$OMP END PARALLEL DO
      end

t1m.cpp

extern "C" void t1m_fort_(int *, float *, float *, float *);
extern "C" void pghpf_init(int *);
int __argc_save;
char **__argv_save;
static int zz = 0;

int main(int argc, char **argv)
{
    __argc_save = argc;
    __argv_save = argv;
    pghpf_init(&zz); 

    int n = 100;
    float *a = new float[n];
    float *b = new float[n];
    float *c = new float[n];
    for (int i = 0; i < n; i++) {
        a[i] = i;
        b[i] = 2 * i;
        c[i] = 0;
    }

    t1m_fort_(&n, a, b, c);

    delete [] a;
    delete [] b;
    delete [] c;
    return 0;
}

Compilation:

pgf90 -c -mp -Minfo=all t1m.for -o t1m_for.o
g++ t1m.cpp t1m_for.o -L/opt/pgi/linux86-64/12.3/libso -lpgmp -lnuma -lpgf90 -lpgf90_rpm1 -lpgf902 -lpgf90rtl -lpgftnrtl -lm -lpgc -lgcc -lrt -o t1m

Thanks,
Andy

Hi Andy,

Our OpenMP run time library needs an initialisation routine linked in. You have two options. Either add the “initmp.o” object to your link, or link with pgf90. Personally, I recommend linking with the Fortran compiler since it’s typically easier to add “-Mnomain” then to try and figure our what libraries and objects are needed.

Hope this helps,
Mat

For example:

% pgf90 -c -mp t1m_for.f90 -o t1m_for.o
% g++ t1m.cpp t1m_for.o -L/opt/pgi/linux86-64/12.3/libso -lpgmp -lnuma -lpgf90 -lpgf90_rpm1 -lpgf902 -lpgf90rtl -lpgftnrtl -lm -lpgc -lgcc -lrt -o t1m /opt/pgi/linux86-64/12.3/lib/initmp.o
% t1m
C=3 297



% g++ -c t1m.cpp
% pgf90 -Mnomain -mp t1m.o t1m_for.f90 -lstdc++ 
t1m_for.f90:
i% a.out
C=3 297