pio.c 10.1 KB
Newer Older
1
2
3
4
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

Deike Kleberg's avatar
Deike Kleberg committed
5
6
7
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Deike Kleberg's avatar
Deike Kleberg committed
8
#include <string.h>
9
#include <unistd.h>
Deike Kleberg's avatar
Deike Kleberg committed
10

11
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
12

Deike Kleberg's avatar
Deike Kleberg committed
13
14
15
#include "cdi.h"
#include "pio.h"
#include "pio_impl.h"
Deike Kleberg's avatar
Deike Kleberg committed
16
#include "pio_util.h"
Deike Kleberg's avatar
Deike Kleberg committed
17

Deike Kleberg's avatar
Deike Kleberg committed
18

Deike Kleberg's avatar
Deike Kleberg committed
19
20
#endif

21
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
22

23
char * command2charP[6] = {"IO_Open_file", "IO_Close_file",
24
25
			   "IO_Get_fp","IO_Set_fp",
			   "IO_Send_buffer", "IO_Finalize"};
Deike Kleberg's avatar
Deike Kleberg committed
26

27
long initial_buffersize = 16 * 1024 * 1024;
28
/*  4 KB <= x < 256 MB */ 
29
/* 16 * 1024 * 1024; */
30
/* 16 * 1024; */
31
/* 4 * 1024; */
Deike Kleberg's avatar
Deike Kleberg committed
32

Deike Kleberg's avatar
Deike Kleberg committed
33
int maxPtype = 4;
34
int maxNnodes  = 249;
35
int tagKey = 100;
36
int maxErrorString = 100;
37

Deike Kleberg's avatar
Deike Kleberg committed
38
39
40
41
42
43
44
45
46
47
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;

48
49
char *token = "%";

Deike Kleberg's avatar
Deike Kleberg committed
50
51
/*****************************************************************************/

52
53
void checkMPI ( int iret, const char *filename, int line, int rank )
{
54
55
  char errorString1[MPI_MAX_ERROR_STRING + 1];
  char errorString2[MPI_MAX_ERROR_STRING + 1];
56
  int len, errorClass;   
Deike Kleberg's avatar
Deike Kleberg committed
57

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

      MPI_Abort ( MPI_COMM_WORLD, iret );
Deike Kleberg's avatar
Deike Kleberg committed
73
74
75
76
77
    }

  return;
}

78
79
/*****************************************************************************/

80
81
void checkMPIstat ( int iret, const char *filename, int line, int rank,  
			MPI_Status *status )
82
{
83
  char errorString[MPI_MAX_ERROR_STRING + 1];
84
85
86
87
88
  int len;

  if ( iret == MPI_ERR_IN_STATUS )
    {
      switch ( status->MPI_ERROR )
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
	{
	  fprintf ( stderr, "------- checking error in request ----------\n" );
	case MPI_SUCCESS :
	  fprintf ( stderr, "-------- mpi_success -----------\n" );
	  break;
	case MPI_ERR_PENDING:
	  fprintf ( stderr, "-------- mpi_err_pending ----------\n");
	  break;
	default:
	  MPI_Error_string ( status->MPI_ERROR, errorString, &len );
	  errorString[len] = '\0';
	  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 );
	}
105
    }
106
  else 
107
    checkMPI ( iret, filename, line, rank );
108
  
109
110
111
  return;
}

112
/***************************************************************/  
Deike Kleberg's avatar
Deike Kleberg committed
113

Deike Kleberg's avatar
Deike Kleberg committed
114
115
int setTag ( int ID, int sc )
{
116
  return ID * tagKey + sc;
Deike Kleberg's avatar
Deike Kleberg committed
117
118
119
120
121
122
123
124
}

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

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

Deike Kleberg's avatar
Deike Kleberg committed
125
  rtag = ( rTag * ) xmalloc ( sizeof ( rTag ));
126
127
  rtag->id = tag / tagKey;
  rtag->command = tag % tagKey;
Deike Kleberg's avatar
Deike Kleberg committed
128
129
130
131
132
133

  return rtag;
}

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

134
135
136
137
138
139
140
141
void ungetTag ( rTag *rtag )
{
  free ( rtag );
  return;
}

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

