Fortran comparison Re: [SciPy-dev] scipy.weave versus simple C++.

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Sat Jan 12 13:31:27 EST 2002


Hi,

>>>>> "PP" == Pearu Peterson <pearu at cens.ioc.ee> writes:

    PP> Just from a curiousity I tried your test also using Fortran
    PP> and here are the results (now comparing only cxx and Fortran):

[snip]

    PP> So, using Fortran you get speed up approx. _two_ times
    PP> compared to cxx!!!  If one would use optimized blas and
    PP> laplack functions, the speed up could be even greater.

    PP> Below is the testing code that I used. Here is how I used it,
    PP> step by step, to illustrate that using Fortran from python is
    PP> very simple:

Sorry for the delayed response.  I was out for most of the day.  Your
example is pretty awesome!  Thanks!  f2py is really very cool!

I'll include this example also in my test script.  One point to note
is that my cxx example was pretty straight forward.  I'm pretty sure
that there might be ways to improve the speed.  I'll try and see if I
can get that done.

I think it would be very nice if there were a way to write inline
fortran code and use f2py to generate the code on the fly and then
also use weave's catalog etc. to handle this transparently.  I guess
that this should not be hard to do since f2py seems to do most of the
dirty work.  If Eric could do it for C++ I guess Fortran should be
very easy for him. :)

I know too little of f2py or weave to actually write this but FWIW
here is some pseudo code that is a start.  I looked at inline briefly
and it looks like the following does something like what it does.


def magic_generate_cf2py(in_=[], out=[], in_out=[], hide=[]):
    d = {'in': in_, 'out':out, 'in,out': in_out, 'hide':hide}
    cf2py = ""
    for key, val in d.items():
        if val:
           cf2py += 'cf2py intent(%s) :: '%key
           for i in val:
               cf2py += i + ', '
           cf2py += '\n'

    return cf2py            


def inlinef(code, arg_names=[], local_dict=None, global_dict=None
            in_=[], out=[], in_out=[], hide=[]):

    # check if already catalogued.  If so simply return catalogued
    # func. 
    function = magic_check_catalog(code)
    if function:
       return function

    # figure out the type of the args and add the declarations at the
    # top.
    declare = magic_declare_headers()

    # get local dict and global dict
    local_dict, global_dict = magic_code()

    # get arg list.
    args = ""
    for name in arg_names:
        args += name + ", "

    # get cf2py stuff
    cf2py = magic_generate_cf2py(in_, out, in_out, hide)
        
    f_code = """c Inline Fortran code.
                     subroutine func_call(%(args)s)
                     %(declare)s
                %(cf2py)s
                %(code)s"""%locals()

    # write fortran code to file and run f2py on it.
    file = open(file_name, "w")
    print >> file, header
    os.system("f2py -c %s -m %s"%(file_name, mod_name))

    # load the module and get the function.
    function = magic_load_func()

    # catalog the function for future use.
    magic_catalog(code, function)

    return function


prabhu



More information about the SciPy-Dev mailing list