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

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

问题描述

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

解决方案

是的,对于 Calc 的GPU实现, GetInv 作为 __ device __ 函数指针。



是可能的,下面是一些工作示例:



例如。 1



Ex。 2



例如, 3



上述大部分示例演示将设备函数指针一直返回到主机代码 。这可能不是您的特定情况所必需的。但是从上面应该是相当明显的,如何获取 __ device __ 函数指针(在设备代码中),并在内核中使用它。


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?

解决方案

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:

Ex. 1

Ex. 2

Ex. 3

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.

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

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