如何调用CUDA内核中的主机函数? [英] How to call a host function in a CUDA kernel?

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

问题描述

正如以下错误所暗示的,在内核中不允许调用主机函数('rand'),我不知道是否有解决方案,如果我需要这样做。

 错误:不允许从__device __ / __ global__函数(xS_v1_cuda)调用主机函数(rand)
__ device__的函数。

修饰符。如果您在设备代码中需要随机数字,请查看cuda随机生成器 curand http://developer.nvidia.com/ curand



如果您有自己的主机函数要从内核调用使用 __ host __ __ device __ 修饰符:

  __ host__ __device__ int add int a,int b)
{
return a + b;
}



当此文件由NVCC编译器驱动程序编译时,编译:一个可由主机代码调用,另一个可由设备代码调用。这就是为什么现在可以通过主机和设备代码调用此函数。


As the following error implies, calling a host function ('rand') is not allowed in kernel, and I wonder whether there is a solution for it if I do need to do that.

error: calling a host function("rand") from a __device__/__global__ function("xS_v1_cuda") is not allowed

解决方案

Unfortunately you can not call functions in device that are not specified with __device__ modifier. If you need in random numbers in device code look at cuda random generator curand http://developer.nvidia.com/curand

If you have your own host function that you want to call from a kernel use both the __host__ and __device__ modifiers on it:

__host__ __device__ int add( int a, int b )
{
    return a + b;
}

When this file is compiled by the NVCC compiler driver, two versions of the functions are compiled: one callable by host code and another callable by device code. And this is why this function can now be called both by host and device code.

这篇关于如何调用CUDA内核中的主机函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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