5 ms·
Anyone have experience with an f2py (fortran to python translator)? How does it compare to translating to native C?
by niggler 14y ago
Anyone have experience with an f2py (fortran to python translator)? How does it compare to translating to native C?
- paddy_m 14y agoWhat is your use case? Have you looked at Numba? There are guarantees about memory that can be made with Fortran that can't be expressed with C. Numba is trying to bring the same type of optimizations to NumPy python code, because it does know some of the memory constraints. Note: I work for Continuum Analytics.
- niggler 14y agoI've got some numerical analysis code in f77 that leverages lapack. I tried translating to C but was never happy with using clapack or atlas.
- tocomment 14y agoSo I watched the numba talk at pycon. What I still don't understand is does it speed up any python code or only code that uses numpy? How does it know if you're using numpy?
- hogu 14y agoit's a numpy-aware compiler - you either tell the jit decorator the types of the arguments the function will be called with, and that information is used in the compilation. It doesn't have to be NumPy arrays, but the type declaration mechanism does know about them and can optimize around that. Similar to how providing type information can allow cython to provide efficient C code, providing type information on the decorator allows Numba to generate efficient llvm byte code. There is also an autojit decorator that watches what you're calling the function with, and compiles it for the given type signature.
- synparb 14y agoAnother option is the approach detailed here: http://fortran90.org/src/best-practices.html#interfacing-with-python http://fortran90.org/src/best-practices.html#interfacing-wit...
- gtani 14y agoYou could look at how pyMC calls out to Fortran (windows and POSIX FFI, source translation is sketchy without thorough unit test suites http://pymc-devs.github.com/pymc/INSTALL.html http://pymc-devs.github.com/pymc/INSTALL.html
- jotaass 14y agof2py is a remarkable tool. And it's much easier than the documentation would lead you to believe. It's literally as simple as doing f2py foo.f -c -m foo Which creates a foo python module with easy access to whatever functions you put in foo.f, with proper type checking and conversion from python objects to fortran arrays. It is also clever enough to remove some unecessary inputs which are common in old fortran. For example, in fortran you usually define an extra input argument to be the length of an input array. f2py determines that this variable is uncessary as it can be determined from the length of the python object and makes it optional. If you write fortran90-95 it's even better, as you can mark a variable as output and f2py puts it on the left side automatically. I wish they would update it to support pointers and custom types, though. That's about the only thing it lacks right now. It's the best and quickest way I know of linking python and native code. Scipy's weave is also nice.