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

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 3086590 was e743307, checked in by John Edenhofner <johneden@…>, 11 years ago

Inserted <stdlib.h>, should have for exit()

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

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[66226e2]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>
[e743307]40#include <stdlib.h>
[66226e2]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#pragma CIVL $assume 0 < NoInterval && NoInterval < 3;
80
81 /* ....MPI initialisation.... */
82 MPI_Init(&argc, &argv);
83 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
84 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
85
86 if (MyRank == Root) {
87 printf("\nEnter the number of intervals : ");
88 scanf("%d", &NoInterval);
89 }
90 /* ....Broadcast the number of subintervals to each processor.... */
91 MPI_Bcast(&NoInterval, 1, MPI_INT, 0, MPI_COMM_WORLD);
92
93 if (NoInterval <= 0) {
94 if (MyRank == Root)
95 printf("Invalid Value for Number of Intervals .....\n");
96 MPI_Finalize();
97 exit(-1);
98 }
99 h = 1.0 / (double) NoInterval;
100 intervalWidth = h;
101
102 /* Start Calcualtions to determine the number of intervals for each */
103 /* processes of the Communicator. */
104
105 MyCount = 0;
106 for (interval = MyRank + 1; interval <= NoInterval; interval += Numprocs)
107 MyCount++;
108
109 MyIntervals = (int *) malloc(sizeof(int) * MyCount);
110
111 for (i = 0, interval = MyRank + 1; interval <= NoInterval; i++, interval += Numprocs)
112 MyIntervals[i] = interval;
113
114 /* Start creating threads. */
115
116 threads = (pthread_t *) malloc(sizeof(pthread_t) * MyCount);
117
118 for (interval = 0; interval < MyCount; interval++)
119 pthread_create(&threads[interval], NULL, (void *(*) (void *)) MyPartOfCalc, (void *) interval);
120
121 for (interval = 0; interval < MyCount; interval++)
122 pthread_join(threads[interval], NULL);
123
124 /* ....Collect the areas calculated in P0.... */
125 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, Root, MPI_COMM_WORLD);
126
127 if (MyRank == Root) {
128 printf("pi is approximately %.16f, Error is %.16f\n",
129 pi, fabs(pi - PI25DT));
130 }
131 MPI_Finalize();
132
133}
Note: See TracBrowser for help on using the repository browser.