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 9157731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:05:33+00:00 2026-06-17T13:05:33+00:00

I spawn 1 block of 256 threads from my Setup() kernel to set up

  • 0

I spawn 1 block of 256 threads from my Setup() kernel to set up an array RNGstates with 256 CURAND states:

__global__ void Setup(curandState *RNGstates, long seed) {
    int tid = threadIdx.x;
    curand_init(seed, tid, 0, &RNGstates[tid]);
}

Now, I spawn 1000 blocks of 256 threads from my Generate() kernel to fill array result with 256,000 random numbers. However, I do so using only the 256 states of RNGstates, such that each state will be accessed by 1000 threads (one from each block):

__global__ void Generate(curandState *RNGstates, float *result) {
    int tid = blockIdx.x*blockDim.x + threadIdx.x;
    float rnd = curand_uniform(&RNGstates[threadIdx.x]);
    result[tid] = rnd;
}

I know that calling curand_uniform() updates the states somehow, so I presume some write operation is taking place.

So should I be worried about data races occuring when the 1000 threads mapped to each of the 256 CURAND states try to update the state implicitly through curand_uniform()? Will this impact the quality of my random numbers (e.g. get frequent duplicate values)?

Many thanks.

  • 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-17T13:05:35+00:00Added an answer on June 17, 2026 at 1:05 pm

    I think sharing states will definitely impact the quality. Duplicate values are the best situation for sharing states. Data race could totally ruin the states.

    You could keep one state for each of your threads.

    When using 1000 blocks, 256,000 states are required for your case. The code should be like

    __global__ void Setup(curandState *RNGstates, long seed) {
      int tid = blockIdx.x*blockDim.x + threadIdx.x;
      curand_init(seed, tid, 0, &RNGstates[tid]);
    }
    

    and

    __global__ void Generate(curandState *RNGstates, float *result) {
      int tid = blockIdx.x*blockDim.x + threadIdx.x;
      float rnd = curand_uniform(&RNGstates[tid]);
      result[tid] = rnd;
    }
    

    To reduce mem requirement for multiple blocks, you could limit your #block to a small number, and generate multiple random numbers per thread, instead of 1 random number per thread.

    __global__ void generate_uniform_kernel(curandState *state, 
                                    unsigned int *result)
    {
        int id = threadIdx.x + blockIdx.x * 64;
        unsigned int count = 0;
        float x;
        /* Copy state to local memory for efficiency */
        curandState localState = state[id];
        /* Generate pseudo-random uniforms */
        for(int n = 0; n < 10000; n++) {
            x = curand_uniform(&localState);
            /* Check if > .5 */
            if(x > .5) {
                count++;
            }
        }
        /* Copy state back to global memory */
        state[id] = localState;
        /* Store results */
        result[id] += count;
    }
    

    See the section Device API Examples in cuRAND ref manual for complete examples on how to deal with mutiple blocks.

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

Sidebar

Related Questions

I need to spawn N consumer threads, which process same InputStream concurrently, e.g -
If I spawn a thread via AsyncTask from the UI thread, is this thread
I would like to spawn a Windows form from the console using C#. Roughly
I'm trying to spawn a new process from my C++-project using fork-exec. I'm using
GOAL:spawn a few greenlet worker deal with the data pop from redis (pop from
ex. <Form> <input type=text/> <span style=display block>your text should be between 4-8 chars</span> </form>
I have a container DIV which contains several block-DIVS. Every block-DIV contains SPAN items
I have the following div <body> <span style=border:1px solid red; display:inline-block> Some text<br />
Our WCF services occaisionally spawn a worker thread to handle something that the client
I am reading The thread building block book. I do not understand this piece

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.