Creating indipendent random numbers on each core

Creating indipendent random numbers on each core

Postby isk » Mon Aug 18, 2014 2:21 pm

Hi,

I am trying to generate on each core independent and identically distributed random numbers using the rand() function.
The seed for the random number generator has to be set. For this purpose the program on the host send different random
numbers to each core, which calls srand() with this number.
Nevertheless the cores produce the same sequence of numbers when calling rand().

In order to demonstrate the problem, the result of this code
Code: Select all
   core_id=e_group_config.core_row+e_group_config.core_col*e_group_config.group_rows;
   e_barrier(barriers, tgt_bars);
   srand(core_id);
   e_barrier(barriers, tgt_bars);
   if (core_id==5) srand(1);
   e_barrier(barriers, tgt_bars);
   *BOx=rand();

is, that *BOx is equal for all cores and is the same sequence obtained by using srand(1).

The result of
Code: Select all
      core_id=e_group_config.core_row+e_group_config.core_col*e_group_config.group_rows;
      e_barrier(barriers, tgt_bars);
      srand(core_id);
      e_barrier(barriers, tgt_bars);
      if (core_id==5) srand(1);
      e_barrier(barriers, tgt_bars);
      if (core_id==5) *BOx=rand();
      *BOx=rand();

is, that only on core with core_id=5 the *BOx is different, the results of the other cores are equal.
Thus it is not possible to implement iid random numbers.

Perhaps somebody has an idea how to overcome this problem?

Thank you in advance
isk
Last edited by isk on Tue Aug 19, 2014 12:32 pm, edited 1 time in total.
isk
 
Posts: 4
Joined: Wed Aug 13, 2014 3:10 pm

Re: Creating indipendendt random numbers on each core

Postby notzed » Tue Aug 19, 2014 10:20 am

Check the binary and linker script to ensure that each has core it's own copy of the various state variables?

Personally I would just find something simple (and small) and good enough for the task and just include the code in your build instead. I needed one for some gpu/OpenCL code and found something suitable on the net with a quick search; can't remember which right now though.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: Creating indipendent random numbers on each core

Postby isk » Wed Aug 20, 2014 11:21 am

Tank you for the answer.
The reasons to post this question are:
  • srand() and rand() are part of the C standard library. Thus that could be a hint for other errors.
  • The available memory is not huge. Thus using the standard libraries is a method to reduce the
    program size.
  • Of course I use now my own random number generator. But it is work to verify that the code
    produces "good" random numbers.
isk
 
Posts: 4
Joined: Wed Aug 13, 2014 3:10 pm

Re: Creating indipendent random numbers on each core

Postby notzed » Wed Aug 20, 2014 12:24 pm

You'll almost certainly save memory by not using the C library because it wasn't designed for size but for standards compliance and portability. There is no shared library mechanism so you wont save a single byte by using a C library function over a replacement either - everything needs to be linked in statically. If you're talking about putting the C library into the shared memory (quite likely the root cause of your problem, as below) then you can also do that with any code you write yourself. It's just up to the linker script and sections you use. But it comes at such a performance penalty it should only be used for code that is very rarely executed.

On your last point thats why you search for someone who has done it already (like knuth) and use theirs? Do you even know if the one in the C library is any "good" or are you just going on trust? If you needed a particularly good PRNG you would have to validate the C library anyway.

As far as I know (but i could be wrong), right now the C library is simply broken if you put any writable storage into the shared memory area because for a given program they will all use the same linked C library instance (and variables). Last time I looked (IIRC, some time ago) the C library itself just isn't built with any support for multi-threading whatsoever. It works fine if every core has it's own private copy because then it isn't physically multi-threading.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: Creating indipendent random numbers on each core

Postby isk » Wed Aug 20, 2014 1:51 pm

Thank you. I was not aware that the standard library is placed in the external memory. Placing it in
the internal memory reduces the available memory considerably. Thus the only solution seems to use my own code.
isk
 
Posts: 4
Joined: Wed Aug 13, 2014 3:10 pm


Return to Scientific Computing

Who is online

Users browsing this forum: No registered users and 1 guest