-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsop-mpi.c
292 lines (258 loc) · 6.13 KB
/
sop-mpi.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
/*
* =====================================================================================
*
* Filename: sop-mpi.c
*
* Description: mpi implementation helpers
*
* Version: 1.0
* Created: 11/11/2012 10:46:12 AM
* Revision: none
* Compiler: gcc
*
* Author: Robert David (davidrob@fit.cvut.cz), Vaclav Sajdl (sajdlvac@fit.cvut.cz)
* Organization: FIT
*
* =====================================================================================
*/
#include "sop.h"
#include "set.h"
#include "stack.h"
/*
* flaten the data struct
*/
void * pack_winner( winner_set * winner )
{
void * help = malloc( winner_size * sizeof(int) );
int i;
int pos=0;
MPI_Pack( &winner->result, 1, MPI_INT, help, winner_size*sizeof(int), &pos, MPI_COMM_WORLD );
MPI_Pack( &winner->num, 1, MPI_INT, help, winner_size*sizeof(int), &pos, MPI_COMM_WORLD );
for( i=0 ; i<input_a ; i++ )
{
MPI_Pack( &winner->set[i].num, 1, MPI_INT, help, winner_size*sizeof(int), &pos, MPI_COMM_WORLD );
MPI_Pack( winner->set[i].member, input_n, MPI_INT, help, winner_size*sizeof(int), &pos, MPI_COMM_WORLD );
}
return help;
}
/*
* parse data struct from flaten data
*/
winner_set * unpack_winner( void * buffer )
{
winner_set * help = initial_winner();
int i;
int pos=0;
MPI_Unpack( buffer, winner_size*sizeof(int), &pos, &help->result, 1, MPI_INT , MPI_COMM_WORLD );
MPI_Unpack( buffer, winner_size*sizeof(int), &pos, &help->num, 1, MPI_INT , MPI_COMM_WORLD );
for( i=0 ; i<input_a ; i++ )
{
MPI_Unpack( buffer, winner_size*sizeof(int), &pos, &help->set[i].num, 1, MPI_INT , MPI_COMM_WORLD );
MPI_Unpack( buffer, winner_size*sizeof(int), &pos, help->set[i].member, input_n, MPI_INT , MPI_COMM_WORLD );
}
free(buffer);
return help;
}
/*
* call finish to all other proceses
*/
void send_finish( )
{
char msg=0;
int rank,nr;
int i;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &nr );
for( i=0 ; i<nr ; i++ )
{
if( i!=rank ) MPI_Send( &msg, 1, MPI_CHAR, i, WINNER_FINISH, MPI_COMM_WORLD );
}
}
/*
* send work asking message
*/
inline void send_ask( int proc )
{
char msg=color;
MPI_Send( &msg, 1, MPI_CHAR, proc, WINNER_ASK, MPI_COMM_WORLD );
}
/*
* recieve computation data
*/
winner_set * get_winner( int source, int flag )
{
MPI_Status status;
void * buffer = malloc( winner_size * sizeof(int) );
MPI_Recv( buffer, winner_size, MPI_INT, source, flag, MPI_COMM_WORLD, &status );
return unpack_winner( buffer );
}
/*
* send computation data with max sum, only caled at the end
*/
inline void send_max( void )
{
void * packed;
packed = pack_winner( max_winner );
MPI_Send( packed, winner_size, MPI_INT, 0, WINNER_MAX, MPI_COMM_WORLD );
free(packed);
}
/*
* collect all max data and set the maximal one
*/
void recieve_max( void )
{
int nr;
winner_set * help;
MPI_Comm_size( MPI_COMM_WORLD, &nr );
while(--nr)
{
/* handle realy slow procs*/
MPI_Send( &nr, 1, MPI_INT, nr, WINNER_MAX, MPI_COMM_WORLD );
help = get_winner( nr, WINNER_MAX );
if( help->result > max_winner->result )
{
clean_winner( max_winner );
max_winner = help;
}
else
{
clean_winner( help );
}
}
}
/*
* provide our stack when whe have spare
*/
void provide_stack( stack_struct * stack )
{
MPI_Status status;
int flag,tmp;
void * packed;
/* only provide spare stack items */
while( stack_size(stack)>1 )
{
MPI_Iprobe( MPI_ANY_SOURCE, WINNER_ASK, MPI_COMM_WORLD, &flag, &status );
if(flag)
{
MPI_Recv( &tmp, 1, MPI_CHAR, MPI_ANY_SOURCE, WINNER_ASK, MPI_COMM_WORLD, &status);
packed = pack_winner(stack_bottom(stack));
MPI_Send( packed, winner_size, MPI_INT, status.MPI_SOURCE, WINNER_SEND, MPI_COMM_WORLD );
free(packed);
color = BLACK;
}
else return;
}
}
/*
* handler for special messages, this is done in evry cycle
*/
int regular_listener( void )
{
MPI_Status status;
int flag,tmp;
MPI_Iprobe( MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status );
if(flag)
switch(status.MPI_TAG)
{
case WINNER_FINISH :
return 1;
case PESEK :
MPI_Recv( &tmp, 1, MPI_INT, MPI_ANY_SOURCE, PESEK, MPI_COMM_WORLD, &status);
have_pesek = 1;
return 0;
}
return 0;
}
/*
* self explanatory
*/
inline void send_pesek( int proc )
{
char msg=color;
MPI_Send( &msg, 1, MPI_CHAR, proc, PESEK, MPI_COMM_WORLD );
}
/*
* ugly but functional dynamic work handler
*/
int ask_for_stack( stack_struct * stack )
{
int rank,nr,flag,tmp;
winner_set * recived;
MPI_Status status;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &nr );
/* do nothing if there is only one proc */
if( nr == 1 ) return 1;
/* dijkstras pesek implemented exactly from the lectures */
if( rank == 0 )
{
color = WHITE;
send_pesek( 1 );
have_pesek = 0;
}
else
{
if( have_pesek )
{
send_pesek( (rank+1)%nr );
color=WHITE;
have_pesek = 0;
}
}
/* ask for work */
send_ask( (rank+1)%nr );
/* ugly,ugly active waiting for job */
while(1)
{
MPI_Iprobe( MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status );
if(flag)
/* check for all possible tags */
switch(status.MPI_TAG)
{
case WINNER_FINISH :
return 1;
case WINNER_SEND :
recived = get_winner( MPI_ANY_SOURCE, WINNER_SEND );
stack_push( stack, recived );
return 0;
case WINNER_DONOTHAVE :
MPI_Recv( &tmp, 1, MPI_INT, status.MPI_SOURCE, WINNER_DONOTHAVE, MPI_COMM_WORLD, &status);
send_ask( (rank+1)%nr );
break;
case WINNER_ASK :
MPI_Recv( &tmp, 1, MPI_INT, status.MPI_SOURCE, WINNER_ASK, MPI_COMM_WORLD, &status);
MPI_Send( &flag, 1, MPI_INT, status.MPI_SOURCE, WINNER_DONOTHAVE, MPI_COMM_WORLD );
break;
case PESEK :
MPI_Recv( &tmp, 1, MPI_INT, MPI_ANY_SOURCE, PESEK, MPI_COMM_WORLD, &status);
have_pesek = 1;
break;
}
/* another peseks handler */
if( have_pesek )
{
if( rank == 0 )
{
if( tmp == WHITE )
{
send_finish();
return 1;
}
else
{
color = WHITE;
send_pesek( 1 );
have_pesek = 0;
}
}
else
{
send_pesek( (rank+1)%nr );
color=WHITE;
have_pesek = 0;
}
}
}
return 1;
}
/* vim: set ts=2 sw=2 :*/