- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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...

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page