<div dir="ltr"><div>We do this all the time. The trick is that you can't use f2py do actually do *any* of the compiling/linking for you. You just have to use it to get the <modulename>module.c file and .f90 file. Then compile that yourself and use whatever compile flags, linking etc you would normally use to get a .so. We've had issues with f2py using gcc to do the final link, instead of the fortran compiler. The following approach we've used on a half a dozen different wrapped fortran codes on a half a dozen different computers/clusters and it has proven to work much more robustly than using f2py directly. </div><div><br></div><div>Hope this helps</div><div>Gaetan</div><div><br></div><div># Generate Python include directory</div><div> $(eval PYTHON_INCLUDES = $(shell $(PYTHON-CONFIG) --includes))</div><div> @ echo "#------------------------------------------------------#"</div><div> @echo Python Inclue Flags $(PYTHON_INCLUDES)</div><div> @echo "#------------------------------------------------------#"</div><div><br></div><div># Generate Numpy inlude directory</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(eval NUMPY_INCLUDES = $(shell $(PYTHON) -c 'import numpy; print numpy.get_include()'))</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo "#------------------------------------------------------#"</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo Numpy Include Directory: $(NUMPY_INCLUDES)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo "#------------------------------------------------------#"</div><div><br></div><div># Generate f2py root directory</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(eval F2PY_ROOT = $(shell $(PYTHON) get_f2py.py))</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo "#------------------------------------------------------#"</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo f2py root directory: $(F2PY_ROOT)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span> @echo "#------------------------------------------------------#"</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>f2py warpustruct.pyf</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>@echo " "</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(CC) $(CC_ALL_FLAGS) $(PYTHON_INCLUDES) -I$(NUMPY_INCLUDES) \</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>-I$(F2PY_ROOT)/src -c warpustructmodule.c</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(CC) $(CC_ALL_FLAGS) $(PYTHON_INCLUDES) -I$(NUMPY_INCLUDES) -c \</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(F2PY_ROOT)/src/fortranobject.c -o fortranobject.o</div><div><br></div><div> # Compiled f2py wrapper file</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(FF90) $(FF90_ALL_FLAGS) -I$(MAIN_DIR)/mod -c warpustruct-f2pywrappers2.f90 </div><div><br></div><div># Final LInk</div><div><br></div><div> enter code here</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>$(FF90) -shared $(PYTHON_OBJECTS) $(LINKER_ALL_FLAGS) -o warpustruct.so</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 23, 2017 at 1:14 PM, Austin Herrema <span dir="ltr"><<a href="mailto:aherrema@iastate.edu" target="_blank">aherrema@iastate.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Sorry to self-bump this. Sounds like there are no quick and easy answers to get this solved, which I understand. I just wanted to say that if anybody has any suggestions about what I should be looking into specifically (C compiler? f2py? etc...) then even that would be helpful as I'm pretty stuck on this. And I have also posted a discussion on StackOverflow (<a href="http://stackoverflow.com/questions/42978049/unable-to-use-f2py-to-link-large-petsc-slepc-fortran-code?noredirect=1#comment73055943_42978049" target="_blank">http://stackoverflow.com/<wbr>questions/42978049/unable-to-<wbr>use-f2py-to-link-large-petsc-<wbr>slepc-fortran-code?noredirect=<wbr>1#comment73055943_42978049</a>) in case anything there might be useful (it's mostly all the same). <br><br>Thank you regardless!<span class="HOEnZb"><font color="#888888"><br>Austin</font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 22, 2017 at 2:08 PM, Austin Herrema <span dir="ltr"><<a href="mailto:aherrema@iastate.edu" target="_blank">aherrema@iastate.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Makes sense, and definitely seems to be a more natural way to go now that I see it.<br><br><div>When compiling using this rule it seems to get close but doesn't compile all the way. Here is the output (in reality, what I was referring to as "modname.so" is "iga_blade_py.so" and the "outer_driver.f90" is called merely "run_analysis.f90"--sorry for the confusion):<br><br><div><font color="#999999">running build</font></div><div><font color="#999999">running config_cc</font></div><div><font color="#999999">unifing config_cc, config, build_clib, build_ext, build commands --compiler options</font></div><div><font color="#999999">running config_fc</font></div><div><font color="#999999">unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options</font></div><div><font color="#999999">running build_src</font></div><div><font color="#999999">build_src</font></div><div><font color="#999999">building extension "iga_blade_py" sources</font></div><div><font color="#999999">f2py options: []</font></div><div><font color="#999999">f2py:> /tmp/tmpIH70ZJ/src.macosx-10.1<wbr>0-x86_64-2.7/iga_blade_pymodul<wbr>e.c</font></div><div><font color="#999999">creating /tmp/tmpIH70ZJ/src.macosx-10.1<wbr>0-x86_64-2.7</font></div><div><font color="#999999">Reading fortran codes...</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Reading file 'run_analysis.f90' (format:free)</font></div><div><font color="#999999">Post-processing...</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Block: iga_blade_py</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Block: run_analysis</font></div><div><font color="#999999">Post-processing (stage 2)...</font></div><div><font color="#999999">Building modules...</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Building module "iga_blade_py"...</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Constructing wrapper function "run_analysis"...</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span> run_analysis()</font></div><div><font color="#999999"><span class="m_-6816571630429443082m_6201953611261867023gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Wrote C/API module "iga_blade_py" to file "/tmp/tmpIH70ZJ/src.macosx-10.<wbr>10-x86_64-2.7/iga_blade_pymodu<wbr>le.c"</font></div><div><font color="#999999"> adding '/tmp/tmpIH70ZJ/src.macosx-10.<wbr>10-x86_64-2.7/fortranobject.c' to sources.</font></div><div><font color="#999999"> adding '/tmp/tmpIH70ZJ/src.macosx-10.<wbr>10-x86_64-2.7' to include_dirs.</font></div><div><font color="#999999">copying /usr/local/lib/python2.7/site-<wbr>packages/numpy/f2py/src/fortra<wbr>nobject.c -> /tmp/tmpIH70ZJ/src.macosx-10.1<wbr>0-x86_64-2.7</font></div><div><font color="#999999">copying /usr/local/lib/python2.7/site-<wbr>packages/numpy/f2py/src/fortra<wbr>nobject.h -> /tmp/tmpIH70ZJ/src.macosx-10.1<wbr>0-x86_64-2.7</font></div><div><font color="#999999">build_src: building npy-pkg config files</font></div><div><font color="#999999">running build_ext</font></div><div><font color="#999999">customize UnixCCompiler</font></div><div><font color="#999999">customize UnixCCompiler using build_ext</font></div><div><font color="#999999">customize Gnu95FCompiler</font></div><div><font color="#999999">Found executable /usr/local/bin/gfortran</font></div><div><font color="#999999">customize Gnu95FCompiler</font></div><div><font color="#999999">customize Gnu95FCompiler using build_ext</font></div><div><font color="#999999">building 'iga_blade_py' extension</font></div><div><font color="#999999">compiling C sources</font></div><div><font color="#999999">C compiler: clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">error: unknown file type '' (from '-Wl,-rpath,/usr/local/Cellar/<wbr>slepc/3.7.3_4/real/lib')</font></div><div><font color="#999999">make: *** [iga_blade_py.so] Error 1</font></div></div></div><div class="m_-6816571630429443082HOEnZb"><div class="m_-6816571630429443082h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 22, 2017 at 1:39 PM, Jose E. Roman <span dir="ltr"><<a href="mailto:jroman@dsic.upv.es" target="_blank">jroman@dsic.upv.es</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
> El 22 mar 2017, a las 19:23, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>> escribió:<br>
><br>
><br>
>> On Mar 22, 2017, at 1:08 PM, Austin Herrema <<a href="mailto:aherrema@iastate.edu" target="_blank">aherrema@iastate.edu</a>> wrote:<br>
>><br>
>> Thank you for the suggestion! Seems like a reasonable way to go. Not working for me, however, I suspect because I'm using homebrew installations of PETSc and SLEPc (I don't think all the makefiles are kept). Any other way to do the same thing by chance? Worst case I could use a non-homebrew installation but I'd prefer not to mess with that if I can help it...<br>
><br>
> How do you link a "regular" SLEPc C program using the home-brew libraries? You need basically the same link line for f2py as you need for C programs.<br>
<br>
<br>
</span>What Barry may be suggesting is: instead of using a script to invoke f2py, add a rule to your makefile<br>
<br>
modname.so: outer_driver.f90<br>
f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o ${SLEPC_EPS_LIB}<br>
<br>
Then 'make modname.so' will pick the libraries from SLEPc makefiles.<br>
<span class="m_-6816571630429443082m_6201953611261867023HOEnZb"><font color="#888888"><br>
Jose<br>
</font></span><div class="m_-6816571630429443082m_6201953611261867023HOEnZb"><div class="m_-6816571630429443082m_6201953611261867023h5"><br>
><br>
>><br>
>> Thanks,<br>
>> Austin<br>
>><br>
>> On Wed, Mar 22, 2017 at 11:20 AM Jose E. Roman <<a href="mailto:jroman@dsic.upv.es" target="_blank">jroman@dsic.upv.es</a>> wrote:<br>
>> Try the following:<br>
>> $ cd $SLEPC_DIR<br>
>> $ make getlinklibs_slepc<br>
>> Then copy the output and paste it at the end of your f2py command.<br>
>><br>
>> Jose<br>
>><br>
>><br>
>>> El 22 mar 2017, a las 16:38, Austin Herrema <<a href="mailto:aherrema@iastate.edu" target="_blank">aherrema@iastate.edu</a>> escribió:<br>
>>><br>
>>> Hello all,<br>
>>><br>
>>> I am trying to do as the subject line describes--use f2py to run a large PETSc/SLEPc fortran finite element code through python. I really only need to wrap the outermost function of the fortran code--don't need any access to subroutines. I'll describe what I'm doing, some of which I'm not 100% confident is correct (not much experience with f2py)--feel free to correct/redirect any of it.<br>
>>><br>
>>> First, I'm editing the fortran code so that the top-level function is a subroutine rather than a main program (it's my understanding that this is required for f2py?).<br>
>>><br>
>>> I use my regular makefile (modeled after a standard SLEPc makefile from the user guide) to compile all of the .f90/.F90 files (many of them) to .o files using SLEPc/PETSc rules. The final linking phase fails since there isn't a main program, but I'm just ignoring that for now since that's not what I ultimately need...<br>
>>><br>
>>> Using a python script, I set up and run the f2py command. Right now it has the form...<br>
>>> "f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o..." etc.<br>
>>><br>
>>> This appears to work, but upon attempting to import, it cannot find the SLEPc (and, I presume, PETSc) objects/functions:<br>
>>><br>
>>>>>> import mod_name<br>
>>> Traceback (most recent call last):<br>
>>> File "<stdin>", line 1, in <module><br>
>>> ImportError: dlopen(./mod_name.so, 2): Symbol not found: _epscreate_<br>
>>> Referenced from: ./mod_name.so<br>
>>> Expected in: flat namespace<br>
>>> in ./mod_name.so<br>
>>><br>
>>> Based on this discussion, I believe I need to somehow include PETSc/SLEPc info when linking with f2py. Is that correct? Any direction on how to do that? I don't quite understand what the OP of that question ultimately ended up doing to get it to work. I tried using the -I flag pointing to the slepc_common file (like the SLEPc makefile does). The problem is that that is a file, not a directory, which contains a number of other makefile-style variables--so it works to include it in a makefile, but doesn't work in python. Maybe there are only a few directories I really need to include? Or is it possible to somehow run f2py through a makefile? I'm a bit ignorant in that realm as well.<br>
>>><br>
>>> Thank you for any help or suggestions!<br>
>>> Austin<br>
>>><br>
>>><br>
>>> --<br>
>>> Austin Herrema<br>
>>> PhD Student | Graduate Research Assistant | Iowa State University<br>
>>> Wind Energy Science, Engineering, and Policy | Mechanical Engineering<br>
>><br>
>> --<br>
>> Austin Herrema<br>
>> PhD Student | Graduate Research Assistant | Iowa State University<br>
>> Wind Energy Science, Engineering, and Policy | Mechanical Engineering<br>
><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="m_-6816571630429443082m_6201953611261867023gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr" style="color:rgb(136,136,136);font-size:12.8px"><b style="font-size:12.8px"><font color="#666666">Austin Herrema</font></b><br></div><div dir="ltr" style="color:rgb(136,136,136);font-size:12.8px"><div><font color="#999999">PhD Student | Graduate Research Assistant | Iowa State University</font></div><div><font color="#999999">Wind Energy Science, Engineering, and Policy | Mechanical Engineering</font></div></div></div></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="m_-6816571630429443082gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr" style="color:rgb(136,136,136);font-size:12.8px"><b style="font-size:12.8px"><font color="#666666">Austin Herrema</font></b><br></div><div dir="ltr" style="color:rgb(136,136,136);font-size:12.8px"><div><font color="#999999">PhD Student | Graduate Research Assistant | Iowa State University</font></div><div><font color="#999999">Wind Energy Science, Engineering, and Policy | Mechanical Engineering</font></div></div></div></div>
</div>
</div></div></blockquote></div><br></div>