I have an already existing Fortran source code that I am working on adding C to, and am having difficulty with passing an array from C into a Fortran function and then receiving another array back.
It compiles without any errors, but when I attempt to run the code, it says that:
"Dimension of array 'p2' (In Fortran Function) has extent 3 instead of -4537421815 (or some other similar number)"
I am not sure what is going on here. I will attach the two codes below.
NOTE: I have removed a lot of the extra variable initialization lines which I think seems unnecessary for finding the problem.
C function:
#include <stdio.h>
#include <stdlib.h>
extern "C" double* __multiphase_tools_MOD_project(double p1[],double *mydt,int *myi,int *myj,int *myk);
extern "C" void cuda_(int *ptr_band, double *ptr_u, double *ptr_v, double *ptr_w)
{
double *pt_out;
double pt_in[3];
// Loop over the domain and compute fluxes near interfaces
//=======================================================================================
// X FACE
//=======================================================================================
for (k = kmin; k <= kmax; k++)
{
for (j = jmin; j <= jmax; j++)
{
for (i = imin; i <= imax; i++)
{
if (abs(band[i-1][j][k]) <= nband_CFL && abs(band[i][j][k]) <= nband_CFL )
{
for (int n = 1; n < 10; n++)
{
pt_in[0] = pt[0][n][1]; pt_in[1] = pt[1][n][1]; pt_in[2] = pt[2][n][1];
pt_out = __multiphase_tools_MOD_project(pt_in,&neg_dt_uvw,&i,&j,&k);
}
}
}
}
}
return;
}
Fortran Function:
function project(p1,mydt,myi,myj,myk) result(p2)
use math
use iso_c_binding
implicit none
real(WP), dimension(3) :: p2
real(WP), dimension(3), intent(in) :: p1
real(WP), intent(in) :: mydt
integer, intent(in) :: myi,myj,myk
real(WP), dimension(3) :: v1,v2,v3,v4
v1=get_velocity(p1 ,myi,myj,myk)
v2=get_velocity(p1+0.5_WP*mydt*v1,myi,myj,myk)
v3=get_velocity(p1+0.5_WP*mydt*v2,myi,myj,myk)
v4=get_velocity(p1+ mydt*v3,myi,myj,myk)
p2=p1+mydt/6.0_WP*(v1+2.0_WP*v2+2.0_WP*v3+v4)
return
end function project
bind(c)to the Fortran function definition and avoid (what I assume to be) manual name mangling? This may address the call issue too. I've just noticed: why are you usingiso_c_binding, but then not referencing any of the entities from it? That module perhaps doesn't do what you think it does.type(c_ptr)then usecall c_f_pointer(c_loc(old_c_ptr), fortran_ptr)to ensure correct C-language style casting.