1

I have a Fortran program in the form

PROGRAM main
  USE a_one
  USE a_two
  ...


END PROGRAM main

with makefile:

# Paths
MDIR            =       ./main
SDIR            =       ./solver
ODIR            =       ./obj
CASE            =       $(SDIR)/CASE/test_
TOP             =       .  

# include machine specific settings based on WLT_COMPILE_HOSTNAME environment variable
FC              =       ifort
CC              =       mpic++ #icc
LINKLIB         =       -lstdc++ -shared-intel -lifcore

FFLAGS          =       -fpp -O1 -DPTR_INTEGER8 -warn nousage -module $(ODIR) -g -traceback
CCFLAG          =       -O1 -g -DDATABASE_INTERFACE_LOWERCASE -DDATABASE_APPEND_UNDERSCORE

# Define rule to make .f90 and .f files
$(ODIR)/%.o : $(SDIR)/%.f90 
        $(FC) -c $(FFLAGS) $< -o $@

# Define rule to make C objects
$(ODIR)/%.cc.o : $(SDIR)/C++/%.cxx
        $(CC) $(CCFLAG) -c $< -o $@

# Data structure type: Tree DB (C++ variable size nodes)
CC_OBJ          =       tree.cc.o

# set executable name based on DB and CASE
EXEC        =       $(dir ${CASE})/test_new.out

# Define list of all object files to be made
_OBJ     =  module_a.o module_b.o $(_CASE).o $(_CASE).PARAMS.o $(CC_OBJ)
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

MAIN_OBJ = $(ODIR)/main.o

All:  my_main

my_main : $(OBJ) $(MAIN_OBJ)
        $(FC) $(FFLAGS) $(OBJ) $(MAIN_OBJ) -o $(EXEC) $(LINKLIB)

As you can see, in addition to fortran codes, there are some c++ codes being used in this program as well. I am trying to refactor this code as a subroutine

SUBROUTINE main (in_matrix, out_matrix, n)
  USE a_one
  USE a_two
  ...


END SUBROUTINE main

and call it from a python code. I was following the directions in

https://modelingguru.nasa.gov/docs/DOC-2343 to use 'f2py' for doing this and couldn't get it to work. Then I realized that I have C++ codes that are probably causing the issue. I was wondering how should I modify the directions in the above link to take c++ code into account. Thanks.

Blade
  • 984
  • 3
  • 12
  • 34
  • I often find `f2py` not so useful. An alternative route for me has been to use Python's `ctypes` while making the Fortran codes, that are directly called from Python, C-interoperable via `iso_c_binding` intrinsic Fortran module, which is available in Fortran 2003 and beyond. – Scientist Jul 01 '19 at 07:53
  • From your makefile, it seems as if C++ and Fortran sources are compiled into objects, that are linked into an executable together with the main program you are now refactoring into a subroutine. Maybe you can use ideas from [this answer](https://stackoverflow.com/a/27275549/3967096) to make use of the same compiled objects in your refactored version? – jbdv Jul 01 '19 at 11:22

0 Answers0