source: CIVL/examples/mpi-pthread/mpi-pthreads-pie-collective.c@ b32c2d8

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

added assumptions to bound variables.

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

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/**********************************************************************
2 ProMCore 2008
3 February 05 - 09, 2008
4
5
6 Example 1.2 : mpi-Pthreads-pie-collective.c
7
8 Objective : Write an MPI-Pthreads program to compute the value
9 of PI by numerical integration of a function f(x)
10 =4/(1+x*x) between the limits 0 and 1.Pthreads and
11 MPI Collective communication library calls are used.
12
13 This example demonstrates the use of
14
15 pthread_create()
16 pthread_join()
17 pthread_mutex_lock()
18 pthread_mutex_unlock()
19
20 MPI_Init
21 MPI_Comm_rank
22 MPI_Comm_size
23 MPI_Bcast
24 MPI_Reduce
25 MPI_Finalize
26
27 Input : Number of Intervals.
28
29 Output : Calculated Value of PI.
30
31 Created : January 2008
32 National PARAM Supercomputing Facility,C-DAC,Pune.
33
34
35 E-mail : betatest@cdac.in
36
37************************************************************************/
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <math.h>
42#include "mpi.h"
43#include <pthread.h>
44
45#ifdef _CIVL
46$input int NUM_INTERVAL_BOUND = 4;
47#endif
48
49double intervalWidth, mypi = 0.0;
50int *MyIntervals;
51int MyRank, Numprocs, Root = 0, MyCount = 0;
52pthread_mutex_t mypi_mutex = PTHREAD_MUTEX_INITIALIZER;
53
54void * MyPartOfCalc(int myID)
55{
56 int myInterval;
57 double myIntervalMidPoint;
58 double myArea = 0.0, result;
59
60 myIntervalMidPoint = ((double) MyIntervals[myID] - 0.5) * intervalWidth;
61 myArea += (4.0 / (1.0 + myIntervalMidPoint * myIntervalMidPoint));
62
63 result = myArea * intervalWidth;
64
65 /* Lock the mutex controlling the access to area. */
66
67 pthread_mutex_lock(&mypi_mutex);
68 mypi += result;
69 pthread_mutex_unlock(&mypi_mutex);
70}
71
72double func(double x)
73{
74 return (4.0 / (1.0 + x * x));
75}
76
77int main(int argc, char *argv[])
78{
79 int NoInterval, interval, i;
80 double pi, sum, h, x;
81 double PI25DT = 3.141592653589793238462643;
82 pthread_t *threads;
83
84 /* ....MPI initialisation.... */
85 MPI_Init(&argc, &argv);
86 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
87 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
88
89 if (MyRank == Root) {
90 printf("\nEnter the number of intervals : ");
91 scanf("%d", &NoInterval);
92 #ifdef _CIVL
93 $assume NoInterval <= NUM_INTERVAL_BOUND;
94 #endif
95 }
96
97 /* ....Broadcast the number of subintervals to each processor.... */
98 MPI_Bcast(&NoInterval, 1, MPI_INT, 0, MPI_COMM_WORLD);
99 #ifdef _CIVL
100 $assume NoInterval > 0;
101 #endif
102 if (NoInterval <= 0) {
103 if (MyRank == Root)
104 printf("Invalid Value for Number of Intervals .....\n");
105 MPI_Finalize();
106 exit(-1);
107 }
108 h = 1.0 / (double) NoInterval;
109 intervalWidth = h;
110
111 /* Start Calcualtions to determine the number of intervals for each */
112 /* processes of the Communicator. */
113
114 MyCount = 0;
115 for (interval = MyRank + 1; interval <= NoInterval; interval += Numprocs)
116 MyCount++;
117
118 MyIntervals = (int *) malloc(sizeof(int) * MyCount);
119
120 for (i = 0, interval = MyRank + 1; interval <= NoInterval; i++, interval += Numprocs)
121 MyIntervals[i] = interval;
122
123 /* Start creating threads. */
124
125 threads = (pthread_t *) malloc(sizeof(pthread_t) * MyCount);
126
127 for (interval = 0; interval < MyCount; interval++)
128 pthread_create(&threads[interval], NULL, (void *(*) (void *)) MyPartOfCalc, (void *) interval);
129
130 for (interval = 0; interval < MyCount; interval++)
131 pthread_join(threads[interval], NULL);
132
133 /* ....Collect the areas calculated in P0.... */
134 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, Root, MPI_COMM_WORLD);
135
136 if (MyRank == Root) {
137 printf("pi is approximately %.16f, Error is %.16f\n",
138 pi, fabs(pi - PI25DT));
139 }
140 free(threads);
141 free(MyIntervals);
142 MPI_Finalize();
143
144}
Note: See TracBrowser for help on using the repository browser.