Reputation: 13
I'm trying to pass a 3d dynamic arrays from C to Fortran. For example, the first time I call the function I use
extern "C" Fun_(double X[], double Y[], double Z[],
int *Nx, int *Ny, int *Nz,
double F[][20][20]);
The second time I have to change it to
extern "C" Fun_(double X[], double Y[], double Z[],
int *Nx, int *Ny, int *Nz,
double F[][10][40]);
and so on. The main code is
int main() {
char path[500],id_type[3];
char *dir_path, *file_path;
Nx=10000,Ny=20,Nz=20; ----> or Ny=10,Nz=40;
double X[Nx], Y[Ny], Z[Nz];
double F[Nx][20][20] --- > or F[Nx][10][40];
....}
The array changes 446 times. So, I have to change its size dynamically. Any suggestions? Thanks
The Fortran code is pretty long but here is the relevant part
SUBROUTINE Fun(X,Y,Z,Nx,Ny,Nz,F)
integer :: Nx,Ny,Nz,inx,iny,inz
real(8) :: F(Nx,Ny,Nz)
real(8) :: X(Nx),Y(Ny),Z(Nz)
....
Upvotes: 1
Views: 244
Reputation: 33631
Caveat: It's been a long time since I did fortran, but this is what I came up with.
The variable length arrays seem easy enough. But, C uses row-major order for its 2D [and higher] arrays. But, Fortran uses column-major order. So, either the C code or the Fortran code will need to be aware of this.
I've created two subroutines: subx
which is native Fortran, column-major order. And, suby
which is C compatible, row-major order.
Also, some test output is below. Because the arrays are created as C-compatible, row-major, only suby
produces the expected output.
Side note: One of my references for this was: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
Here's the C code:
#include <stdio.h>
void subx_(int *,int *,int *);
void suby_(int *,int *,int *);
void
dotest(int Dy,int Dx)
{
int val = 0;
int arr[Dy][Dx];
for (int y = 0; y < Dy; ++y) {
for (int x = 0; x < Dx; ++x)
arr[y][x] = val++;
}
subx_(&Dy,&Dx,&arr[0][0]);
suby_(&Dy,&Dx,&arr[0][0]);
}
int
main(void)
{
printf("testing ...\n");
dotest(3,4);
dotest(6,5);
return 0;
}
Here's the Fortran code:
! this uses column-major order -- _not_ C compatible
subroutine subx(Dy,Dx,arr)
integer :: Dy,Dx
integer :: arr(Dy,Dx)
integer :: x,y
write(*,700) ""
700 format (a)
write(*,800) "subx: Dy=",Dy," Dx=",Dx
800 format (a,i4,a,i4)
do y = 1, Dy
do x = 1, Dx
write(*,900) "subx ",x,y,arr(y,x)
900 format (a,i8,i8,i8)
end do
end do
end subroutine subx
! this uses row-major order -- _is_ C compatible
subroutine suby(Dy,Dx,arr)
integer :: Dy,Dx
integer :: arr(Dx,Dy)
integer :: x,y
write(*,700) ""
700 format (a)
write(*,800) "suby: Dy=",Dy," Dx=",Dx
800 format (a,i4,a,i4)
do y = 1, Dy
do x = 1, Dx
write(*,900) "suby ",x,y,arr(x,y)
900 format (a,i8,i8,i8)
end do
end do
end subroutine suby
Here's the program output:
testing ...
subx: Dy= 3 Dx= 4
subx 1 1 0
subx 2 1 3
subx 3 1 6
subx 4 1 9
subx 1 2 1
subx 2 2 4
subx 3 2 7
subx 4 2 10
subx 1 3 2
subx 2 3 5
subx 3 3 8
subx 4 3 11
suby: Dy= 3 Dx= 4
suby 1 1 0
suby 2 1 1
suby 3 1 2
suby 4 1 3
suby 1 2 4
suby 2 2 5
suby 3 2 6
suby 4 2 7
suby 1 3 8
suby 2 3 9
suby 3 3 10
suby 4 3 11
subx: Dy= 6 Dx= 5
subx 1 1 0
subx 2 1 6
subx 3 1 12
subx 4 1 18
subx 5 1 24
subx 1 2 1
subx 2 2 7
subx 3 2 13
subx 4 2 19
subx 5 2 25
subx 1 3 2
subx 2 3 8
subx 3 3 14
subx 4 3 20
subx 5 3 26
subx 1 4 3
subx 2 4 9
subx 3 4 15
subx 4 4 21
subx 5 4 27
subx 1 5 4
subx 2 5 10
subx 3 5 16
subx 4 5 22
subx 5 5 28
subx 1 6 5
subx 2 6 11
subx 3 6 17
subx 4 6 23
subx 5 6 29
suby: Dy= 6 Dx= 5
suby 1 1 0
suby 2 1 1
suby 3 1 2
suby 4 1 3
suby 5 1 4
suby 1 2 5
suby 2 2 6
suby 3 2 7
suby 4 2 8
suby 5 2 9
suby 1 3 10
suby 2 3 11
suby 3 3 12
suby 4 3 13
suby 5 3 14
suby 1 4 15
suby 2 4 16
suby 3 4 17
suby 4 4 18
suby 5 4 19
suby 1 5 20
suby 2 5 21
suby 3 5 22
suby 4 5 23
suby 5 5 24
suby 1 6 25
suby 2 6 26
suby 3 6 27
suby 4 6 28
suby 5 6 29
Upvotes: 2