GPU设备函数如何访问主机功能中定义的对象
How can the GPU device function access class objects defined in host functions?
我有一个现有的C 程序,我想将其迁移到GPU版本。内核功能需要访问主机函数中定义的类对象。例如,线程中将使用stringstream
对象。但是,它无法通过CUDA中的编译。内核函数如何访问主机函数中定义的类对象?
这是一个例子。
#include <cstdio>
#include <sstream>
using namespace std;
__global__ void kernel(stringstream * sstr)
{
printf("%sn", sstr->str());
}
int main(int argc, char ** argv)
{
stringstream * sstr;
cudaMallocManaged(&sstr, sizeof(stringstream));
*sstr << "Hello worldn";
kernel<<<32, 32>>>(sstr);
cudaDeviceSynchronize();
cudaFree(sstr);
return 0;
}
我有以下编译错误。
$ nvcc -o bin src.cu
src.cu(8): warning: non-POD class type passed through ellipsis
src.cu(8): error: calling a __host__ function("std::__cxx11::basic_stringstream<char, ::std::char_traits<char> , std::allocator<char> > ::str const") from a __global__ function("kernel") is not allowed
src.cu(8): error: identifier "std::__cxx11::basic_stringstream<char, ::std::char_traits<char> , std::allocator<char> > ::str const" is undefined in device code
src.cu(8): error: calling a __host__ function("std::__cxx11::basic_string<char, ::std::char_traits<char> , std::allocator<char> > ::~basic_string") from a __global__ function("kernel") is not allowed
src.cu(8): error: identifier "std::__cxx11::basic_string<char, ::std::char_traits<char> , std::allocator<char> > ::~basic_string" is undefined in device code
4 errors detected in the compilation of "/tmp/tmpxft_00003bd0_00000000-8_src.cpp1.ii".
您不应在内核内使用C STD类,因为std :: stringstream相关功能已预先编译并从OS链接,NVCC不会生成相应的__device__
功能。p>请参阅此主题
std::stringstream
可能具有动态分配的数组,您将无法在设备代码中访问。这已经使将这些课程传递给GPU是个坏主意。
您的汇编失败了,因为您还尝试从设备代码调用__host__
函数,这是不可能的。如果您想起作用,您可能需要将自定义stringstream
适用于CUDA。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 为什么以下 POP 功能无法在主机或设备 (CUDA) 上运行?
- GPU设备函数如何访问主机功能中定义的对象
- 在 CUDA9 中,"cudaMemcpyAsync()"既是设备又是主机功能吗?
- 设置主机和执行功能有什么区别