New Contributed Code for Cilk™ Plus: DotMix, a Deterministic Parallel Random-Number Generator

If you have been browsing around http://cilkplus.org recently, you may have stumbled across some new code available for download. At the end of 2012, we posted a new download under the Contributed Code section: DotMix, a deterministic parallel random-number generator for Intel® Cilk™ Plus. DotMix utilizes pedigrees, a new feature of Intel Cilk Plus, to provide programmers with a repeatable but efficient way to generate pseudorandom numbers in parallel. 

After reading this short description, you might have more questions than answers. What does "contributed code" mean, and where does it come from? What is a "deterministic parallel random-number generator," and why might we want one for Cilk Plus? Finally, what on earth are pedigrees? Over the next few weeks, I hope to answer these questions, and possibly motivate some of you to contribute your own Cilk Plus code. In today’s post, I will address the first of these questions by telling you more about contributed code.



Contributed Code


The Contributed Code section provides a convenient way for users of Cilk Plus to share their code with the larger Cilk Plus community. As a member of the Cilk Plus runtime development team, I am one of a number of Intel employees who help create and maintain the larger collection of materials on the Downloads page. These materials include Cilk Plus tools, sample programs, source code, and documentation. We are not, however, the only ones who generate content that may be useful to Cilk Plus programmers. We welcome contributions from others in community at large. Code which is submitted through our contribution page, and makes it through a review process will appear in the Contributed Code section.

Just so I have the appropriate caveats in place, this review process is not intended to be comprehensive code review, nor does it imply that Intel assumes any responsibility for supporting or maintaining any contributed code. Individual contributors may choose to help support code at their own discretion. In my personal opinion, the review process is akin to moderation in an Internet forum: it keeps the obvious spam out, but does not otherwise provide any guarantees on the quality of content. :) 

Until now, code contributions have largely come from within the Cilk Plus runtime development team. To start the ball rolling on contributed code originating from outside Intel, I have submitted DotMix, code developed by Tao B. Schardl and myself, with input from Charles E. Leiserson. DotMix is code for deterministic parallel random-number generation that comes out of some recent research done at MIT.   For those of you who are not familiar with our project’s history, Cilk Plus is a descendant of the Cilk project from MIT in the mid 90's. Since that time, there has been a long tradition of Cilk-related research coming out of MIT, particularly from the SuperTech research group in MIT CSAIL, headed by Charles E. Leiserson. Before joining Intel, I was an MIT student, member of the SuperTech group, and a frequent user of Cilk technologies. During my time at MIT, I had the opportunity to work on the research project that eventually led to our paper and DotMix.

Next time, I will tell you more of the story of DotMix, and explain why deterministic parallel random-number generation turns out to be an interesting problem in Cilk Plus. For today, I’ll present a simple example showing how you can use DotMix in a simple program.

#include <cstdio>
#include <cilk/cilk.h>
#include <cilk/reducer_opadd.h>
#include <cilkpub/dotmix.h>      // Include file for DotMix

// Global reducer to add up random numbers.
cilk::reducer_opadd<uint64_t> sum(0);

// Create and seed a global DotMix RNG.
cilkpub::DotMix global_rng(234909128); 

int rand_fib(int n) {
   if (n < 2) {
      // Generate a random number, add it to sum.
      sum += global_rng.get() * n;
      return n;
   }
   else {
      int x, y;
      sum += global_rng.get() * n;
      x = cilk_spawn rand_fib(n-1);
      sum += global_rng.get() * n;
      y = rand_fib(n-2);
      cilk_sync;
      sum += global_rng.get() * n;
      return x+y;
  }
}

int main(void) {
   int n = 30;
   int ans = rand_fib(n);
   std::printf("fib(%d) = %d\n", n, ans);
   std::printf("sum is %llu\n", sum.get_value());
   return 0;
}

In the program above, the rand_fib() method computes the nth Fibonacci number, but also uses DotMix to generate random numbers during the computation. Each invocation of fib(n) multiplies the random numbers it generates by n (as a proxy for a more interesting computation), and then adds the result into a reducer sum. The code online has also been recently updated to DotMix Version 1.01, which includes the sample program shown above as samples/sample_rand_fib.cpp.

NOTE:  You will need a compiler that implements Cilk Plus ABI 1 to use DotMix, because DotMix utilizes pedigrees, a new feature introduced for ABI 1. Cilk Plus ABI 1 is described in the ABI document version 1.1.    Intel® Composer XE 2013 does support ABI 1, but as of today (2013-03-08), the Cilk Plus branch of GCC currently supports only ABI 0.

The DotMix random-number generator has two main advantages over a typical random-number generator such as rand(). First, because DotMix is designed as a parallel random-number generator, it does not have the problems with cache contention on shared state or locks that a traditional serial generator would have. Second, because DotMix is designed specifically for Cilk Plus, its behavior is deterministic for many Cilk Plus programs. More precisely, in many programs, when using the same seed and program input, DotMix will generate the same set of random numbers in every run of the program, even if that program executes in parallel on multiple Cilk Plus worker threads.   Stay tuned — I will discuss these two advantages in greater detail next time.

Summary


The DotMix DPRNG is the first of what I hope will eventually be many user-contributed codes available for download on the website. If this code seems like it might be useful for you, download it, try it out, and provide us with feedback. Also, if you have your own Cilk Plus code that you would like to share, please consider submitting your contribution!

For more information about Intel Cilk Plus, see the website http://cilkplus.org. For questions and discussions about Intel Cilk Plus, see the forum http://software.intel.com/en-us/forums/intel-cilk-plus.

Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.