字符串数据类型可以在C ++ CUDA内核中使用吗? [英] Can string data types be used in C++ CUDA kernels?

查看:544
本文介绍了字符串数据类型可以在C ++ CUDA内核中使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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不包含可以在GPU上运行的C ++ String库的设备版本。即使可以在内核中使用 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屋!

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