source: CIVL/examples/MPI/ser_pi_calc.c@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b was e48510a, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

improved IO transformer; added a method for printing AST as CIVL-C code; added two examples for MPI transformer, still need to improve MPI transformer; added a simple CIVL implementation of stdlib.h.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1082 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[e48510a]1/***************************************************************************
2 * FILE: ser_pi_calc.c
3 * DESCRIPTION:
4 * Serial pi Calculation - C Version
5 * This program calculates pi using a "dartboard" algorithm. See
6 * Fox et al.(1988) Solving Problems on Concurrent Processors, vol.1
7 * page 207.
8 * AUTHOR: unknown
9 * REVISED: 02/23/12 Blaise Barney
10***************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13
14void srandom (unsigned seed);
15double dboard (int darts);
16
17#define DARTS 10000 /* number of throws at dartboard */
18#define ROUNDS 100 /* number of times "darts" is iterated */
19
20int main(int argc, char *argv[])
21{
22double pi; /* average of pi after "darts" is thrown */
23double avepi; /* average pi value for all iterations */
24int i, n;
25
26printf("Starting serial version of pi calculation using dartboard algorithm...\n");
27srandom (5); /* seed the random number generator */
28avepi = 0;
29for (i = 0; i < ROUNDS; i++) {
30 /* Perform pi calculation on serial processor */
31 pi = dboard(DARTS);
32 avepi = ((avepi * i) + pi)/(i + 1);
33 printf(" After %3d throws, average value of pi = %10.8f\n",
34 (DARTS * (i + 1)),avepi);
35 }
36printf("\nReal value of PI: 3.1415926535897 \n");
37}
38
39
40/*****************************************************************************
41 * dboard
42 *****************************************************************************/
43#define sqr(x) ((x)*(x))
44long random(void);
45
46double dboard(int darts)
47{
48 double x_coord, /* x coordinate, between -1 and 1 */
49 y_coord, /* y coordinate, between -1 and 1 */
50 pi, /* pi */
51 r; /* random number scaled between 0 and 1 */
52 int score, /* number of darts that hit circle */
53 n;
54 unsigned int cconst; /* must be 4-bytes in size */
55/*************************************************************************
56 * The cconst variable must be 4 bytes. We check this and bail if it is
57 * not the right size
58 ************************************************************************/
59 if (sizeof(cconst) != 4) {
60 printf("Wrong data size for cconst variable in dboard routine!\n");
61 printf("See comments in source file. Quitting.\n");
62 exit(1);
63 }
64 /* 2 bit shifted to MAX_RAND later used to scale random number between 0 and 1 */
65 cconst = 2 << (31 - 1);
66 score = 0;
67
68/***********************************************************************
69 * Throw darts at board. Done by generating random numbers
70 * between 0 and 1 and converting them to values for x and y
71 * coordinates and then testing to see if they "land" in
72 * the circle." If so, score is incremented. After throwing the
73 * specified number of darts, pi is calculated. The computed value
74 * of pi is returned as the value of this function, dboard.
75 ************************************************************************/
76
77 for (n = 1; n <= darts; n++) {
78 /* generate random numbers for x and y coordinates */
79 r = (double)random()/cconst;
80 x_coord = (2.0 * r) - 1.0;
81 r = (double)random()/cconst;
82 y_coord = (2.0 * r) - 1.0;
83
84 /* if dart lands in circle, increment score */
85 if ((sqr(x_coord) + sqr(y_coord)) <= 1.0)
86 score++;
87 }
88
89 /* calculate pi */
90 pi = 4.0 * (double)score/(double)darts;
91 return(pi);
92}
Note: See TracBrowser for help on using the repository browser.