This repository was archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWMpiComm.h
375 lines (282 loc) · 8.34 KB
/
MWMpiComm.h
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// MWMpiComm.h
//
#ifndef __MWMpiComm_h
#define __MWMpiComm_h
#include <vector>
#include "MWRMComm.h"
#ifdef USING_MPI
#include "mpi.h"
#endif
class MWMpiComm;
#ifdef USING_MPI
/** This class is used to manage the status of messages within
* MWMpiComm.
*/
class MsgStatus
{
friend class MWMpiComm;
protected:
MsgStatus(int size) {nbytes=0; buf.resize(size);}
public:
void reset() {nbytes=0; index=0; id=-1;}
void resize(unsigned int num)
{
if ((index+num) > buf.size())
buf.resize(std::max(2*buf.size(), index+num));
}
std::vector<char> buf;
MPI_Request request;
int id;
int nbytes;
int index;
};
/** This class is an derived from the Resource Management (RM) and
Communication (Comm) layer.
*/
class MWMpiComm : public MWRMComm
{
public:
/** @name Main Routines */
//@{
/// Constructor. Sets data to -1's.
MWMpiComm();
/// Destructor...
virtual ~MWMpiComm() {}
//@}
/** @name Resource Management Functions */
//@{
///
void exit( int exitval );
///
int setup( int& argc, char**& argv, int *my_id, int *master_id,
MPI_Comm comm_);
///
int setup( int& argc, char**& argv, int *my_id, int *master_id)
{return setup(argc,argv,my_id,master_id,MPI_COMM_WORLD);}
///
// TODO int config( int *nhosts, int *narches, MWWorkerID ***w ) {return 0;}
/** Start a worker on a machine that has been given to you.
This is really only important if the process of starting
a worker is two-stage. For instance, in pvm, you
first get a machine. THEN you have to spawn an
executable on that machine. For something like MW-files,
once you get a worker it already has the executable
started on it. In that case this will basically be
a no-op.
@param w A pointer to a MWWorkerID. This must point
to allocated memory. It is filled with info like the
machine name, id2, and the arch.
@return id2 on success, -1 on failure
*/
virtual int start_worker( MWWorkerID<MWMpiComm>* w );
///
int init_workers();
///
int restart_workers() {return 0;}
/** Configure a single worker */
void config_worker(MWWorkerID<MWMpiComm>* ); // TODO
/** Remove a worker from the virtual machine. This call
will delete w, so don't reference that memory again!
@param w The MWWorkerID of the machine to remove.
@return 0 on success, a negative number on failure
*/
virtual int removeWorker( MWWorkerID<MWMpiComm>* w ) {return -1;} // TODO
//@}
/** @name Host Management Members */
//@{
///
int hostaddlogic( std::vector<int>& )
{return 0;}
//@}
/**@name Communication Routines */
//@{
/// TODO
void hostadd ( )
{ }
///
void who ( int *wh );
///
int initsend ( int encoding = 0 );
///
int send ( int to_whom, int msgtag );
///
bool recv ( int from_whom, int msgtag, int &buf_id );
///
int bufinfo ( int buf_id, int *len, int *tag, int *from );
///
void get_packinfo(int& bytes_packed, int& bytes_unpacked);
/** @name Pack/UnPack Functions */
//@{
/// float
int pack ( float *f, int nitem=1, int stride = 1 )
{return do_pack(f,nitem,stride);}
/// double
int pack ( double *d, int nitem=1, int stride = 1 )
{return do_pack(d,nitem,stride);}
/// int
int pack ( int *i, int nitem=1, int stride = 1 )
{return do_pack(i,nitem,stride);}
/// unsigned int
int pack ( unsigned int *ui, int nitem=1, int stride = 1 )
{return do_pack(ui,nitem,stride);}
/// short
int pack ( short *sh, int nitem=1, int stride = 1 )
{return do_pack(sh,nitem,stride);}
/// unsigned short
int pack ( unsigned short *ush, int nitem=1, int stride = 1 )
{return do_pack(ush,nitem,stride);}
/// long
int pack ( long *l, int nitem=1, int stride = 1 )
{return do_pack(l,nitem,stride);}
/// unsigned long
int pack ( unsigned long *ul, int nitem=1, int stride = 1 )
{return do_pack(ul,nitem,stride);}
/// Pack some bytes
int pack ( const char *bytes, int nitem, int stride = 1 )
{return do_pack(bytes,nitem,stride);}
/// Pack a NULL-terminated string
int pack ( char* string );
/// Pack a NULL-terminated string
int pack ( const char *string );
///
int pack ( const std::string& str )
{return pack(str.data());}
///
int pack_explicit( const char* bytes, int nitem);
/// float
int unpack ( float *f, int nitem=1, int stride = 1 )
{return do_unpack(f,nitem,stride);}
/// double
int unpack ( double *d, int nitem=1, int stride = 1 )
{return do_unpack(d,nitem,stride);}
/// int
int unpack ( int *i, int nitem=1, int stride = 1 )
{return do_unpack(i,nitem,stride);}
/// unsigned int
int unpack ( unsigned int *ui, int nitem=1, int stride = 1 )
{return do_unpack(ui,nitem,stride);}
/// short
int unpack ( short *sh, int nitem=1, int stride = 1 )
{return do_unpack(sh,nitem,stride);}
/// unsigned short
int unpack ( unsigned short *ush, int nitem=1, int stride = 1 )
{return do_unpack(ush,nitem,stride);}
/// long
int unpack ( long *l, int nitem=1, int stride = 1 )
{return do_unpack(l,nitem,stride);}
/// unsigned long
int unpack ( unsigned long *ul, int nitem=1, int stride = 1 )
{return do_unpack(ul,nitem,stride);}
/// bytes
int unpack ( char *bytes, int nitem, int stride = 1 )
{return do_unpack(bytes,nitem,stride);}
/// Unpack a NULL-terminated string
int unpack ( char *string );
/// Unpack a string object
int unpack ( string& str );
/// Used to hand off the unpack buffer to a seperate application
const char* unpack_buffer()
{return &(received.msg->buf[0]);}
/// Used to hand off the unpack buffer to a seperate application
int unpack_index()
{return received.index;}
/// Used to hand off the unpack buffer to a seperate application
int unpack_buffer_size()
{return received.msg->nbytes;}
/** Used to update the unpack buffer after it has been used by a seperate
application.
*/
void unpack_index(int index)
{received.index = index;}
//@}
//@}
protected:
/// Pack some bytes
template <class TYPE>
int do_pack(TYPE* bytes, int nitem, int stride);
/// Unpack some bytes
template <class TYPE>
int do_unpack(TYPE* data, int nitem, int stride);
///
int bytes_packed;
///
int bytes_unpacked;
#ifdef USING_MPI
/// True if MWMpiComm calls MPI_Init()
bool comm_initialized_locally;
///
int num_send_msgs;
///
int num_recv_msgs;
///
int bufsize;
/// The set of messages that have been asynchronously sent out
std::vector<MsgStatus*> outgoing_msgs;
/// The message that is being prepared to send out.
MsgStatus* curr_send_msg;
/// The most recently received message
struct {
public:
MsgStatus* msg;
int tag;
int source;
int index;
} received;
///
int rank;
///
int size;
///
int iDoIO;
///
int ioProc;
///
MPI_Comm comm;
///
template <class TYPE>
MPI_Datatype mpi_datatype()
{return MPI_DATATYPE_NULL;}
#endif
/// The ID of the next worker ... configured by config_worker
int next_worker;
};
template <class TYPE>
inline int MWMpiComm::do_pack(TYPE* data, int nitem, int stride)
{
if (mpi_datatype<TYPE>() == MPI_DATATYPE_NULL)
abort();
int size_;
MPI_Pack_size(1, mpi_datatype<TYPE>(), MPI_COMM_WORLD, &size_);
curr_send_msg->resize(size_*nitem);
int start = curr_send_msg->index;
for ( int i = 0; i < nitem; i++ ) {
int tmpsize = curr_send_msg->buf.size();
MPI_Pack((void*)(&data[i*stride]), 1, mpi_datatype<TYPE>(),
&(curr_send_msg->buf[0]), tmpsize,
&(curr_send_msg->index), comm);
}
bytes_packed += (curr_send_msg->index-start);
MWprintf(90,"packing bytes=%d nitem=%d type=%d\n",
(curr_send_msg->index-start), nitem, mpi_datatype<TYPE>());
return 0;
}
template <class TYPE>
int MWMpiComm::do_unpack(TYPE* data, int nitem, int stride)
{
int start = received.index;
for ( int i = 0; i < nitem; i++ ) {
MPI_Unpack( &(received.msg->buf[0]),
received.msg->nbytes,
&(received.index),
(void*)(&data[i*stride]),
1, mpi_datatype<TYPE>(),
comm);
}
bytes_unpacked += (received.index - start);
MWprintf(90,"unpacked bytes=%d nitem=%d type=%d\n",
(received.index-start), nitem, mpi_datatype<TYPE>());
return 0;
}
#endif
#endif