[SciPy-dev] Re: Two fortran/f2py questions
Pearu Peterson
pearu at scipy.org
Mon Dec 13 16:04:57 EST 2004
On Mon, 13 Dec 2004, Tom Loredo wrote:
>> I am not sure if I follow you correctly here but when using f2py from
>> its CVS, all fortran objects have _cpointer attribute that is
>> a C pointer to the actual Fortran or C function.
>
> Well, I'm not sure I've followed you, either. 8-) Here's what
> I'm trying to do. I have fortran functions and subroutines that
> need to evaluate special functions as part of what they're doing.
> E.g., for gamma functions, right now I just define a function
> "gammaln" that returns the log of a gamma function, within the
> fortran source of my module. But Scipy already has such a
> function somewhere (in the Cephes library). Is there a way
> I can use that Cephes function? For example, could I just
> figure out the appropriate Cephes call (sorting out the
> appropriate underscores and pointers) and write a setup.py
> that will let my fortran routine use the Cephes library that
> Scipy has installed somewhere? For performance, I'd rather
> not have to send my fortran module a Python callback; after
> all, the raw Cephes routine is sitting *somewhere*.
Thanks for the explaination. Now I get the problem and using _cpointer is
not a solution indeed.
A working solution could be figured out from Lib/special/setup_special.py
that compiles cephes library sources. Assuming that you have scipy source
tree somewhere then in setup.py file use
scipy_src_path = '/path/to/scipy/Lib'
cephes = glob(os.path.join(scipy_src_path,'special','cephes','*.c'))
ext_args = {}
dict_append(ext_args,
name = 'extname',
sources = [...]
libraries = ['cephes'])
ext = Extension(**ext_args)
setup(ext_modules = [ext],
libraries = [('cephes',cephes)])
See Lib/special/setup_special.py for details and various
aspects making the above example more portable.
A better solution (using get_info like below for lapack) has to wait until
we have moved various 3rd party libraries in scipy to Lib/lib where
get_info could pick up cephes library, for instance, and return appropiate
dictionary of libraries and paths to be used when linking your extension
module.
> There are other examples; another module I have needs a
> Cholesky decomposition in the middle of a calculation. Right
> now it's just hard-coded in the C for the module. I should
> just call the appropriate lapack/atlas routine for this,
> and I'd love to know how to write the C code and accompanying
> setup.py file to be able to do this portably without the
> overhead of a Python callback.
Ok, that problem is easier to solve if you have lapack/atlas libraries
installed in you system. In your setup.py file use
from scipy_distutils.system_info import get_info, dict_append
lapack_opt = get_info('lapack_opt',notfound_action=2)
ext_args = {}
dict_append(ext_args,
name = 'extname',
sources = [...])
dict_append(ext_args,**lapack_opt)
ext = Extension(**ext_args)
to define your Extension module that will be linked against
optimized lapack libraries. See Lib/lib/{lapack,blas}/setup_*.py
files for more examples.
Pearu
More information about the SciPy-Dev
mailing list