Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Using MKL on a mixed c++/Fortran code

Bomble__Laetitia
Beginner
779 Views

Hello,

I have a code that is a mix of c++ and fortran (c++ is calling fortran as library). The Fortran part was using netlib lapack library. I would like to  move to MKL library.

I use GNU compiler (gfortran for fortran part, g++ for cpp and linking the whole).

I compile each part with the proper  compilation  option ( -fdefault-integer-8 -m64  -I/ for fortran file and -DMKL_ILP64 -m64 -I for c++).  For the linking line I put options of fortran and cpp.

The compilation succeed all right. The problem is when I execute: the cpp code call the fortran code giving it arguments. The integer arguments have now a issue, they are not passed properly and take wrong values.

Is it because of the -fdefault-integer-8 fortran option? Does it make cpp and fortran integer incompatible?

Thanks for insights

0 Kudos
1 Solution
mecej4
Honored Contributor III
752 Views

Laetitia, as a user you can choose whether to use 8-byte integer arguments (ILP64 model) or 4-byte integer arguments (LP64), and MKL supports both choices. So do GNU Fortran and C/C++, provided you use the correct options and your code is written with an awareness of the two models (LP64/ILP64).

Using 8-byte integers when they are not really required for the current applications does have performance penalties, both in memory consumption and run times.

I routinely use MKL on Windows and Linux using the LP64 model.

View solution in original post

0 Kudos
5 Replies
mecej4
Honored Contributor III
769 Views

You did not state so explicitly, but you seem to be using a Linux system. Correct?

You are calling Fortran subprograms from C/C++, and the Fortran subprograms are, in turn, calling MKL or Lapack routines, passing 8-byte integer arguments. You notice that those 8-byte integer arguments have incorrect values when the Fortran routines are entered. Since this occurs prior to calling the MKL or Lapack routines, the issue has nothing to do with whether you use MKL or Lapack from Netlib or elsewhere.

When it appeared that things were working correctly, were you passing 8-byte integers from C++ to Fortran? Do you declare the relevant variables as 8-byte integers in the C++ code?

Do you really require 8-byte integers? Are you aware that the option -DMKL_ILP64 will have no effect on your compiled C++ code unless there are embedded preprocessor directives in your C++ source files to make the integer arguments to Lapack calls to be 8-byte integers if MKL_ILP64 is defined or has the value TRUE?

 

0 Kudos
Bomble__Laetitia
Beginner
764 Views

Hi, Thank for your answer

 

>You did not state so explicitly, but you seem to be using a Linux system. Correct?

yes, indeed

>. Since this occurs prior to calling the MKL or Lapack routines, the issue has nothing to do with whether you use MKL or Lapack from Netlib or elsewhere.

well I was not having this problem when using netlib. And with netlib I was not using 8 bytes integer, I added this option because it os needed to compile with mkl.

>Do you declare the relevant variables as 8-byte integers in the C++ code?

hum, not sure... the cpp part is huge and not mine, to be honest I don't get it well, I'll try to investigate.

>Do you really require 8-byte integers?

MKL compile with gfortran require it (https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html).

> Are you aware that the option -DMKL_ILP64 will have no effect on your compiled C++ code unless there are embedded preprocessor directives in your C++ source files to make the integer arguments to Lapack calls to be 8-byte integers if MKL_ILP64 is defined or has the value TRUE?

No I am not. However I compile cpp with mkl for a try but it should be useless right? Cause it's the fortran part that is calling MKL not the cpp part.

Thank you very much for your insights.

0 Kudos
mecej4
Honored Contributor III
753 Views

Laetitia, as a user you can choose whether to use 8-byte integer arguments (ILP64 model) or 4-byte integer arguments (LP64), and MKL supports both choices. So do GNU Fortran and C/C++, provided you use the correct options and your code is written with an awareness of the two models (LP64/ILP64).

Using 8-byte integers when they are not really required for the current applications does have performance penalties, both in memory consumption and run times.

I routinely use MKL on Windows and Linux using the LP64 model.

0 Kudos
Bomble__Laetitia
Beginner
744 Views

>Laetitia, as a user you can choose whether to have to use 8-byte integer arguments (ILP64 model) or 4-byte integer arguments (LP64), and MKL supports both choice

Indeed! I missed that point. Thank you very much.  It execute very well know.

Best

0 Kudos
RahulV_intel
Moderator
729 Views

Hi @Bomble__Laetitia,

 

As rightly pointed out by @mecej4, MKL supports both 4-byte/8-byte integer arguments.

Since your issue is resolved, I will go ahead and close this thread from my end. Further discussions on this thread will be considered community only.

 

Thanks,

Rahul

0 Kudos
Reply