Re: [f2py] OpenMP - again

Paul Anton Letnes <[email protected]> Fri, 10 Sep 2010 15:12:32 +0200
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
On 6. sep. 2010, at 19.46, Pearu Peterson wrote:

> On Sun, Sep 5, 2010 at 1:55 PM, Paul Anton Letnes
> <[email protected]> wrote:
>> Hi again!
>> 
>> On 4. sep. 2010, at 15.48, Pearu Peterson wrote:
>> 
>>> Here follows a solution when using setup.py file.
>>> So, create a setup.py file containing
>>> 
>>> #!/usr/bin/env python
>>> def configuration(parent_package='',top_path=None):
>>>    from numpy.distutils.misc_util import Configuration
>>>    config = Configuration(None,parent_package,top_path)
>>>    config.add_extension('wrapper',
>>>                         ['fortranmodule.f95','wrapper.f95'],
>>>                         libraries = ['gomp','blas'],
>>>                         library_dirs = ['/opt/local/lib/gcc44'],
>>>                         f2py_options = [],
>>>                         define_macros = [('F2PY_REPORT_ON_ARRAY_COPY','1')],
>>>                         extra_link_args = ['-fopenmp']
>>>                         )
>>> 
>>>    return config
>>> 
>>> if __name__ == "__main__":
>>>    from numpy.distutils.core import setup
>>>    setup(configuration=configuration)
>>> #eof
>>> 
>>> and then run
>>> 
>>> python setup.py build_ext --inplace --fcompiler=gnu95
>>> 
>>> HTH,
>>> Pearu
>> 
>> Thanks for your help there - I am closer to a workable solution now. There are still a few quirks, however. I attach my source files and the output (in output.txt) I get when running "make clean; make test" in the terminal.
>> 
>> Questions:
>> 
>> Why do I get this warning? fortranmodule.f95 is a pure fortran module, and can be compiled as such; hence, real(wp) should work (fortranmodule.f95 line 8 and 9).
>> Warning:
>> getctype: "real(kind=wp)" not supported (use .f2py_f2cmap).
> 
> See
> 
>  http://cens.ioc.ee/projects/f2py2e/FAQ.html#q-what-if-fortran-90-code-uses-type-spec-kind-kind
> 
> for an explanation.
> 
>> Your setup.py lets the program compile and run nicely. However, there is no speedup and only 1 OMP thread running when using your setup.py. When calling setup.py with the env variable F90FLAGS=-fopenmp set, however, I do get several threads and a close to optimal speedup - see this line in Makefile:
>> env F90FLAGS=-fopenmp python setup.py build_ext --inplace --fcompiler=gnu95
> 
> You can use the following command to enable -fopenmp for fortran compilation:
> 
> python setup.py config_fc --opt="-fopenmp -O3 -funroll-loops"
> build_ext --inplace
> 
> (I wish I had time to make specifying extra compiler/linker options
> uniform within a setup.py file as well as in f2py command line..)
> 
>> Finally, something which might be related to gfortran performance rather than f2py: I notice that on the Linux desktop which I've also been running this program, the program runs essentially only half as fast as on my laptop. CPUs are core 2 duo 2.4 GHz on my laptop and a more recent core 2 quad 2.83 GHz on the desktop. This seems rather odd to me - the  desktop should be roughly as fast "per core".
>> 
>> If you find this example interesting enough to use in some f2py documentation, please feel free to. I guess other people might be trying to use OpenMP with fortran as well, as it's a rather simple way of parallelizing fortran code.
> 
> If you have time then could you create a wiki page, say f2py_OpenMP in
> the scipy cookbook, something along the lines of the following pages:
>  http://www.scipy.org/Cookbook/F2Py
>  http://www.scipy.org/Cookbook/f2py_and_NumPy
> 
> Best regards,
> Pearu
I will see if I can find the time. I think it would be a helpful thing to have!

Paul