设备或主机均可调用的 CUDA 函数 [英] CUDA function call-able by either the device or host

查看:17
本文介绍了设备或主机均可调用的 CUDA 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些 CUDA 代码中有一个可重用的函数,需要从设备和主机调用.有合适的限定词吗?

I have a re-useable function in some CUDA code that needs to be called from both the device and the host. Is there an appropriate qualifier for this?

例如在这种情况下,func1 的正确定义是什么:

e.g. what's the correct definition for func1 in this case:

int func1 (int a, int b) {
    return a+b;
}

__global__ devicecode (float *A) {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    A[i] = func1(i,i);
}

void main() {
    // Normal cuda memory set-up

    // Call func1 from inside main:
    int j = func1(2,4)

    // Normal cuda memory copy / program run / retrieve data
}

到目前为止,我只能通过两次使用该功能来使其工作:一次明确用于设备,一次用于主机.有没有更好的办法?

So far I can only get this to work by having the function twice: once explicitly for the device and once for the host. Is there a better way?

推荐答案

来自 CUDA 编程指南:

From the CUDA Programming Guide:

__device____host__ 限定词可以一起使用,但是,在在这种情况下,函数会同时为主机和设备编译.

The __device__ and __host__ qualifiers can be used together however, in which case the function is compiled for both the host and the device.

这篇关于设备或主机均可调用的 CUDA 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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