pio_posixasynch.c 11.7 KB
Newer Older
1
2
3
4
/* 
   todo  
   README: specialRank Pe closes down, when all output files are closed    
*/
5
6
7
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
Deike Kleberg's avatar
Deike Kleberg committed
8

9
10

#ifdef USE_MPI
11
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
12
13
14
15

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
16
#include <unistd.h>
Deike Kleberg's avatar
Deike Kleberg committed
17
18
19
#include <errno.h>

#include <aio.h>
20
21
#include <sys/types.h>
#include <sys/stat.h>
Deike Kleberg's avatar
Deike Kleberg committed
22
23
24
25
#include <fcntl.h>

#include "mpi.h"
#include "pio.h"
26
#include "pio_comm.h"
Deike Kleberg's avatar
Deike Kleberg committed
27
#include "pio_impl.h"
Deike Kleberg's avatar
Deike Kleberg committed
28
29
#include "pio_util.h"

30
extern char * command2charP[6];
Deike Kleberg's avatar
Deike Kleberg committed
31

32
33
extern char *token;

Deike Kleberg's avatar
Deike Kleberg committed
34
35
36
37
38
39
40
41
extern double accumProbe;
extern double accumRecv;
extern double accumSend;
extern double accumSuspend;
extern double accumWait;
extern double accumWrite;


42
typedef struct
Deike Kleberg's avatar
Deike Kleberg committed
43
44
45
46
47
48
49
50
51
52
53
{
  char *name;
  size_t size;
  struct dBuffer *fb;
  struct aiocb *ctrlBlks;
  off_t offset;
  int currOpIndex;
  int nextOpIndex;
  int prefIndex;
  bool finished;
  bool *nfinished;
54
  int handle, fileID;
55
} bFiledataPA;
Deike Kleberg's avatar
Deike Kleberg committed
56

57
static int
58
fileIDTest(void *a, void *fileID)
59
{
60
  return ((bFiledataPA *)a)->fileID == (int)(intptr_t)fileID;
61
62
}

63
int nPrefStreams = 4;
Deike Kleberg's avatar
Deike Kleberg committed
64
65
66

/***************************************************************/

