将主机函数作为函数指针传递给 __global__ 或 __device__ 函数中的 CUDA [英] Passing Host Function as a function pointer in __global__ OR __device__ function in CUDA

查看:19
本文介绍了将主机函数作为函数指针传递给 __global__ 或 __device__ 函数中的 CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发 CPU 功能的 GPU 版本(例如函数 Calc(int a, int b, double* c, souble* d, CalcInvFunction GetInv )),其中宿主函数作为函数指针传递(例如,在上面的示例中 GetInv 是 CalcInvFunction 类型的宿主函数).我的问题是,如果我必须将 Calc() 函数完全放在 GPU 中,我必须将 GetInv 函数作为函数指针参数传递给设备函数/内核函数,这可能吗?

I am currently developing a GPU version of a CPU function (e.g. function Calc(int a, int b, double* c, souble* d, CalcInvFunction GetInv )), in which a host function is passes as a function pointer(e.g. in above example GetInv is the host function of CalcInvFunction type). My question is, if i have to put Calc() function entirely in GPU, i have to pass the GetInv function as a function pointer argument in device function/kernel function, and is that possible?

推荐答案

是的,对于 Calc 的 GPU 实现,您应该将 GetInv 作为 传递__device__ 函数指针.

Yes, for a GPU implementation of Calc, you should pass the GetInv as a __device__ function pointer.

有可能,这里有一些工作示例:

It is possible, here are some worked examples:

例如.1

例如.2

例如.3

上述大多数示例都演示了将设备函数指针一直返回到主机代码.对于您的特定情况,这可能不是必需的.但是从上面应该可以很明显地看出如何获取 __device__ 函数指针(在设备代码中)并在内核中使用它.

Most of the above examples demonstrate bringing the device function pointer all the way back to the host code. This may not be necessary for your particular case. But it should be fairly obvious from above how to grab a __device__ function pointer (in device code) and use it in a kernel.

这篇关于将主机函数作为函数指针传递给 __global__ 或 __device__ 函数中的 CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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