source: CIVL/examples/mpi/seq/ser_pi_calc.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

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