67
68
static bFiledataPA *
initBFiledataPA(char *filename, size_t bs, int nc)
Deike Kleberg's avatar
Deike Kleberg committed
69
{
70
  bFiledataPA *bfd;
Deike Kleberg's avatar
Deike Kleberg committed
71
72
  int i;

73
74
  xdebug ( "filename=%s, buffersize=%zu, ncollectors=%d, nPrefetchStreams=%d",
           filename, bs, nc, nPrefStreams );
Deike Kleberg's avatar
Deike Kleberg committed
75

76
  bfd = xmalloc ( sizeof ( bfd[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
77

Thomas Jahns's avatar
Thomas Jahns committed
78
  bfd->name = xmalloc (( strlen ( filename ) + 1) * sizeof ( bfd->name[0] ));
79
80
  strcpy ( bfd->name, filename );
  bfd->size = bs;
Deike Kleberg's avatar
Deike Kleberg committed
81

82
  if (( bfd->handle = open ( bfd->name, O_CREAT | O_WRONLY, 0666 )) == -1 )
83
    xabort("Failed to open %s", bfd->name);
Deike Kleberg's avatar
Deike Kleberg committed
84

85
  dbuffer_init ( &( bfd->fb ), ( size_t )( nPrefStreams * bfd->size ));
Deike Kleberg's avatar
Deike Kleberg committed
86

87
  bfd->ctrlBlks = xcalloc(nPrefStreams, sizeof (bfd->ctrlBlks[0]));
Deike Kleberg's avatar
Deike Kleberg committed
88
89
90

  for ( i = 0; i < nPrefStreams; i++ )
    {
91
92
93
94
      bfd->ctrlBlks[i].aio_fildes     = bfd->handle;
      bfd->ctrlBlks[i].aio_buf        = bfd->fb->buffer + ( i * bfd->size );
      bfd->ctrlBlks[i].aio_reqprio    = 0;
      bfd->ctrlBlks[i].aio_sigevent.sigev_notify = SIGEV_NONE;   
Deike Kleberg's avatar
Deike Kleberg committed
95
96
    }
  
97
98
99
100
  bfd->nextOpIndex = 0;
  bfd->prefIndex = 0; 
  bfd->offset = 0;
  bfd->finished = false;
101
  bfd->nfinished = xmalloc ( nc * sizeof ( bfd->nfinished[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
102

Deike Kleberg's avatar
Deike Kleberg committed
103
  for ( i = 0; i < nc; i++ )
104
    bfd->nfinished[i] = false;
Deike Kleberg's avatar
Deike Kleberg committed
105

106
  xdebug ( "filename=%s, opened file, return", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
107

108
  return bfd;
Deike Kleberg's avatar
Deike Kleberg committed
109
110
111
112
}

/***************************************************************/

113
114
static int
destroyBFiledataPA ( void *v )
Deike Kleberg's avatar
Deike Kleberg committed
115
{
116
  bFiledataPA *bfd = (bFiledataPA * ) v;
Deike Kleberg's avatar
Deike Kleberg committed
117
118
  const struct aiocb *ccBP[1];
  int iret = 0;
Thomas Jahns's avatar
Thomas Jahns committed
119
  ssize_t ssiret;
120
121
  int nextFinishOp = (bfd->nextOpIndex - bfd->prefIndex + nPrefStreams)
    % nPrefStreams;
122
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
123

Thomas Jahns's avatar
Thomas Jahns committed
124
  xdebug ( "filename=%s, cleanup and close file", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
125
126
127

  /* close file */

128
  for (; bfd->prefIndex > 0 ; bfd->prefIndex --)
Deike Kleberg's avatar
Deike Kleberg committed
129
    {
Thomas Jahns's avatar
Thomas Jahns committed
130
      xdebug("file: %s, prefIndex=%d", bfd->name, (int)bfd->prefIndex);
131
      ccBP[0] = ( bfd->ctrlBlks + nextFinishOp );
Deike Kleberg's avatar
Deike Kleberg committed
132
133
134
135
136
137
138

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
139
	  if ( iret < 0 && errno != EINTR ) xabort ( "aio_suspend () failed" );
140
	}
141
      while ( iret != 0 );
Deike Kleberg's avatar
Deike Kleberg committed
142
143
144

      if ( ddebug )
	accumSuspend += ( MPI_Wtime () - startTime);
145
146
147

      iret = aio_error(bfd->ctrlBlks + nextFinishOp);
      if (( ssiret = aio_return ( bfd->ctrlBlks + nextFinishOp )) == -1 )
Thomas Jahns's avatar
Thomas Jahns committed
148
	xabort("aio_return () failed: %s", strerror(iret));
149
150

      nextFinishOp = ( nextFinishOp + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
151
152
    }

153
154
  if ((iret = ftruncate(bfd->handle, bfd->offset)) == -1)
    xabort("failed to truncate file %s: %s", bfd->name, strerror(errno));
155
  if (( iret = close ( bfd->handle )) == -1 )
Thomas Jahns's avatar
Thomas Jahns committed
156
    xabort("failed to close %s", bfd->name);
Deike Kleberg's avatar
Deike Kleberg committed
157
158
159

  /* file closed, cleanup */

160
  dbuffer_cleanup ( &( bfd->fb ));
Deike Kleberg's avatar
Deike Kleberg committed
161

162
163
164
165
  free ( bfd->nfinished );
  free ( bfd->ctrlBlks );
  free ( bfd->name );
  free ( bfd );
Deike Kleberg's avatar
Deike Kleberg committed
166

167
  xdebug("%s", "closed file and cleaned up, return");
Deike Kleberg's avatar
Deike Kleberg committed
168
169
170
171
172
173

  return iret;
}

/***************************************************************/

174
175
static bool
compareNamesBPA(void *v1, void *v2)
176
{
177
  bFiledataPA *bfd1 = v1, *bfd2 = v2;
178

179
  return !strcmp(bfd1->name, bfd2->name);
180
181
182
183
}

/***************************************************************/

184
185
static void
writePA(bFiledataPA *bfd, long amount)
Deike Kleberg's avatar
Deike Kleberg committed
186
187
188
{
  const struct aiocb *ccBP[1];
  int iret;
189
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
190

191
  xdebug ( "file %s, in", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
192
  
193
194
  bfd->ctrlBlks[bfd->currOpIndex].aio_nbytes = amount;
  bfd->ctrlBlks[bfd->currOpIndex].aio_offset = bfd->offset;
Deike Kleberg's avatar
Deike Kleberg committed
195

196
197
198
  xdebug ( " before aio_write(), file %s, aio_nbytes=%zu, aio_offset=%zu",
           bfd->name, bfd->ctrlBlks[bfd->currOpIndex].aio_nbytes,
           bfd->ctrlBlks[bfd->currOpIndex].aio_offset );
Deike Kleberg's avatar
Deike Kleberg committed
199

200
  if ( ddebug ) startTime = MPI_Wtime ();
Deike Kleberg's avatar
Deike Kleberg committed
201

202
  iret = aio_write ( bfd->ctrlBlks + bfd->currOpIndex );
Deike Kleberg's avatar
Deike Kleberg committed
203

204
  if ( ddebug ) accumWrite += ( MPI_Wtime () - startTime);
Deike Kleberg's avatar
Deike Kleberg committed
205

206
207
208
209
  xdebug ( "after aio_write(), file %s, aio_nbytes=%zu, aio_offset=%zu,"
           "iret=aio_write()=%d",
           bfd->name, bfd->ctrlBlks[bfd->currOpIndex].aio_nbytes,
           bfd->ctrlBlks[bfd->currOpIndex].aio_offset, iret );
210
211
212
213
214
   
  if ( iret == -1 ) 
    {
      xabort ( "did not succeed writing buffer" );
    }
215
216
217
  else
    xdebug ( "buffer written to %s",  bfd->name );
     
218
219
  bfd->offset += ( off_t ) amount;
  bfd->prefIndex ++;
Deike Kleberg's avatar
Deike Kleberg committed
220

221
  if ( bfd->prefIndex >= nPrefStreams ) 
Deike Kleberg's avatar
Deike Kleberg committed
222
    {
223
      ccBP[0] = ( bfd->ctrlBlks + bfd->nextOpIndex );
Deike Kleberg's avatar
Deike Kleberg committed
224
225
226
227
228
229
230
231

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
	  if ( iret < 0 && errno != EINTR )
232
	    xabort ( "aio_suspend () failed" );
Deike Kleberg's avatar
Deike Kleberg committed
233
234
235
236
237
	} while ( iret != 0 );

      if ( ddebug )
	accumSuspend += ( MPI_Wtime () - startTime);
	      
238
      if (( iret = aio_return ( bfd->ctrlBlks + bfd->nextOpIndex )) == -1 ) 
239
	xabort ( "aio_return () failed" );
Deike Kleberg's avatar
Deike Kleberg committed
240

241
      bfd->prefIndex --;
Deike Kleberg's avatar
Deike Kleberg committed
242
243
    }

244
  xdebug ( "filename=%s, prefIndex=%d, return", bfd->name, bfd->prefIndex );
Deike Kleberg's avatar
Deike Kleberg committed
245
246
247
}

/***************************************************************/
248
static void
249
elemCheck(void *q, void *nm)
250
{
251
252
  bFiledataPA *bfd = q;
  const char *name = nm;
253

254
255
  if (!strcmp(name, bfd->name))
    xabort("Filename %s is already enqueued\n", name);
256
}
Deike Kleberg's avatar
Deike Kleberg committed
257

258
259
/***************************************************************/

260
void pioWriterAIO(void)
Deike Kleberg's avatar
Deike Kleberg committed
261
{
262
  bFiledataPA *bfd; 
Deike Kleberg's avatar
Deike Kleberg committed
263
  queue_t * bibBFiledataPA;
Deike Kleberg's avatar
Deike Kleberg committed
264
265
  long amount, buffersize;
  char *messageBuffer, *pMB, *filename, *temp;
Deike Kleberg's avatar
Deike Kleberg committed
266
  int messagesize, source, tag, i, id;
Deike Kleberg's avatar
Deike Kleberg committed
267
  tag_t *rtag;
Deike Kleberg's avatar
Deike Kleberg committed
268
  MPI_Status status;
269
270
  MPI_Comm commNode = commInqCommNode ();
  int nProcsCollNode = commInqSizeNode () - commInqSizeColl ();
271
  bool * sentFinalize, doFinalize;
Deike Kleberg's avatar
Deike Kleberg committed
272

273
  if ( nPrefStreams < 1 ) xabort("USAGE: # PREFETCH STREAMS >= 1");
274
  xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode );
Deike Kleberg's avatar
Deike Kleberg committed
275
 
276
  bibBFiledataPA = queueInit ( destroyBFiledataPA, compareNamesBPA );
277
  sentFinalize = xmalloc ( nProcsCollNode * sizeof ( sentFinalize ));
Deike Kleberg's avatar
Deike Kleberg committed
278
279
  
  for ( ;; )
280
281
282
283
    {   
      xmpiStat ( MPI_Probe ( MPI_ANY_SOURCE, MPI_ANY_TAG, commNode, 
                             &status ), &status );

Deike Kleberg's avatar
Deike Kleberg committed
284
285
      source = status.MPI_SOURCE;
      tag    = status.MPI_TAG;
Deike Kleberg's avatar
Deike Kleberg committed
286
      rtag   = getTag ( tag );
Deike Kleberg's avatar
Deike Kleberg committed
287

288
      xmpi ( MPI_Get_count ( &status, MPI_CHAR, &messagesize ));
Deike Kleberg's avatar
Deike Kleberg committed
289

290
291
292
293
      xdebug ( "receive message from source=%d, id=%d, command=%d ( %s ), "
               "messagesize=%d",
               source, rtag->id, rtag->command, 
               command2charP [ rtag->command ], messagesize);
Deike Kleberg's avatar
Deike Kleberg committed
294
295
296
297

      switch ( rtag->command )
	{
      	case IO_Open_file:
Deike Kleberg's avatar
Deike Kleberg committed
298

299
300
	  messageBuffer = ( char *) xmalloc ( messagesize * 
                                              sizeof ( messageBuffer[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
301
302
	  pMB = messageBuffer;

303
304
	  xmpi ( MPI_Recv ( messageBuffer, messagesize, MPI_CHAR, source, 
                            tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
305
306
307
308
309
310
311
312

	  filename = strtok ( pMB, token );
	  pMB += ( strlen ( filename ) + 1 );
	  temp =  strtok ( pMB, token );
	  buffersize =  strtol ( temp, NULL, 16 );
	  pMB += ( strlen ( temp ) + 1 );
	  amount = ( long ) ( messageBuffer + messagesize - pMB );

313
314
	  xdebug ( "command  %s, filename=%s, buffersize=%ld, amount=%ld",
                   command2charP [ rtag->command ], filename, 
315
                   buffersize, amount );
Deike Kleberg's avatar
Deike Kleberg committed
316

317
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
318
                               (void *)(intptr_t)rtag->id)))
Deike Kleberg's avatar
Deike Kleberg committed
319
	    {
320
              queueForeach(bibBFiledataPA, elemCheck, filename);
321
	      bfd = initBFiledataPA(filename, buffersize, nProcsCollNode);
Thomas Jahns's avatar
Thomas Jahns committed
322
              if ((id = queueAdd(bibBFiledataPA, bfd)) < 0)
Thomas Jahns's avatar
Thomas Jahns committed
323
                xabort("fileID=%d not unique", rtag->id);
Deike Kleberg's avatar
Deike Kleberg committed
324
	    }
325
	  else
Thomas Jahns's avatar
Thomas Jahns committed
326
327
	    if (strcmp(filename, bfd->name) != 0)
              xabort("filename is not consistent, fileID=%d", rtag->id);
Deike Kleberg's avatar
Deike Kleberg committed
328

329
330
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
331

Thomas Jahns's avatar
Thomas Jahns committed
332
	  memcpy ( bfd->fb->buffer + ( bfd->currOpIndex * bfd->size ),
333
                   pMB, ( int ) amount );
Deike Kleberg's avatar
Deike Kleberg committed
334

335
	  writePA ( bfd, amount );
Thomas Jahns's avatar
Thomas Jahns committed
336

Deike Kleberg's avatar
Deike Kleberg committed
337
338
339
340
341
	  free ( messageBuffer );

	  break;

	case IO_Send_buffer:
342

343
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
344
                               (void *)(intptr_t)rtag->id)))
345
346
            xabort("fileID=%d is not in queue", rtag->id);

Deike Kleberg's avatar
Deike Kleberg committed
347
348
	  amount = messagesize;

349
350
	  xdebug ( "command: %s, id=%d, name=%s", 
                   command2charP [ rtag->command ], rtag->id, bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
351

352
353
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
354
	  
355
356
	  xmpi(MPI_Recv((void *)bfd->ctrlBlks[bfd->currOpIndex].aio_buf,
                        amount, MPI_CHAR, source, tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
357

358
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
359
360
361
362
	  
	  break;

	case IO_Close_file:
363

364
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
365
                               (void *)(intptr_t)rtag->id)))
366
367
            xabort("fileID=%d is not in queue", rtag->id );

Deike Kleberg's avatar
Deike Kleberg committed
368
369
	  amount = messagesize;

370
	  xdebug ( " command %s, id=%d, name=%s", 
371
                   command2charP [ rtag->command ], rtag->id, bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
372

373
	  bfd->currOpIndex = bfd->nextOpIndex;
Deike Kleberg's avatar
Deike Kleberg committed
374

375
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
376

377
	  MPI_Recv (  bfd->fb->buffer + ( bfd->currOpIndex * bfd->size ), 
378
		      amount, MPI_CHAR, source,  tag, commNode, &status );
Deike Kleberg's avatar
Deike Kleberg committed
379

380
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
381

382
	  bfd->nfinished[source] = true;
383
	  bfd->finished          = true;
Deike Kleberg's avatar
Deike Kleberg committed
384
	  
385
	  for ( i = 0; i < nProcsCollNode; i++ )
386
	    if ( !( bfd->nfinished[i] ))
Deike Kleberg's avatar
Deike Kleberg committed
387
	      {
388
		bfd->finished = false;
Deike Kleberg's avatar
Deike Kleberg committed
389
390
391
		break;
	      }
	  
392
	  if ( bfd->finished )
Deike Kleberg's avatar
Deike Kleberg committed
393
	    {
394
              xdebug ( "all are finished with file %d, delete node", rtag->id );
395
              queueDelNode(bibBFiledataPA, fileIDTest,
396
                           (void *)(intptr_t)rtag->id);
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
	    }
          break;
        case IO_Finalize:
          {
            int buffer = CDI_UNDEFID, collID;

            xmpi ( MPI_Recv ( &buffer, 1, MPI_INT, source, tag, commNode, &status ));
            sentFinalize[source] = true;
            doFinalize = true;
            for ( collID = 0; collID < nProcsCollNode; collID++ )
              if ( !sentFinalize[collID] ) 
                {
                  doFinalize = false;
                  break;
                }
            if ( doFinalize )
              {
414
415
                if (!queueIsEmpty(bibBFiledataPA))
                  xabort("Queue bibBfiledataP is not empty.");
416
417
                else
                  {
418
419
                    xdebug("%s", "all files are finished, destroy queue,"
                           " return");
420
421
422
423
424
425
426
427
428
429
                    queueDestroy ( bibBFiledataPA );
                  }
                if ( rtag ) ungetTag ( rtag );
                return;
              }
          }

          break;
        default:
          xabort ( "COMMAND NOT IMPLEMENTED" );
Deike Kleberg's avatar
Deike Kleberg committed
430
431
432
433
434
435
436
437
	}
    }
}



/***************************************************************/

438
439
/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
440
441

#endif
442
#endif
443
444
445
446
447
448
449
450
451
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */