Search code examples
pythonnumpyoptimizationfortranf2py

Disabling fp-model strict when using ifort with f2py


I'm using f2py to offload performance critical sections of my python code to fortran. I have two fortran compilers, gfortran and ifort. Since the code I run will be on intel processors, ifort is usually faster. But for the most critical part of my code, I observe the opposite, with gfortran being faster by 40%! After inspecting the actual command line ifort is being called with, I noticed that f2py appends -fp-model strict to the command line no matter what I specify in the FFLAGS environment variable. I suspect that this is the cause of the ifort slowness.

Is there a way to make f2py stop enforcing this floating point model? I've tried both adding -fp-model fast to FFLAGS and f2py's --f90flags argument, but while they appear on the final command line, they do so before the strict flag, and are therefore overridden. While I appreciate support for NaNs etc., it is not worth a 40% slowdown.

Here is my f2py command line:

FFLAGS="-openmp -Ofast -fPIC" f2py --fcompiler=intelem -c -m pmat_core_32 pmat_core_32.f90 -liomp5

and here are the flags that are actually passed to ifort, according to the f2py output:

-FI -openmp -Ofast -fPIC -xhost -openmp -fp-model strict

Solution

  • The flags -xhost -openmp -fp-model strict come from

    def get_flags_opt(self):
        return ['-xhost -openmp -fp-model strict']
    

    in the file site-packages/numpy/distutils/fcompiler/intel.py for the classes that invoke ifort.

    You have two options to modify the behavior of these flags:

    • call f2py with the --noopt flag to suppress these flags
    • call f2py with the --opt='blah' to override these flags

    You can get the flags you desire with:

    FFLAGS="-fPIC" f2py --fcompiler=intelem --opt='-xhost -0fast -openmp' -c -m pmat_core_32 pmat_core_32.f90 -liomp5