pio.c 11 KB
Newer Older
Deike Kleberg's avatar
Deike Kleberg committed
1
2
3
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Deike Kleberg's avatar
Deike Kleberg committed
4
#include <string.h>
5
#include <unistd.h>
Deike Kleberg's avatar
Deike Kleberg committed
6
7
8

#ifndef NOMPI

Deike Kleberg's avatar
Deike Kleberg committed
9
10
11
12
13
#include "mpi.h"
#include "cdi.h"
#include "pio.h"
#include "pio_impl.h"

Deike Kleberg's avatar
Deike Kleberg committed
14
15
#endif

16
bool ddebug = false;
Deike Kleberg's avatar
Deike Kleberg committed
17

Deike Kleberg's avatar
Deike Kleberg committed
18
19
#ifndef NOMPI

20
21
22
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
23

24
25
26
long initial_buffersize = 16 * 1024 * 1024;
/*  4 KB <= x < 256 MB */ 
/* 16 * 1024 * 1024; */
27
/* 16 * 1024; */
28
/* 4 * 1024; */
Deike Kleberg's avatar
Deike Kleberg committed
29

Deike Kleberg's avatar
Deike Kleberg committed
30
int maxPtype = 4;
31
int maxNnodes  = 249;
32
int tagKey = 100;
33
int maxErrorString = 100;
34

Deike Kleberg's avatar
Deike Kleberg committed
35
36
37
38
39
40
41
42
43
44
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;

45
46
char *token = "%";

Deike Kleberg's avatar
Deike Kleberg committed
47
48
/*****************************************************************************/

49
void errorPIO ( char *errorString, const char *filename, int line, int rank )
Deike Kleberg's avatar
Deike Kleberg committed
50
{
51
52
53
  fprintf ( stderr, "PIO ERROR, pe%d, %s, line %d, errorString: \"%s\"\n",
	    rank, filename, line, errorString );
  MPI_Abort ( MPI_COMM_WORLD, 1 );
54
55
56
57
58
59
}

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

void checkMPI ( int iret, const char *filename, int line, int rank )
{
60
61
62
  char errorString1[MPI_MAX_ERROR_STRING + 1];
  char errorString2[MPI_MAX_ERROR_STRING + 1];
  int len, errorClass;   
Deike Kleberg's avatar
Deike Kleberg committed
63

64
  if ( iret != MPI_SUCCESS )
Deike Kleberg's avatar
Deike Kleberg committed
65
    {
66
67
68
69
70
71
72
73
74
75
76
77
78
      MPI_Error_class ( iret, &errorClass );
      MPI_Error_string ( errorClass, errorString1, &len );
      errorString1[len] = '\0';
      MPI_Error_string ( iret, errorString2, &len);
      errorString2[len] = '\0';
      
      fprintf ( stderr, "MPI ERROR, pe%d, %s, line %d,"
		"errorClass: \"%s\""
		"errorString: \"%s\"\n",
		rank, filename, line, 
		errorString1, errorString2);

      MPI_Abort ( MPI_COMM_WORLD, iret );
Deike Kleberg's avatar
Deike Kleberg committed
79
80
81
82
83
    }

  return;
}

84
85
/*****************************************************************************/

86
87
void checkMPIstat ( int iret, const char *filename, int line, int rank,  
			MPI_Status *status )
88
{
89
  char errorString[MPI_MAX_ERROR_STRING + 1];
90
91
92
93
94
95
  int len;

  if ( iret == MPI_ERR_IN_STATUS )
    {
      switch ( status->MPI_ERROR )
	{
Deike Kleberg's avatar
Deike Kleberg committed
96
	  fprintf ( stderr, "------- checking error in request ----------\n" );
97
	case MPI_SUCCESS :
Deike Kleberg's avatar
Deike Kleberg committed
98
	  fprintf ( stderr, "-------- mpi_success -----------\n" );
99
100
	  break;
	case MPI_ERR_PENDING:
Deike Kleberg's avatar
Deike Kleberg committed
101
	  fprintf ( stderr, "-------- mpi_err_pending ----------\n");
102
103
	  break;
	default:
104
105
	  MPI_Error_string ( status->MPI_ERROR, errorString, &len );
	  errorString[len] = '\0';
106
107
108
109
	  fprintf ( stderr,"MPI ERROR in request, pe%d, %s, line %d,"
		    "return value: %d, error_string: %s\n", 
		    rank, filename, line, iret, errorString ); 
	  MPI_Abort ( MPI_COMM_WORLD, iret );
110
111
112
	}
    }
  else 
113
    checkMPI ( iret, filename, line, rank );
114
115
116
117
  
  return;
}

