-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1-program3.c
More file actions
218 lines (189 loc) · 5.19 KB
/
hw1-program3.c
File metadata and controls
218 lines (189 loc) · 5.19 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
//Aaron Holt
//HW1 Program 2: trapezoidal rule
//Run on 6core AMD64 using MPICH
/*
In this program I assume a is the lower bound and b is the upper bound.
In the even of a factional number of arcs for each process, I just round up
to the nearest number of arcs that is evenly disivible.
Example:
n=2 arcs, np=2
3/2 =1.5
ceil(1.5) = 2
Each process gets 2 arcs
*/
float f(float x)
{
return x*x - x;
}
float simpson(float a, float b, int n, int verbose, int world_rank)
{
float integral_edges, integral_4s, integral_2s;
double step;
int i;
step = (b-a)/n;
integral_edges = 0.0;
integral_2s = 0.0;
integral_4s = 0.0;
int interval[n];
//1+4+2+4+...+1
//Edges
integral_edges = (f(a) + f(b));
//4's
for (i=1; i<n; i=i+2)
{
integral_4s += 4*f(a + i*step);
if (verbose == 1)
{
if (world_rank == 0)
{
interval[i] = 4;
}
}
}
//2's
for (i=2; i<n; i=i+2)
{
integral_2s += 2*f(a + i*step);
if (verbose == 1)
{
if (world_rank == 0)
{
interval[i] = 2;
}
}
}
//All process
if (world_rank == 0)
{
if (verbose == 1)
{
interval[0] = 1;
interval[n-1] = 1;
printf("Interval for all processes = { ");
for (i=0; i<n; i++)
{
printf("%d ", interval[i]);
}
printf("}\n");
}
}
return (step / 3) * (integral_4s + integral_2s + integral_edges);
}
int main(int argc, char *argv[])
{
int i, j; //counters for loops
int n = 1000; //Default number of arcs
//a and b are lower and upper bounds respectively
//Process n is number of areas per process
float a, b, step, process_n;
int verbose = 0; //Default verbose off
char option_n[] = "-n";
char option_v[] = "-verbose";
char option_v2[] = "-v";
//Argument parsing.
for (i=1; i< argc; i++)
{
//Get n value if present
if (sscanf (argv[i], "%i", &n)!=1) {}
//Verbose?
if (strcmp(argv[i], option_v)==0) {
// printf("%s\n", argv[i]);
verbose = 1;
}
if (strcmp(argv[i], option_v2)==0) {
// printf("%s\n", argv[i]);
verbose = 1;
}
}
// printf("N val = %d\n", n);
//Start MPI section
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
if (world_rank == 0)
{
a = 0; //lower bound
b = 1; //upper bound
//number of arcs per process, round up in case
//of indivisible amount
process_n = ceil( ((float)n) / ((float)world_size) );
//Update n so when I print it at the end it's correct
n = process_n * world_size;
}
//Broadcast relevant information to all processes
MPI_Bcast(&a, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&b, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&process_n, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&verbose, 1, MPI_INT, 0, MPI_COMM_WORLD);
float local_a, local_b, local_result;
int interval[(int)(process_n/world_size)]; //To print out intervals
//Process specific start point
local_a = ((b - a) / world_size) * world_rank + a;
//Process specific end point
local_b = ((b - a) / world_size) * (world_rank + 1) + a;
//Show numeric intervals each process did
if (verbose == 1)
{
if (world_rank==0)
{
float start, end;
for (i=0; i<world_size; i++)
{
start = ((b - a) / world_size) * i + a;
end = ((b - a) / world_size) * (i + 1) + a;
printf("Process %d from %f to %f \n", i, start, end);
}
}
}
//Calculate local results
local_result = simpson(local_a, local_b, process_n, verbose, world_rank);
// printf("result = %f, process %d \n", local_result, world_rank);
//Send results, adding along the way
//Account for 1 process case
if (world_size > 1)
{
//Start sending with last process
if (world_rank == (world_size-1))
{
MPI_Send(&local_result, 1, MPI_FLOAT, world_rank-1, 0, MPI_COMM_WORLD);
}
else
{
//temp variable so the receive doesn't overwrite
float temp = local_result;
//Receive intermediate result
MPI_Recv(&local_result, 1, MPI_FLOAT, world_rank+1, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
local_result += temp; //Add intermediate result to current total
//Send new intermediate result along
if (world_rank != 0)
{
MPI_Send(&local_result, 1, MPI_FLOAT, world_rank-1, 0, MPI_COMM_WORLD);
}
//Display final result
else
{
printf("Final result with n = %d: %f \n", n, local_result);
}
}
}
else
{
printf("Final result with n = %d: %f \n", n, local_result);
}
MPI_Finalize();
}