pio.c 8.31 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
12
13
#include "pio.h"
#include "pio_util.h"

14
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
15

Deike Kleberg's avatar
Deike Kleberg committed
16
17
#include "cdi.h"
#include "pio_impl.h"
Deike Kleberg's avatar
Deike Kleberg committed
18

Deike Kleberg's avatar
Deike Kleberg committed
19

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

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

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

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

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

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

49
50
char *token = "%";

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

Deike Kleberg's avatar
Deike Kleberg committed
53
54
int setTag ( int ID, int sc )
{
55
  return ID * tagKey + sc;
Deike Kleberg's avatar
Deike Kleberg committed
56
57
58
59
60
61
62
63
}

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

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

Deike Kleberg's avatar
Deike Kleberg committed
64
  rtag = ( rTag * ) xmalloc ( sizeof ( rTag ));
65
66
  rtag->id = tag / tagKey;
  rtag->command = tag % tagKey;
Deike Kleberg's avatar
Deike Kleberg committed
67
68
69
70
71
72

  return rtag;
}

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

73
74
75
76
77
78
79
80
void ungetTag ( rTag *rtag )
{
  free ( rtag );
  return;
}

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

Deike Kleberg's avatar
Deike Kleberg committed
81
82
83
84
85
86
size_t pioFileWrite ( int id, int tsId, const void *buffer, size_t len )
{
  size_t iret;

  switch ( pioinfo->type )
    {
87
88
    case PIO_MPI_NONB:
      iret = fwMPINONB ( id, tsId, buffer, len );
Deike Kleberg's avatar
Deike Kleberg committed
89
      break;
Deike Kleberg's avatar
Deike Kleberg committed
90
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
91
92
93
    case PIO_POSIX_ASYNCH:
      iret = fwPOSIXASYNCH ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
94
#endif
95
96
97
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fwPOSIXFPGUARDSENDRECV ( id, tsId, buffer, len );
      break;
98
99
100
101
102
103
    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;
104
105
106
    case PIO_POSIX_NONB:
      iret = fwPOSIXNONB ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
107
108
109
110
111
112
113
114
115
116
    }

  return iret;
}

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

int pioFileClose ( int id )
{
  int iret;
117

Deike Kleberg's avatar
Deike Kleberg committed
118
119
  switch ( pioinfo->type )
    {
120
121
    case PIO_MPI_NONB:
      iret = fcMPINONB ( id );
Deike Kleberg's avatar
Deike Kleberg committed
122
      break;
Deike Kleberg's avatar
Deike Kleberg committed
123
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
124
125
126
    case PIO_POSIX_ASYNCH:
      iret = fcPOSIXASYNCH ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
127
#endif
128
129
130
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fcPOSIXFPGUARDSENDRECV ( id );
      break;
131
132
133
134
135
136
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fcPOSIXFPGUARDTHREAD ( id );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fcPOSIXFPGUARDTHREADREFUSE ( id );
      break;
137
138
139
    case PIO_POSIX_NONB:
      iret = fcPOSIXNONB ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
140
141
142
143
144
145
146
147
148
149
    }

  return iret;
}

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

int pioFileOpenW ( const char *filename )
{
  int iret;
150

Deike Kleberg's avatar
Deike Kleberg committed
151
152
  switch ( pioinfo->type )
    {
153
154
    case PIO_MPI_NONB:
      iret = fowMPINONB ( filename );
Deike Kleberg's avatar
Deike Kleberg committed
155
      break;
Deike Kleberg's avatar
Deike Kleberg committed
156
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
157
158
159
    case PIO_POSIX_ASYNCH:
      iret = fowPOSIXASYNCH ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
160
#endif
161
162
163
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fowPOSIXFPGUARDSENDRECV ( filename );
      break;
164
165
166
167
168
169
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fowPOSIXFPGUARDTHREAD ( filename );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fowPOSIXFPGUARDTHREADREFUSE ( filename );
      break;
170
171
172
    case PIO_POSIX_NONB:
      iret = fowPOSIXNONB ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
173
    }
174

Deike Kleberg's avatar
Deike Kleberg committed
175
176
177
178
179
  return iret;
}

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

180
181
static int cmpr ( const void *a, const void *b )
{
182
183
184
185
186
  return strcmp ( *( char ** ) a, *( char ** ) b);
}

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

187
188
void setPioCommunicator ( MPI_Comm *myComm, MPI_Comm commF2C, int *color,
                          int *nnodes )
