Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:01:18+00:00 2026-06-07T12:01:18+00:00

Let us assume that we have the following strings that we need to store

  • 0

Let us assume that we have the following strings that we need to store in a CUDA array.

“hi there”

“this is”

“who is”

How do we declare a array on the GPU to do this. I tried using C++ strings but it does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:01:20+00:00Added an answer on June 7, 2026 at 12:01 pm

    Probably the best way to do this is to use structure that is similar to common compressed sparse matrix formats. Store the character data packed into a single piece of linear memory, then use a separate integer array to store the starting indices, and perhaps a third array to store the string lengths. The storage overhead of the latter might be more efficient that storing a string termination byte for every entry in the data and trying to parse for the terminator inside the GPU code.

    So you might have something like this:

    struct gpuStringArray {
        unsigned int * pos; 
        unsigned int * length;  // could be a smaller type if strings are short
        char4 * data; // 32 bit data type will improve memory throughput, could be 8 bit
    } 
    

    Note I used a char4 type for the string data; the vector type will give better memory throughput, but it will mean strings need to be aligned/suitably padded to 4 byte boundaries. That may or may not be a problem depending on what a typical real string looks like in your application. Also, the type of the (optional) length parameter should probably be chosen to reflect the maximum admissible string length. If you have a lot of very short strings, it might be worth using an 8 or 16 bit unsigned type for the lengths to save memory.


    A really simplistic code to compare strings stored this way in the style of strcmp might look something like this:

    __device__ __host__
    int cmp4(const char4 & c1, const char4 & c2)
    {
        int result;
    
        result = c1.x - c2.x; if (result !=0) return result; 
        result = c1.y - c2.y; if (result !=0) return result; 
        result = c1.z - c2.z; if (result !=0) return result; 
        result = c1.w - c2.w; if (result !=0) return result; 
    
        return 0;
    }
    
    __device__ __host__
    int strncmp4(const char4 * s1, const char4 * s2, const unsigned int nwords)
    {
        for(unsigned int i=0; i<nwords; i++) {
            int result = cmp4(s1[i], s2[i]);
            if (result != 0) return result;
        }
    
        return 0;
    }
    
    __global__
    void tkernel(const struct gpuStringArray a, const gpuStringArray b, int * result)
    {
        int idx = threadIdx.x + blockIdx.x * blockDim.x;
    
        char4 * s1 = a.data + a.pos[idx];
        char4 * s2 = b.data + b.pos[idx];
        unsigned int slen = min(a.length[idx], b.length[idx]);
    
        result[idx] = strncmp4(s1, s2, slen);
    }
    

    [disclaimer: never compiled, never tested, no warranty real or implied, use at your own risk]

    There are some corner cases and assumptions in this which might catch you out depending on exactly what the real strings in your code look like, but I will leave those as an exercise to the reader to resolve. You should be able to adapt and expand this into whatever it is you are trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding string modification. Let's assume that we have the following
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
Let's assume that I have mytable in mySQL CREATE TABLE `mytable` ( `gender` enum('MALE','FEMALE','UNISEX')
Let's assume that I have a collection in mongodb, where all of its documents
Let's assume that we have a vector like x = -1:0.05:1; ids = randperm(length(x));
(Let's assume that I have good reasons not to remove my NSAssert() checks in
Let's assume that I have files a.cpp b.cpp and file c.h. Both of the
Let's assume that we have a data frame x which contains the columns job
Let's assume that I have a complex hash reference $hash_ref , and I would
I have a question regarding static function in php. let's assume that I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.