-
Notifications
You must be signed in to change notification settings - Fork 12
/
communication.c
280 lines (225 loc) · 8.59 KB
/
communication.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
/*
The MIT License (MIT)
Copyright (c) 2014 Adam Simpson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <mpi.h>
#include "fractal.h"
#include "communication.h"
#include "exit_menu_gl.h"
#define WORKTAG 1
#define DIETAG 2
#define MAX_ROWS 1
void get_work(FRAC_INFO *info, WORK_DATA *work)
{
if(info->rows_taken >= info->num_rows){
work->num_rows = 0;
return;
}
int rows = MAX_ROWS;
work->start_row = info->rows_taken;
int num_rows = info->rows_taken+rows<info->num_rows?rows:info->num_rows-info->rows_taken;
work->num_rows = num_rows;
info->rows_taken += num_rows;
}
int get_max_work_size(FRAC_INFO *info)
{
return MAX_ROWS*info->num_cols*info->channels;
}
void master_pack_and_send(WORK_DATA *work, char *pack_buffer, int buffer_size)
{
int position;
//pack and send work
position = 0;
MPI_Pack(&work->start_row,1,MPI_INT,pack_buffer, buffer_size, &position,MPI_COMM_WORLD);
MPI_Pack(&work->num_rows,1,MPI_INT,pack_buffer, buffer_size, &position,MPI_COMM_WORLD);
MPI_Send(pack_buffer, position, MPI_PACKED, work->rank, WORKTAG, MPI_COMM_WORLD);
}
int master_recv_and_unpack(WORK_DATA *work, char *pack_buffer, int buffer_size)
{
int tag, source_rank, position, msg_size, num_pixels;
FRAC_INFO *frac;
MPI_Status status;
// Recieve and unpack work
MPI_Recv(pack_buffer, buffer_size, MPI_PACKED, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
position = 0;
work->rank = status.MPI_SOURCE;
// Count of MPI_PACKED type returns bytes
MPI_Get_count(&status, MPI_PACKED, &msg_size);
source_rank = status.MPI_SOURCE;
MPI_Unpack(pack_buffer, msg_size, &position, &work->start_row,1,MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(pack_buffer, msg_size, &position, &work->num_rows,1,MPI_INT, MPI_COMM_WORLD);
num_pixels = msg_size - position; // Assumes pixel channels are 1 byte each
MPI_Unpack(pack_buffer, msg_size, &position, work->pixels, num_pixels, MPI_UNSIGNED_CHAR, MPI_COMM_WORLD);
return tag;
}
void slave_pack_and_send(WORK_DATA *work, FRAC_INFO *frac_info, char *pack_buffer, int buffer_size)
{
int position;
//pack and send work
position = 0;
MPI_Pack(&work->start_row,1,MPI_INT,pack_buffer, buffer_size,&position, MPI_COMM_WORLD);
MPI_Pack(&work->num_rows,1,MPI_INT,pack_buffer, buffer_size,&position, MPI_COMM_WORLD);
MPI_Pack(work->pixels, work->num_rows*frac_info->num_cols*frac_info->channels, MPI_UNSIGNED_CHAR, pack_buffer, buffer_size, &position, MPI_COMM_WORLD);
MPI_Send(pack_buffer, position, MPI_PACKED, 0, WORKTAG, MPI_COMM_WORLD);
}
int slave_recv_and_unpack(WORK_DATA *work, char *pack_buffer, int buffer_size)
{
int tag, position, msg_size;
MPI_Status status;
// Recieve and unpack work
MPI_Recv(pack_buffer, buffer_size, MPI_PACKED, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
// Check tag for work/die
tag = status.MPI_TAG;
if(tag == DIETAG) {
return tag;
}
position = 0;
MPI_Get_count(&status, MPI_PACKED, &msg_size);
MPI_Unpack(pack_buffer, msg_size, &position, &work->start_row,1,MPI_INT,MPI_COMM_WORLD);
MPI_Unpack(pack_buffer, msg_size, &position, &work->num_rows,1,MPI_INT,MPI_COMM_WORLD);
return tag;
}
void master(render_t *render_state, FRAC_INFO *frac_left, FRAC_INFO *frac_right, texture_t *texture_state)
{
int ntasks, dest, side;
WORK_DATA work_send;
WORK_DATA work_recv;
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
// Maximum sizes
int left_size, right_size;
left_size = get_max_work_size(frac_left);
right_size = get_max_work_size(frac_right);
unsigned long max_work_size = left_size > right_size ? left_size : right_size;
// Allocate buffer to hold received pixel data
size_t size = sizeof(unsigned char) * max_work_size;
work_recv.pixels = (unsigned char*)malloc(size);
if(!work_recv.pixels) {
printf("row pixel buffer allocation failed, %lu bytes\n", size);
exit(1);
}
// Allocate pack buffer
int member_size, empty_size, full_size;
int position;
char *buffer;
MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &member_size);
empty_size = member_size;
MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &member_size);
empty_size += member_size;
MPI_Pack_size(max_work_size, MPI_UNSIGNED_CHAR, MPI_COMM_WORLD, &member_size);
full_size = empty_size + member_size;
buffer = malloc(full_size);
if(!buffer) {
printf("buffer allocation failed, %d bytes\n",full_size);
exit(1);
}
FRAC_INFO *frac;
// Send initial data
for (dest = 1; dest < ntasks; dest++) {
// Rank 1 handles the left fractal
if(dest==1)
frac = frac_left;
else
frac = frac_right;
//Get next work item
get_work(frac, &work_send);
// Set destination to send work to
work_send.rank = dest;
// Pack work and send
master_pack_and_send(&work_send, buffer, empty_size);
}
printf("sent initial work\n");
int num_ranks_complete = 0;
while(num_ranks_complete < ntasks-1) {
// Receive work load and unpack
master_recv_and_unpack(&work_recv, buffer, full_size);
if(work_recv.rank==1){
frac = frac_left;
side = LEFT;
}
else {
frac = frac_right;
side = RIGHT;
}
// Update texture with recieved buffer
update_fractal_rows(texture_state, side, work_recv.start_row, work_recv.num_rows, work_recv.pixels);
// Check user input
check_user_input(texture_state->gl_state);
// Kill if needed
if(window_should_close(texture_state->gl_state)) {
for(dest=1; dest<ntasks; dest++)
MPI_Send(0,0,MPI_INT,dest,DIETAG,MPI_COMM_WORLD);
break;
}
// Render exit menu
if(render_state->quit_mode)
render_exit_menu(render_state->exit_menu_state, render_state->mouse_x, render_state->mouse_y);
// Swap buffers
swap_ogl(texture_state->gl_state);
// Get more work
work_send.rank = work_recv.rank;
get_work(frac, &work_send);
// Send more work or kill slaves
if(work_send.num_rows > 0)
master_pack_and_send(&work_send, buffer, empty_size);
else{
MPI_Send(0,0,MPI_INT,work_send.rank,DIETAG,MPI_COMM_WORLD);
num_ranks_complete++;
}
}
free(buffer);
free(work_recv.pixels);
}
void slave(FRAC_INFO *frac_info)
{
MPI_Status status;
int tag, rank;
WORK_DATA work;
work.pixels = (unsigned char*)malloc(get_max_work_size(frac_info)*sizeof(unsigned char));
// Allocate buffers
int member_size, empty_size, full_size;
int position;
char *buffer;
MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &member_size);
empty_size = member_size;
MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &member_size);
empty_size += member_size;
MPI_Pack_size(get_max_work_size(frac_info), MPI_UNSIGNED_CHAR, MPI_COMM_WORLD, &member_size);
full_size = empty_size+member_size;
buffer = malloc(full_size);
while(1) {
// Receive work load and unpack
tag = slave_recv_and_unpack(&work, buffer, empty_size);
// Check tag for work/die
if(tag == DIETAG) {
free(work.pixels);
free(buffer);
return;
}
// calcPixels
if(frac_info->color)
calcColorPixels(frac_info, &work);
else
calcPixels(frac_info, &work);
// Pack and send data back
slave_pack_and_send(&work, frac_info, buffer, full_size);
}
}