source: examples/cpi.c

Last change on this file was 9ab5fd, checked in by Pavan Balaji <balaji@…>, 6 months ago

White space cleanup

Includes manual cleanup and script cleanup.

No reviewer.

Co-authored-by: Pavan Balaji <balaji@…>

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2/*
3 *  (C) 2001 by Argonne National Laboratory.
4 *      See COPYRIGHT in top-level directory.
5 */
6
7#include "mpi.h"
8#include <stdio.h>
9#include <math.h>
10
11double f(double);
12
13double f(double a)
14{
15    return (4.0 / (1.0 + a * a));
16}
17
18int main(int argc, char *argv[])
19{
20    int n, myid, numprocs, i;
21    double PI25DT = 3.141592653589793238462643;
22    double mypi, pi, h, sum, x;
23    double startwtime = 0.0, endwtime;
24    int namelen;
25    char processor_name[MPI_MAX_PROCESSOR_NAME];
26
27    MPI_Init(&argc, &argv);
28    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
29    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
30    MPI_Get_processor_name(processor_name, &namelen);
31
32    fprintf(stdout, "Process %d of %d is on %s\n", myid, numprocs, processor_name);
33    fflush(stdout);
34
35    n = 10000;  /* default # of rectangles */
36    if (myid == 0)
37        startwtime = MPI_Wtime();
38
39    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
40
41    h = 1.0 / (double) n;
42    sum = 0.0;
43    /* A slightly better approach starts from large i and works back */
44    for (i = myid + 1; i <= n; i += numprocs) {
45        x = h * ((double) i - 0.5);
46        sum += f(x);
47    }
48    mypi = h * sum;
49
50    MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
51
52    if (myid == 0) {
53        endwtime = MPI_Wtime();
54        printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT));
55        printf("wall clock time = %f\n", endwtime - startwtime);
56        fflush(stdout);
57    }
58
59    MPI_Finalize();
60    return 0;
61}
Note: See TracBrowser for help on using the repository browser.