Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29280 Discussions

passing multidimensional arrays from Fortran to C

rreis
New Contributor I
1,430 Views
Hi

I need to program some algorithms in C from a Fortran code and I'm just starting my first steps. First stumble was how can I pass a multidimensional array to a C function, don't knowing apriori (compile time) the arrays dimensions?

For instance, in Fortran side:


subroutine cu_calc_sim_ten_prod(&
a, tena, tenb, res, ni,nj,nk) &
bind (c, name='cu_calc_sim_ten_prod')

use, intrinsic :: iso_c_binding

implicit none

integer(c_int), value, intent(in) :: ni,nj,nk
real(c_float), value, intent(in) :: a
real(c_float), dimension(ni,nj,nk,6), intent(in) :: tena, tenb
real(c_float), dimension(ni,nj,nk), intent(out) :: res

end subroutine cu_calc_sim_ten_prod

C function prototype ?

void cu_calc_sim_ten_prod(float a, float **** tena, float **** tenb,
float *** res,
int ni, int nj, int nk);



Or should I pass my arrays as pointers (using c_loc) and using pointer arithmetic on the other side... ?

thanks for your help
0 Kudos
3 Replies
rreis
New Contributor I
1,430 Views
I think I can settle with

subroutine cu_calc_sim_ten_prod(&
a, tena, tenb, res, ni,nj,nk) &
bind (c, name='cu_calc_sim_ten_prod')

use, intrinsic :: iso_c_binding

implicit none

integer(c_int), value, intent(in) :: ni,nj,nk
real(c_float), value, intent(in) :: a
real(c_float), dimension(ni*nj*nk*6), intent(in) :: tena, tenb
real(c_float), dimension(ni*nj*nk), intent(out) :: res

end subroutine cu_calc_sim_ten_prod

but was wondering if there is a better way...

thanks for your trouble
0 Kudos
Steven_L_Intel1
Employee
1,430 Views

You can use dimension(*) in the interface for the Fortran side. However, my understanding is that C multidimensional arrays are arrays of pointers and don't map to Fortran arrays. If you pass the array as dimension(*), then the address of the first element will be passed. How you deal with that in C is up to you.
0 Kudos
rreis
New Contributor I
1,430 Views

You can use dimension(*) in the interface for the Fortran side. However, my understanding is that C multidimensional arrays are arrays of pointers and don't map to Fortran arrays. If you pass the array as dimension(*), then the address of the first element will be passed. How you deal with that in C is up to you.

Thanks Steve. In the end I've just used my arrays as a big unidimensional one, using a pointer array to shortcut some pointer arithematic...
0 Kudos
Reply