-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_par_brot.cpp
251 lines (198 loc) · 5.53 KB
/
stat_par_brot.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include "mpi.h"
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
#define WRITE_FLAGS "w"
struct complex{
float real;
float imag;
};
bool pim_write_black_and_white(const char * const fileName,
const int width,
const int height,
const unsigned char * pixels);
bool pim_write_black_and_white(const char * const fileName,
const int width,
const int height,
const unsigned char ** pixels);
int cal_pixel( complex c );
int main (int argc, char *argv[])
{
//MPI information
int actorCount, echelon;
MPI_Status Stat;
//Image and calculation data
int real_max = 2;
int real_min = -2;
int imag_max = 2;
int imag_min = -2;
int disp_width = 500;
int disp_height =500;
float scale_real = (float)(real_max - real_min)/disp_width;
float scale_imag = (float)(imag_max - real_min)/disp_height;
//Time data
double start;
double end;
double duration;
//Load balancing data
int count;
int row;
//The lord commands the vassals
int vassal;
int lord = 0;
//Tags that keep track of when to do certain processes
int data_tag, source_tag, terminator_tag, result_tag;
data_tag = 0;
result_tag = 2;
terminator_tag = 3;
//flag to see if there are an odd numbered of processors
bool oddProcs;
//If there is an odd number of slaves, one will have to do the left over rows
int remainingRows;
//The fresco is the image we are creating from this process
//It will have a mandelbrot saved to it
unsigned char ** fresco;
fresco = new unsigned char * [ disp_height ];
for( int i = 0; i < disp_height; i++ )
{
fresco[ i ] = new unsigned char [ disp_width ];
}
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &actorCount);
MPI_Comm_rank(MPI_COMM_WORLD, &echelon);
int slaves = actorCount - 1;
if( slaves % 2 == 0 )
{
oddProcs = false;
}
else
{
oddProcs = true;
}
int rowsPerProcess = disp_height / slaves;
if( oddProcs )
{
remainingRows = disp_height - (rowsPerProcess * slaves );
}
//Lord process
if( echelon == lord )
{
//The strip to send to the vassals
unsigned char strip[ disp_width ];
//The row that was returned by the vassal
int returnedRow;
count = 0;
row = 0;
//Start sending out rows to vassals
for( int actor = 1; actor < actorCount; actor++ )
{
if( oddProcs && (actor == actorCount - 1) )
{
row += remainingRows;
}
MPI_Send( &row , 1 , MPI_INT , actor , data_tag , MPI_COMM_WORLD );
row += rowsPerProcess;
}
start = 0;
start = MPI_Wtime();
//Copy the strip the lord received from the vassal into the image
for( int i = 0; i < (rowsPerProcess * slaves); i++ )
{
MPI_Recv( &strip , disp_width , MPI_UNSIGNED_CHAR , MPI_ANY_SOURCE , MPI_ANY_TAG , MPI_COMM_WORLD , &Stat );
returnedRow = Stat.MPI_TAG;
for( int dot = 0; dot < disp_width; dot++ )
{
fresco[ returnedRow ][ dot ] = strip[ dot ];
}
}
end = MPI_Wtime();
duration = end - start;
cout << duration << endl;
//Save the image
pim_write_black_and_white( "static_img",
disp_width,
disp_height,
(const unsigned char **) fresco);
}
//Vassal processes
if( echelon > lord )
{
//The array contains the pixel data to return to the lord
unsigned char arr[ disp_width ];
//The coordinates that help calculate the mandelbrot
int abscissa;
int ordinate;
int row;
//The number passed into the cal_pixel function
complex c;
c.real = 0;
c.imag = 0;
//The lord tells me which row to compute
MPI_Recv( &row , 1 , MPI_INT , lord , data_tag , MPI_COMM_WORLD , &Stat );
//I calculate the mandelbrot data pixels for the row I am assigned
for( ordinate = row; ordinate < (row + rowsPerProcess); ordinate++ )
{
c.imag = imag_min + ( (float) ordinate * scale_imag);
for( abscissa = 0; abscissa < disp_width; abscissa++ )
{
c.real = real_min + ( (float) abscissa * scale_real);
arr[ abscissa ] = cal_pixel( c );
}
//I send my hard work back to the lord
MPI_Send( &arr , disp_width , MPI_UNSIGNED_CHAR , lord , ordinate , MPI_COMM_WORLD );
}
}
MPI_Finalize();
return 0;
}
//Calculates the pixel data based on given complex number
//Given by the textbook
int cal_pixel( complex c )
{
int count, max_iter;
complex z;
float temp, lengthsq;
max_iter = 256;
z.real = 0;
z.imag = 0;
count = 0;
do {
temp = z.real * z.real - z.imag * z.imag + c.real;
z.imag = 2 * z.real * z.imag + c.imag;
z.real = temp;
lengthsq = z.real * z.real + z.imag * z.imag;
count++;
} while( (lengthsq < 4.0) && (count < max_iter));
return count;
}
//Saves the unsigned char * data to the row in the imagee
//Given by instructor
bool pim_write_black_and_white(const char * const fileName,
const int width,
const int height,
const unsigned char * pixels)
{
FILE * fp = fopen(fileName, WRITE_FLAGS);
if (!fp) return false;
fprintf(fp, "P5\n%i %i 255\n", width, height);
fwrite(pixels, width * height, 1, fp);
fclose(fp);
return true;
}
//Saves the unsigned char ** data to the image
//Given by instructor
bool pim_write_black_and_white(const char * const fileName,
const int width,
const int height,
const unsigned char ** pixels)
{
int i;
bool ret;
unsigned char * t = new unsigned char[width * height];
for (i = 0; i < height; ++i) memcpy(t + width * i, pixels[i], width);
ret = pim_write_black_and_white(fileName, width, height, t);
delete [] t;
return ret;
}