在cuda中使用cpu函数 [英] Use cpu function in cuda

查看:1129
本文介绍了在cuda中使用cpu函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CUDA内核中包含一个C ++函数,但是这个函数是这样写的:

I would like to include a C++ function in a CUDA Kernel, but this function is written for CPU like this:

inline float random(int rangeMin,int rangeMax){
    return rand(rangeMin,rangeMax);
}

假设rand()函数使用curand.h推力cuda库。

Assume that the rand() function use either curand.h or Thrust cuda library.

我以为在使用一个内核函数(只有一个GPU线程) cuda编译器会为GPU生成二进制文件。

这是可能吗?如果是这样,我想在CUDA内核函数中包含为cpu编写的另一个内联函数。

类似这样:

- InlineCpuFunction.h和InlineCpuFunction.cpp

-- InlineCpuFunction.h and InlineCpuFunction.cpp

- CudaKernel.cuh和CudaKernel.cu它是CUDA内核中的函数)

-- CudaKernel.cuh and CudaKernel.cu (this one include the above header and uses it's function in the CUDA kernel)

如果你需要一些更多的解释(因为这可能看起来很混乱)。请问我。

If you need some more explanation (as this may look confusing) please ask me.

推荐答案

您可以使用 __ host__ __device __ 装饰器在设备和主机上标记要使用的函数它是为你的cpu和gpu编译的。

You can tag the functions you want to use on both the device and the host with both the __host__ __device__ decorators that way it's compiled for your cpu and gpu.

这篇关于在cuda中使用cpu函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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