Deike Kleberg's avatar
Deike Kleberg committed
118
119
120
121
122
123
124
/***************************************************************/     

void * xmalloc ( size_t size )
{
  void * value = malloc ( size );

  if ( value == NULL )
125
126
    errorPIO ( "malloc did not succeed", __FILE__, __LINE__, -1 );
  
Deike Kleberg's avatar
Deike Kleberg committed
127
128
129
130
  return value;
}

/***************************************************************/  
Deike Kleberg's avatar
Deike Kleberg committed
131

Deike Kleberg's avatar
Deike Kleberg committed
132
133
int setTag ( int ID, int sc )
{
134
  return ID * tagKey + sc;
Deike Kleberg's avatar
Deike Kleberg committed
135
136
137
138
139
140
141
142
}

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

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

Deike Kleberg's avatar
Deike Kleberg committed
143
  rtag = ( rTag * ) xmalloc ( sizeof ( rTag ));
144
145
  rtag->id = tag / tagKey;
  rtag->command = tag % tagKey;
Deike Kleberg's avatar
Deike Kleberg committed
146
147
148
149
150
151

  return rtag;
}

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

152
153
154
155
156
157
158
159
void ungetTag ( rTag *rtag )
{
  free ( rtag );
  return;
}

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

Deike Kleberg's avatar
Deike Kleberg committed
160
161
162
163
164
165
size_t pioFileWrite ( int id, int tsId, const void *buffer, size_t len )
{
  size_t iret;

  switch ( pioinfo->type )
    {
166
167
    case PIO_MPI_NONB:
      iret = fwMPINONB ( id, tsId, buffer, len );
Deike Kleberg's avatar
Deike Kleberg committed
168
      break;
Deike Kleberg's avatar
Deike Kleberg committed
169
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
170
171
172
    case PIO_POSIX_ASYNCH:
      iret = fwPOSIXASYNCH ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
173
#endif
174
175
176
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fwPOSIXFPGUARDSENDRECV ( id, tsId, buffer, len );
      break;
177
178
179
180
181
182
    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;
183
184
185
    case PIO_POSIX_NONB:
      iret = fwPOSIXNONB ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
186
187
188
189
190
191
192
193
194
195
196
197
198
    }

  return iret;
}

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

int pioFileClose ( int id )
{
  int iret;
  
  switch ( pioinfo->type )
    {
199
200
    case PIO_MPI_NONB:
      iret = fcMPINONB ( id );
Deike Kleberg's avatar
Deike Kleberg committed
201
      break;
Deike Kleberg's avatar
Deike Kleberg committed
202
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
203
204
205
    case PIO_POSIX_ASYNCH:
      iret = fcPOSIXASYNCH ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
206
#endif
207
208
209
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fcPOSIXFPGUARDSENDRECV ( id );
      break;
210
211
212
213
214
215
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fcPOSIXFPGUARDTHREAD ( id );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fcPOSIXFPGUARDTHREADREFUSE ( id );
      break;
216
217
218
    case PIO_POSIX_NONB:
      iret = fcPOSIXNONB ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
219
220
221
222
223
224
225
226
227
228
229
230
231
    }

  return iret;
}

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

int pioFileOpenW ( const char *filename )
{
  int iret;
  
  switch ( pioinfo->type )
    {
232
233
    case PIO_MPI_NONB:
      iret = fowMPINONB ( filename );
Deike Kleberg's avatar
Deike Kleberg committed
234
      break;
Deike Kleberg's avatar
Deike Kleberg committed
235
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
236
237
238
    case PIO_POSIX_ASYNCH:
      iret = fowPOSIXASYNCH ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
239
#endif
240
241
242
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fowPOSIXFPGUARDSENDRECV ( filename );
      break;
243
244
245
246
247
248
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fowPOSIXFPGUARDTHREAD ( filename );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fowPOSIXFPGUARDTHREADREFUSE ( filename );
      break;
249
250
251
    case PIO_POSIX_NONB:
      iret = fowPOSIXNONB ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
252
253
254
255
256
257
258
    }
  
  return iret;
}

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

259
260
261
262
263
264
265
266
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, 
267
			  int *nnodes )