Deike Kleberg's avatar
Deike Kleberg committed
189
{
190
191
  int size, rank, len, npes_node, key, test, i, j;

Deike Kleberg's avatar
Deike Kleberg committed
192
  char *myHost, **allHosts, *allHosts0, *curr;
193
194
  char hostname [ MPI_MAX_PROCESSOR_NAME + 1 ];

195
196
  MPI_Comm_size ( commF2C, &size );
  MPI_Comm_rank ( commF2C, &rank );
197

Deike Kleberg's avatar
Deike Kleberg committed
198
  myHost = ( char * ) xmalloc ( MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
199

Deike Kleberg's avatar
Deike Kleberg committed
200
  memset ( myHost, 0, MPI_MAX_PROCESSOR_NAME * sizeof ( char ));
201

Deike Kleberg's avatar
Deike Kleberg committed
202
  xmpi ( MPI_Get_processor_name ( myHost, &len ));
Deike Kleberg's avatar
Deike Kleberg committed
203

204
  if ( myHost[0] == '\0' ) xabort ( "did not succeed to set hostname" );
Deike Kleberg's avatar
Deike Kleberg committed
205

Deike Kleberg's avatar
Deike Kleberg committed
206
  if ( ddebug == MAXDEBUG )
207
208
209
    {
      strncpy ( hostname, myHost, len );
      hostname [ len ] = '\0';
Deike Kleberg's avatar
Deike Kleberg committed
210
      fprintf ( stderr, "pe%d: myHost = %s\n", rank,  hostname );
211
    }
212

Deike Kleberg's avatar
Deike Kleberg committed
213
  allHosts = ( char ** ) xmalloc ( size * sizeof ( char * ));
214
215
  allHosts[0] = ( char * ) xmalloc ( size * MPI_MAX_PROCESSOR_NAME *
                                     sizeof ( char ));
Deike Kleberg's avatar
Deike Kleberg committed
216
  allHosts0 = allHosts[0];
Deike Kleberg's avatar
Deike Kleberg committed
217

218
219
  for ( i = 1; i < size; i++ )
    allHosts[i] = allHosts[0] + i * MPI_MAX_PROCESSOR_NAME;
220

221
222
223
  MPI_Allgather ( myHost, MPI_MAX_PROCESSOR_NAME, MPI_CHAR,
                            & ( allHosts[0][0] ), MPI_MAX_PROCESSOR_NAME,
                            MPI_CHAR, commF2C );
224
225
226
227
228
229

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

  *color = 0;
  i = 0;
  j = 0;
230

231
  while ( i < size )
232
233
    {
      curr = allHosts[i];
234
      j++;
235
      if (( test = strcmp ( myHost, curr )) == 0 ) *color = j;
236

237
      while ( ++i < size )
238
239
        if (( test = strcmp ( allHosts[i], curr )) != 0)
          break;
240
    }
Deike Kleberg's avatar
Deike Kleberg committed
241

242
  *nnodes = j;
243

244
  if ( *color == 0 ) xabort ( "Color is not set" );
245

246
  npes_node = size / ( * nnodes );
Deike Kleberg's avatar
Deike Kleberg committed
247
248
  key = rank % npes_node;

Deike Kleberg's avatar
Deike Kleberg committed
249
  xmpi ( MPI_Comm_split ( commF2C, *color, key, myComm));
250

Deike Kleberg's avatar
Deike Kleberg committed
251
  free ( allHosts0 );
252
253
  free ( allHosts );
  free ( myHost );
Deike Kleberg's avatar
Deike Kleberg committed
254

Deike Kleberg's avatar
Deike Kleberg committed
255
  if ( ddebug == MAXDEBUG )
256
257
258
259
    fprintf ( stderr,
              "pe%d in setPioCommunicator, color=%d, before return\n",
              rank, *color );

260
261
262
263
264
265
266
  return;
}

#endif

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

Deike Kleberg's avatar
Deike Kleberg committed
267
MPI_Comm bInit ( int ptype, MPI_Comm comm, int *color, int *nnodes )
268
{
Deike Kleberg's avatar
Deike Kleberg committed
269
  char fnName[] = "bInit()";
Deike Kleberg's avatar
Deike Kleberg committed
270
  int size, rank;
271
  int collectingData = 1;
272
273
  *nnodes = 1;
  *color = 1;
274

Deike Kleberg's avatar
Deike Kleberg committed
275
  if ( ddebug == MAXDEBUG ) xdebug ();
Deike Kleberg's avatar
Deike Kleberg committed
276
#ifdef USE_MPI
277
278
  MPI_Comm_size ( comm, &size );
  MPI_Comm_rank ( comm, &rank );
279
280

  if ( ptype < 0 || ptype > maxPtype )
281
    xabort ( "PIOTYPE is no valid modus" );
Deike Kleberg's avatar
Deike Kleberg committed
282
283
284

#ifdef _SX
  if ( ptype ==  PIO_POSIX_ASYNCH )
285
    xabort ( "PIO_POSIX_ASYNCH does not work on SX" );
Deike Kleberg's avatar
Deike Kleberg committed
286
#endif
287

288
  pioinfo = ( pioInfo * ) xmalloc ( sizeof ( pioInfo ));
289
290

  pioinfo->type = ptype;
Deike Kleberg's avatar
Deike Kleberg committed
291

292
  setPioCommunicator ( & ( pioinfo->comm ), comm, color, nnodes );
293

294
  if (( pioinfo->type == PIO_NONE ) && ( *nnodes != size ))
295
    xabort ( "PIOTYPE, NNODES: not a valid combination" );
296

297
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
298
299
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));
300

