pio.c 8.16 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

Thomas Jahns's avatar
Thomas Jahns committed
11
#include "cdi.h"
12
13
14
#include "pio.h"
#include "pio_util.h"

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

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

Deike Kleberg's avatar
Deike Kleberg committed
20

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

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

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

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

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

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

50
51
char *token = "%";

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

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

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

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

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

  return rtag;
}

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

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

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

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

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

  return iret;
}

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

int pioFileClose ( int id )
{
Thomas Jahns's avatar
Thomas Jahns committed
117
  int iret = CDI_UNDEFID;
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
    }

  return iret;
}

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

int pioFileOpenW ( const char *filename )
{
Thomas Jahns's avatar
Thomas Jahns committed
149
  int iret = CDI_UNDEFID;
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';
210
      xdebug ( "myHost = %s", 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

255
  xdebug ( "color=%d", *color );
256

257
258
259
260
261
262
263
  return;
}

#endif

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

Deike Kleberg's avatar
Deike Kleberg committed
264
MPI_Comm bInit ( int ptype, MPI_Comm comm, int *color, int *nnodes )
265
{
Deike Kleberg's avatar
Deike Kleberg committed
266
  int size, rank;
267
  int collectingData = 1;
268
269
  *nnodes = 1;
  *color = 1;
270

Deike Kleberg's avatar
Deike Kleberg committed
271
#ifdef USE_MPI
272
273
  MPI_Comm_size ( comm, &size );
  MPI_Comm_rank ( comm, &rank );
274
275

  if ( ptype < 0 || ptype > maxPtype )
276
    xabort ( "PIOTYPE is no valid modus" );
Deike Kleberg's avatar
Deike Kleberg committed
277
278
279

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

283
  pioinfo = ( pioInfo * ) xmalloc ( sizeof ( pioInfo ));
284
285

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

287
  setPioCommunicator ( & ( pioinfo->comm ), comm, color, nnodes );
288

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

292
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
293

Deike Kleberg's avatar
Deike Kleberg committed
294
295
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));
296

297
  xdebug( "IOPE%d in bInit(), ptype=%d, initial_buffersize=%ld: "
298
299
          "after init pioinfo ...",
          pioinfo->rank, pioinfo->type, initial_buffersize );
300

301
  pioinfo->collectorComm = MPI_COMM_NULL;
Deike Kleberg's avatar
Deike Kleberg committed
302

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

#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
333
  return pioinfo->collectorComm;
334
335
336
#else
  return 0;
#endif
Deike Kleberg's avatar
Deike Kleberg committed
337
338
339
340
}

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

Deike Kleberg's avatar
Deike Kleberg committed
341
void bFinalize ()
Deike Kleberg's avatar
Deike Kleberg committed
342
{
343
#ifdef USE_MPI
344
345
346
347
348
349
350
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

351
  if ( pioinfo->collectorComm != MPI_COMM_NULL )
352
    MPI_Comm_free ( &( pioinfo->collectorComm ));
353
354

  if ( pioinfo->comm != MPI_COMM_NULL )
355
356
    MPI_Comm_free ( &( pioinfo->comm ));

Deike Kleberg's avatar
Deike Kleberg committed
357
  free ( pioinfo );
358
#endif
Deike Kleberg's avatar
Deike Kleberg committed
359
}
360
361
362
363
364
365
366
367
368
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */