1 / 11

Computer Science 320

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

ricky
Download Presentation

Computer Science 320

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 320 Random Numbers for Parallel Programs

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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!)

  8. 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

  9. 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

  10. Running Time of PiSmp2vs3 And the estimates of π for Seq3 and Smp3 are the same

  11. Efficiency of PiSmp2vs3 And the estimates of π for Seq3 and Smp3 are the same

More Related