-
Notifications
You must be signed in to change notification settings - Fork 1
/
trapezoidal.c
56 lines (50 loc) · 1.41 KB
/
trapezoidal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* This program evaluates the trapezoidal rule estimate
* for an integral of F(x). In this case, F(x) is exp(x)
* evaluated for the interval of 0 to 1. Each case provides
* 2X increase in precision. Therefore each result should be
* similar but, not the same.
*
* Testing for Nps 1 ... 11 should execute programs.
* Testing for Nps 12 and above should stop the program.
*
* This program requires the following call(s)
* MPI_Init
* MPI_Comm_rank
* MPI_Comm_size
* MPI_Abort
* MPI_Finalize
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "mpi.h"
#define F(x) (exp(x))
main (int argc, char **argv)
{
double TN, SUM, X, H,T1,T2,DH,res,
A = 0.0,
B = 1.0;
int nworkers, whoami, i, errcode;
/* Initialize MPI */
MPI_Init(&argc, &argv);
/* Find out this processor number */
MPI_Comm_rank(MPI_COMM_WORLD, &whoami);
/* Find out the number of processors */
MPI_Comm_size(MPI_COMM_WORLD, &nworkers);
DH=(B-A)/nworkers;
whoami = whoami + 1;
T1=A+(whoami-1)*DH;
T2=A+whoami*DH;
H=(T2-T1)/50.0;
SUM = 0.0;
for (i = 0; i <=49; i++)
SUM = SUM+F(T1+H*i)+F(T1+H*(i+1));
TN = SUM*H;
printf("whoami %d\t, T1= %f\t, T2= %f\t, TN %f \n", whoami-1, T1, T2, TN);
//MPI_Bcast (&TN,1,MPI_DOUBLE, 1, MPI_COMM_WORLD);
MPI_Reduce (&TN, &res,1,MPI_DOUBLE, MPI_SUM, 1, MPI_COMM_WORLD);
if(whoami==2)printf("Integral = %10.6f\n",res);
MPI_Finalize();
}