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

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

minor correction to get mpi-pthread-pie-collective work.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1996 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
45double intervalWidth, mypi = 0.0;
46int *MyIntervals;
47int MyRank, Numprocs, Root = 0, MyCount = 0;
48pthread_mutex_t mypi_mutex = PTHREAD_MUTEX_INITIALIZER;
49
50void * MyPartOfCalc(int myID)
51{
52 int myInterval;
53 double myIntervalMidPoint;
54 double myArea = 0.0, result;
55
56 myIntervalMidPoint = ((double) MyIntervals[myID] - 0.5) * intervalWidth;
57 myArea += (4.0 / (1.0 + myIntervalMidPoint * myIntervalMidPoint));
58
59 result = myArea * intervalWidth;
60
61 /* Lock the mutex controlling the access to area. */
62
63 pthread_mutex_lock(&mypi_mutex);
64 mypi += result;
65 pthread_mutex_unlock(&mypi_mutex);
66}
67
68double func(double x)
69{
70 return (4.0 / (1.0 + x * x));
71}
72
73int main(int argc, char *argv[])
74{
75 int NoInterval, interval, i;
76 double pi, sum, h, x;
77 double PI25DT = 3.141592653589793238462643;
78 pthread_t *threads;
79
80 /* ....MPI initialisation.... */
81 MPI_Init(&argc, &argv);
82 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
83 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
84
85 if (MyRank == Root) {
86 printf("\nEnter the number of intervals : ");
87 scanf("%d", &NoInterval);
88 #ifdef _CIVL
89 $assume NoInterval < 10;
90 #endif
91 }
92
93 /* ....Broadcast the number of subintervals to each processor.... */
94 MPI_Bcast(&NoInterval, 1, MPI_INT, 0, MPI_COMM_WORLD);
95 #ifdef _CIVL
96 $assume NoInterval > 0;
97 #endif
98 if (NoInterval <= 0) {
99 if (MyRank == Root)
100 printf("Invalid Value for Number of Intervals .....\n");
101 MPI_Finalize();
102 exit(-1);
103 }
104 h = 1.0 / (double) NoInterval;
105 intervalWidth = h;
106
107 /* Start Calcualtions to determine the number of intervals for each */
108 /* processes of the Communicator. */
109
110 MyCount = 0;
111 for (interval = MyRank + 1; interval <= NoInterval; interval += Numprocs)
112 MyCount++;
113
114 MyIntervals = (int *) malloc(sizeof(int) * MyCount);
115
116 for (i = 0, interval = MyRank + 1; interval <= NoInterval; i++, interval += Numprocs)
117 MyIntervals[i] = interval;
118
119 /* Start creating threads. */
120
121 threads = (pthread_t *) malloc(sizeof(pthread_t) * MyCount);
122
123 for (interval = 0; interval < MyCount; interval++)
124 pthread_create(&threads[interval], NULL, (void *(*) (void *)) MyPartOfCalc, (void *) interval);
125
126 for (interval = 0; interval < MyCount; interval++)
127 pthread_join(threads[interval], NULL);
128
129 /* ....Collect the areas calculated in P0.... */
130 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, Root, MPI_COMM_WORLD);
131
132 if (MyRank == Root) {
133 printf("pi is approximately %.16f, Error is %.16f\n",
134 pi, fabs(pi - PI25DT));
135 }
136 free(threads);
137 free(MyIntervals);
138 MPI_Finalize();
139
140}
Note: See TracBrowser for help on using the repository browser.