I am trying to pass MPI communicator from Fortran to C/C++. I am not sure what kind of handle to pass: type(MPI_Comm), MPI_Fint, or just integer. I tried several combinations of mentioned options but either I could not compile or got segmentation fault. The following codes produce the error message:
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.
Interface
module Tailor_module
use mpi
use, intrinsic :: ISO_C_Binding, only: C_int, C_double, C_char, c_associated
use, intrinsic :: ISO_C_Binding, only: c_ptr, C_NULL_ptr
implicit none
private
interface
function Tailor__getobject(comm) result(optr) bind(C, name="Tailor__getobject")
import c_ptr
implicit none
integer, intent(in), value :: comm
type(c_ptr) :: optr
end function Tailor__getobject
end interface
type(c_ptr), save :: obj = c_null_ptr
public :: Create
CONTAINS
subroutine Create(com)
integer, intent(in) :: com
obj = Tailor__getobject(comm=com)
return
end subroutine Create
end module Tailor_module
Fortran driver
program main
use mpi
use Tailor_module, only : Create
IMPLICIT NONE
integer error
call MPI_Init(error)
call Create(MPI_COMM_WORLD);
call MPI_Finalize (error)
end
C++
typedef void * OpaqueObject;
extern "C"
{
OpaqueObject Tailor__getobject(MPI_Fint *f_handle);
}
OpaqueObject Tailor__getobject(MPI_Fint *f_handle)
{
MPI_Comm comm;
comm = MPI_Comm_f2c(*f_handle);
OpaqueObject s;
return s;
}
Related
I have the following test C library:
#include <stdlib.h>
struct mystruct {
int a;
double b;
};
struct mystruct *return_array_of_structs(int *size);
struct mystruct *return_array_of_structs(int *size) {
int i;
struct mystruct *ptr;
ptr = malloc(sizeof(struct mystruct)*10);
for(i=0; i<10; i++) {
ptr[i].a = i+1;
ptr[i].b = (i+1)*1.0L;
}
*size=10;
return ptr;
}
And the following module intended to be compiled with f2py:
module test_c_lib
use iso_c_binding
implicit none
type :: t_mystruct
integer :: a
real(8) :: b
end type
contains
subroutine test()
use iso_c_binding
type(c_ptr) :: ret_c_ptr
integer :: length
! Interface to C function
interface
type(c_ptr) function c_return_array_of_structs(a) bind(C, name="return_array_of_structs")
import
integer(c_int) :: a
end function
end interface
! Call C function
ret_c_ptr = c_return_array_of_structs(length)
end subroutine
end module
The following makefile compiles this:
f_mod.so: f_mod.f90 c_lib.o
f2py -c f_mod.f90 c_lib.o -m f_mod
c_lib.o: c_lib.c
gcc -c -fpic c_lib.c -o c_lib.o
I can load the library in Python normally and execute the test() subroutine without problems.
import f_mod
f_mod.test_c_lib.test()
But I do not how to convert ret_c_ptr into an array of derived type t_mystruct that I can normally operate as an array in Fortran. Any hint?
Note: the question is related to iso c bindings and Fortran, not to f2py or its integration with Python.
If you have a C pointer and want to associate a Fortran pointer with the target of that C pointer, you want to use c_f_pointer to do that association. c_f_pointer doesn't (generally) care whether the type is an intrinsic one or not, or scalar or not.
Let's define the Fortran derived type as an interoperable1 one:
type, bind(c) :: t_mystruct
integer(c_int) :: a
real(c_double) :: b
end type
and then have a Fortran pointer array of that type:
type(t_mystruct), pointer :: ptr_f(:)
With ptr_c pointing to a suitable lump of memory we can do the association:
call c_f_pointer(ptr_c, ptr_f, [length])
to make ptr_f an array of shape [length] pointing to the C target.
1 Being interoperable isn't necessary, but it's certainly helpful to be if it can be.
The right answer to this question is the one provided by francescalus above, however I include the complete excerpt of code with the functions exposed by f2py to Python.
f2py will not allow to return numpy arrays of derived types (which would be a numpy vector of a custom dtype), so you need to return individual vectors per field. Also f2py can not return allocatable arrays without explicit size, therefore this function needs to be split into two calls, one to get the length and another one to get the actual array.
While these are clear limitations of f2py this allows to call directly Fortran functions (or C encapsulated functions as in this example) and get numpy arrays as return values.
The code is not optimised as it implies invoking the C function twice, maybe there is the possibility to use sort kind of static variable like in C, so the second invoke would already had computed the values, although I do not know:
if that is possible at all in Fortran
if that is possible within the context of a shared object (which is what f2py creates from the module)
As there is not that many examples around f2py this might be useful for someone:
module test_c_lib
use iso_c_binding
implicit none
type, bind(c) :: t_mystruct
integer(c_int) :: a
real(c_double) :: b
end type
contains
subroutine get_array_len(l)
use iso_c_binding
integer, intent(out) :: l
type(c_ptr) :: ret_c_ptr
integer :: length
! Interface to C function
interface
type(c_ptr) function c_return_array_of_structs(a) bind(C, name="return_array_of_structs")
import
integer(c_int) :: a
end function
end interface
! Call C function
ret_c_ptr = c_return_array_of_structs(length)
l = length
end subroutine
subroutine get_array(l, a, b)
use iso_c_binding
integer, intent(in) :: l
integer, intent(out) :: a(l)
real(8), intent(out) :: b(l)
type(c_ptr) :: ret_c_ptr
type(t_mystruct), pointer :: f_ptr(:)
integer :: length
! Interface to C function
interface
type(c_ptr) function c_return_array_of_structs(a) bind(C, name="return_array_of_structs")
import
integer(c_int) :: a
end function
end interface
! Call C function
ret_c_ptr = c_return_array_of_structs(length)
call c_f_pointer(ret_c_ptr, f_ptr, [length])
a = f_ptr%a
b = f_ptr%b
end subroutine
end module
I have a C function that uses structures. I developed a Fortran Wrapper which calls the C function. Both libraries are built successfully. I have passed the variables form the PSCAD model and run the case. The PSCAD model is throwing the error.
Error Message:
Type Id Component Namespace Description
dll_df FORTRA~1.LIB(dlltestx.obj) : error LNK2019: unresolved external symbol _test_cfun referenced in function _AUX_CFUN
C-Code:
// This is an example of an exported variable
typedef struct _goose_c
{
int in1;
float in2;
}goose_c;
__declspec(dllexport) void test_cfunc(goose_c *V, double *out1)
{
//*out1 = (*V).in1 + (*V).in2;
*out1 = V->in1 + V->in2;
}
Fortran Wrapper Code:
SUBROUTINE AUX_CFUN(ip1, ip2, out1)
use, intrinsic :: iso_c_binding
implicit none
integer, intent(in) :: ip1
real, intent(in) :: ip2
real, intent(out) :: out1
type, bind(C) :: goose_t
integer(c_int) :: in1
real(c_double) :: in2
end type goose_t
type(goose_t) :: goose_f
! Fortran 90 interface to a C procedure
INTERFACE
SUBROUTINE TEST_CFUN (goose_f,out1) bind (C)
use iso_c_binding
import :: goose_t
type(goose_t), intent (in) :: goose_f
real(c_double), intent (out) :: out1
END SUBROUTINE TEST_CFUN
END INTERFACE
goose_f%in1 = ip1
goose_f%in2 = ip2
! call of the C procedure
CALL TEST_CFUN(goose_f,out1)
RETURN
END SUBROUTINE AUX_CFUN
The bind (C) clause in the interface should be completed with the exact (including the correct case) C name of the function you want to interface; so in your example it should read bind (C, name='test_cfunc'). The Fortran name associated to the C function (TEST_CFUN in your example) can be in principle different, it's up to you to decide whether this is a good idea or not, and it is used in your fortran program with the usual fortran rules, so it is case-insensitive.
I am writing an iso_c_binding in Fortran to call a C-function with the below prototype
int zmat_run(
const size_t inputsize,
unsigned char *inputstr,
size_t *outputsize,
unsigned char **outputbuf,
const int zipid,
int *ret,
const int iscompress
);
My question is how do I declare unsigned char **outputbuf, a pointer that is used inside the c-function to allocate output buffer, in this interface?
Also, what data type I should be using in Fortran as the real parameter to pass to this outputbuf parameter? should it be allocatable? (if it is allocated inside the c-function)?
I currently drafted this module, but haven't tested it (I doubt it will work).
module zmatlib
use iso_c_binding, only: c_char, c_size_t, c_ptr, C_NULL_CHAR
interface
integer(c_int) function zmat_run(inputsize, inputbuf, outputsize, outputbuf, zipid, ret, level) bind(C, name="zmat_run")
use iso_c_binding
integer(c_size_t), value :: inputsize
integer(c_int), value :: zipid, level
integer(c_size_t), intent(out) :: outputsize
integer(c_int), intent(out) :: ret
character(kind=c_char), intent(in) :: inputbuf(*)
character pointer(c_ptr),intent(out) :: outputbuf
end function zmat_run
end interface
end module
Try type (C_PTR), intent (out). Then you will need to use Fortran function c_f_pointer to associate the C pointer with a Fortran pointer. Probably of type C_CHAR.
Consider the following Fortran module Foo_mod and its submodule Foo_smod,
module CallbackInterface_mod
abstract interface
function getLogFunc4C_proc(ndim,Point) result(logFunc) bind(C)
use, intrinsic :: iso_c_binding, only : c_int32_t, c_double, c_int
integer(c_int32_t), intent(in) :: ndim
real(c_double), intent(in) :: Point(ndim)
real(c_double) :: logFunc
end function getLogFunc4C_proc
end interface
end module CallbackInterface_mod
!***********************************************************************************************************************************
!***********************************************************************************************************************************
module Foo_mod
interface
module subroutine runFoo4C(ndim, getLogFuncFromC, inputString, inputStringLen) bind(C, name="runFoo")
use, intrinsic :: iso_c_binding, only: c_int32_t, c_char, c_funptr, c_f_procpointer, c_size_t
use CallbackInterface_mod, only: getLogFunc4C_proc
implicit none
integer(c_int32_t) , intent(in) :: ndim
character(len=1, kind=c_char), dimension(*), intent(in) :: inputString
integer(c_size_t) , intent(in) :: inputStringLen
type(c_funptr), intent(in), value :: getLogFuncFromC
end subroutine runFoo4C
end interface
contains
subroutine runFoo(ndim, getLogFunc, string)
use CallbackInterface_mod, only: getLogFunc4C_proc
use, intrinsic :: iso_fortran_env, only: RK => real64
implicit none
integer :: ndim
procedure(getLogFunc4C_proc) :: getLogFunc
character(*), intent(in) :: string
real(RK) :: Point(ndim)
character(:), allocatable :: mystring
Point = [1._RK,1._RK]
write(*,*) "Hi again, this is a call from inside runFoo!"
write(*,*) "getLogFunc(2,[1,1]) = ", getLogFunc(ndim,Point)
write(*,*) "string = ", string
end subroutine
end module Foo_mod
!***********************************************************************************************************************************
!***********************************************************************************************************************************
submodule (Foo_mod) Foo_smod
contains
module subroutine runFoo4C(ndim, getLogFuncFromC, InputString, inputStringLen) bind(C, name="runFoo")
use, intrinsic :: iso_c_binding, only: c_double, c_int32_t, c_char, c_funptr, c_f_procpointer, c_size_t
use CallbackInterface_mod, only: getLogFunc4C_proc
implicit none
integer(c_int32_t) , intent(in) :: ndim
character(len=1, kind=c_char), dimension(*), intent(in) :: InputString
integer(c_size_t) , intent(in) :: inputStringLen
type(c_funptr), intent(in), value :: getLogFuncFromC
procedure(getLogFunc4C_proc), pointer :: getLogFunc
real(c_double) :: Point(ndim)
character(:), allocatable :: inputString4tran
integer :: i
write(*,*) "InputString: ", InputString(1:inputStringLen)
allocate( character(len=inputStringLen) :: inputString4tran )
do i=1,inputStringLen
inputString4tran(i:i) = InputString(i)
end do
write(*,*) "inputString4tran: ", inputString4tran
! associate the input C procedure pointer to a Fortran procedure pointer
call c_f_procpointer(cptr=getLogFuncFromC, fptr=getLogFunc)
Point = [1._c_double, 1._c_double]
write(*,*) "getLogFunc(ndim=2, [1._c_double, 1._c_double]): ", getLogFunc( ndim, Point )
call runFoo(ndim, getLogFunc, inputString4tran)
end subroutine runFoo4C
end submodule Foo_smod
Now, this code seems like a perfectly sound Fortran module to me, and indeed it does compile and link successfully to C programs via Intel Fortran Compiler 2018, on both Windows and Linux, and runs correctly. However, when compiled by gfortran 7.3.0, it gives the following error:
gfortran -c ../Foo_mod.f90
../Foo_mod.f90:54:18:
submodule (Foo_mod) Foo_smod
1
Error: BIND(C) attribute at (1) can only be used for variables or common blocks
The problem can be resolved if I remove the bind(c) attribute from the interface of the interoperable subroutine runFoo4C() in module Foo_mod, like the following,
...
module subroutine runFoo4C(ndim, getLogFuncFromC, inputString, inputStringLen) ! bind(C, name="runFoo")
...
However, the Intel Fortran compiler would then complain about the incompatibility of the subroutine's interface in module Foo_mod and the implementation of the subroutine in submodule Foo_smod. This seems to me more like a bug in GFORTRAN, than programming mistake. But your comments are appreciated before I go on to report it.
It is quite clear to me that this is a gfortran bug related to submodules. As that's a fairly recent feature addition, it's not unexpected that there may be bugs here and there.
I will comment that when asking a question such as this here, you are strongly encouraged to provide a Minimal, Complete and Verifiable Example Since you don't show the module that the submodule goes with, we can't verify the issue ourselves without unnecessary extra work.
I want to load the same .so file twice as separate instances. Based on the example, I have created the app with two dlopen commands.
However, I was facing some issues and I understood that dlmopen should be used if I am using multiple instances of a same .so. However, I don't know how to pass the arguments. Can someone help me how to do this in GFortran?
My code is as below,
program example
use :: iso_c_binding
implicit none
integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
!
! interface to linux API
interface
function dlopen(filename,mode) bind(c,name="dlopen")
! void *dlopen(const char *filename, int mode);
use iso_c_binding
implicit none
type(c_ptr) :: dlopen
character(c_char), intent(in) :: filename(*)
integer(c_int), value :: mode
end function
function dlsym(handle,name) bind(c,name="dlsym")
! void *dlsym(void *handle, const char *name);
use iso_c_binding
implicit none
type(c_funptr) :: dlsym
type(c_ptr), value :: handle
character(c_char), intent(in) :: name(*)
end function
function dlclose(handle) bind(c,name="dlclose")
! int dlclose(void *handle);
use iso_c_binding
implicit none
integer(c_int) :: dlclose
type(c_ptr), value :: handle
end function
end interface
! Define interface of call-back routine.
abstract interface
subroutine called_proc (i, i2) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int), intent(in) :: i
integer(c_int), intent(out) :: i2
end subroutine called_proc
end interface
! testing the dynamic loading
integer i, i2
type(c_funptr) :: proc_addr
type(c_ptr) :: handle1, handle2
character(256) :: pName, lName
procedure(called_proc), bind(c), pointer :: proc
!
i = 15
handle1=dlopen("./test.so"//c_null_char, RTLD_LAZY)
if (.not. c_associated(handle1))then
print*, 'Unable to load DLL ./test.so - First time'
stop
end if
handle2=dlopen("./test.so"//c_null_char, RTLD_LAZY)
if (.not. c_associated(handle2))then
print*, 'Unable to load DLL ./test.so - Second time'
stop
end if
! If I can use dlmopen() I dont know how to pass the arguments
proc_addr=dlsym(handle, "t_times2"//c_null_char)
if (.not. c_associated(proc_addr))then
write(*,*) 'Unable to load the procedure t_times2'
stop
end if
call c_f_procpointer( proc_addr, proc )
call proc(i,i2)
write(*,*) "t_times2, i2=", i2
!
proc_addr=dlsym( handle, "t_square"//c_null_char )
if ( .not. c_associated(proc_addr) )then
write(*,*)'Unable to load the procedure t_square'
stop
end if
call c_f_procpointer(proc_addr, proc)
call proc(i,i2)
write(*,*) "t_square, i2=", i2
contains
end program example
Update:
Based on the suggestion from Vladmir, I tried below but I get Unable to load DLL ./test.so - Third time,
function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
! void *dlopen(const char *filename, int mode);
use iso_c_binding
implicit none
type(c_ptr) :: dlopen
integer(c_long), value :: lmid_t
character(c_char), intent(in) :: filename(*)
integer(c_int), value :: mode
end function
handle3=dlmopen(1,"./test.so"//c_null_char, RTLD_LAZY)
if (.not. c_associated(handle3))then
print*, 'Unable to load DLL ./test.so - Third time'
stop
end if
This is the base that you can start from, it is a modification of your code and because of the #define macros it requires the cpp flags. You can change the defines to a normal parameter declaration if you want, but just copying the macros from the header is easier.
use iso_c_binding
implicit none
! from dlfcn.h
# define LM_ID_BASE 0 /* Initial namespace. */
# define LM_ID_NEWLM -1 /* For dlmopen: request new namespace. */
integer(c_long) :: dlist = LM_ID_NEWLM
integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
type(c_ptr) :: handle3
interface
function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
! void *dlmopen (Lmid_t lmid, const char *filename, int flags);
use iso_c_binding
implicit none
type(c_ptr) :: dlmopen
integer(c_long), value :: lmid_t
character(c_char), intent(in) :: filename(*)
integer(c_int), value :: mode
end function
end interface
handle3=dlmopen(dlist,"test.so"//c_null_char, RTLD_LAZY)
if (.not. c_associated(handle3))then
print*, 'Unable to load DLL ./test.so - Third time'
stop
end if
end
According to the manual, there are two principal values that you can pass as dlist, either LM_ID_BASE or LM_ID_NEWLM. Their values are defined in the header dlfcn.h that is located ammong other standard C and POSIX headers (/usr/include/ or similar). You should not just pass 1, but one of these two values which happen to be 0 and -1 on my computer.