Calling multithreaded (openmp) C ++ - routines from Fortran program

I have a C ++ routine standalone_c.cpp and a wrapper for that in fortran standalone_f.f90 that wraps standalone_c.cpp. standalone_c.cpp is multithreaded using the openmp pragma. I can compile both standalone_c.cpp and wrapper standalone_f.f90. However, when I try to link the two, I get errors such as an undefined link to omp_get_thread_num, an undefined link to omp_get_num_procs. Does anyone have experience calling multithreaded c or C ++ code from a fortran procedure? Can anyone guess why this is happening?

I can post some pseudo code if that is enough.

Edit: Compilation of commands:

gcc-4.3.3/bin/g++ -O -openmp $(IFLAGS) -c standalone_c.cpp 
fce/10.1.015/bin/ifort -g -O0 standalone_f.f90
fce/10.1.015/bin/ifort $(LFLAGS) standalone_c.o standalone_f.o -o standalone

IFLAGS for some libraries that I need, LFLAGS are the linker flags for these libraries.

+3
source share
2 answers

The -openmp flag in ifort does more than just enable the OpenMP directive. He also links to the appropriate libraries. Assuming you have already dealt with underscores in the naming routine, then if you add -openmp to the ifort link step, which takes care of the OpenMP libraries, and adding -lstdC ++ will handle C ++ links (e.g. __gxx_personality_v0).

Or you can use the options provided by ifort. A simple example:

$> cat a.f90
program a
  print *, "calling C++ program"
  call b()
end program a

$> cat b.cpp
#include <omp.h>
#include <stdio.h> 

extern "C" {
void b_(void); }

void b_(void) {
  int i;

  #pragma omp parallel for
  for (i = 0; i < 10; i++)
    printf("t#: %i  i: %i\n", omp_get_thread_num(), i);

}

$> g++ -fopenmp -c -o b.o b.cpp
$> ifort -g -O0 -c -o a.o a.f90
$> ifort -openmp -cxxlib -openmp-lib compat b.o a.o
$> export OMP_NUM_THREADS=4
$> a.out
 calling C++ program
t#: 2  i: 6
t#: 2  i: 7
t#: 2  i: 8
t#: 3  i: 9
t#: 0  i: 0
t#: 0  i: 1
t#: 0  i: 2
t#: 1  i: 3
t#: 1  i: 4
t#: 1  i: 5

You must tell ifort to use OpenMP (-openmp), be compatible with the GNU OpenMP runtime library libgomp (-openmp-lib compat), and link using the C ++ runtime libraries provided by g ++ (- cxxlib).

+3
source

GNU OpenMP - -fopenmp, -openmp, .

-, -fopenmp GNU OpenMP (libgomp). ifort, gfortran, .

, , -fopenmp . , , , GNU Fortran (gfortran) ifort. -fopenmp gfortran, Fortran OpenMP.

0

Source: https://habr.com/ru/post/1791554/


All Articles