pio_posixasynch.c 11 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
{
  struct dBuffer *fb;
  struct aiocb *ctrlBlks;
  off_t offset;
  int currOpIndex;
  int nextOpIndex;
  int prefIndex;
46
  int activeCollectors;
47
  int handle, fileID;
48
  char name[];
49
} bFiledataPA;
Deike Kleberg's avatar
Deike Kleberg committed
50

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

57
int nPrefStreams = 4;
Deike Kleberg's avatar
Deike Kleberg committed
58
59
60

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

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

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

70
71
  bfd = xmalloc( sizeof (*bfd) + strlen(filename) + 1);
  strcpy(bfd->name, filename);
Deike Kleberg's avatar
Deike Kleberg committed
72

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

76
  dbuffer_init(&(bfd->fb), (size_t)(nPrefStreams * bs));
Deike Kleberg's avatar
Deike Kleberg committed
77

78
  bfd->ctrlBlks = xcalloc(nPrefStreams, sizeof (bfd->ctrlBlks[0]));
Deike Kleberg's avatar
Deike Kleberg committed
79
80
81

  for ( i = 0; i < nPrefStreams; i++ )
    {
82
      bfd->ctrlBlks[i].aio_fildes     = bfd->handle;
83
      bfd->ctrlBlks[i].aio_buf = bfd->fb->buffer + i * bs;
84
85
      bfd->ctrlBlks[i].aio_reqprio    = 0;
      bfd->ctrlBlks[i].aio_sigevent.sigev_notify = SIGEV_NONE;   
Deike Kleberg's avatar
Deike Kleberg committed
86
87
    }
  
88
89
90
  bfd->nextOpIndex = 0;
  bfd->prefIndex = 0; 
  bfd->offset = 0;
91
  bfd->activeCollectors = nc;
Deike Kleberg's avatar
Deike Kleberg committed
92

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

95
  return bfd;
Deike Kleberg's avatar
Deike Kleberg committed
96
97
98
99
}

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

100
101
static int
destroyBFiledataPA ( void *v )
Deike Kleberg's avatar
Deike Kleberg committed
102
{
103
  bFiledataPA *bfd = (bFiledataPA * ) v;
Deike Kleberg's avatar
Deike Kleberg committed
104
105
  const struct aiocb *ccBP[1];
  int iret = 0;
Thomas Jahns's avatar
Thomas Jahns committed
106
  ssize_t ssiret;
107
108
  int nextFinishOp = (bfd->nextOpIndex - bfd->prefIndex + nPrefStreams)
    % nPrefStreams;
109
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
110

Thomas Jahns's avatar
Thomas Jahns committed
111
  xdebug ( "filename=%s, cleanup and close file", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
112
113
114

  /* close file */

115
  for (; bfd->prefIndex > 0 ; --(bfd->prefIndex))
Deike Kleberg's avatar
Deike Kleberg committed
116
    {
Thomas Jahns's avatar
Thomas Jahns committed
117
      xdebug("file: %s, prefIndex=%d", bfd->name, (int)bfd->prefIndex);
118
      ccBP[0] = ( bfd->ctrlBlks + nextFinishOp );
Deike Kleberg's avatar
Deike Kleberg committed
119
120
121
122
123
124
125

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
126
	  if ( iret < 0 && errno != EINTR ) xabort ( "aio_suspend () failed" );
127
	}
128
      while ( iret != 0 );
Deike Kleberg's avatar
Deike Kleberg committed
129
130
131

      if ( ddebug )
	accumSuspend += ( MPI_Wtime () - startTime);
132
133
134

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

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

140
141
  if ((iret = ftruncate(bfd->handle, bfd->offset)) == -1)
    xabort("failed to truncate file %s: %s", bfd->name, strerror(errno));
142
  if (( iret = close ( bfd->handle )) == -1 )
Thomas Jahns's avatar
Thomas Jahns committed
143
    xabort("failed to close %s", bfd->name);
Deike Kleberg's avatar
Deike Kleberg committed
144
145
146

  /* file closed, cleanup */

147
  dbuffer_cleanup ( &( bfd->fb ));
Deike Kleberg's avatar
Deike Kleberg committed
148

149
  free(bfd->ctrlBlks);
150
  free(bfd);
Deike Kleberg's avatar
Deike Kleberg committed
151

152
  xdebug("%s", "closed file and cleaned up, return");
Deike Kleberg's avatar
Deike Kleberg committed
153
154
155
156
157
158

  return iret;
}

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

159
160
static bool
compareNamesBPA(void *v1, void *v2)
161
{
162
  bFiledataPA *bfd1 = v1, *bfd2 = v2;
163

164
  return !strcmp(bfd1->name, bfd2->name);
165
166
167
168
}

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

169
170
static void
writePA(bFiledataPA *bfd, long amount)
Deike Kleberg's avatar
Deike Kleberg committed
171
172
173
{
  const struct aiocb *ccBP[1];
  int iret;
174
  double startTime;
Deike Kleberg's avatar
Deike Kleberg committed
175

176
  xdebug ( "file %s, in", bfd->name );
Deike Kleberg's avatar
Deike Kleberg committed
177
  
178
179
  bfd->ctrlBlks[bfd->currOpIndex].aio_nbytes = amount;
  bfd->ctrlBlks[bfd->currOpIndex].aio_offset = bfd->offset;
Deike Kleberg's avatar
Deike Kleberg committed
180

181
182
183
  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
184

185
  if ( ddebug ) startTime = MPI_Wtime ();
Deike Kleberg's avatar
Deike Kleberg committed
186

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

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

191
192
193
194
  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 );
195
196
197
198
199
   
  if ( iret == -1 ) 
    {
      xabort ( "did not succeed writing buffer" );
    }
200
201
202
  else
    xdebug ( "buffer written to %s",  bfd->name );
     
203
204
  bfd->offset += ( off_t ) amount;
  bfd->prefIndex ++;
Deike Kleberg's avatar
Deike Kleberg committed
205

206
  if ( bfd->prefIndex >= nPrefStreams ) 
Deike Kleberg's avatar
Deike Kleberg committed
207
    {
208
      ccBP[0] = ( bfd->ctrlBlks + bfd->nextOpIndex );
Deike Kleberg's avatar
Deike Kleberg committed
209
210
211
212
213
214
215
216

      if ( ddebug )
	startTime = MPI_Wtime ();

      do
	{
	  iret = aio_suspend ( ccBP, 1, NULL );
	  if ( iret < 0 && errno != EINTR )
217
	    xabort ( "aio_suspend () failed" );
Deike Kleberg's avatar
Deike Kleberg committed
218
219
220
221
222
	} while ( iret != 0 );

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

226
      bfd->prefIndex --;
Deike Kleberg's avatar
Deike Kleberg committed
227
228
    }

229
  xdebug ( "filename=%s, prefIndex=%d, return", bfd->name, bfd->prefIndex );
Deike Kleberg's avatar
Deike Kleberg committed
230
231
232
}

