/* tab:4 * * pi.sc - A simple split-c example which computes pi. * * "Copyright (c) 1993 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Version: 9 * Creation Date: Fri Jan 22 17:01:51 1993 * Filename: pi.sc * History: * LTL 8 Wed Aug 2 23:08:30 1995 * Used drand48() instead of rand() to get better results */ static char _version_string_[] = "\nVersion:8:pi.sc\0\n"; #include #include #include #include #include #include int hit() { double x = (double) drand48(); double y = (double) drand48(); if ((x*x+y*y) <= 1.0) return(1); else return(0); } /* ----------------------- splitc_main -------------------- */ splitc_main(int argc, char **argv) { int i, total_hits, hits = 0; double pi; int trials, my_trials; /* Set the number of total iterations to compute pi */ if (argc != 2) trials = 1000000; else trials = atoi(argv[1]); /* Calculate how many trials this processor must compute */ my_trials = (trials + PROCS - 1 - 2*MYPROC)/PROCS; /* Different seed on each processor */ srand48(MYPROC*17); /* Compute the number of hits on this processor */ for (i=0; i < my_trials; i++) hits += hit(); /* * Add up the number of hits across the processors. Note that * only one processor receives this sum. */ total_hits = all_reduce_to_one_add(hits); /* One processor displays the calculated value of pi */ on_one { pi = 4.0*total_hits/trials; printf("PI estimated at %f from %d trials on %d processors.\n", pi, trials, PROCS); } }