jeudi 17 février 2022

OpenCL Pseudo random generator race condition

Using this question as basis I implemented a pseudo-random number generator with a global state:

  __global uint global_random_state;

  void set_random_seed(uint seed){
    global_random_state = seed;
  }

  uint get_random_number(uint range){
    uint seed = global_random_state + get_global_id(0);
    uint t = seed ^ (seed << 11);
    uint result = seed ^ (seed >> 19) ^ (t ^ (t >> 8));
    global_random_state = result; /* race condition? */
    return result % range;
  }

Since these functions will be used from multiple threads, there will be a race condition present when writing to global_random_state.

This might actually help the system to be more unpredictable, so it seems like a good thing, but I'd like to know if there are any consequences to this that might not surface immediately. Are there any side-effects inside the GPU which might cause problems later on when the kernel is run?




Aucun commentaire:

Enregistrer un commentaire