pio_posixasynch.c 11.4 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
extern double accumSuspend;
extern double accumWrite;


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

53
static int
54
fileIDTest(void *a, void *fileID)
55
{
56
  return ((bFiledataPA *)a)->fileID == (int)(intptr_t)fileID;
57
58
}

59
int nPrefStreams = 4;
Deike Kleberg's avatar
Deike Kleberg committed
60
61
62

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

63
64
static bFiledataPA *
initBFiledataPA(char *filename, size_t bs, int nc)
Deike Kleberg's avatar
Deike Kleberg committed
65
{
66
  bFiledataPA *bfd;
Deike Kleberg's avatar
Deike Kleberg committed
67
68
  int i;

69
70
  xdebug ( "filename=%s, buffersize=%zu, ncollectors=%d, nPrefetchStreams=%d",
           filename, bs, nc, nPrefStreams );
Deike Kleberg's avatar
Deike Kleberg committed
71

72
73
  bfd = xmalloc( sizeof (*bfd) + strlen(filename) + 1);
  strcpy(bfd->name, filename);
74
  bfd->size = bs;
Deike Kleberg's avatar
Deike Kleberg committed
75

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

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

81
  bfd->ctrlBlks = xcalloc(nPrefStreams, sizeof (bfd->ctrlBlks[0]));
Deike Kleberg's avatar
Deike Kleberg committed
82
83
84

  for ( i = 0; i < nPrefStreams; i++ )
    {
85
86
87
88
      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
89
90
    }
  
91
92
93
94
  bfd->nextOpIndex = 0;
  bfd->prefIndex = 0; 
  bfd->offset = 0;
  bfd->finished = false;
95
  bfd->nfinished = xmalloc ( nc * sizeof ( bfd->nfinished[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
96

Deike Kleberg's avatar
Deike Kleberg committed
97
  for ( i = 0; i < nc; i++ )
98
    bfd->nfinished[i] = false;
Deike Kleberg's avatar
Deike Kleberg committed
99

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

102
  return bfd;
Deike Kleberg's avatar
Deike Kleberg committed
103
104
105
106
}

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

107
108
static int
destroyBFiledataPA ( void *v )
Deike Kleberg's avatar
Deike Kleberg committed
109
{
110
  bFiledataPA *bfd = (bFiledataPA * ) v;
Deike Kleberg's avatar
Deike Kleberg committed
111
112
  const struct aiocb *ccBP[1];
  int iret = 0;
Thomas Jahns's avatar
Thomas Jahns committed
113
  ssize_t ssiret;
114
115
  int nextFinishOp = (bfd->nextOpIndex - bfd->prefIndex + nPrefStreams)
    % nPrefStreams;
116
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
117

Thomas Jahns's avatar
Thomas Jahns committed
118
  xdebug ( "filename=%s, cleanup and close file", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
119
120
121

  /* close file */

122
  for (; bfd->prefIndex > 0 ; bfd->prefIndex --)
Deike Kleberg's avatar
Deike Kleberg committed
123
    {
Thomas Jahns's avatar
Thomas Jahns committed
124
      xdebug("file: %s, prefIndex=%d", bfd->name, (int)bfd->prefIndex);
125
      ccBP[0] = ( bfd->ctrlBlks + nextFinishOp );
Deike Kleberg's avatar
Deike Kleberg committed
126
127
128
129
130
131
132

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
133
	  if ( iret < 0 && errno != EINTR ) xabort ( "aio_suspend () failed" );
134
	}
135
      while ( iret != 0 );
Deike Kleberg's avatar
Deike Kleberg committed
136
137
138

      if ( ddebug )
	accumSuspend += ( MPI_Wtime () - startTime);
139
140
141

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

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

147
148
  if ((iret = ftruncate(bfd->handle, bfd->offset)) == -1)
    xabort("failed to truncate file %s: %s", bfd->name, strerror(errno));
149
  if (( iret = close ( bfd->handle )) == -1 )
Thomas Jahns's avatar
Thomas Jahns committed
150
    xabort("failed to close %s", bfd->name);
Deike Kleberg's avatar
Deike Kleberg committed
151
152
153

  /* file closed, cleanup */

154
  dbuffer_cleanup ( &( bfd->fb ));
Deike Kleberg's avatar
Deike Kleberg committed
155

156
157
  free ( bfd->nfinished );
  free ( bfd->ctrlBlks );
158
  free(bfd);
Deike Kleberg's avatar
Deike Kleberg committed
159

160
  xdebug("%s", "closed file and cleaned up, return");
Deike Kleberg's avatar
Deike Kleberg committed
161
162
163
164
165
166

  return iret;
}

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

167
168
static bool
compareNamesBPA(void *v1, void *v2)
169
{
170
  bFiledataPA *bfd1 = v1, *bfd2 = v2;
171

172
  return !strcmp(bfd1->name, bfd2->name);
173
174
175
176
}

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

177
178
static void
writePA(bFiledataPA *bfd, long amount)
Deike Kleberg's avatar
Deike Kleberg committed
179
180
181
{
  const struct aiocb *ccBP[1];
  int iret;
182
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
183

184
  xdebug ( "file %s, in", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
185
  
186
187
  bfd->ctrlBlks[bfd->currOpIndex].aio_nbytes = amount;
  bfd->ctrlBlks[bfd->currOpIndex].aio_offset = bfd->offset;
Deike Kleberg's avatar
Deike Kleberg committed
188

189
190
191
  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
192

193
  if ( ddebug ) startTime = MPI_Wtime ();
Deike Kleberg's avatar
Deike Kleberg committed
194

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

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

199
200
201
202
  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 );
203
204
205
206
207
   
  if ( iret == -1 ) 
    {
      xabort ( "did not succeed writing buffer" );
    }
208
209
210
  else
    xdebug ( "buffer written to %s",  bfd->name );
     
211
212
  bfd->offset += ( off_t ) amount;
  bfd->prefIndex ++;
Deike Kleberg's avatar
Deike Kleberg committed
213

214
  if ( bfd->prefIndex >= nPrefStreams ) 
Deike Kleberg's avatar
Deike Kleberg committed
215
    {
216
      ccBP[0] = ( bfd->ctrlBlks + bfd->nextOpIndex );
Deike Kleberg's avatar
Deike Kleberg committed
217
218
219
220
221
222
223
224

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
	  if ( iret < 0 && errno != EINTR )
225
	    xabort ( "aio_suspend () failed" );
Deike Kleberg's avatar
Deike Kleberg committed
226
227
228
229
230
	} while ( iret != 0 );

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

234
      bfd->prefIndex --;
Deike Kleberg's avatar
Deike Kleberg committed
235
236
    }

237
  xdebug ( "filename=%s, prefIndex=%d, return", bfd->name, bfd->prefIndex );
Deike Kleberg's avatar
Deike Kleberg committed
238
239
240
}

/***************************************************************/
241
static void
242
elemCheck(void *q, void *nm)
243
{
244
245
  bFiledataPA *bfd = q;
  const char *name = nm;
246

247
248
  if (!strcmp(name, bfd->name))
    xabort("Filename %s is already enqueued\n", name);
249
}
Deike Kleberg's avatar
Deike Kleberg committed
250

251
252
/***************************************************************/

253
void pioWriterAIO(void)
Deike Kleberg's avatar
Deike Kleberg committed
254
{
255
  bFiledataPA *bfd; 
Deike Kleberg's avatar
Deike Kleberg committed
256
  queue_t * bibBFiledataPA;
Deike Kleberg's avatar
Deike Kleberg committed
257
258
  long amount, buffersize;
  char *messageBuffer, *pMB, *filename, *temp;
Deike Kleberg's avatar
Deike Kleberg committed
259
  int messagesize, source, tag, i, id;
260
  struct fileOpTag rtag;
Deike Kleberg's avatar
Deike Kleberg committed
261
  MPI_Status status;
262
263
  MPI_Comm commNode = commInqCommNode ();
  int nProcsCollNode = commInqSizeNode () - commInqSizeColl ();
264
  bool * sentFinalize, doFinalize;
Deike Kleberg's avatar
Deike Kleberg committed
265

266
  if ( nPrefStreams < 1 ) xabort("USAGE: # PREFETCH STREAMS >= 1");
267
  xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode );
Deike Kleberg's avatar
Deike Kleberg committed
268
 
269
  bibBFiledataPA = queueInit ( destroyBFiledataPA, compareNamesBPA );
270
  sentFinalize = xmalloc ( nProcsCollNode * sizeof ( sentFinalize ));
Deike Kleberg's avatar
Deike Kleberg committed
271
272
  
  for ( ;; )
273
274
275
276
    {   
      xmpiStat ( MPI_Probe ( MPI_ANY_SOURCE, MPI_ANY_TAG, commNode, 
                             &status ), &status );

Deike Kleberg's avatar
Deike Kleberg committed
277
278
      source = status.MPI_SOURCE;
      tag    = status.MPI_TAG;
279
      rtag = decodeFileOpTag(tag);
Deike Kleberg's avatar
Deike Kleberg committed
280

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

283
      xdebug ( "receive message from source=%d, id=%d, command=%d ( %s ), "
284
285
               "messagesize=%d", source, rtag.id, rtag.command,
               command2charP[rtag.command], messagesize);
Deike Kleberg's avatar
Deike Kleberg committed
286

287
      switch (rtag.command)
Deike Kleberg's avatar
Deike Kleberg committed
288
289
	{
      	case IO_Open_file:
Deike Kleberg's avatar
Deike Kleberg committed
290

291
292
	  messageBuffer = ( char *) xmalloc ( messagesize * 
                                              sizeof ( messageBuffer[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
293
294
	  pMB = messageBuffer;

295
296
	  xmpi ( MPI_Recv ( messageBuffer, messagesize, MPI_CHAR, source, 
                            tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
297
298
299
300
301
302
303
304

	  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 );

305
306
	  xdebug("command  %s, filename=%s, buffersize=%ld, amount=%ld",
                 command2charP[rtag.command], filename, buffersize, amount);
Deike Kleberg's avatar
Deike Kleberg committed
307

308
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
309
                               (void *)(intptr_t)rtag.id)))
Deike Kleberg's avatar
Deike Kleberg committed
310
	    {
311
              queueForeach(bibBFiledataPA, elemCheck, filename);
312
	      bfd = initBFiledataPA(filename, buffersize, nProcsCollNode);
Thomas Jahns's avatar
Thomas Jahns committed
313
              if ((id = queueAdd(bibBFiledataPA, bfd)) < 0)
314
                xabort("fileID=%d not unique", rtag.id);
Deike Kleberg's avatar
Deike Kleberg committed
315
	    }
316
	  else
Thomas Jahns's avatar
Thomas Jahns committed
317
	    if (strcmp(filename, bfd->name) != 0)
318
              xabort("filename is not consistent, fileID=%d", rtag.id);
Deike Kleberg's avatar
Deike Kleberg committed
319

320
321
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
322

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

326
	  writePA ( bfd, amount );
Thomas Jahns's avatar
Thomas Jahns committed
327

Deike Kleberg's avatar
Deike Kleberg committed
328
329
330
331
332
	  free ( messageBuffer );

	  break;

	case IO_Send_buffer:
333

334
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
335
336
                               (void *)(intptr_t)rtag.id)))
            xabort("fileID=%d is not in queue", rtag.id);
337

Deike Kleberg's avatar
Deike Kleberg committed
338
339
	  amount = messagesize;

340
341
	  xdebug("command: %s, id=%d, name=%s",
                 command2charP[rtag.command], rtag.id, bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
342

343
344
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
345
	  
346
347
	  xmpi(MPI_Recv((void *)bfd->ctrlBlks[bfd->currOpIndex].aio_buf,
                        amount, MPI_CHAR, source, tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
348

349
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
350
351
352
353
	  
	  break;

	case IO_Close_file:
354

355
          if (!(bfd = queueGet(bibBFiledataPA, fileIDTest,
356
357
                               (void *)(intptr_t)rtag.id)))
            xabort("fileID=%d is not in queue", rtag.id);
358

Deike Kleberg's avatar
Deike Kleberg committed
359
360
	  amount = messagesize;

361
362
	  xdebug(" command %s, id=%d, name=%s",
                 command2charP[rtag.command], rtag.id, bfd->name);
Deike Kleberg's avatar
Deike Kleberg committed
363

364
	  bfd->currOpIndex = bfd->nextOpIndex;
Deike Kleberg's avatar
Deike Kleberg committed
365

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

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

371
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
372

373
	  bfd->nfinished[source] = true;
374
	  bfd->finished          = true;
Deike Kleberg's avatar
Deike Kleberg committed
375
	  
376
	  for ( i = 0; i < nProcsCollNode; i++ )
377
	    if ( !( bfd->nfinished[i] ))
Deike Kleberg's avatar
Deike Kleberg committed
378
	      {
379
		bfd->finished = false;
Deike Kleberg's avatar
Deike Kleberg committed
380
381
382
		break;
	      }
	  
383
	  if ( bfd->finished )
Deike Kleberg's avatar
Deike Kleberg committed
384
	    {
385
              xdebug ( "all are finished with file %d, delete node", rtag.id);
386
              queueDelNode(bibBFiledataPA, fileIDTest,
387
                           (void *)(intptr_t)rtag.id);
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
	    }
          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 )
              {
405
406
                if (!queueIsEmpty(bibBFiledataPA))
                  xabort("Queue bibBfiledataP is not empty.");
407
408
                else
                  {
409
410
                    xdebug("%s", "all files are finished, destroy queue,"
                           " return");
411
412
413
414
415
416
417
418
419
                    queueDestroy ( bibBFiledataPA );
                  }
                return;
              }
          }

          break;
        default:
          xabort ( "COMMAND NOT IMPLEMENTED" );
Deike Kleberg's avatar
Deike Kleberg committed
420
421
422
423
424
425
426
427
	}
    }
}



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

428
429
/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
430
431

#endif
432
#endif
433
434
435
436
437
438
439
440
441
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */