可以在 C++ CUDA 内核中使用字符串数据类型吗? [英] Can string data types be used in C++ CUDA kernels?
问题描述
我正在编写一个 CUDA 内核,我在其中使用 C++ 中的 string
数据类型.但是,编译器抛出以下错误:
I am writing a CUDA kernel in which I'm using the string
data type in C++. However, the compiler is throwing the following error :
error: calling a host function("std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator =") from a __device__/__global__ function("doDecompression") is not allowed
内核中不允许使用字符串吗?如果没有,在内核中为 char 数组分配空间的解决方法是什么?
Are strings not allowed within a kernel? if not, what is the workaround to allocate space for a char array within a kernel?
推荐答案
您不能在内核中使用 C++ string
类型,因为 CUDA 不包含 C++ String 库的设备版本能够在 GPU 上运行.即使可以在内核中使用 string
,这也不是您想要做的事情,因为 string
动态处理内存,这可能会很慢.
You cannot use the C++ string
type in a kernel because CUDA does not include a device version of the C++ String library that would be able run on the GPU. Even if it was possible to use string
in a kernel, it's not something you would want to do because string
handles memory dynamically, which would be likely to be slow.
相反,创建一个固定长度的字符串数组并将字符串复制到其中.然后将数组复制到 GPU.将字符串数组的基地址传递给内核,并让每个线程通过将基于其索引的偏移量添加到基地址来计算给定字符串的地址.
Instead, create an array of fixed length strings and copy the strings to it. Then copy the array to the GPU. Pass the base address of the array of strings to your kernel and have each thread calculate the address to a given string by adding an offset based on its index to the base address.
这篇关于可以在 C++ CUDA 内核中使用字符串数据类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!