Managing device memory in C and passing pointers in Fortran


I am trying to call several C functions from my Fortran code which allocate device memory and then call CUDA C kernels. In order to pass the data between C functions I tried to declare pointers in Fortran of type c_devptr. I seem to have got myself in a real pointer mess with this and was wondering if anyone has done this before? If so could they share a quick example of how they did it?



I think you are making it a lot harder than it needs to be:

Fortran will pass assumed size arrays (using F77 calling conventions) just like C expects them. You can also pass arguments by value:

%> cat
// Kernel that executes on the CUDA device

extern “C” global void square_array(float *a, int N)
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N)
a[idx] = a[idx]a[idx];

%> cat z1.cuf
program t
use cudafor
attributes(global) subroutine square_array(a, n) bind©
real, device :: a(
integer, value :: n
end subroutine
end interface
real, allocatable, device :: a(:)
a = 2.0
call square_array<<<1,10>>> (a, 10)
print *,sum(a)

%> nvcc -c
%> pgf90 z1.cuf t1.o

%> ./a.out