-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegration_Any.cpp
More file actions
executable file
·151 lines (148 loc) · 5.75 KB
/
Copy pathIntegration_Any.cpp
File metadata and controls
executable file
·151 lines (148 loc) · 5.75 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "/home/pnpninja/PC/Monte Carlo/prng_engine.hpp"
#include "mpi.h"
#define sitmo_rand_max 4294967295
using namespace std;
double sine_reduction(int power,double lower_limit,double upper_limit)
{
if(power==0)
return (upper_limit - lower_limit);
else if(power==1)
return (-cos(upper_limit)+cos(lower_limit));
else
return ((-1/power)*(cos(upper_limit)*pow(sin(upper_limit),power-1)))-((-1/power)*(cos(lower_limit)*pow(sin(lower_limit),power-1)))+(((power-1)/power)*sine_reduction(power-2,lower_limit,upper_limit));
}
double cos_reduction(int power,double lower_limit,double upper_limit)
{
if(power==0)
return (upper_limit - lower_limit);
else if(power==1)
return (sin(upper_limit)-sin(lower_limit));
else
return ((1/power)*(sin(upper_limit)*pow(cos(upper_limit),power-1)))-((1/power)*(sin(lower_limit)*pow(cos(lower_limit),power-1)))+(((power-1)/power)*cos_reduction(power-2,lower_limit,upper_limit));
}
double tan_reduction(int power,double lower_limit,double upper_limit)
{
if(power==0)
return (upper_limit - lower_limit);
else if(power ==1)
return (-log(abs(cos(upper_limit)))+log(abs(cos(lower_limit))));
else
return (((1/(n-1))*pow(tan(upper_limit),n-1))-((1/(n-1))*pow(tan(lower_limit),n-1))+tan_reduction(power-2,lower_limit,upper_limit))
}
double
int main(int argc,char* argv[])
{
//Initialization of variables
int numprocs,myid,rc;
double iterations,eachintegration,integrationsum,pi,start,total_time,actualsum,llimit,rlimit,eachllimit,eachrlimit,coefficients[26],highest_coefficient,sine_coeffecients[26],sine_highest_coeffecient,cos_coeffecients[26],cos_highest_coeffecient,tan_coeffecients[26],tan_highest_coeffecients;
//Initialization on MPI Variables
MPI_Init(&argc,&argv);
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
//Get number of iterations in MASTER Process / Process 0
if(myid == 0)
{
cout<<"Enter the highest coeffecient of x (highest is 25): ";
cin>>highest_coefficient;
for(int temp=highest_coefficient;temp>=0;temp--)
{
printf("Enter the coefficient of degree %d : ",temp);
cin>>coefficients[temp];
}
cout<<"Enter the highest coeffecient of sine (highest is 25): ";
cin>>sine_highest_coefficient;
for(int temp=sine_highest_coefficient;temp>=1;temp--)
{
printf("Enter the coefficient of degree %d : ",temp);
cin>>sine_coefficients[temp];
}
cout<<"Enter the highest coeffecient of cos (highest is 25): ";
cin>>cos_highest_coefficient;
for(int temp=cos_highest_coefficient;temp>=1;temp--)
{
printf("Enter the coefficient of degree %d : ",temp);
cin>>cos_coefficients[temp];
}
cout<<"Enter the highest coeffecient of tan (highest is 25): ";
cin>>tan_highest_coefficient;
for(int temp=tan_highest_coefficient;temp>=1;temp--)
{
printf("Enter the coefficient of degree %d : ",temp);
cin>>tan_coefficients[temp];
}
cout<<"Enter the lower limit : ";
cin>>llimit;
cout<<"Enter the upper limit : ";
cin>>rlimit;
cout<<"Enter the number of iterations : ";
cin>>iterations;
start = MPI_Wtime();
}
//Broadcast it to all other processes
MPI_Bcast(&highest_coefficient,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&sine_highest_coefficient,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&cos_highest_coefficient,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&tan_highest_coefficient,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(coefficients,26,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(sin_coefficients,26,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(cos_coefficients,26,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(tan_coefficients,26,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&llimit,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&rlimit,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Bcast(&iterations,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
//Parallel Random Generator with seed as Process ID
sitmo::prng_engine eng(MPI_Wtime());
eachintegration=0;
eachllimit = llimit+(((rlimit-llimit)/numprocs)*myid);
eachrlimit = llimit+(((rlimit-llimit)/numprocs)*(myid+1));
for(int a=1;a<=iterations/numprocs;a++)
{
double point = eachllimit + ((double(eng())/(double(sitmo_rand_max)))*(eachrlimit-eachllimit));
for(int b=0;b<=highest_coefficient;b++)
{
eachintegration+=(coefficients[b]*(pow(point,b)));
}
for(int b=1;b<=sine_highest_coefficient;b++)
{
eachintegration+=(sine_coefficients[b]*(pow(sin(point),b)));
}
for(int b=1;b<=cos_highest_coefficient;b++)
{
eachintegration+=(cos_coefficients[b]*(pow(cos(point),b)));
}
for(int b=1;b<=tan_highest_coefficient;b++)
{
eachintegration+=(tan_coefficients[b]*(pow(tan(point),b)));
}
}
rc = MPI_Reduce(&eachintegration,&integrationsum,1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD);
if(myid == 0)
{
total_time = MPI_Wtime() - start;
cout<<"\nThe integration by Monte Carlo method is : "<<((integrationsum/iterations)*(pow(rlimit-llimit,1)));
actualsum=0;
for(int c=0;c<=highest_coefficient;c++)
{
actualsum+=(coefficients[c]*((pow(rlimit,c+1))-(pow(llimit,c+1)))/(double(c+1)));
}
for(int c=1;c<=sine_highest_coefficient;c++)
{
actualsum+=(sine_coefficients[c]*sine_reduction(c,llimit,rlimit));
}
for(int c=1;c<=cos_highest_coefficient;c++)
{
actualsum+=(cos_coefficients[c]*cos_reduction(c,llimit,rlimit));
}
for(int c=1;c<=tan_highest_coefficient;c++)
{
actualsum+=(tan_coefficients[c]*tan_reduction(c,llimit,rlimit));
}
cout<<"\nThe error is : "<<((integrationsum/iterations)*(pow(rlimit-llimit,1)))-actualsum;
cout<<"\nThe time taken is : "<<total_time<<"\n";
}
MPI_Finalize();
}