[Omp] initialize a array with rundom numbers in a parallel loop
Mattson, Timothy G
timothy.g.mattson at intel.com
Thu Jan 5 12:50:10 PST 2006
Oh dear, I have a ton of work to do and need to be good. I tried so
hard to stay out of this. But I just can't any longer.
A reentrant generator only solves part of the problem. It still
doesn't guarantee that you will get a good sequence of random numbers.
(even if you fix the race condition Larry mentioned).
Look, you have to think of random number generators in the context of
how they are used. What you are doing is generating sequences of
pseudo-random numbers. Since you are generating them in a piece of
software, they are not truly random. Now each thread will happily go
off and generate pseudorandom numbers. If you have a single seed, each
thread will generate the same sequence. So instead of one long sequence
of pseudo-random numbers, you will have multiple copies of the same
sequence. If you give each thread its own seed, you'll get different
sequences for each thread, but you have no guarantee that those
sequences won't overlap.
This is a vital issue. You use pseudo random numbers to sample a
domain. If the pseudorandom numbers overlap, then you are over sampling
subdomains and screwing up your statistics.
So it takes a bit of extra effort, but you MUST use one of those fancy
parallel generators that address this problem. That's what the SPRNG
package that Larry mentioned takes care of for you. It will also give
you a reproducible sequence in parallel ... which is very nice if you
want to test your software.
--Tim
-----Original Message-----
From: Omp-bounces at openmp.org [mailto:Omp-bounces at openmp.org] On Behalf
Of Meadows, Lawrence F
Sent: Thursday, January 05, 2006 11:35 AM
To: Priya Unnikrishnan; Omp at openmp.org
Subject: Re: [Omp] initialize a array with rundom numbers in a parallel
loop
Looks a lot like a race condition to me -- seed is a shared variable.
Parallel random numbers are non-trivial
>-----Original Message-----
>From: Omp-bounces at openmp.org [mailto:Omp-bounces at openmp.org]
>On Behalf Of Priya Unnikrishnan
>Sent: Thursday, January 05, 2006 10:50 AM
>To: Omp at openmp.org
>Subject: [Omp] initialize a array with rundom numbers in a
>parallel loop
>
>You can use rand_r - a reentrant pseudo-random integer
>generator.
>
>#include<stdio.h>
>#include<stdlib.h>
>
>int main(){
> int a[1000],i;
> unsigned int seed=5;
>
># pragma omp parallel for
> for(i=0;i<999;i++){
> a[i] = rand_r(&seed);
> }
>
>
>Lado Kumsiashvili herrlado at arcor.de
>Wed Jan 4 17:57:31 EST 2006
>
>Hallo!
>
>To solve my excersize i have to initialize a array
>with random numbers
>in a parallel loop(for). But i found out, that
>random() function from
><stdlib.h> not threadsafe ist. Is there any better
>Solution ?
>
>Lado
>
>
>
>--
>
>
>
>-----------------------------------
> Lado Kumsiashvili
>lado at student.uni-kassel.de
>-----------------------------------
>
>
>
>
>
>
>__________________________________________
>Yahoo! DSL - Something to write home about.
>Just $16.99/mo. or less.
>dsl.yahoo.com
>
>
>_______________________________________________
>Omp mailing list
>Omp at openmp.org
>http://openmp.org/mailman/listinfo/omp_openmp.org
>
_______________________________________________
Omp mailing list
Omp at openmp.org
http://openmp.org/mailman/listinfo/omp_openmp.org
More information about the Omp
mailing list