-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandlebrot_mpi_static.cpp
204 lines (165 loc) · 5.11 KB
/
mandlebrot_mpi_static.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
#define BOOST_CHRONO_HEADER_ONLY
#include <mpi.h>
#include <iostream>
#include <vector>
#include <boost/chrono.hpp>
#include <png++/png.hpp>
using std::cout;
using std::endl;
using std::cerr;
struct complex{
double real;
double imag;
};
// Function prototypes
void generateMandlebrotImage(png::image< png::index_pixel > *image);
int cal_pixel(complex c);
inline void setPixel(int x, int y, int color, png::image< png::index_pixel > *image);
void runMasterProcess(int world_rank, int world_size);
void runSlaveProcess(int world_rank, int world_size);
// Global constants
int IMAGE_HEIGHT = 800;
int IMAGE_WIDTH = 1200;
int REAL_MAX = 1;
int REAL_MIN = -2;
int IMAG_MAX = 1;
int IMAG_MIN = -1;
int ROWS_PER_PROCESS = 10;
int main(int argc, char** argv)
{
// Initialize the MPI environment
MPI_Init(&argc, &argv);
// Find out rank, size
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Run master or slave processes
if (world_rank == 0)
runMasterProcess(world_rank, world_size);
else runSlaveProcess(world_rank, world_size);
// Clean up
MPI_Finalize();
return 0;
}
void runMasterProcess(int world_rank, int world_size)
{
// Initialize png image and create palette
png::image< png::index_pixel > image(IMAGE_WIDTH, IMAGE_HEIGHT);
png::palette pal(256);
for (size_t i = 0; i < pal.size(); ++i){
pal[i] = png::color(i, i*2.2, i*4.4); // can change these values to create new palette
}
image.set_palette(pal);
// Start timer
boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
generateMandlebrotImage(&image); // Compute all the things!
// End timer
boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
std::cout << "took " << sec.count() << " seconds\n";
// image.write("mandlebrot.png");
}
void runSlaveProcess(int world_rank, int world_size)
{
double scale_real = double(REAL_MAX - REAL_MIN) / IMAGE_WIDTH;
double scale_imag = double(IMAG_MAX - IMAG_MIN) / IMAGE_HEIGHT;
// Recive row number
MPI_Status status;
int row_num = -1;
MPI_Recv(&row_num,
1,
MPI_INT,
0,
0,
MPI_COMM_WORLD,
&status);
// Create structure for results.
// Should really create MPI_STRUCT but will just
// use array of doubles for now.
// Each pixel need three values: x, y, and color.
std::vector<double> results;
for(int x = 0; x < IMAGE_WIDTH; ++x){
for(int y = row_num; y < row_num + ROWS_PER_PROCESS; ++y){
complex c;
c.real = REAL_MIN + ((double) x * scale_real);
c.imag = IMAG_MIN + ((double) y * scale_imag);
int color = cal_pixel(c);
// Put in results array
results.push_back(x);
results.push_back(y);
results.push_back(static_cast<double> (color));
}
}
// Send results to master
MPI_Send(&(results[0]),
results.size(),
MPI_DOUBLE,
0,
0,
MPI_COMM_WORLD);
}
void generateMandlebrotImage(png::image< png::index_pixel > *image)
{
// Ensure rows are divisible by ROWS_PER_PROCESS
assert( IMAGE_HEIGHT % ROWS_PER_PROCESS == 0);
double scale_real = double(REAL_MAX - REAL_MIN) / IMAGE_WIDTH;
double scale_imag = double(IMAG_MAX - IMAG_MIN) / IMAGE_HEIGHT;
int slave_process_id = 1;
for (int row = 0; row < IMAGE_HEIGHT; row += ROWS_PER_PROCESS){
// Send row numbers to slaves
int return_val = MPI_Send(&row,
1,
MPI_INT,
slave_process_id,
0,
MPI_COMM_WORLD);
// Check for send errors
if (return_val != MPI_SUCCESS){
int str_len;
char* err_str;
MPI_Error_string(return_val, err_str, &str_len);
cerr << "Error sending to slaves: " << err_str << endl;
}
slave_process_id ++;
}
// Revieve sub-area back from each process
int sub_area_size = ROWS_PER_PROCESS * IMAGE_WIDTH * 3; // 3 values for each pixel (x, y, color)
double *sub_area = new double[sub_area_size];
for (int i = 0; i < IMAGE_HEIGHT / ROWS_PER_PROCESS ; ++i){
MPI_Status status;
MPI_Recv(sub_area,
sub_area_size,
MPI_DOUBLE,
MPI_ANY_SOURCE,
0,
MPI_COMM_WORLD,
&status);
// Set pixel values for sub-area
for (int j = 0; j < sub_area_size - 3; j+= 3){
setPixel(sub_area[j], sub_area[j + 1], static_cast<int>(sub_area[j + 2]), image);
}
}
}
int cal_pixel(complex c)
{
int max_iter = 256;
int count = 0;
complex z;
z.real = 0;
z.imag = 0;
double temp, lengthsq;
// See if pixel is in Mandlebrot set, and if so, compute its intensity
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;
}
inline void setPixel(int x, int y, int color, png::image< png::index_pixel > *image)
{
(*image)[y][x] = png::index_pixel(color);
}