Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1 View

Conditional compilation on MPI_VERSION

I am working on a shared code in Fortran. The code is used by multiple groups, and not everyone have the latest MPI libraries and compilers.

Now I want to use a feature from MPI-3, however, I cannot rely on everyone having this yet (yes, I know it's sad). In C this would have been trivial, in 'mpi.h' there is something like '#define MPI_VERSION 3' I could have used to switch to the new MPI-3 functions in the code, like:

#if MPI_VERSION >= 3
    mpi_new_v3_function()
#else
    mpi_send()
    mpi_recv()
#endif

In the MPI module (or mpif.h) there is only a PARAMETER with the same information.

So I made a test, and the following works great in gfortran:

PROGRAM main

    USE mpi
    IMPLICIT NONE
    
    IF (mpi_version == 4) CALL mpi_nonexisting_call()
    WRITE(*,*) mpi_version 
  
END PROGRAM main

I can compile this with both -O0 and -O3, with and without the -g flag. It execute perfectly and print the current MPI version.

With Intel Fortran I get the error "undefined reference to `mpi_nonexisting_call_'" when trying to compile with -O0. With higher optimizations, I don't get this error.

I could of course give a define-directive to the compiler manually, and use the preprocessor like in C, but this require some effort from the users side, or it require me to use some build system like autoconf or cmake (currently we only have a hand-written makefile). We already use some define directives in our Fortran code, so it would not change any compilation workflow.

Do you have any ideas on how I can get the desired behaviour in this case working with the Intel Fortran compiler?

0 Kudos
4 Replies
Highlighted
1 View

Intel compiler with "-O0"

Intel compiler with "-O0" gives undefined reference error because "-O0" disable all optimizations including the removal of the false branch of the IF statement at compilation time. gfortran always do that regardless of "-O" level.

I think the call to "mpi_nonexisting_call" in the code is a compilation time check on purpose so ifort's behavior shouldn't be an issue.

Thanks,

Xiaoping Duan

Intel Customer Support

0 Kudos
Highlighted
Beginner
1 View

Quote:Xiaoping D. (Intel)

Xiaoping D. (Intel) wrote:

Intel compiler with "-O0" gives undefined reference error because "-O0" disable all optimizations including the removal of the false branch of the IF statement at compilation time. gfortran always do that regardless of "-O" level.

Yes, I understand that.

Xiaoping D. (Intel) wrote:

I think the call to "mpi_nonexisting_call" in the code is a compilation time check on purpose so ifort's behavior shouldn't be an issue.

What do you mean? My only goal is to be able to write a code where I can use "modern" MPI-3 functions if the MPI implementation support this, and fall back to "old" functions if the support for MPI-3 is not present. Like I can with C and the define-directives. How it is done is not important, but it would be nice if it were transparent to the users. Is this at all possible in Fortran?

0 Kudos
Highlighted
1 View

Quote:hakostra wrote:

hakostra wrote:

Quote:

Xiaoping D. (Intel) wrote:

 

I think the call to "mpi_nonexisting_call" in the code is a compilation time check on purpose so ifort's behavior shouldn't be an issue.

 

 

What do you mean? My only goal is to be able to write a code where I can use "modern" MPI-3 functions if the MPI implementation support this, and fall back to "old" functions if the support for MPI-3 is not present. Like I can with C and the define-directives. How it is done is not important, but it would be nice if it were transparent to the users. Is this at all possible in Fortran?

 

You can use fpp preprocessor with Intel fortran compiler: https://software.intel.com/en-us/node/581531

For your case:

PROGRAM main

    USE mpi
    IMPLICIT NONE
#if mpi_version == 4
    CALL mpi_nonexisting_call()
#endif
    WRITE(*,*) mpi_version 
  
END PROGRAM main

Compile it with fpp option as:

$ mpiifort  -fpp -O0 test.f90

$./a.out

3

 

Thanks,

Xiaoping Duan

Intel Customer Support

 

0 Kudos
Highlighted
Beginner
1 View

Oh... It's that simple. I

Oh... It's that simple. I didn't try that. Sorry for bothering you. Thanks.

0 Kudos