pio.c 10.7 KB
Newer Older
Deike Kleberg's avatar
Deike Kleberg committed
1
2
3
4
5
#ifndef NOMPI

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Deike Kleberg's avatar
Deike Kleberg committed
6
#include <string.h>
7
#include <unistd.h>
Deike Kleberg's avatar
Deike Kleberg committed
8
9
10
11
12
#include "mpi.h"
#include "cdi.h"
#include "pio.h"
#include "pio_impl.h"

13
bool ddebug = false;
Deike Kleberg's avatar
Deike Kleberg committed
14

15
16
17
char * command2charP[6] = {"IO_Open_file", "IO_Close_file",
			   "IO_Get_fp","IO_Set_fp",
			   "IO_Send_buffer", "IO_Finalize"};
Deike Kleberg's avatar
Deike Kleberg committed
18

19
20
21
long initial_buffersize = 16 * 1024 * 1024;
/*  4 KB <= x < 256 MB */ 
/* 16 * 1024 * 1024; */
22
/* 16 * 1024; */
23
/* 4 * 1024; */
Deike Kleberg's avatar
Deike Kleberg committed
24

25
26
int maxPtype = 6;
int maxNnodes  = 249;
27
28
int tagKey = 100;

Deike Kleberg's avatar
Deike Kleberg committed
29
30
31
32
33
34
35
36
37
38
double startTime;
double accumProbe   = 0.0;
double accumRecv    = 0.0;
double accumSend    = 0.0;
double accumSuspend = 0.0;
double accumWait    = 0.0;
double accumWrite   = 0.0;

pioInfo *pioinfo;

39
40
char *token = "%";

Deike Kleberg's avatar
Deike Kleberg committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*****************************************************************************/

void check_mpi ( int line, int iret )
{
  char error_string[MPI_MAX_ERROR_STRING+1];
  int len;

  if  ( iret != MPI_SUCCESS ) 
    {
      MPI_Error_string ( iret, error_string, &len ); 
      error_string[len] = '\0';
      fprintf ( stderr,"\nLine %8d MPI error %4d: %s\n\n", line, iret, 
		error_string ); 
    }

  return;
}

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
/*****************************************************************************/

void check_mpi_status ( int line, MPI_Comm *comm, MPI_Status *status, int iret )
{
  char error_string[MPI_MAX_ERROR_STRING];
  int len;

  if ( iret == MPI_ERR_IN_STATUS )
    {
      switch ( status->MPI_ERROR )
	{
	  fprintf ( stdout, "------- checking error in request ----------\n" );
	case MPI_SUCCESS :
	  fprintf ( stdout, "-------- mpi_success -----------\n" );
	  break;
	case MPI_ERR_PENDING:
	  fprintf ( stdout, "-------- mpi_err_pending ----------\n");
	  break;
	default:
	  MPI_Error_string ( status->MPI_ERROR, error_string, &len );
	  fprintf ( stdout, "---------- error in request, error string: --------\n");
	  fprintf ( stdout, error_string );
	  MPI_Abort ( *comm, 1 );
	}
    }
  else 
    check_mpi ( line, iret );
  
  return;
}

Deike Kleberg's avatar
Deike Kleberg committed
90
91
/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
92
93
int setTag ( int ID, int sc )
{
94
  return ID * tagKey + sc;
Deike Kleberg's avatar
Deike Kleberg committed
95
96
97
98
99
100
101
102
103
}

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

rTag * getTag ( int tag )
{
  rTag *rtag;

  rtag = ( rTag * ) malloc ( sizeof ( rTag ));
104
105
  rtag->id = tag / tagKey;
  rtag->command = tag % tagKey;
Deike Kleberg's avatar
Deike Kleberg committed
106
107
108
109
110
111

  return rtag;
}

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

112
113
114
115
116
117
118
119
void ungetTag ( rTag *rtag )
{
  free ( rtag );
  return;
}

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

Deike Kleberg's avatar
Deike Kleberg committed
120
121
122
123
124
125
size_t pioFileWrite ( int id, int tsId, const void *buffer, size_t len )
{
  size_t iret;

  switch ( pioinfo->type )
    {
126
127
    case PIO_MPI_NONB:
      iret = fwMPINONB ( id, tsId, buffer, len );
Deike Kleberg's avatar
Deike Kleberg committed
128
      break;
Deike Kleberg's avatar
Deike Kleberg committed
129
130
131
    case PIO_POSIX_ASYNCH:
      iret = fwPOSIXASYNCH ( id, tsId, buffer, len );
      break;
132
133
134
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fwPOSIXFPGUARDSENDRECV ( id, tsId, buffer, len );
      break;
135
136
137
138
139
140
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fwPOSIXFPGUARDTHREAD ( id, tsId, buffer, len );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fwPOSIXFPGUARDTHREADREFUSE ( id, tsId, buffer, len );
      break;
141
142
143
    case PIO_POSIX_NONB:
      iret = fwPOSIXNONB ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
144
145
146
147
148
149
150
151
152
153
154
155
156
    }

  return iret;
}

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

int pioFileClose ( int id )
{
  int iret;
  
  switch ( pioinfo->type )
    {
157
158
    case PIO_MPI_NONB:
      iret = fcMPINONB ( id );
Deike Kleberg's avatar
Deike Kleberg committed
159
      break;
Deike Kleberg's avatar
Deike Kleberg committed
160
161
162
    case PIO_POSIX_ASYNCH:
      iret = fcPOSIXASYNCH ( id );
      break;
163
164
165
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fcPOSIXFPGUARDSENDRECV ( id );
      break;
166
167
168
169
170
171
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fcPOSIXFPGUARDTHREAD ( id );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fcPOSIXFPGUARDTHREADREFUSE ( id );
      break;
172
173
174
    case PIO_POSIX_NONB:
      iret = fcPOSIXNONB ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
175
176
177
178
179
180
181
182
183
184
185
186
187
    }

  return iret;
}

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

int pioFileOpenW ( const char *filename )
{
  int iret;
  
  switch ( pioinfo->type )
    {
188
189
    case PIO_MPI_NONB:
      iret = fowMPINONB ( filename );
Deike Kleberg's avatar
Deike Kleberg committed
190
      break;
Deike Kleberg's avatar
Deike Kleberg committed
191
192
193
    case PIO_POSIX_ASYNCH:
      iret = fowPOSIXASYNCH ( filename );
      break;
194
195
196
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fowPOSIXFPGUARDSENDRECV ( filename );
      break;
197
198
199
200
201
202
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fowPOSIXFPGUARDTHREAD ( filename );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fowPOSIXFPGUARDTHREADREFUSE ( filename );
      break;
203
204
205
    case PIO_POSIX_NONB:
      iret = fowPOSIXNONB ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
206
207
208
209
210
211
212
    }
  
  return iret;
}

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

213
214
215
216
217
218
219
220
221
static int cmpr ( const void *a, const void *b ) 
{ 
  return strcmp ( *( char ** ) a, *( char ** ) b);
}

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

void setPioCommunicator ( MPI_Comm *myComm, MPI_Comm commF2C, int *color, 
			  int nnodes, int * myComm2F )
Deike Kleberg's avatar
Deike Kleberg committed
222
{
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  int size, rank, len, npes_node, key, test, i, j;

  char *myHost, **allHosts, *curr;
  char hostname [ MPI_MAX_PROCESSOR_NAME + 1 ];

  MPI_Comm_size ( commF2C, &size );
  MPI_Comm_rank ( commF2C, &rank );

  myHost = (char * ) malloc (( MPI_MAX_PROCESSOR_NAME + 1 ) * sizeof( char ));

  MPI_Get_processor_name ( myHost, &len );

  if ( ddebug )
    {
      strncpy ( hostname, myHost, len );
      hostname [ len ] = '\0';
      fprintf ( stdout, "pe%d: myHost = %s\n", rank,  hostname );
    }
Deike Kleberg's avatar
Deike Kleberg committed
241

Deike Kleberg's avatar
Deike Kleberg committed
242

243
244
245
246
247
  allHosts = ( char ** ) malloc ( size * sizeof ( char * ));
  allHosts[0] = ( char * ) malloc 
    ( size * MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
  for ( i = 1; i < size; i++ )
    allHosts[i] = allHosts[0] + i * MPI_MAX_PROCESSOR_NAME;
Deike Kleberg's avatar
Deike Kleberg committed
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
  MPI_Allgather ( myHost, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, 
		  &( allHosts[0][0] ), MPI_MAX_PROCESSOR_NAME, MPI_CHAR, commF2C );
  
  if ( ddebug )
    for ( i = 0; i < size; i++ )
      {
	strcpy ( hostname, &( allHosts[i][0] ));
	fprintf ( stdout, "pe%d: before qsort, allHosts[%d][0] = %s\n", 
		  rank, i, hostname );
      }  

  qsort ( allHosts, size, sizeof ( char * ), cmpr );
  
  if ( ddebug )
    for ( i = 0; i < size; i++ )
      {
	strcpy ( hostname, &( allHosts[i][0] ));
	fprintf ( stdout, "pe%d: after qsort, allHosts[%d][0] = %s\n", 
		  rank, i, hostname );
      }

  *color = 0;
  i = 0;
  j = 0;
  while ( ++j <= nnodes )
    {
      curr = allHosts[i];
      if (( test = strcmp ( myHost, curr )) == 0 ) *color = j;
      while (( ++i < size ) && 
	     (( test = strcmp ( allHosts[i], curr )) == 0));;    
    } 
Deike Kleberg's avatar
Deike Kleberg committed
280

281
282
283
284
285
286
  if ( *color <= 0 || *color > nnodes )
    {
      fprintf ( stderr, "color not set correctly\n" );
      MPI_Abort ( commF2C, 1 );
    }
  
Deike Kleberg's avatar
Deike Kleberg committed
287
288
289
  npes_node = size/nnodes;
  key = rank % npes_node;

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
  MPI_Comm_split ( commF2C, *color, key, myComm);
  MPI_Errhandler_set ( *myComm, MPI_ERRORS_RETURN );
  
  if ( ddebug )
    fprintf ( stdout, "pe%d, after mpi_comm_split, color=%d\n", 
	      rank, *color );

  *myComm2F = 0;
  if (( *myComm2F = MPI_Comm_c2f ( *myComm )) == 0 )
    {
      fprintf ( stderr, "mpi_comm_c2f didn't succeed\n");
      MPI_Abort ( commF2C , 1 );
    }
  
  if ( ddebug )
    fprintf ( stdout, "pe%d, after mpi_comm_c2f, color=%d\n", 
	      rank, *color );


  free ( allHosts[0] );
  free ( allHosts );
  free ( myHost );
  
  return;
}

#endif

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

int pioInit ( int ptype, int commF, int *color, int nnodes, int *pioComm, int *ncollectors )
{

  int collectingData = 1;
324
  int rank;
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

#ifndef NOMPI

  MPI_Comm comm;

  if ( ptype < 0 || ptype > maxPtype )
    {
      fprintf ( stderr, "1. arg of pioInit is no valid modus\n");
      MPI_Abort ( MPI_COMM_WORLD, 1 );
    }

  if (( comm = MPI_Comm_f2c (( MPI_Fint ) commF )) == NULL )
    {
      fprintf ( stderr, "2. arg of pioInit is no valid Fortran handle to a mpi communicator\n");
      MPI_Abort ( MPI_COMM_WORLD, 1 );
    }

  if ( nnodes < 1 || nnodes > maxNnodes )
    {
      fprintf ( stderr, "4. arg of pioInit is no valid no for nnodes\n");
      MPI_Abort ( MPI_COMM_WORLD, 1 );
    }

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

  if ( !(( ptype == PIO_NONE && nnodes == 1 ) ||
	 ( ptype == PIO_MPI_NONB ) ||
	 ( ptype == PIO_POSIX_FPGUARD_SENDRECV && nnodes == 1 ) ||
	 ( ptype == PIO_POSIX_NONB && nnodes == 1 )))
    {
      MPI_Comm_rank ( comm, &rank );     
      if ( rank == 0 )
	fprintf ( stderr, "\n\npioInit(): arg(1) PTYPE=%d and arg(4) NNODES=%d are not valid. \
\nPossible combinations are:\
\nptype = PIO_NONE (:=0), \t\t  nnodes = 1\
\nptype = PIO_MPI_NONB (:=1)\
\nptype = PIO_POSIX_FPGUARD_SENDRECV (:=3), \t nnodes = 1\
\nptype = PIO_POSIX_NONB (:=6), \t\t nnodes = 1\n\n\n",
		ptype, nnodes );
      MPI_Abort ( comm, 1 );
    }

366
367
368
369
370
371
372
373
374
  if (( pioinfo = ( pioInfo * ) malloc ( sizeof ( pioInfo ))) == NULL )
    {
      fprintf ( stderr, "malloc of pioInfo didn't succeed\n");
      MPI_Abort ( MPI_COMM_WORLD, 1 );
    }

  pioinfo->type = ptype; 
  pioinfo->collectorComm = MPI_COMM_NULL;
  setPioCommunicator ( & ( pioinfo->comm ), comm, color, nnodes, pioComm );
375
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));

  if ( pioinfo->type == PIO_NONE && pioinfo->size != 1 )
    {
      fprintf ( stderr, 
		"PTYPE should be set to a parallel I/O type or npe should be 1." );
      fprintf ( stderr, "PTYPE = %d, npe = %d\n", 
		pioinfo->type , pioinfo->size );
      MPI_Abort ( pioinfo->comm, 1 );
    }

  if ( ddebug && pioinfo->rank == 0 )
    fprintf ( stdout, 
390
391
	      "pe%d in pioDefPtype(), ptype=%d, initial_buffersize=%ld: init pioinfo ...\n", 
	      pioinfo->rank, pioinfo->type, initial_buffersize );
Deike Kleberg's avatar
Deike Kleberg committed
392
393
394

  switch ( pioinfo->type )
    {
395
396
    case PIO_MPI_NONB:
      collectingData = initMPINONB ( ncollectors );
Deike Kleberg's avatar
Deike Kleberg committed
397
      break;
Deike Kleberg's avatar
Deike Kleberg committed
398
399
400
    case PIO_POSIX_ASYNCH:
      collectingData = initPOSIXASYNCH ( ncollectors );
      break;
401
402
403
    case PIO_POSIX_FPGUARD_SENDRECV:
      collectingData = initPOSIXFPGUARDSENDRECV ( ncollectors );
      break;
404
405
406
407
408
409
    case PIO_POSIX_FPGUARD_THREAD:
      collectingData = initPOSIXFPGUARDTHREAD ( ncollectors );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      collectingData = initPOSIXFPGUARDTHREADREFUSE ( ncollectors );
      break;
410
411
412
    case PIO_POSIX_NONB:
      collectingData = initPOSIXNONB ( ncollectors );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
413
414
    }

415
#endif
Deike Kleberg's avatar
Deike Kleberg committed
416

Deike Kleberg's avatar
Deike Kleberg committed
417
418
419
420
421
422
423
  return collectingData;
}

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

void pioFinalize ()
{
424
#ifndef NOMPI
425
426
427
428
429
430
431
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

432
433
434
435
436
437
  if ( pioinfo->collectorComm != MPI_COMM_NULL ) 
    MPI_Comm_free ( &( pioinfo->collectorComm ));
  
  if ( pioinfo->comm != MPI_COMM_NULL ) 
    MPI_Comm_free ( &( pioinfo->comm ));

Deike Kleberg's avatar
Deike Kleberg committed
438
  free ( pioinfo );
439
#endif
Deike Kleberg's avatar
Deike Kleberg committed
440
441
442
443

  return;
}