Deike Kleberg's avatar
Deike Kleberg committed
268
{
269
270
  int size, rank, len, npes_node, key, test, i, j;

Deike Kleberg's avatar
Deike Kleberg committed
271
  char *myHost, **allHosts, *allHosts0, *curr;
272
273
  char hostname [ MPI_MAX_PROCESSOR_NAME + 1 ];

274
275
  MPI_Comm_size ( commF2C, &size );
  MPI_Comm_rank ( commF2C, &rank );
276

Deike Kleberg's avatar
Deike Kleberg committed
277
  myHost = ( char * ) xmalloc ( MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
278
  
Deike Kleberg's avatar
Deike Kleberg committed
279
  memset ( myHost, 0, MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
280
281
  
  checkMPI (( MPI_Get_processor_name ( myHost, &len )), __FILE__, __LINE__, rank );
Deike Kleberg's avatar
Deike Kleberg committed
282

283
  if ( myHost[0] == '\0' ) 
284
    errorPIO ( "did not succeed to set hostname", __FILE__, __LINE__ , rank);
Deike Kleberg's avatar
Deike Kleberg committed
285

286
287
288
289
  if ( ddebug )
    {
      strncpy ( hostname, myHost, len );
      hostname [ len ] = '\0';
Deike Kleberg's avatar
Deike Kleberg committed
290
      fprintf ( stderr, "pe%d: myHost = %s\n", rank,  hostname );
291
    }
292
  
Deike Kleberg's avatar
Deike Kleberg committed
293
294
295
296
  allHosts = ( char ** ) xmalloc ( size * sizeof ( char * ));
  allHosts[0] = ( char * ) xmalloc ( size * MPI_MAX_PROCESSOR_NAME * 
				     sizeof ( char ));
  allHosts0 = allHosts[0];
Deike Kleberg's avatar
Deike Kleberg committed
297

298
299
  for ( i = 1; i < size; i++ )
    allHosts[i] = allHosts[0] + i * MPI_MAX_PROCESSOR_NAME;
300
301

  MPI_Allgather ( myHost, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, 
Deike Kleberg's avatar
Deike Kleberg committed
302
			    & ( allHosts[0][0] ), MPI_MAX_PROCESSOR_NAME, 
303
			    MPI_CHAR, commF2C );
304
305
306
307
308
309

  qsort ( allHosts, size, sizeof ( char * ), cmpr );

  *color = 0;
  i = 0;
  j = 0;
310

311
  while ( i < size )
312
313
    {
      curr = allHosts[i];
314
      j++;
315
      if (( test = strcmp ( myHost, curr )) == 0 ) *color = j;
316
317
318
319
     
      while ( ++i < size )
	if (( test = strcmp ( allHosts[i], curr )) != 0)
	  break;
320
    }
Deike Kleberg's avatar
Deike Kleberg committed
321

322
323
  *nnodes = j;
      
324
  if ( *color == 0 ) errorPIO ( "Color is not set", __FILE__, __LINE__, rank );
325
  
326
  npes_node = size / ( * nnodes );
Deike Kleberg's avatar
Deike Kleberg committed
327
328
  key = rank % npes_node;

Deike Kleberg's avatar
Deike Kleberg committed
329
330
  checkMPI ( MPI_Comm_split ( commF2C, *color, key, myComm),
	     __FILE__, __LINE__, rank );
331

Deike Kleberg's avatar
Deike Kleberg committed
332
  free ( allHosts0 );
333
334
  free ( allHosts );
  free ( myHost );
Deike Kleberg's avatar
Deike Kleberg committed
335
336

  if ( ddebug )
Deike Kleberg's avatar
Deike Kleberg committed
337
    fprintf ( stderr, 
Deike Kleberg's avatar
Deike Kleberg committed
338
339
	      "pe%d in setPioCommunicator, color=%d, before return\n", 
	      rank, *color );
340
341
342
343
344
345
346
347
  
  return;
}

#endif

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

348
int pioInit ( int ptype, int commF, int *color, int *nnodes, int *pioCollComm2F )
349
{
Deike Kleberg's avatar
Deike Kleberg committed
350
351
352
353
#ifndef NOMPI
  MPI_Comm comm;
  int size, rank;
#endif
354
  int collectingData = 1;
355
356
  *nnodes = 1;
  *color = 1;
357
  *pioCollComm2F = 0;
358
359

#ifndef NOMPI
Deike Kleberg's avatar
Deike Kleberg committed
360
361
  
  comm = MPI_COMM_NULL;
362

Deike Kleberg's avatar
Deike Kleberg committed
363
  if (( comm = MPI_Comm_f2c (( MPI_Fint ) commF )) == NULL )
364
    errorPIO ( "MPI_Comm_f2c didn't succeed", __FILE__, __LINE__, -1 );
Deike Kleberg's avatar
Deike Kleberg committed
365

366
367
368
  MPI_Comm_set_errhandler ( comm, MPI_ERRORS_RETURN );
  MPI_Comm_size ( comm, &size );
  MPI_Comm_rank ( comm, &rank );
369
  
370
371
  if ( ptype < 0 || ptype > maxPtype ) 
    errorPIO ( "PIOTYPE is no valid modus", __FILE__, __LINE__, rank );
Deike Kleberg's avatar
Deike Kleberg committed
372
373
374
375
376

#ifdef _SX
  if ( ptype ==  PIO_POSIX_ASYNCH )
    errorPIO ( "PIO_POSIX_ASYNCH does not work on SX", __FILE__, __LINE__, rank );
#endif
377
  
378
  pioinfo = ( pioInfo * ) xmalloc ( sizeof ( pioInfo ));
379
  
380
  pioinfo->type = ptype; 
Deike Kleberg's avatar
Deike Kleberg committed
381

382
  setPioCommunicator ( & ( pioinfo->comm ), comm, color, nnodes );
383
  
384
385
  if (( pioinfo->type == PIO_NONE ) && ( *nnodes != size ))
    errorPIO ( "PIOTYPE, NNODES: not a valid combination", __FILE__, __LINE__, rank );
386
  
387
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
388
389
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));
390
  
Deike Kleberg's avatar
Deike Kleberg committed
391
  if ( ddebug && pioinfo->rank == 0 )
Deike Kleberg's avatar
Deike Kleberg committed
392
393
    fprintf ( stderr, 
	      "pe%d in pioInit(), ptype=%d, initial_buffersize=%ld: "
Deike Kleberg's avatar
Deike Kleberg committed
394
	      "after init pioinfo ...\n", 
395
	      pioinfo->rank, pioinfo->type, initial_buffersize );
396
  
397
  pioinfo->collectorComm = MPI_COMM_NULL;
Deike Kleberg's avatar
Deike Kleberg committed
398

Deike Kleberg's avatar
Deike Kleberg committed
399
400
  switch ( pioinfo->type )
    {
401
402
    case PIO_NONE:
      MPI_Comm_dup ( pioinfo->comm, &( pioinfo->collectorComm ));
403
      collectingData = 1;
404
      break;
405
    case PIO_MPI_NONB:
406
      collectingData = initMPINONB ();
Deike Kleberg's avatar
Deike Kleberg committed
407
      break;
Deike Kleberg's avatar
Deike Kleberg committed
408
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
409
    case PIO_POSIX_ASYNCH:
410
      collectingData = initPOSIXASYNCH ();
Deike Kleberg's avatar
Deike Kleberg committed
411
      break;
Deike Kleberg's avatar
Deike Kleberg committed
412
#endif
413
    case PIO_POSIX_FPGUARD_SENDRECV:
414
      collectingData = initPOSIXFPGUARDSENDRECV ();
415
      break;
416
    case PIO_POSIX_FPGUARD_THREAD:
417
      collectingData = initPOSIXFPGUARDTHREAD ();
418
419
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
420
      collectingData = initPOSIXFPGUARDTHREADREFUSE ();
421
      break;
Deike Kleberg's avatar
Deike Kleberg committed
422
    case PIO_POSIX_NONB:
423
      collectingData = initPOSIXNONB ();
424
      break;
Deike Kleberg's avatar
Deike Kleberg committed
425
    }
426
  
Deike Kleberg's avatar
Deike Kleberg committed
427
428
429
430
431
432
  /* ????? MPI_Comm_c2f hardwired to 0 ????
   *pioCollComm2F  = 0;
   if (( *pioCollComm2F = MPI_Comm_c2f ( pioinfo->collectorComm ) == 0 )
   errorPIO ( "MPI_Comm_c2f didn't succeed\n", __FILE__, __LINE__ , rank);
  */
  *pioCollComm2F = MPI_Comm_c2f ( pioinfo->collectorComm );
433
#endif
Deike Kleberg's avatar
Deike Kleberg committed
434
435
  
  if ( ddebug )
Deike Kleberg's avatar
Deike Kleberg committed
436
    fprintf ( stderr, "pe in pioinit out\n" );
437
  
Deike Kleberg's avatar
Deike Kleberg committed
438
439
440
441
442
443
444
  return collectingData;
}

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

void pioFinalize ()
{
445
#ifndef NOMPI
446
447
448
449
450
451
452
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

453
454
455
456
457
458
  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
459
  free ( pioinfo );
460
#endif
Deike Kleberg's avatar
Deike Kleberg committed
461
462
463
464

  return;
}