-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_xfer_tasks.cpp
382 lines (279 loc) · 10.9 KB
/
io_xfer_tasks.cpp
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
376
377
378
379
380
//-----------------------------------------------------------------------bl-
//--------------------------------------------------------------------------
//
// datasort - an IO/data distribution utility for large data sorts.
//
// Copyright (C) 2013 Karl W. Schulz
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Version 2.1 GNU Lesser General
// Public License as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc. 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
//-----------------------------------------------------------------------el-
#include "sortio.h"
// --------------------------------------------------------------------
// Transfer_Tasks_Work(): work manager for data transfer tasks
//
// This method configured to run on IO_COMM
// --------------------------------------------------------------------
void sortio_Class::Transfer_Tasks_Work()
{
assert(initialized_);
const int USLEEP_INTERVAL = 10000; // sleep interval if no data available to send
int tagXFER = 1000; // initial MPI message tag
int numTransferredFiles = 0;
int count = 0;
bool waitFlag;
int tagLocal;
int iter;
int bufNum;
std::vector<int> buffersPacked;
int messageSize;
int destRank;
int numFilesToSend;
MPI_Request requestHandle0;
MPI_Request requestHandle;
std::vector<int> fullQueueCounts(numIoTasks_,0);
std::vector<int> destRanks (numIoTasks_,-1);
std::vector<int> messageTags (numIoTasks_,0);
nextDestRank_ = numIoTasks_; // initialize first destination rank
// before we begin main xfer loop, we wait for the first read to
// occur on master_io rank and distribute the file size (assumed
// constant for now)
unsigned long int initialRecordsPerFile;
if(isMasterIO_)
{
for(int iter=0;iter<50;iter++)
{
if(isFirstRead_)
usleep(100000);
else
break;
}
if(isFirstRead_)
MPI_Abort(MPI_COMM_WORLD,43);
initialRecordsPerFile = recordsPerFile_;
}
// Note: # of records for 100 MB file = 1000000
assert( MPI_Bcast(&initialRecordsPerFile,1,MPI_UNSIGNED_LONG,0,XFER_COMM) == MPI_SUCCESS );
messageSize = initialRecordsPerFile*REC_SIZE;
assert(messageSize <= MAX_FILE_SIZE_IN_MBS*1000*1000);
if(isMasterIO_)
grvy_printf(INFO,"[sortio][IO/XFER] Message size for XFERS = %i\n",messageSize);
// Begin main data xfer loop -----------------------------------------------
while (true)
{
// Transfer completed? We are done when we have received all the files *and* there
// are no more outstanding messages active on any XFER sending processes
if(numTransferredFiles == numFilesTotal_)
{
// Before we stop, make sure all processes have empty
// message queues
int remainingLocal = messageQueue_.size();
int remainingGlobal = 1;
assert (MPI_Allreduce(&remainingLocal,&remainingGlobal,1,MPI_INTEGER,MPI_SUM,IO_COMM) == MPI_SUCCESS);
if(isMasterIO_)
grvy_printf(DEBUG,"[sortio][IO/XFER] All files sent, total # of outstanding messages = %i\n",
remainingGlobal);
if(remainingGlobal == 0) // <-- finito
break;
}
// Gather up which processors have data available to send
int localCount = fullQueue_.size();
assert (MPI_Gather(&localCount,1,MPI_INTEGER,fullQueueCounts.data(),1,MPI_INTEGER,0,IO_COMM) == MPI_SUCCESS);
// Assign destination ranks and message tags (master IO rank is
// tasked with this bookkeeping)
int numBuffersToTransfer = 0;
if(isMasterIO_)
{
for(int i=0;i<numIoTasks_;i++)
if(fullQueueCounts[i] >= 1)
{
destRanks[i] = CycleDestRank();
//messageTags[i] = ++tagXFER;
tagXFER += 2;
//tagXFER += maxMessagesToSend_+1;
messageTags[i] = tagXFER;
numBuffersToTransfer++;
}
}
// distribute data from master_io on next set of destination ranks, message tags, etc
//printf("koomie prior to bcast.....\n");
assert( MPI_Bcast(&numBuffersToTransfer,1,MPI_INT,0,IO_COMM) == MPI_SUCCESS );
if(numBuffersToTransfer > 0)
{
MPI_Scatter(destRanks.data(), 1,MPI_INT,&destRank,1,MPI_INT,0,IO_COMM);
MPI_Scatter(messageTags.data(),1,MPI_INT,&tagLocal,1,MPI_INT,0,IO_COMM);
}
if( (numBuffersToTransfer > 0) && isMasterIO_)
grvy_printf(DEBUG,"[sortio][IO/XFER] Number of hosts with data to send = %3i -> iter = %i\n",
numBuffersToTransfer,count);
// Send one buffer's worth of data from each IO task that has it
// available locally; if no processors are ready yet, iterate
// and check again
numFilesToSend = 0;
if( numBuffersToTransfer > 0)
{
if(localCount > 0)
{
// step 1: check that we do not have an overwhelming
// number of messages in flight from this host; if we
// are over a runtime-specified watermark, let's stall
// and flush the local message queue;
grvy_printf(DEBUG,"[sortio][IO/XFER][%.4i] outstanding sends = %i\n",ioRank_,messageQueue_.size());
checkForSendCompletion(waitFlag=true,MAX_MESSAGES_WATERMARK,iter=count);
assert( (int)messageQueue_.size() <= MAX_MESSAGES_WATERMARK);
// step 2: lock the oldest data transfer buffer on this processor
std::vector<int> buffersPacked;
// step2a: koomie test; sleep in case where we only have 1 buffer in use (to avoid repeated lock
// contention with reader task);
#if 1
if(fullQueue_.size() == 1)
{
grvy_printf(INFO,"[sortio][IO/XFER][%.4i] sleeping when fullQueue size = 1\n",ioRank_);
///usleep(1000000);
usleep(100000);
}
#endif
#pragma omp critical (IO_XFER_UPDATES_lock) // Thread-safety: all queue updates are locked
{
bufNum = fullQueue_.front();
fullQueue_.pop_front();
numFilesToSend = 1;
buffersPacked.push_back(bufNum);
// look for possiblity of more sequential data to send...
int bufNumNext = bufNum + 1;
for(int i=1;i<maxMessagesToSend_;i++)
{
if(fullQueue_.size() == 0)
break;
if(fullQueue_.front() == bufNumNext)
{
fullQueue_.pop_front();
numFilesToSend++;
buffersPacked.push_back(bufNumNext);
bufNumNext++;
}
else
break;
}
grvy_printf(INFO,"[sortio][IO/XFER][%.4i] removed %i buffers (%i->%i) from fullQueue\n",
ioRank_,numFilesToSend,bufNum,bufNumNext);
}
assert(buffers_[bufNum] != NULL);
// step3: send buffers to XFER ranks asynchronously
int payLoadSize = numFilesToSend*messageSize;
//usleep(200000); // debug testing koomie to force multipe file xfers at small scale (hack)
MPI_Bsend(&payLoadSize,1,MPI_INT,destRank,tagLocal,XFER_COMM);
MPI_Isend(buffers_[bufNum],payLoadSize,
MPI_UNSIGNED_CHAR,destRank,tagLocal+1,XFER_COMM,&requestHandle);
grvy_printf(DEBUG,"[sortio][IO/XFER][%.4i] issued iSend to rank %i (tag = %i)\n",
ioRank_,destRank,tagLocal);
// queue up these messages as being in flight
MsgRecord message(buffersPacked,requestHandle);
messageQueue_.push_back(message);
// verifyMode = 1 -> dump data sent to compare against
// input (fixme todo; likely broken with variable message size)
if(verifyMode_ == 1)
{
char filename[1024];
sprintf(filename,"./parttosend%i",numTransferredFiles+ioRank_);
FILE *fp = fopen(filename,"wb");
assert(fp != NULL);
fwrite(&buffers_[bufNum][0],sizeof(char),messageSize,fp);
fclose(fp);
}
}
} // end if (numBuffersToTransfer > 0)
// All IO ranks keep track of total number of files transferred;
// since we may have sent more than 1 file from a process, tally
// up the total sent here
int numFilesSentTotal;
assert (MPI_Allreduce(&numFilesToSend,&numFilesSentTotal,1,MPI_INTEGER,MPI_SUM,IO_COMM) == MPI_SUCCESS);
//numTransferredFiles += numBuffersToTransfer;
numTransferredFiles += numFilesSentTotal;
// Check for any completed messages prior to next iteration
checkForSendCompletion(waitFlag=false,0,iter=count);
count++;
} // end xfer of all files
if(isMasterIO_)
grvy_printf(INFO,"[sortio][IO/XFER][%.4i]: data XFER COMPLETED\n",ioRank_);
// gather up amount delivered
#if 0
int dataTransferredLocal = 0;
MPI_Allreduce(&dataTransferredLocal,&dataTransferred_,1,MPI_LONG,MPI_SUM,XFER_COMM);
#endif
fflush(NULL);
return;
}
// -------------------------------------------------------------------------
// checkForSendCompletion() Check on messages in flight and free up
// data transfer buffers for any which have completed.
// -------------------------------------------------------------------------
void sortio_Class::checkForSendCompletion(bool waitFlag, int waterMark, int iter)
{
// a NOOP if we have no outstanding messages (or if the number of
// outstanding message is less than provided waterMark). To wait for
// all outstanding messages, enable waitFlag and set waterMark=0
//
// note that waterMark has no impact if !waitFlag
if(messageQueue_.size() == 0 || ( (int)messageQueue_.size() <= waterMark) )
return;
std::list<MsgRecord>::iterator it = messageQueue_.begin();
while(it != messageQueue_.end() )
{
int messageCompleted, bufNum;
std::vector<int> bufNums;
MPI_Status status;
MPI_Request handle;
bufNums = it->getBufNums();
handle = it->getHandle();
// check if send has completed
MPI_Test(&handle,&messageCompleted,&status);
if(messageCompleted)
{
#ifdef DEBUG
for(int i=0;i<bufNums.size();i++)
grvy_printf(DEBUG,"[sortio][IO/XFER][%.4i] message from buf %i complete (iter=%i)\n",
ioRank_,bufNums[i],iter);
#endif
it = messageQueue_.erase(it++);
// re-enable these buffers for eligibility
for(int i=0;i<bufNums.size();i++)
addBuffertoEmptyQueue(bufNums[i]);
}
else if(waitFlag)
{
#ifdef DEBUG
for(int i=0;i<bufNums.size();i++)
grvy_printf(INFO,"[sortio][IO/XFER][%.4i] Stalling for previously unfinished iSend (buf=%i,iter=%i)\n",
ioRank_,bufNums[i],iter);
#endif
MPI_Wait(&handle,&status);
it = messageQueue_.erase(it++);
for(int i=0;i<bufNums.size();i++)
addBuffertoEmptyQueue(bufNums[i]);
}
else
++it; // <-- message still active
}
return;
}
int sortio_Class::CycleDestRank()
{
int startingDestRank = nextDestRank_;
nextDestRank_++;
if(nextDestRank_ >= numXferTasks_)
nextDestRank_ = numIoTasks_;
return(startingDestRank);
}