-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
182 lines (159 loc) · 5.18 KB
/
util.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
#include "util.h"
#include "complex.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mpi.h"
int master(int num_tasks, char *file_in, char* file_out)
{
Input in;
in = parse_input_file(file_in);
if(in.tip_multime != 0 && in.tip_multime != 1) {
printf("ERROR: Set not implemented or error parsing input file\n");
MPI_Abort(MPI_COMM_WORLD, 911);
exit(EXIT_FAILURE);
}
MPI_Status status;
long *data;
double start[START_MSG_LEN];
int first_row, crt_row, block_size, default_block, remaining;
int i, j;
int width = floor((in.x_max - in.x_min) / in.rezolutie);
int height = floor((in.y_max - in.y_min) / in.rezolutie);
data = malloc((width + 1) * sizeof(long));
/* Determina liniile pe care le va prelucra fiecare proces slave */
first_row = 0;
default_block = height / num_tasks; /* cate linii are de prelucrat un slave */
remaining = height % num_tasks; /* cate linii mai raman */
for (i = 0; i < num_tasks; ++i) {
block_size = default_block;
if (i < remaining)
++block_size;
/* Transmite procesului slave prima linie pe care o are de prelucrat
* impreuna cu numarul de linii si celelalte date utile
* din fisierul de intrare de care are nevoie
*/
start[0] = first_row;
start[1] = block_size;
start[2] = in.tip_multime;
start[3] = in.x_min;
start[4] = in.x_max;
start[5] = in.y_min;
start[6] = in.y_max;
start[7] = in.rezolutie;
start[8] = in.max_steps;
start[9] = in.julia_param1;
start[10] = in.julia_param2;
/* Anunta procesul slave ca are de lucru! */
MPI_Send(start, START_MSG_LEN, MPI_DOUBLE, i + 1,
START_TAG, MPI_COMM_WORLD);
first_row += block_size;
}
/* Matricea de pixeli 0 - 255 */
int **colors;
colors = malloc(height * sizeof(int*));
for(i = 0; i < height; ++i)
colors[i] = malloc(width * sizeof(int));
/* Primeste rezultate de la "sclavi" si salveaza in matricea de culori */
for (i = 0; i < height; ++i) {
MPI_Recv(data, width + 1, MPI_LONG, MPI_ANY_SOURCE,
DATA_TAG, MPI_COMM_WORLD, &status);
/* Fiecare slave ii spune master-ului datele carei linii le-a trimis */
crt_row = data[0];
for (j = 0; j < width; ++j) {
colors[crt_row][j] = data[j + 1];
}
}
/* Creeaza imaginea */
plot_pgm(file_out, width, height, colors);
return EXIT_SUCCESS;
}
int slave(int rank)
{
MPI_Status status;
long *data;
double start[START_MSG_LEN];
int first_row, block_size;
/* Primeste anuntul de START si obtine cantitatea de lucru:
* first_row = linia de start
* block_size = cate linii prelucrez (deplasament)
*/
MPI_Recv(start, START_MSG_LEN, MPI_DOUBLE, 0, START_TAG,
MPI_COMM_WORLD, &status);
Input in;
first_row = (int)start[0];
block_size = (int)start[1];
in.tip_multime = (int)start[2];
in.x_min = start[3];
in.x_max = start[4];
in.y_min = start[5];
in.y_max = start[6];
in.rezolutie = start[7];
in.max_steps = (int)start[8];
in.julia_param1 = start[9];
in.julia_param2 = start[10];
int width = floor((in.x_max - in.x_min) / in.rezolutie);
data = malloc((width + 1) * sizeof(long));
int i, j;
if(in.tip_multime == 0) { /* Mandelbrot set */
/* Prelucreaza blocul de linii alocat, conform algoritmului din cerinta*/
for(i = first_row; i < (first_row + block_size); ++i) {
data[0] = i; /* Pentru ca masterul sa stie ca linie a prelucrat acest slave */
for(j = 0; j < width; ++j) {
data[j + 1] = compute_Mandelbrot(in, i ,j);
}
/* Trimite rezultatele liniei curente master-ului */
MPI_Send(data, width + 1, MPI_LONG, 0, DATA_TAG, MPI_COMM_WORLD);
}
} else if(in.tip_multime == 1) { /* Julia set */
/* Prelucreaza blocul de linii alocat, conform algoritmului din cerinta
* Modul de lucru este identic cu cel de la Mandelbrot
*/
for(i = first_row; i < (first_row + block_size); ++i) {
data[0] = i;
for(j = 0; j < width; ++j) {
data[j + 1] = compute_Julia(in, i, j);
}
MPI_Send(data, width + 1, MPI_LONG, 0, DATA_TAG, MPI_COMM_WORLD);
}
}
return EXIT_SUCCESS;
}
Input parse_input_file(char* filename)
{
FILE *in = fopen(filename, "r");
Input data;
if(!in) {
printf("ERROR: while opening %s \n", filename);
data.tip_multime = -1;
return data;
}
fscanf(in, "%d", &data.tip_multime);
fscanf(in, "%lf %lf %lf %lf", &data.x_min, &data.x_max, &data.y_min, &data.y_max);
fscanf(in, "%lf", &data.rezolutie);
fscanf(in, "%d", &data.max_steps);
if(data.tip_multime == 1) {
fscanf(in, "%lf %lf", &data.julia_param1, &data.julia_param2);
} else {
data.julia_param1 = 0./0.;
data.julia_param2 = 0./0.;
}
fclose(in);
return data;
}
void plot_pgm(char* filename, int width, int height, int **colors)
{
FILE *f;
f = fopen(filename, "w");
int i, j;
fprintf(f, "%s\n","P2");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "%d\n", NUM_COLORS - 1);
for(i = 0; i < height; ++i) {
for(j = 0; j < width; ++j){
fprintf(f, "%d ", colors[height - i - 1][j]);
}
fprintf(f, "\n");
}
fclose(f);
}