Deike Kleberg's avatar
Deike Kleberg committed
142
143
144
145
146
147
size_t pioFileWrite ( int id, int tsId, const void *buffer, size_t len )
{
  size_t iret;

  switch ( pioinfo->type )
    {
148
149
    case PIO_MPI_NONB:
      iret = fwMPINONB ( id, tsId, buffer, len );
Deike Kleberg's avatar
Deike Kleberg committed
150
      break;
Deike Kleberg's avatar
Deike Kleberg committed
151
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
152
153
154
    case PIO_POSIX_ASYNCH:
      iret = fwPOSIXASYNCH ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
155
#endif
156
157
158
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fwPOSIXFPGUARDSENDRECV ( id, tsId, buffer, len );
      break;
159
160
161
162
163
164
    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;
165
166
167
    case PIO_POSIX_NONB:
      iret = fwPOSIXNONB ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
168
169
170
171
172
173
174
175
176
177
    }

  return iret;
}

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

int pioFileClose ( int id )
{
  int iret;
178
  
Deike Kleberg's avatar
Deike Kleberg committed
179
180
  switch ( pioinfo->type )
    {
181
182
    case PIO_MPI_NONB:
      iret = fcMPINONB ( id );
Deike Kleberg's avatar
Deike Kleberg committed
183
      break;
Deike Kleberg's avatar
Deike Kleberg committed
184
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
185
186
187
    case PIO_POSIX_ASYNCH:
      iret = fcPOSIXASYNCH ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
188
#endif
189
190
191
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fcPOSIXFPGUARDSENDRECV ( id );
      break;
192
193
194
195
196
197
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fcPOSIXFPGUARDTHREAD ( id );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fcPOSIXFPGUARDTHREADREFUSE ( id );
      break;
198
199
200
    case PIO_POSIX_NONB:
      iret = fcPOSIXNONB ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
201
202
203
204
205
206
207
208
209
210
    }

  return iret;
}

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

int pioFileOpenW ( const char *filename )
{
  int iret;
211
  
Deike Kleberg's avatar
Deike Kleberg committed
212
213
  switch ( pioinfo->type )
    {
214
215
    case PIO_MPI_NONB:
      iret = fowMPINONB ( filename );
Deike Kleberg's avatar
Deike Kleberg committed
216
      break;
Deike Kleberg's avatar
Deike Kleberg committed
217
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
218
219
220
    case PIO_POSIX_ASYNCH:
      iret = fowPOSIXASYNCH ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
221
#endif
222
223
224
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fowPOSIXFPGUARDSENDRECV ( filename );
      break;
225
226
227
228
229
230
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fowPOSIXFPGUARDTHREAD ( filename );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fowPOSIXFPGUARDTHREADREFUSE ( filename );
      break;
231
232
233
    case PIO_POSIX_NONB:
      iret = fowPOSIXNONB ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
234
    }
235
  
Deike Kleberg's avatar
Deike Kleberg committed
236
237
238
239
240
  return iret;
}

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

241
242
static int cmpr ( const void *a, const void *b ) 
{ 
243
244
245
246
247
  return strcmp ( *( char ** ) a, *( char ** ) b);
}

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

248
249
void setPioCommunicator ( MPI_Comm *myComm, MPI_Comm commF2C, int *color, 
			  int *nnodes )
