Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling Intel Fortran from Python

ferrad1
New Contributor I
1,508 Views

I am a newbie at this.  I started with the example in https://www.numfys.net/howto/F2PY/

I have just installed Anaconda and already have Intel Fortran.  I created the file primes.f95, and issued the command:   

python -m numpy.f2py -c primes.f95 -m primes

out of which followed 100 lines of screen dump.  Two items I noted here was that it doesn't know which Fortran complier I want to use - it went through my path and found all sorts of old Fortran compilers I have used for ages (Compaq, Gnu, etc).  So y first question is how to I tell python (or f2py?) that I want to use Intel Visual Fortran?

The second issue I see is that it doesn't seem to understand spaces in the ifort.exe path:

Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.2.187\windows\bin\intel64\ifort.exe
Could not locate executable C:\Program

ifort: command line warning #10161: unrecognized source type 'Files'; object file assumed
ifort: command line warning #10161: unrecognized source type '(x86)\IntelSWTools\compilers_and_libraries_2017.2.187\windows\bin\intel64\ifort.exe'; object file assumed
ifort: command line warning #10006: ignoring unknown option '/c '

Perhaps the latter problem will go away if I can give a proper path to answer the first problem.

 

Maybe there are better examples or starring points out there? Any help greatly appreciated.

 

0 Kudos
6 Replies
ferrad1
New Contributor I
1,501 Views

Adding this fixes the first problem:

--fcompiler=intelvem

I still have the "space" issue.

0 Kudos
avinashs
New Contributor I
1,460 Views

Although some of the above problems can be resolved, my experience with one API for this application is that you finally end up with "internal compiler error" or "catastrophic error", which according to advice from the experts on this forum is "always a compiler bug". It may work for a simple example like fprimes.f90 but fails for real projects. However, the same Fortran files compile and run successfully with ifort in MSVS or with the command line in Windows 10 as a Fortran project. So the error seems to be within numpy.f2py compiler options. Looking at other forums and posts, this appears to be an unsolved problem for f2py in Windows OS that either a contributor to the Python project or at Intel needs to solve. Further, linking of libraries and .dlls in Windows 10 also fails, which makes it ultimately incovenient to use.

Note that I am specifically referring to the numpy.f2py method of building the file and not the use of Python and Fortran within MSVS as a mixed language programming project. People seem to have had success with the latter.

I would appreciate you posting any further results on this thread.

0 Kudos
FortranFan
Honored Contributor II
1,477 Views

@ferrad1 ,

 

Please see the following for an unrelated example and thus inapplicable for your space issue specifically but perhaps useful for some tidbits with other aspects you may be pursuing with Fortran?

https://community.intel.com/t5/Intel-Fortran-Compiler/Passing-by-reference-a-Python-string-to-Fortran-function/m-p/1297543/highlight/true#M156738

0 Kudos
JohnNichols
Valued Contributor III
1,437 Views

Do a google search this morning found a lot of examples by Uni Professors even some good samples.  I suggest you seek a Uni professor who is doing such a call as they have a lot of spare time to get things working and grad students.  

0 Kudos
ferrad1
New Contributor I
1,414 Views

I realized last night that my application is different from the primes application I am using as a starter template.  This combines the Python and Fortran into one entity, however what I need is a separate Python entity which calls (functions within) my Fortran executable.  There can be a number of different executables, with the Python wrapper passing information between them.

I found this online: https://dektoud.github.io/blog/post/fortran_dlls/ which shows how to call a Fortran DLL from Python.  This is moving in the right direction, however I need to access these DLLs from Python while the Fortran EXE is running.  Any ideas?

0 Kudos
AndrewC
New Contributor III
1,371 Views

If you want to call a FORTRAN routine from Python

  • Make your FORTRAN routine   follow "C" calling convention using [C,ALIAS:'MyFunction']
  • Build your FORTRAN code into a DLL with matching .LIB ( if windows)
  • Follow the many examples for how to call a C function from Python

If you have a "running" FORTRAN program you can't call your FORTRAN functions in the running program's address space from an external Python process ( without a fairly complex RPC solution where your FORTRAN .exe is a RPC server called from a Python client).

When you start python and call a function in your DLL , then Python will load the DLL into the Python address space, completely separate from any running FORTRAN .EXE using the same DLL. Or maybe I misunderstood.

0 Kudos
Reply