4

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

Are strings not allowed within a kernel? if not, what is the workaround to allocate space for a char array within a kernel?

6
  • Use C strings in kernel functions Commented Mar 28, 2012 at 15:26
  • @PaulR: Assume i am passing an array of C++ strings to my kernel. CUDA does not complain about this. How do I cast this into a C string within the kernel. Please give example Commented Mar 28, 2012 at 15:32
  • You just need to pass the underlying data to the kernel - use the string::c_str() method to extract pointers to the C data in the host code and pass these to the kernel function Commented Mar 28, 2012 at 15:44
  • 1
    Paul: That is not enough. The underlying char array (c_str) needs to be copied to device memory. Commented Mar 28, 2012 at 16:26
  • @Ashwin: Can you please elaborate on this with an example. I have no idea what Paul said Commented Mar 29, 2012 at 4:19

1 Answer 1

10

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.