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

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

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

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

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

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

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

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

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

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

  for ( i = 0; i < nPrefStreams; i++ )
    {
84
85
86
87
      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
88
89
    }
  
90
91
92
  bfd->nextOpIndex = 0;
  bfd->prefIndex = 0; 
  bfd->offset = 0;
93
  bfd->activeCollectors = nc;
Deike Kleberg's avatar
Deike Kleberg committed
94

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

97
  return bfd;
Deike Kleberg's avatar
Deike Kleberg committed
98
99
100
101
}

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

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

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

  /* close file */

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

      if ( ddebug )
	startTime = MPI_Wtime ();

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

      if ( ddebug )
	accumSuspend += ( MPI_Wtime () - startTime);
134
135
136

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

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

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

  /* file closed, cleanup */

149
  dbuffer_cleanup ( &( bfd->fb ));
Deike Kleberg's avatar
Deike Kleberg committed
150

151
  free(bfd->ctrlBlks);
152
  free(bfd);
Deike Kleberg's avatar
Deike Kleberg committed
153

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

  return iret;
}

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

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

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

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

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

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

183
184
185
  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
186

187
  if ( ddebug ) startTime = MPI_Wtime ();
Deike Kleberg's avatar
Deike Kleberg committed
188

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

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

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

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

      if ( ddebug )
	startTime = MPI_Wtime ();

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

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

228
      bfd->prefIndex --;
Deike Kleberg's avatar
Deike Kleberg committed
229
230
    }

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

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

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

245
246
/***************************************************************/

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

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

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

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

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

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

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

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

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

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
317
	  memcpy ( bfd->fb->buffer + ( bfd->currOpIndex * bfd->size ),
318
                   pMB, ( int ) 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
	  MPI_Recv (  bfd->fb->buffer + ( bfd->currOpIndex * bfd->size ), 
363
		      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:
 */