Deike Kleberg's avatar
Deike Kleberg committed
250
{
251
252
  int size, rank, len, npes_node, key, test, i, j;

Deike Kleberg's avatar
Deike Kleberg committed
253
  char *myHost, **allHosts, *allHosts0, *curr;
254
255
  char hostname [ MPI_MAX_PROCESSOR_NAME + 1 ];

256
257
  MPI_Comm_size ( commF2C, &size );
  MPI_Comm_rank ( commF2C, &rank );
258

Deike Kleberg's avatar
Deike Kleberg committed
259
  myHost = ( char * ) xmalloc ( MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
260
  
Deike Kleberg's avatar
Deike Kleberg committed
261
  memset ( myHost, 0, MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
262
  
263
  checkMPI (( MPI_Get_processor_name ( myHost, &len )), __FILE__, __LINE__, rank );
Deike Kleberg's avatar
Deike Kleberg committed
264

265
  if ( myHost[0] == '\0' ) 
Deike Kleberg's avatar
Deike Kleberg committed
266
    pcdiAbort ( "did not succeed to set hostname", __FILE__, __LINE__ , rank);
Deike Kleberg's avatar
Deike Kleberg committed
267

Deike Kleberg's avatar
Deike Kleberg committed
268
  if ( ddebug == MAXDEBUG )
269
270
271
    {
      strncpy ( hostname, myHost, len );
      hostname [ len ] = '\0';
Deike Kleberg's avatar
Deike Kleberg committed
272
      fprintf ( stderr, "pe%d: myHost = %s\n", rank,  hostname );
273
    }
274
  
Deike Kleberg's avatar
Deike Kleberg committed
275
  allHosts = ( char ** ) xmalloc ( size * sizeof ( char * ));
276
277
  allHosts[0] = ( char * ) xmalloc ( size * MPI_MAX_PROCESSOR_NAME * 
				     sizeof ( char ));
Deike Kleberg's avatar
Deike Kleberg committed
278
  allHosts0 = allHosts[0];
Deike Kleberg's avatar
Deike Kleberg committed
279

280
281
  for ( i = 1; i < size; i++ )
    allHosts[i] = allHosts[0] + i * MPI_MAX_PROCESSOR_NAME;
282

283
284
285
  MPI_Allgather ( myHost, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, 
			    & ( allHosts[0][0] ), MPI_MAX_PROCESSOR_NAME, 
			    MPI_CHAR, commF2C );
286
287
288
289
290
291

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

  *color = 0;
  i = 0;
  j = 0;
292

293
  while ( i < size )
294
295
    {
      curr = allHosts[i];
296
      j++;
297
      if (( test = strcmp ( myHost, curr )) == 0 ) *color = j;
298
     
299
      while ( ++i < size )
300
301
	if (( test = strcmp ( allHosts[i], curr )) != 0)
	  break;
302
    }
Deike Kleberg's avatar
Deike Kleberg committed
303

304
  *nnodes = j;
305
      
Deike Kleberg's avatar
Deike Kleberg committed
306
  if ( *color == 0 ) pcdiAbort ( "Color is not set", __FILE__, __LINE__, rank );
307
  
308
  npes_node = size / ( * nnodes );
Deike Kleberg's avatar
Deike Kleberg committed
309
310
  key = rank % npes_node;

Deike Kleberg's avatar
Deike Kleberg committed
311
  checkMPI ( MPI_Comm_split ( commF2C, *color, key, myComm),
312
	     __FILE__, __LINE__, rank );
313

Deike Kleberg's avatar
Deike Kleberg committed
314
  free ( allHosts0 );
315
316
  free ( allHosts );
  free ( myHost );
Deike Kleberg's avatar
Deike Kleberg committed
317

Deike Kleberg's avatar
Deike Kleberg committed
318
  if ( ddebug == MAXDEBUG )
319
320
321
322
    fprintf ( stderr, 
	      "pe%d in setPioCommunicator, color=%d, before return\n", 
	      rank, *color );
  
323
324
325
326
327
328
329
  return;
}

#endif

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

Deike Kleberg's avatar
Deike Kleberg committed
330
MPI_Comm bInit ( int ptype, MPI_Comm comm, int *color, int *nnodes )
331
{
Deike Kleberg's avatar
Deike Kleberg committed
332
  char fnName[] = "bInit()";
Deike Kleberg's avatar
Deike Kleberg committed
333
  int size, rank;
334
  int collectingData = 1;
335
336
  *nnodes = 1;
  *color = 1;
337

Deike Kleberg's avatar
Deike Kleberg committed
338
  if ( ddebug == MAXDEBUG )
Deike Kleberg's avatar
Deike Kleberg committed
339
    myDebug ( __FILE__, fnName, __LINE__ );
Deike Kleberg's avatar
Deike Kleberg committed
340

Deike Kleberg's avatar
Deike Kleberg committed
341
#ifdef USE_MPI
342
343
  MPI_Comm_size ( comm, &size );
  MPI_Comm_rank ( comm, &rank );
344
345
  
  if ( ptype < 0 || ptype > maxPtype ) 
Deike Kleberg's avatar
Deike Kleberg committed
346
    pcdiAbort ( "PIOTYPE is no valid modus", __FILE__, __LINE__, rank );
Deike Kleberg's avatar
Deike Kleberg committed
347
348
349

#ifdef _SX
  if ( ptype ==  PIO_POSIX_ASYNCH )
Deike Kleberg's avatar
Deike Kleberg committed
350
    pcdiAbort ( "PIO_POSIX_ASYNCH does not work on SX", __FILE__, __LINE__, rank );
Deike Kleberg's avatar
Deike Kleberg committed
351
#endif
352
  
353
  pioinfo = ( pioInfo * ) xmalloc ( sizeof ( pioInfo ));
354
355
  
  pioinfo->type = ptype; 
Deike Kleberg's avatar
Deike Kleberg committed
356

357
  setPioCommunicator ( & ( pioinfo->comm ), comm, color, nnodes );
358
  
359
  if (( pioinfo->type == PIO_NONE ) && ( *nnodes != size ))
Deike Kleberg's avatar
Deike Kleberg committed
360
    pcdiAbort ( "PIOTYPE, NNODES: not a valid combination", __FILE__, __LINE__, rank );
361
  
362
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
363
364
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));
365
  
Deike Kleberg's avatar
Deike Kleberg committed
366
  if ( ddebug == MAXDEBUG && pioinfo->rank == 0 )
367
    fprintf ( stderr, 
Deike Kleberg's avatar
Deike Kleberg committed
368
	      "pe%d in bInit(), ptype=%d, initial_buffersize=%ld: "
369
370
371
	      "after init pioinfo ...\n", 
	      pioinfo->rank, pioinfo->type, initial_buffersize );
  
372
  pioinfo->collectorComm = MPI_COMM_NULL;
Deike Kleberg's avatar
Deike Kleberg committed
373

Deike Kleberg's avatar
Deike Kleberg committed
374
375
  switch ( pioinfo->type )
    {
376
377
    case PIO_NONE:
      MPI_Comm_dup ( pioinfo->comm, &( pioinfo->collectorComm ));
378
      collectingData = 1;
379
      break;
380
    case PIO_MPI_NONB:
381
      collectingData = initMPINONB ();
Deike Kleberg's avatar
Deike Kleberg committed
382
      break;
Deike Kleberg's avatar
Deike Kleberg committed
383
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
384
    case PIO_POSIX_ASYNCH:
385
      collectingData = initPOSIXASYNCH ();
Deike Kleberg's avatar
Deike Kleberg committed
386
      break;
Deike Kleberg's avatar
Deike Kleberg committed
387
#endif
388
    case PIO_POSIX_FPGUARD_SENDRECV:
389
      collectingData = initPOSIXFPGUARDSENDRECV ();
390
      break;
391
    case PIO_POSIX_FPGUARD_THREAD:
392
      collectingData = initPOSIXFPGUARDTHREAD ();
393
394
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
395
      collectingData = initPOSIXFPGUARDTHREADREFUSE ();
396
      break;
Deike Kleberg's avatar
Deike Kleberg committed
397
    case PIO_POSIX_NONB:
398
      collectingData = initPOSIXNONB ();
399
      break;
Deike Kleberg's avatar
Deike Kleberg committed
400
    }
401
402
#endif
  
Deike Kleberg's avatar
Deike Kleberg committed
403
  if ( ddebug == MAXDEBUG )
Deike Kleberg's avatar
Deike Kleberg committed
404
    fprintf ( stderr, "pe in bInit out\n" );
405
  
Deike Kleberg's avatar
Deike Kleberg committed
406
  return pioinfo->collectorComm;
Deike Kleberg's avatar
Deike Kleberg committed
407
408
409
410
}

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

Deike Kleberg's avatar
Deike Kleberg committed
411
void bFinalize ()
Deike Kleberg's avatar
Deike Kleberg committed
412
{
413
#ifdef USE_MPI
414
415
416
417
418
419
420
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

421
  if ( pioinfo->collectorComm != MPI_COMM_NULL ) 
422
    MPI_Comm_free ( &( pioinfo->collectorComm ));
423
424
  
  if ( pioinfo->comm != MPI_COMM_NULL ) 
425
426
    MPI_Comm_free ( &( pioinfo->comm ));

Deike Kleberg's avatar
Deike Kleberg committed
427
  free ( pioinfo );
428
#endif
Deike Kleberg's avatar
Deike Kleberg committed
429
430
431

  return;
}
432