如何在代码C中为类型void ** ptr创建Fortran接口 [英] How to create Fortran interface for type void ** ptr in code C

查看:634
本文介绍了如何在代码C中为类型void ** ptr创建Fortran接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的使用Fortran,并为ac功能如下:

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

我想创建一个Fortran接口来使用这个c函数,但是如何修复void ** ptr?谁能帮助我?提前感谢!

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!

推荐答案

我不知道它是否会正常工作与设备指针(即,如果 cudaMalloc 可以从非CUDA C)调用,但一般在Fortran-C互操作性中,你表示 void * as 类型(c_ptr) iso_c_binding 模块。 C可互操作的过程默认通过引用传递它们的参数,所以这应该工作:

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

指针通过引用传递,以便C侧看到一个指向指针的指针,并且可以改变它来存储

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.

这篇关于如何在代码C中为类型void ** ptr创建Fortran接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