0

I am trying to create a Random generator for initialization of a vector.

The kernel function that I wrote in CUDA is as below:

__global__ void randgenvec( int *a, int t) {
    int tid = blockIdx.x; // handle the data at this index
    if (tid < t){
        a[tid] = rand() % 100; //****ang replacement for rand() here expect cuRAND?*****
    }
}

I got to know that I cant use rand() function inside a kernel function. Is there any other function which I can replace with rand().

I know we have cuRAND library which we can use. But want to make my code more portable and light and cuRAND needs linker for execution, which I don't want to use.

The objective of my randomisation is to generate any int between 1 to 100.

By linker, I mean :: linker flag -lcurand

Any other suggestion much appreciated.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Alankrit
  • 658
  • 1
  • 6
  • 17
  • What do you mean you need linker for execution? If you're using CUDA, you're already linking Nvidia libraries and drivers in your project. – Kaldrr Nov 18 '20 at 08:02
  • Hi @Kaldrr, By linker I mean - `linker flag -lcurand`. I have edited my question more clearly. – Alankrit Nov 18 '20 at 08:46
  • 1
    Please do not vote to reopen this. The linked duplicate is a good answer – talonmies Nov 18 '20 at 12:05

1 Answers1

-2

attempt this

 unsigned short lfsr = 0xACE1u;
  unsigned bit;

  unsigned randomize()
  {
    bit  = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
    return lfsr =  (lfsr >> 1) | (bit << 15);
  }
  • Hi @MaslovKk, thanks for your answer. I am looking to generate numbers between 1 to 100. will code work if I do `randomize()%100` ? – Alankrit Nov 18 '20 at 08:30
  • 1
    No, don't attempt this. It isn't thread safe and won't work correctly in a massively parallel environment like CUDA, which it the question – talonmies Nov 18 '20 at 09:42
  • @talonmies, can you suggest me some other functions I can use for randomization? – Alankrit Nov 18 '20 at 10:04
  • Yes. Use curand. High quality random numbers are difficult to get right. Doubly so in parallel. Use the library designed for the purpose. Your rationale for not doing so makes no sense – talonmies Nov 18 '20 at 12:05
  • Okay I have decided to use cuRAND, but I am not able to figureout how to generate random numbers between 0 and 100 with cuRAND. – Alankrit Nov 18 '20 at 15:16