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

查看:363
本文介绍了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编程指南:

__ 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天全站免登陆