我们可以在内核中使用C ++中的字符串数据类型 [英] Can we use the string data type in C++ within kernels

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

问题描述

我在写一个CUDA内核,我使用C ++中的 string 数据类型。然而,编译器抛出以下错误:

I am writing a CUDA kernel is which I am 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 work around to allocate space for a char array within the kernel

推荐答案

c $ 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 ++中的字符串数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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