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

1.23 2.0 main test-branch
Last change on this file since 2dfc427 was d7270ab, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

added more mpi+openmp examples.

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

  • Property mode set to 100644
File size: 3.1 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
25double
26func(double x)
27{
28 return (4.0 / (1.0 + x * x));
29}
30
31/* Main Program */
32
33int
34main(int argc, char *argv[])
35{
36 int NoIntervals, interval, ScatterSize, Noofthreads;
37 int MyRank, Numprocs,threadid,start,end;
38 int iproc, Root = 0, valid;
39 int Source, Source_tag;
40 int Destination, Destination_tag;
41 double PI25DT = 3.141592653589793238462643;
42 double mypi, pi, h, totalsum, x,partialsum,finalsum;
43 MPI_Status status;
44
45 /* ....MPI Initialisation.... */
46 MPI_Init(&argc, &argv);
47 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
48 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
49
50 if (MyRank == Root) {
51 printf("Enter The NoIntervals Of Intervals: ");
52 /* scanf("%d", &NoIntervals); */
53 NoIntervals=1000;
54 }
55 MPI_Bcast(&NoIntervals, 1, MPI_INT, Root, MPI_COMM_WORLD);
56
57 if (NoIntervals <= 0) {
58 if (MyRank == Root)
59 printf("Invalid Value For Number Of Intervals .....\n");
60 MPI_Finalize();
61 exit(-1);
62 }
63 /* Checking For Equal Division Of Intervals Across The Processors */
64
65 if (NoIntervals % Numprocs != 0) {
66 if (MyRank == Root)
67 printf("NoofIntervals Cannot Be Evenly Distributed \n");
68 MPI_Finalize();
69 exit(0);
70 }
71 if (MyRank == Root)
72 ScatterSize = NoIntervals / Numprocs;
73
74
75 MPI_Bcast(&ScatterSize, 1, MPI_INT, Root, MPI_COMM_WORLD);
76
77 /*printf("the ScatterSize is %d",ScatterSize);*/
78 h = 1.0 / (double) NoIntervals;
79 totalsum = 0.0;
80 finalsum = 0.0;
81
82 /* OpenMP Parallel For Directive And Critical Section */
83
84 partialsum=0.0;
85
86 start=MyRank * ScatterSize + 1 ;
87 end=MyRank * ScatterSize + ScatterSize;
88
89 omp_set_num_threads(4);
90#pragma omp parallel for private(x,interval)
91 for (interval = start; interval <= end; interval=interval+1)
92 {
93 x = h * ((double) interval - 0.5);
94 #pragma omp critical
95 partialsum = partialsum + func(x);
96 /*printf("the threadid Myrank and partial sum for each threadis %d %d %f\n",omp_get_thread_num(),MyRank,partialsum); */
97 }
98
99 mypi = partialsum * h;
100 /* printf("the each process totalsum and mypi value for each rank is %f %f %d\n",totalsum,mypi,MyRank); */
101 /* ....Collect The Areas Calculated In P0,P1.... */
102 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, Root, MPI_COMM_WORLD);
103
104
105 if (MyRank == Root) {
106 printf("\nPi Is Approximately %.16f, Error Is %.16f \n",
107 pi, fabs(pi - PI25DT));
108 }
109 /* MPI_Termination */
110
111 MPI_Finalize();
112
113}
Note: See TracBrowser for help on using the repository browser.