110 likes | 210 Views
Computer Science 320. Random Numbers for Parallel Programs. Problems with PRNGs. Per-thread PRNGs generate the same sequence of random numbers when given the same seed
E N D
Computer Science 320 Random Numbers for Parallel Programs
Problems with PRNGs Per-thread PRNGs generate the same sequence of random numbers when given the same seed The result is that there are N / K unique numbers rather than N unique numbers (actually, there are 2 * N numbers in the example program, because we’re generating coordinates of points)
Independent Sequences Use a different seed for each PRNG to get different starting points But if N is large enough, some numbers might overlap It’s also very unlikely that the data sets of the sequential and parallel programs will be the same
Leapfrogging Use the same seed, but have each thread I skip over I random numbers (thread 0 doesn’t skip, thread 1 skips one number, etc.) Then, each thread tells its PRNG to skip over K – 1 numbers, the ones that the other threads are generating The data sets of the sequential and parallel programs will be the same
Leapfrogging Use the same seed, but have each thread I skip over I random numbers (thread 0 doesn’t skip, thread 1 skips one number, etc.) Then, each thread tells its PRNG to skip over K – 1 numbers, the ones that the other threads are generating The data sets of the sequential and parallel programs will be the same In our example, the pairs of random numbers require skipping 2(K – 1) random numbers
Sequence Splitting Use the same seed, but have each thread I skip over I * N / K random numbers (thread 0 doesn’t skip, thread 1 skips N / K numbers, etc.) Then, each thread generates random numbers without any skipping The data sets of the sequential and parallel programs will be the same
Tradeoffs of Strategies • Skipping may take more time than generating the next number, so if you don’t need the same data sets for different runs, use independent sequence • Leapfrogging requires more running time than sequence splitting • Sequence splitting requires knowing N ahead of time • Must have an efficient skip operation (not linear with the distance of the skip!)
Parallel PRNG Resource • The class edu.rit.util.Random supports fast skipping and the generation of values of type double, float, int, and boolean • Not multiple-thread safe, because intended for per-thread PRNGs; synchronization overhead goes away! import edu.rit.util.Random; Random prng = Random.getInstance(104556); // Instantiate prng.skip(50); // Skip 50 numbers prng.nextInt(); // Get the next integer prng.nextInt(2); // Skip 2 and get the next
new ParallelTeam().execute (new ParallelRegion(){ public void run() throws Exception{ execute (0, N-1, new LongForLoop(){ // Set up per-thread PRNG and counter. Random prng_thread = Random.getInstance(seed); long count_thread = 0; // Extra padding to avert cache interference. long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7; long pad8, pad9, pada, padb, padc, padd, pade, padf; // Parallel loop body. public void run (long first, long last){ // Skip PRNG ahead to index <first> prng_thread.setSeed(seed); prng_thread.skip(2 * first); // Generate random points. for (long i = first; i <= last; ++ i){ double x = prng_thread.nextDouble(); double y = prng_thread.nextDouble(); if (x * x + y * y <= 1.0) ++ count_thread; } } Parallel Program PiSmp3
Running Time of PiSmp2vs3 And the estimates of π for Seq3 and Smp3 are the same
Efficiency of PiSmp2vs3 And the estimates of π for Seq3 and Smp3 are the same