Installing and Using Fortran To Python (F2PY) Interface Generator
This post provides you info for installing and using the Fortran to Python (f2py) interface generator on windows. f2py is part of the numpy package allows you to connect Python and Fortran languages mainly with the intention to benefit from Fortran's performance. To get f2py to work on windows x64 with a python 3.6 installation, you need to do the following:
To compile the code, open a command shell and type: f2py -c -m tri_centers tri_centers.f --fcompiler=gnu95 --compiler=mingw32. Once the compilation has completed successfully, you should find a file tri_centers.cp36-win_amd64.pyd in your compilation directory.
To test the newly generated module, I am using the following python test script:
On my laptop the f2py version took 9.837e-02sec and the eqivalent numpy implementation took 1.478e-01sec, making the f2py version a factor 1.5x faster.
- If you have not done this already, download and install python36
- I like to install python under C:\Python36\ rather than the default location.
- Also, make sure you check the boxes for adding python to your environment variables
- Install numpy by using python's pip command.
- Open a command shell and type pip install numpy which will start the installation of Python's numerical package including the f2py interface.
- Install a Fortran compiler in order to compile code to combine it with Python. There is no default compiler that is shipped with either Python or numpy and therefore needs to be downloaded and installed separately. I like to use mingw, which is a runtime environment for gcc to support binaries native to Windows 64-bit operating systems. However, you can also use other Fortran compilers as well
- Download the win x64 installer from here.
- Execute the downloaded installer. Note: when prompted, select Architecture x_86_64. Leave the rest with the default settings.
- Set environment variables to make compilation simpler. Depending on your installation you may want to add some paths to your environment Path variable so that you can compile code pretty much from everywhere without having to reference the full paths to mingw and f2py. Note: on some systems, these paths are added automatically to the Path, however, if not add the the following to your system environment Path variable:
- C:\Python36\Scripts -> This path points to my Python installation-Scripts folder which contains f2py.py and can be called from everywhere once it is in your Path variable.
- C:\Program Files\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0\mingw64\bin -> This path points to my mingw installation-bin folder which contains the compiler files. Adding this path will help f2py to find the compiler.
- The next thing you want to do is to check whether your compilers can be found by f2py.
- To check for available fortran compilers open a command shell and type f2py -c --help-fcompiler.
- f2py will list compilers found on your system. What you are looking for is a line that says Fortran compilers found: --fcompiler=gnu95 GNU Fortran 95 compiler.
- The same thing can can be done for checking available c compilers by typing f2py -c --help-compiler in the command shell. Again here you want to look for a line stating: List of available compilers:...(followed by a list of available compilers including --compiler=mingw32 MinGW32 port of GNU C Compiler for Win32 (for MSC build Python)).
To compile the code, open a command shell and type: f2py -c -m tri_centers tri_centers.f --fcompiler=gnu95 --compiler=mingw32. Once the compilation has completed successfully, you should find a file tri_centers.cp36-win_amd64.pyd in your compilation directory.
To test the newly generated module, I am using the following python test script:
On my laptop the f2py version took 9.837e-02sec and the eqivalent numpy implementation took 1.478e-01sec, making the f2py version a factor 1.5x faster.
Comments
Post a Comment