How to create Fortran interface for type void ** ptr in code C

余生颓废 提交于 2019-12-13 11:26:21

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/30481686/how-to-create-fortran-interface-for-type-void-ptr-in-code-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!