-4

I am new to use Fortran, and for a c function like below:

cudaError_t cudaMalloc  (void** devPtr, size_t size)

Allocates size bytes of linear memory on the device and returns in *devPtr a pointer to the allocated memory. The allocated memory is suitably aligned for any kind of variable. The memory is not cleared. cudaMalloc() returns cudaErrorMemoryAllocation in case of failure.

Parameters:
devPtr  - Pointer to allocated device memory
size    - Requested allocation size in bytes

Returns:
cudaSuccess, cudaErrorMemoryAllocation

I want to create an Fortran interface to use this c function but how to fix void** ptr? Can anyone help me? Thanks in advance!

3
  • What did you try so far? Commented May 27, 2015 at 11:56
  • Why is this tagged C ? Commented May 27, 2015 at 11:59
  • This may be of interest. Commented May 27, 2015 at 13:16

1 Answer 1

2

I have no idea whether it will work OK with the device pointers (i.e., if cudaMalloc is callable from non-CUDA C), but generally in Fortran-C interoperability you represent void* as type(c_ptr) from the iso_c_binding module. C interoperable procedures pass their arguments by reference by default, so this should work:

integer(c_int) function cudaMalloc(devPtr, size) bind(C,name="cudaMalloc")
  use iso_c_binding
  type(c_ptr) :: devPtr
  integer(c_size_t), value :: size
end function

The pointer is passed by reference, so that the C side sees a pointer to pointer and can change it to store the value of the device pointer.

With some more work you can also define the enumeration with the return codes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.