source: CIVL/examples/translation/mpi/seq/ser_pi_calc.c@ 899207c

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 899207c was 5cca46e, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added CIVL pragmas.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1348 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 }
38printf("\nReal value of PI: 3.1415926535897 \n");
39}
40
41
42/*****************************************************************************
43 * dboard
44 *****************************************************************************/
45#define sqr(x) ((x)*(x))
46long random(void);
47
48double dboard(int darts)
49{
50 double x_coord, /* x coordinate, between -1 and 1 */
51 y_coord, /* y coordinate, between -1 and 1 */
52 pi, /* pi */
53 r; /* random number scaled between 0 and 1 */
54 int score, /* number of darts that hit circle */
55 n;
56 unsigned int cconst; /* must be 4-bytes in size */
57/*************************************************************************
58 * The cconst variable must be 4 bytes. We check this and bail if it is
59 * not the right size
60 ************************************************************************/
61 if (sizeof(cconst) != 4) {
62 printf("Wrong data size for cconst variable in dboard routine!\n");
63 printf("See comments in source file. Quitting.\n");
64 exit(1);
65 }
66 /* 2 bit shifted to MAX_RAND later used to scale random number between 0 and 1 */
67 cconst = 2 << (31 - 1);
68 score = 0;
69
70/***********************************************************************
71 * Throw darts at board. Done by generating random numbers
72 * between 0 and 1 and converting them to values for x and y
73 * coordinates and then testing to see if they "land" in
74 * the circle." If so, score is incremented. After throwing the
75 * specified number of darts, pi is calculated. The computed value
76 * of pi is returned as the value of this function, dboard.
77 ************************************************************************/
78
79 for (n = 1; n <= darts; n++) {
80 /* generate random numbers for x and y coordinates */
81 r = (double)random()/cconst;
82 x_coord = (2.0 * r) - 1.0;
83 r = (double)random()/cconst;
84 y_coord = (2.0 * r) - 1.0;
85
86 /* if dart lands in circle, increment score */
87 if ((sqr(x_coord) + sqr(y_coord)) <= 1.0)
88 score++;
89 }
90
91 /* calculate pi */
92 pi = 4.0 * (double)score/(double)darts;
93 return(pi);
94}
Note: See TracBrowser for help on using the repository browser.