/***************************************************************/
233
static void
234
elemCheck(void *q, void *nm)
235
{
236
237
  bFiledataPA *bfd = q;
  const char *name = nm;
238

239
  if (!strcmp(name, bfd->name))
240
    xabort("Filename %s has already been inserted\n", name);
241
}
Deike Kleberg's avatar
Deike Kleberg committed
242

243
244
/***************************************************************/

245
void pioWriterAIO(void)
Deike Kleberg's avatar
Deike Kleberg committed
246
{
247
  bFiledataPA *bfd; 
248
  listSet * bibBFiledataPA;
Deike Kleberg's avatar
Deike Kleberg committed
249
250
  long amount, buffersize;
  char *messageBuffer, *pMB, *filename, *temp;
Thomas Jahns's avatar
Thomas Jahns committed
251
  int messagesize, source, tag, id;
252
  struct fileOpTag rtag;
Deike Kleberg's avatar
Deike Kleberg committed
253
  MPI_Status status;
254
255
  MPI_Comm commNode = commInqCommNode ();
  int nProcsCollNode = commInqSizeNode () - commInqSizeColl ();
256
  bool * sentFinalize, doFinalize;
Deike Kleberg's avatar
Deike Kleberg committed
257

258
  if ( nPrefStreams < 1 ) xabort("USAGE: # PREFETCH STREAMS >= 1");
259
  xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode );
Deike Kleberg's avatar
Deike Kleberg committed
260
 
261
  bibBFiledataPA = listSetNew(destroyBFiledataPA, compareNamesBPA);
262
  sentFinalize = xmalloc ( nProcsCollNode * sizeof ( sentFinalize ));
Deike Kleberg's avatar
Deike Kleberg committed
263
264
  
  for ( ;; )
