source: CIVL/examples/mpi-omp/mpi-omp-pie-calculation.c@ bb03188

main test-branch
Last change on this file since bb03188 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.3 KB
Line 
1/****************************************************************************
2 C-DAC Tech Workshop : HeGaPa-2012
3 July 16-20,2012
4
5 Example 2 : Mpi-Omp_PI_Calculation.c
6
7 Objective : Write an MPI-OpenMP Program to compute numerical
8 integration of PI value
9
10 Input : The number of intervals.
11
12 Output : The calculated value of PI with Abs value.
13
14 Created : MAY-2012
15****************************************************************************/
16
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <math.h>
21#include "mpi.h"
22#include <omp.h>
23
24#define INTERVALS 6 //Originally 1000
25
26
27double
28func(double x)
29{
30 return (4.0 / (1.0 + x * x));
31}
32
33/* Main Program */
34
35int
36main(int argc, char *argv[])
37{
38 int NoIntervals, interval, ScatterSize, Noofthreads;
39 int MyRank, Numprocs,threadid,start,end;
40 int iproc, Root = 0, valid;
41 int Source, Source_tag;
42 int Destination, Destination_tag;
43 double PI25DT = 3.141592653589793238462643;
44 double mypi, pi, h, totalsum, x,partialsum,finalsum;
45 MPI_Status status;
46
47 /* ....MPI Initialisation.... */
48 MPI_Init(&argc, &argv);
49 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
50 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
51
52 if (MyRank == Root) {
53 printf("Enter The NoIntervals Of Intervals: ");
54 /* scanf("%d", &NoIntervals); */
55 NoIntervals=INTERVALS;
56 }
57 MPI_Bcast(&NoIntervals, 1, MPI_INT, Root, MPI_COMM_WORLD);
58
59 if (NoIntervals <= 0) {
60 if (MyRank == Root)
61 printf("Invalid Value For Number Of Intervals .....\n");
62 MPI_Finalize();
63 exit(-1);
64 }
65 /* Checking For Equal Division Of Intervals Across The Processors */
66
67 if (NoIntervals % Numprocs != 0) {
68 if (MyRank == Root)
69 printf("NoofIntervals Cannot Be Evenly Distributed \n");
70 MPI_Finalize();
71 exit(0);
72 }
73 if (MyRank == Root)
74 ScatterSize = NoIntervals / Numprocs;
75
76
77 MPI_Bcast(&ScatterSize, 1, MPI_INT, Root, MPI_COMM_WORLD);
78
79 /*printf("the ScatterSize is %d",ScatterSize);*/
80 h = 1.0 / (double) NoIntervals;
81 totalsum = 0.0;
82 finalsum = 0.0;
83
84 /* OpenMP Parallel For Directive And Critical Section */
85
86 partialsum=0.0;
87
88 start=MyRank * ScatterSize + 1 ;
89 end=MyRank * ScatterSize + ScatterSize;
90
91 omp_set_num_threads(4);
92/*
93Originally was ...
94#pragma omp parallel for private(x,interval)
95*/
96#pragma omp parallel for private(x,interval) reduction(+ : partialsum)
97 for (interval = start; interval <= end; interval=interval+1)
98 {
99 x = h * ((double) interval - 0.5);
100/*
101Originally had ...
102 #pragma omp critical
103replaced with reduction
104*/
105 partialsum = partialsum + func(x);
106 /*printf("the threadid Myrank and partial sum for each threadis %d %d %f\n",omp_get_thread_num(),MyRank,partialsum); */
107 }
108
109 mypi = partialsum * h;
110 /* printf("the each process totalsum and mypi value for each rank is %f %f %d\n",totalsum,mypi,MyRank); */
111 /* ....Collect The Areas Calculated In P0,P1.... */
112 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, Root, MPI_COMM_WORLD);
113
114
115 if (MyRank == Root) {
116 printf("\nPi Is Approximately %.16f, Error Is %.16f \n",
117 pi, fabs(pi - PI25DT));
118 }
119 /* MPI_Termination */
120
121 MPI_Finalize();
122
123}
Note: See TracBrowser for help on using the repository browser.