Deike Kleberg's avatar
Deike Kleberg committed
301
  if ( ddebug == MAXDEBUG && pioinfo->rank == 0 )
302
303
304
305
306
    fprintf ( stderr,
              "pe%d in bInit(), ptype=%d, initial_buffersize=%ld: "
              "after init pioinfo ...\n",
              pioinfo->rank, pioinfo->type, initial_buffersize );

307
  pioinfo->collectorComm = MPI_COMM_NULL;
Deike Kleberg's avatar
Deike Kleberg committed
308

Deike Kleberg's avatar
Deike Kleberg committed
309
310
  switch ( pioinfo->type )
    {
311
312
    case PIO_NONE:
      MPI_Comm_dup ( pioinfo->comm, &( pioinfo->collectorComm ));
313
      collectingData = 1;
314
      break;
315
    case PIO_MPI_NONB:
316
      collectingData = initMPINONB ();
Deike Kleberg's avatar
Deike Kleberg committed
317
      break;
Deike Kleberg's avatar
Deike Kleberg committed
318
#ifndef _SX
Deike Kleberg's avatar
Deike Kleberg committed
319
    case PIO_POSIX_ASYNCH:
320
      collectingData = initPOSIXASYNCH ();
Deike Kleberg's avatar
Deike Kleberg committed
321
      break;
Deike Kleberg's avatar
Deike Kleberg committed
322
#endif
323
    case PIO_POSIX_FPGUARD_SENDRECV:
324
      collectingData = initPOSIXFPGUARDSENDRECV ();
325
      break;
326
    case PIO_POSIX_FPGUARD_THREAD:
327
      collectingData = initPOSIXFPGUARDTHREAD ();
328
329
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
330
      collectingData = initPOSIXFPGUARDTHREADREFUSE ();
331
      break;
Deike Kleberg's avatar
Deike Kleberg committed
332
    case PIO_POSIX_NONB:
333
      collectingData = initPOSIXNONB ();
334
      break;
Deike Kleberg's avatar
Deike Kleberg committed
335
    }
336
#endif
337

Deike Kleberg's avatar
Deike Kleberg committed
338
  if ( ddebug == MAXDEBUG )
Deike Kleberg's avatar
Deike Kleberg committed
339
    fprintf ( stderr, "pe in bInit out\n" );
340
341

#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
342
  return pioinfo->collectorComm;
343
344
345
#else
  return 0;
#endif
Deike Kleberg's avatar
Deike Kleberg committed
346
347
348
349
}

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

Deike Kleberg's avatar
Deike Kleberg committed
350
void bFinalize ()
Deike Kleberg's avatar
Deike Kleberg committed
351
{
352
#ifdef USE_MPI
353
354
355
356
357
358
359
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

360
  if ( pioinfo->collectorComm != MPI_COMM_NULL )
361
    MPI_Comm_free ( &( pioinfo->collectorComm ));
362
363

  if ( pioinfo->comm != MPI_COMM_NULL )
364
365
    MPI_Comm_free ( &( pioinfo->comm ));

Deike Kleberg's avatar
Deike Kleberg committed
366
  free ( pioinfo );
367
#endif
Deike Kleberg's avatar
Deike Kleberg committed
368
369
370

  return;
}