265
266
267
268
    {   
      xmpiStat ( MPI_Probe ( MPI_ANY_SOURCE, MPI_ANY_TAG, commNode, 
                             &status ), &status );

Deike Kleberg's avatar
Deike Kleberg committed
269
270
      source = status.MPI_SOURCE;
      tag    = status.MPI_TAG;
271
      rtag = decodeFileOpTag(tag);
Deike Kleberg's avatar
Deike Kleberg committed
272

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

275
      xdebug ( "receive message from source=%d, id=%d, command=%d ( %s ), "
276
277
               "messagesize=%d", source, rtag.id, rtag.command,
               command2charP[rtag.command], messagesize);
Deike Kleberg's avatar
Deike Kleberg committed
278

279
      switch (rtag.command)
Deike Kleberg's avatar
Deike Kleberg committed
280
281
	{
      	case IO_Open_file:
Deike Kleberg's avatar
Deike Kleberg committed
282

283
284
	  messageBuffer = ( char *) xmalloc ( messagesize * 
                                              sizeof ( messageBuffer[0] ));
Deike Kleberg's avatar
Deike Kleberg committed
285
286
	  pMB = messageBuffer;

287
288
	  xmpi ( MPI_Recv ( messageBuffer, messagesize, MPI_CHAR, source, 
                            tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
289
290
291
292
293
294
295
296

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

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

300
          if (!(bfd = listSetGet(bibBFiledataPA, fileIDTest,
301
                               (void *)(intptr_t)rtag.id)))
Deike Kleberg's avatar
Deike Kleberg committed
302
	    {
303
              listSetForeach(bibBFiledataPA, elemCheck, filename);
304
	      bfd = initBFiledataPA(filename, buffersize, nProcsCollNode);
305
              if ((id = listSetAdd(bibBFiledataPA, bfd)) < 0)
306
                xabort("fileID=%d not unique", rtag.id);
307
              bfd->fileID = id;
Deike Kleberg's avatar
Deike Kleberg committed
308
	    }
309
	  else
Thomas Jahns's avatar
Thomas Jahns committed
310
	    if (strcmp(filename, bfd->name) != 0)
311
              xabort("filename is not consistent, fileID=%d", rtag.id);
Deike Kleberg's avatar
Deike Kleberg committed
312

313
314
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
315

316
317
318
          xassert(amount >= 0);
	  memcpy((void *)bfd->ctrlBlks[bfd->currOpIndex].aio_buf,
                 pMB, (size_t)amount);
Deike Kleberg's avatar
Deike Kleberg committed
319

320
	  writePA ( bfd, amount );
Thomas Jahns's avatar
Thomas Jahns committed
321

Deike Kleberg's avatar
Deike Kleberg committed
322
323
324
325
326
	  free ( messageBuffer );

	  break;

	case IO_Send_buffer:
327

328
          if (!(bfd = listSetGet(bibBFiledataPA, fileIDTest,
329
                               (void *)(intptr_t)rtag.id)))
330
            xabort("fileID=%d is not in set", rtag.id);
331

Deike Kleberg's avatar
Deike Kleberg committed
332
333
	  amount = messagesize;

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

337
338
	  bfd->currOpIndex = bfd->nextOpIndex;
	  bfd->nextOpIndex = ( bfd->nextOpIndex + 1 ) % nPrefStreams;
Deike Kleberg's avatar
Deike Kleberg committed
339
	  
340
341
	  xmpi(MPI_Recv((void *)bfd->ctrlBlks[bfd->currOpIndex].aio_buf,
                        amount, MPI_CHAR, source, tag, commNode, &status ));
Deike Kleberg's avatar
Deike Kleberg committed
342

343
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
344
345
346
347
	  
	  break;

	case IO_Close_file:
348

349
          if (!(bfd = listSetGet(bibBFiledataPA, fileIDTest,
350
                               (void *)(intptr_t)rtag.id)))
351
            xabort("fileID=%d is not in set", rtag.id);
352

Deike Kleberg's avatar
Deike Kleberg committed
353
354
	  amount = messagesize;

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

358
	  bfd->currOpIndex = bfd->nextOpIndex;
Deike Kleberg's avatar
Deike Kleberg committed
359

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

362
363
	  MPI_Recv((void *)bfd->ctrlBlks[bfd->currOpIndex].aio_buf,
                   amount, MPI_CHAR, source, tag, commNode, &status);
Deike Kleberg's avatar
Deike Kleberg committed
364

365
	  writePA ( bfd, amount );
Deike Kleberg's avatar
Deike Kleberg committed
366

367
	  if ( ! --(bfd->activeCollectors))
Deike Kleberg's avatar
Deike Kleberg committed
368
	    {
369
              xdebug ( "all are finished with file %d, delete node", rtag.id);
370
371
              listSetRemove(bibBFiledataPA, fileIDTest,
                            (void *)(intptr_t)rtag.id);
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
	    }
          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 )
              {
389
390
                if (!listSetIsEmpty(bibBFiledataPA))
                  xabort("Set bibBfiledataP is not empty.");
391
392
                else
                  {
393
                    xdebug("%s", "all files are finished, destroy set,"
394
                           " return");
395
                    listSetDelete(bibBFiledataPA);
396
397
398
399
400
401
402
403
                  }
                return;
              }
          }

          break;
        default:
          xabort ( "COMMAND NOT IMPLEMENTED" );
Deike Kleberg's avatar
Deike Kleberg committed
404
405
406
407
408
409
410
411
	}
    }
}



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

412
413
/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
414
415

#endif
416
#endif
417
418
419
420
421
422
423
424
425
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */