-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot_mpi_2.c
295 lines (245 loc) · 6.49 KB
/
mandelbrot_mpi_2.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
MPI implementation of Mandelbrot set by creating 2 groups of processors and using inter-communicators and intra-communicators
*/
#include<stdio.h>
#include<stdlib.h>
#include <mpi.h>
#define N 1000
#define ITR 10000
#define RE_POSMAX 2.0
#define RE_POSCEN 0.0
#define RE_POSMIN -2.0
#define IM_POSMAX 2.0
#define IM_POSCEN 0.0
#define IM_POSMIN -2.0
#define ROOT 0
//function to test if point belongs to mandelbrot set
int m_pixel_calc(double c_re, double c_im){
int count = 0;
double temp, zmod_sq;
double z_re = 0.0, z_im = 0.0;
do{
temp = z_re * z_re - z_im * z_im + c_re;
z_im = (2*z_re*z_im) + c_im;
z_re = temp;
zmod_sq = (z_re * z_re) + (z_im * z_im);
count++;
}while((zmod_sq < 4.0) && (count < ITR));
if(count == ITR){
return 1;
}else{
return 0;
}
}
//function for mandelbrot computations
void prep(int* myid, int* nprocs, MPI_Comm* comm, int quad, int** data_root, int*** m_grid_root){
int i,j;
int ierr;
int rem, div;
int p_rows, p_start, p_end;
int **m_grid_myid;
int *data_myid;
double c_re, c_im;
int re_range, im_range, im_start;
int Ni = ((0.5)*N);
FILE *fp,*fp2;
//arrays used for displacement and sizes in gatherv
int displ[*nprocs];
int rcounts[*nprocs];
//calculate the no.of rows, start point and end point for each process
p_rows = (Ni)/(*nprocs);
p_start = (p_rows)*(*myid);
rem = ((Ni)%(*nprocs));
div = ((Ni)/(*nprocs));
if(*myid<rem){
p_rows= div + 1;
}else{
p_rows = div;
}
if(*myid==ROOT){
p_start = 0;
}else if((*myid-1)<rem){
p_start = ((*myid * div) + *myid);
}else{
p_start = ((*myid * div) + rem);
}
p_end = p_start + p_rows - 1;
//allocate local memory on each process to store its part of the result
m_grid_myid = (int**)malloc((p_rows)*sizeof(int*));
data_myid = (int*)malloc(N* (p_rows) * sizeof(int));
for(i=0; i<(p_rows); ++i){
m_grid_myid[i] = &data_myid[i*N];
}
//check which half of the workload it is and calculate bounds
if(quad==0){
re_range = (RE_POSMAX) - (RE_POSMIN);
im_range = (IM_POSMAX) - (IM_POSCEN);
im_start = (IM_POSMAX);
}else if(quad==1){
re_range = (RE_POSMAX) - (RE_POSMIN);
im_range = (IM_POSCEN) - (IM_POSMIN);
im_start = (IM_POSCEN);
}
//calculate mandelbrot set
for(i=p_start; i<=p_end; ++i){
for(j=0; j<N; ++j){
c_re = (RE_POSMIN) + (((double)re_range/(double)N)*j);
c_im = (im_start) - ((((double)im_range)/((double)Ni))*i);
m_grid_myid[i-p_start][j] = m_pixel_calc(c_re,c_im);
}
}
//populate rcounts and displ
for(i=0;i<*nprocs;++i){
if(i<rem){
rcounts[i]=N*(div + 1);
}else{
rcounts[i]=N*div;
}
if(i==0){
displ[i] = 0;
}else if((i-1)<rem){
displ[i] = N*((i * div) + i);
}else{
displ[i] = N*((i * div) + rem);
}
}
//gather points to root process
ierr=MPI_Gatherv(data_myid, p_rows*N, MPI_INT, (*data_root), rcounts, displ, MPI_INT, 0, *comm);
//free memory on local process
free(data_myid);
free(m_grid_myid);
//print points to file
if(*myid == 0){
fp = fopen("dots.dat", "a+");
fp2 = fopen("points.dat", "a+");
for(i=0; i<Ni; ++i){
for(j=0; j<N; ++j){
fprintf(fp, "%d ", (*m_grid_root)[i][j]);
if((*m_grid_root)[i][j]==1){
fprintf(fp2, "%.03lf\t%.03lf\n", (double)(RE_POSMIN)+(((re_range)/(N))*j),(double)(im_start)-(((im_range)/(Ni))*i));
}
}
fprintf(fp, "\n");
}
fclose(fp);
fclose(fp2);
}
}
int main(int argc, char *argv[]){
int i, j, k;
int nprocs,myid,ierr;
int rem, div;
int p_rows, p_start, p_end;
int **m_grid_myid, **m_grid_root;
int *data_myid, *data_root;
double c_re, c_im;
MPI_Status *status;
MPI_Request *request;
FILE *fp,*fp2;
double t_start, t_end, t;
int world_rank, world_size;
int nprocs1, nprocs2;
int **g_grid_root;
int *g_data;
fp = fopen("dots.dat","w");
fp2 = fopen("points2.dat","w");
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &world_size);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
//start timer
if(world_rank==ROOT){
t_start = MPI_Wtime();
}
//create world group
MPI_Group world_group;
MPI_Comm_group(MPI_COMM_WORLD, &world_group);
//divide the number of processors in half
nprocs1 = (world_size)/(2);
nprocs2 = (world_size)-nprocs1;
//assign these to 2 group arrays
int group1_rank[nprocs1];
int group2_rank[nprocs2];
for(i=0;i<nprocs1;++i){
group1_rank[i]=i;
if(i<nprocs2){
group2_rank[i]=nprocs1+i;
}
}
//construct groups
MPI_Group group1;
MPI_Group_incl(world_group, nprocs1, group1_rank, &group1);
MPI_Group group2;
MPI_Group_incl(world_group, nprocs2, group2_rank, & group2);
//create new communicators
MPI_Comm group1_comm;
MPI_Comm_create_group(MPI_COMM_WORLD, group1, 1, &group1_comm);
MPI_Comm group2_comm;
MPI_Comm_create_group(MPI_COMM_WORLD, group2, 2, &group2_comm);
//get process information for the new communicators
int g1_rank=-1, g1_size=-1;
if(MPI_COMM_NULL != group1_comm){
MPI_Comm_rank(group1_comm, &g1_rank);
MPI_Comm_size(group1_comm, &g1_size);
}
int g2_rank=-1, g2_size=-1;
if(MPI_COMM_NULL != group2_comm){
MPI_Comm_rank(group2_comm, &g2_rank);
MPI_Comm_size(group2_comm, &g2_size);
}
int Ni = 0.5*N;
//the grid is split into 2 halves above and below the real axis
//each group is assigned 1 half of the work
//mandelbrot calculations for group 1
if(MPI_COMM_NULL != group1_comm){
if(g1_rank==ROOT){
g_grid_root = (int**)malloc(Ni*sizeof(int*));
g_data = (int*)malloc(N*Ni*sizeof(int));
for(i=0;i<Ni;i++){
g_grid_root[i] = &g_data[i*N];
}
}
//do computations for group 1
prep(&g1_rank, &g1_size, &group1_comm, 1, &g_data, &g_grid_root);
}
//mandelbrot calculations for group 2
if(MPI_COMM_NULL != group2_comm){
if(g2_rank==ROOT){
g_grid_root = (int**)malloc(Ni*sizeof(int*));
g_data = (int*)malloc(N*Ni*sizeof(int));
for(i=0;i<Ni;i++){
g_grid_root[i] = &g_data[i*N];
}
}
//do computations for group 2
prep(&g2_rank, &g2_size, &group2_comm, 0, &g_data, &g_grid_root);
}
//end timer
if(myid==ROOT){
t_end = MPI_Wtime();
printf("time = %.09lf\n", (t_end-t_start));
}
//free memory and communicators for group 1
if(MPI_COMM_NULL != group1_comm){
if(g1_rank==ROOT){
free(g_grid_root);
free(g_data);
}
MPI_Group_free(&group1);
MPI_Comm_free(&group1_comm);
}
//free memory and communicators on group 2
if(MPI_COMM_NULL != group2_comm){
if(g2_rank==ROOT){
free(g_grid_root);
free(g_data);
}
MPI_Group_free(&group2);
MPI_Comm_free(&group2_comm);
}
//free group for world
if(MPI_COMM_NULL != MPI_COMM_WORLD){
MPI_Group_free(&world_group);
}
ierr=MPI_Finalize();
return 0;
}