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

5
#include <limits.h>
Deike Kleberg's avatar
Deike Kleberg committed
6
#include <stdlib.h>
7
#include <stdio.h>
8
#include <stdarg.h>
9
10
11

#ifdef USE_MPI
#include <mpi.h>
12
#include <yaxt.h>
13
14
#endif

15
#include "cdi.h"
Deike Kleberg's avatar
Deike Kleberg committed
16
#include "pio_util.h"
Deike Kleberg's avatar
Deike Kleberg committed
17
#include "vlist_var.h"
18
19

#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
20
#include "namespace.h"
21
#include "pio.h"
22
#include "pio_serialize.h"
23
#include "pio_interface.h"
Deike Kleberg's avatar
Deike Kleberg committed
24
#include "pio_comm.h"
Deike Kleberg's avatar
Deike Kleberg committed
25
#include "pio_rpc.h"
26
#include "pio_client.h"
Deike Kleberg's avatar
Deike Kleberg committed
27
#include "pio_server.h"
28
#include "resource_handle.h"
29
#include "cdi_int.h"
Deike Kleberg's avatar
Deike Kleberg committed
30
#include "vlist.h"
31

Deike Kleberg's avatar
Deike Kleberg committed
32
extern resOps streamOps;
Deike Kleberg's avatar
Deike Kleberg committed
33

34

35
static struct rdmaWin
36
37
{
  size_t size;
38
  unsigned char *buffer, *head;
39
  MPI_Win win;
40
  int postSet, refuseFuncCall;
41
  MPI_Group ioGroup;
42
  int dictSize, dictDataUsed, dictRPCUsed, dict;
43
44
} *txWin = NULL;

Deike Kleberg's avatar
Deike Kleberg committed
45

46
47
48
const char * const funcMap[numRPCFuncs] = {
  "streamOpen",
  "streamDefVlist",
49
50
  "streamClose",
  "streamDefTimestep",
51
};
Deike Kleberg's avatar
Deike Kleberg committed
52

53
float cdiPIOpartInflate_;
Deike Kleberg's avatar
Deike Kleberg committed
54

Deike Kleberg's avatar
Deike Kleberg committed
55
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
56

57
static int cmp ( const void * va, const void * vb )
Deike Kleberg's avatar
Deike Kleberg committed
58
{
59
60
    const int ** a, ** b;

Deike Kleberg's avatar
Deike Kleberg committed
61
62
  a = ( const int ** ) va;
  b = ( const int ** ) vb;
63

Deike Kleberg's avatar
Deike Kleberg committed
64
65
66
  return (( **a < **b ) - ( **a > **b ));
}

67
68
69
70
71
72
73
74
75
void memcpyPackFunc(void *dataDesc, void *buf, int size, int *pos,
                    void *context)
{
  struct memCpyDataDesc *p = dataDesc;
  xassert(size >= *pos && (size_t)(size - *pos) >= p->obj_size);
  memcpy((unsigned char *)buf + *pos, p->obj, p->obj_size);
  *pos += (int)p->obj_size;
}

Deike Kleberg's avatar
Deike Kleberg committed
76
/****************************************************/
77

78
79
80
static void
mapProblems(int problemSizes[], int * problemMapping, int nProblems,
            int nWriter, double * w)
Deike Kleberg's avatar
Deike Kleberg committed
81
82
83
84
85
86
87
{
  int *ip[nProblems];
  int dummy[nProblems];
  int buckets[nWriter];
  int currCapacity, nextCapacity;
  double meanBucket[nWriter];
  int sum = 0;
Thomas Jahns's avatar
Thomas Jahns committed
88
  int writerIdx = 0;
Deike Kleberg's avatar
Deike Kleberg committed
89
  int i, j;
90

Deike Kleberg's avatar
Deike Kleberg committed
91
92
93
94
95
96
97
98
99
100
101

  for ( i = 0; i < nProblems; i++ )
    {
      ip[i] = &problemSizes[i];
      sum += problemSizes[i];
    }

  qsort ( ip, nProblems, sizeof ( int * ), cmp );

  for ( i = 0; i < nProblems; i++ )
    dummy[i] = ip[i] - problemSizes;
102
103

  for ( j = 0; j < nWriter; j++ )
Deike Kleberg's avatar
Deike Kleberg committed
104
105
    meanBucket[j] = ( double ) sum * ( * ( w + j ));

Thomas Jahns's avatar
Thomas Jahns committed
106
107
  memset(buckets, 0, sizeof (buckets));

Deike Kleberg's avatar
Deike Kleberg committed
108
109
110
  for ( i = 0; i < nProblems; i++ )
    {
      currCapacity = INT_MIN;
111

Deike Kleberg's avatar
Deike Kleberg committed
112
      for ( j = 0; j < nWriter; j++ )
113
114
	{
	  nextCapacity = meanBucket[j] - ( buckets[j] + ( *ip[i] ));
115

116
117
118
119
120
121
	  if ( nextCapacity > currCapacity )
	    {
	      currCapacity = nextCapacity;
	      writerIdx = j;
	    }
	}
Thomas Jahns's avatar
Thomas Jahns committed
122
      problemMapping[ dummy[i] ] = writerIdx;
Deike Kleberg's avatar
Deike Kleberg committed
123
124
      buckets[writerIdx] +=  *ip[i];
    }
Deike Kleberg's avatar
Deike Kleberg committed
125

126
127
128
129
130
131
132
133
134
  if ( ddebug )
    {
      xprintArray3 (  "problemSizes = ", problemSizes, nProblems, DATATYPE_INT );
      xprintArray3 ( "vector of indices, qsort of problemSizes", dummy,
                    nProblems, DATATYPE_INT );
      xprintArray3 ( "problemMapping", problemMapping, nProblems, DATATYPE_INT );
      xprintArray3 ( "meanBucket", meanBucket, nWriter, DATATYPE_FLT );
      xprintArray3 ( "actual buckets", buckets, nWriter, DATATYPE_INT );
    }
Deike Kleberg's avatar
Deike Kleberg committed
135
136
}

137
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
138
139
140
141
142

/**
   @brief is encapsulated in CDI library.

   @param vSizes array with number of levels for all var_t 's in order of streams
143

Deike Kleberg's avatar
Deike Kleberg committed
144
145
   @param sSizes array with number of var_t for all stream_t 's

146
   @param varMapping return value, array with ranks of I/O PEs assigned to var_t 's
Deike Kleberg's avatar
Deike Kleberg committed
147
148
149
in order of vSizes

   @param nStreams number of stream_t 's
150
151

   @param nodeSizes array of number of I/O PEs on each physical nodes, increasing
Deike Kleberg's avatar
Deike Kleberg committed
152
153
154
   order of ranks assumed

   @param nNodes number of physical nodes hosting I/O PEs
155

Deike Kleberg's avatar
Deike Kleberg committed
156
157
   @return
*/
158

159
160
161
static void
varMapGen(int *vSizes, int *sSizes, int *varMapping,
          int nStreams, int *nodeSizes, int nNodes)
Deike Kleberg's avatar
Deike Kleberg committed
162
{
163

Deike Kleberg's avatar
Deike Kleberg committed
164
165
166
167
168
  int weightsStreams[nStreams];
  int streamMapping[nStreams];
  int nPEs = 0, nVars = 0;
  int i, j, k, offset = 0, offsetN = 0;
  double * w;
169

Deike Kleberg's avatar
Deike Kleberg committed
170
171
172
  int * weightsVarsNode;
  int * varMappingNode;
  int nVarsNode, summandRank = 0;
173
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
174

175
  int buckets[nProcsColl];
Deike Kleberg's avatar
Deike Kleberg committed
176
177
178
179
180
181

  for ( i = 0; i < nStreams; i++ )
    {
      nVars += * ( sSizes + i );
      weightsStreams[i] = 0;
      for ( j = 0; j < * ( sSizes + i ); j++ )
182
	weightsStreams[i] += * ( vSizes + offset++ );
Deike Kleberg's avatar
Deike Kleberg committed
183
184
    }

Deike Kleberg's avatar
Deike Kleberg committed
185
  w = ( double * ) xmalloc ( nNodes * sizeof ( double ));
Deike Kleberg's avatar
Deike Kleberg committed
186
187
188
189
190
191
192
193
194
195
196
197
198
  for ( j = 0; j < nNodes; j++ )
    nPEs += * ( nodeSizes + j );

  for ( j = 0; j < nNodes; j++ )
    w[j] = ( double ) * ( nodeSizes + j ) / ( double ) nPEs;

  mapProblems ( weightsStreams, streamMapping, nStreams, nNodes, w );
  free ( w );

  for ( i = 0; i < nNodes; i++ )
    {
      nVarsNode = 0;
      for ( j = 0; j < nStreams; j++ )
199
	if ( * ( streamMapping + j ) == i )
200
201
	  nVarsNode += * ( sSizes + j );

Deike Kleberg's avatar
Deike Kleberg committed
202
203
204
      weightsVarsNode = ( int * ) xmalloc ( nVarsNode * sizeof ( int ));
      varMappingNode = ( int * ) xmalloc ( nVarsNode * sizeof ( int ));
      w = ( double * ) xmalloc ( * ( nodeSizes + i ) * sizeof ( double ));
Deike Kleberg's avatar
Deike Kleberg committed
205
206
207
208
      offset = 0;
      offsetN = 0;

      for ( j = 0; j < nStreams; j++ )
209
210
211
212
213
	if ( * ( streamMapping + j ) == i )
	  for ( k = 0; k < * ( sSizes + j ); k ++ )
	    * ( weightsVarsNode + offsetN++ ) = * ( vSizes + offset++ );
	else
	  offset += * ( sSizes + j );
Deike Kleberg's avatar
Deike Kleberg committed
214
215

      for ( j = 0; j < * ( nodeSizes + i ); j ++ )
216
	w[j] = 1.0 / ( double ) * ( nodeSizes + i );
Deike Kleberg's avatar
Deike Kleberg committed
217

218
      mapProblems ( weightsVarsNode, varMappingNode, nVarsNode,
219
		    * ( nodeSizes + i ),  w );
Deike Kleberg's avatar
Deike Kleberg committed
220
221
222
223
224

      offset = 0;
      offsetN = 0;

      for ( j = 0; j < nStreams; j++ )
225
226
	if ( * ( streamMapping + j ) == i )
	  for ( k = 0; k < * ( sSizes + j ); k ++ )
227
	    * ( varMapping + offset ++ ) =
228
              commCollID2RankGlob ( * ( varMappingNode + offsetN ++ ) +
229
                                    summandRank );
230
231
	else
	  offset += * ( sSizes + j );
Deike Kleberg's avatar
Deike Kleberg committed
232
233
234
235
236
237
238

      summandRank += * ( nodeSizes + i );

      free ( w );
      free ( varMappingNode );
      free ( weightsVarsNode );
    }
239

240
  if ( ddebug )
Deike Kleberg's avatar
Deike Kleberg committed
241
    {
Deike Kleberg's avatar
Deike Kleberg committed
242
      xprintArray ( "varMapping", varMapping, nVars, DATATYPE_INT  );
243
      for ( i = 0; i < nProcsColl; i++ )
244
	buckets[i] = 0;
Deike Kleberg's avatar
Deike Kleberg committed
245
      for ( i = 0; i < nVars; i ++ )
246
	buckets[commRankGlob2CollID ( *(varMapping + i ))] += * ( vSizes + i );
247
      xprintArray ( "buckets", buckets, nProcsColl, DATATYPE_INT );
Deike Kleberg's avatar
Deike Kleberg committed
248
    }
249
}
Deike Kleberg's avatar
Deike Kleberg committed
250

251
/************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
252

253
254
static void
varsMapNDeco(int nNodes, int *nodeSizes)
Deike Kleberg's avatar
Deike Kleberg committed
255
{
256
  int nStreams, nVars, * resHs, * streamSizes, * varSizes, * varMapping,
Thomas Jahns's avatar
Thomas Jahns committed
257
    * collectsData;
Deike Kleberg's avatar
Deike Kleberg committed
258
  int i, j, k = 0;
Deike Kleberg's avatar
Deike Kleberg committed
259
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
260

261
262
  xdebug ( "START, nProcsColl=%d", nProcsColl );

Deike Kleberg's avatar
Deike Kleberg committed
263
  nStreams = streamSize ();
264

265
266
  resHs       = xmalloc ( nStreams * sizeof ( resHs[0] ));
  streamSizes = xmalloc ( nStreams * sizeof ( streamSizes[0] ));
267
  collectsData = xmalloc ( nProcsColl * sizeof ( collectsData[0] ));
268
  streamGetIndexList ( nStreams, resHs );
269

Deike Kleberg's avatar
Deike Kleberg committed
270
  for ( i = 0; i < nStreams; i++ )
271
    streamSizes[i] = streamInqNvars ( * ( resHs + i ));
272

Deike Kleberg's avatar
Deike Kleberg committed
273
  nVars = xsum ( nStreams, streamSizes );
274
275
  varSizes   = xmalloc ( nVars * sizeof ( varSizes[0] ));
  varMapping = xmalloc ( nVars * sizeof ( varMapping[0] ));
276

Deike Kleberg's avatar
Deike Kleberg committed
277
278
  for ( i = 0; i < nStreams; i++ )
    for ( j = 0; j < * ( streamSizes + i ); j++ )
279
      varSizes[k++] += vlistInqVarSize ( streamInqVlist ( * ( resHs + i )), j );
280

Deike Kleberg's avatar
Deike Kleberg committed
281
  xassert ( k == nVars );
282

283
  varMapGen ( varSizes, streamSizes, varMapping,
Deike Kleberg's avatar
Deike Kleberg committed
284
	      nStreams, nodeSizes, nNodes );
285
286
287

  k = 0;
  for ( i = 0; i < nStreams; i++ )
288
    for ( j = 0; j < * ( streamSizes + i ); j++ )
289
      {
290
        vlistDefVarIOrank ( streamInqVlist ( * ( resHs + i )), j,
291
                            * ( varMapping + k ));
292
        vlistDefVarIOrank ( streamInqVlistIDorig ( * ( resHs + i )), j,
293
294
                            * ( varMapping + k ));
        collectsData[commRankGlob2CollID ( varMapping[k++] )] = 1;
295
      }
296
297
298

  for ( j = 0; j < nProcsColl; j++ )
    if ( collectsData[j] == 0 )
299
300
301
302
      xabort("AT LEAST ONE COLLECTOR PROCESS IDLES, "
             "CURRENTLY NOT COVERED: "
             "PE%d collects no data",
             commCollID2RankGlob(j));
303

304
305
306
307
308
  if ( varMapping )   free ( varMapping );
  if ( varSizes )     free ( varSizes );
  if ( collectsData ) free ( collectsData );
  if ( streamSizes )  free ( streamSizes );
  if ( resHs )        free ( resHs );
309

310
  xdebug("%s", "RETURN");
Deike Kleberg's avatar
Deike Kleberg committed
311
312
313
314
}

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

315
static
Deike Kleberg's avatar
Deike Kleberg committed
316
317
void modelWinCleanup ( void )
{
Deike Kleberg's avatar
Deike Kleberg committed
318
  int collID;
Deike Kleberg's avatar
Deike Kleberg committed
319

320
  xdebug("%s", "START");
321
  if (txWin != NULL)
322
323
324
325
326
327
328
329
330
331
332
    {
      for ( collID = 0; collID < commInqNProcsColl (); collID ++ )
        {
          if (txWin[collID].postSet)
            xmpi(MPI_Win_wait(txWin[collID].win));
          xmpi(MPI_Win_free(&txWin[collID].win));
          xmpi ( MPI_Free_mem ( txWin[collID].buffer ));
          xmpi(MPI_Group_free(&txWin[collID].ioGroup));
        }
      free(txWin);
    }
Deike Kleberg's avatar
Deike Kleberg committed
333

334
  xdebug("%s", "RETURN. CLEANED UP MPI_WIN'S");
Deike Kleberg's avatar
Deike Kleberg committed
335
336
337
}

/************************************************************************/
338

339
340
struct collDesc
{
341
  int numDataRecords, numRPCRecords;
342
343
};

344
345
static void
modelWinDefBufferSizes(void)
Deike Kleberg's avatar
Deike Kleberg committed
346
{
347
  int collID, nstreams, * streamIndexList, streamNo, nvars, varID;
348
  int sumWinBufferSize = 0;
349
350
351
  int nProcsColl  = commInqNProcsColl ();
  int rankGlob    = commInqRankGlob ();
  int root = commInqRootGlob ();
352
  struct collDesc *collIndex;
Deike Kleberg's avatar
Deike Kleberg committed
353

354
  xdebug("%s", "START");
355
  xassert(txWin != NULL);
356

Deike Kleberg's avatar
Deike Kleberg committed
357
  nstreams = reshCountType ( &streamOps );
358
  streamIndexList = xmalloc ( nstreams * sizeof ( streamIndexList[0] ));
359
  collIndex = xcalloc(nProcsColl, sizeof (collIndex[0]));
360
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
Deike Kleberg's avatar
Deike Kleberg committed
361
362
  for ( streamNo = 0; streamNo < nstreams; streamNo++ )
    {
363
364
365
      // memory required for data
      int streamID = streamIndexList[streamNo];
      int vlistID = streamInqVlist(streamID);
Deike Kleberg's avatar
Deike Kleberg committed
366
367
368
      nvars = vlistNvars ( vlistID );
      for ( varID = 0; varID < nvars; varID++ )
        {
369
370
371
372
373
374
375
376
          int collID = commRankGlob2CollID(vlistInqVarIOrank(vlistID, varID));
          int collIDchunk;
          {
            int varSize = vlistInqVarSize(vlistID, varID);
            int nProcsModel = commInqNProcsModel();
            collIDchunk = (int)ceilf(cdiPIOpartInflate_
                                     * (varSize + nProcsModel - 1)/nProcsModel);
          }
Deike Kleberg's avatar
Deike Kleberg committed
377
          xassert ( collID != CDI_UNDEFID && collIDchunk > 0 );
378
379
380
381
382
383
          collIndex[collID].numDataRecords += 2;
          txWin[collID].size += (size_t)collIDchunk * sizeof (double)
            /* re-align chunks to multiple of double size */
            + sizeof (double) - 1
            /* one header for data record, one for corresponding part
             * descriptor*/
384
            + 2 * sizeof (struct winHeaderEntry)
385
386
            /* FIXME: heuristic for size of packed Xt_idxlist */
            + sizeof (Xt_int) * collIDchunk * 3;
Deike Kleberg's avatar
Deike Kleberg committed
387
        }
388

389
390
      // memory required for the function calls encoded
      // for remote execution
Deike Kleberg's avatar
Deike Kleberg committed
391
      // once per stream and timestep for all collprocs only on the modelproc root
392
393
      if ( rankGlob == root )
        for ( collID = 0; collID < nProcsColl; collID++ )
394
          {
395
            collIndex[collID].numRPCRecords += numRPCFuncs;
396
            txWin[collID].size +=
397
              numRPCFuncs * sizeof (struct winHeaderEntry)
398
399
400
              + MAXDATAFILENAME
              /* data part of streamDefTimestep */
              + (2 * CDI_MAX_NAME + sizeof (taxis_t));
401
          }
Deike Kleberg's avatar
Deike Kleberg committed
402
    }
403
  for (collID = 0; collID < nProcsColl; ++collID)
404
    {
405
406
      int numRecords = 1 + collIndex[collID].numDataRecords
        + collIndex[collID].numRPCRecords;
407
      txWin[collID].dictSize = numRecords;
408
409
      txWin[collID].dictDataUsed = 1;
      txWin[collID].dictRPCUsed = 0;
410
      /* account for size header */
411
      txWin[collID].size += sizeof (struct winHeaderEntry);
412
413
      txWin[collID].size = roundUpToMultiple(txWin[collID].size,
                                             PIO_WIN_ALIGN);
414
      sumWinBufferSize += txWin[collID].size;
415
    }
416
417
418
  free(collIndex);
  free ( streamIndexList );

419
420
  xdebug("sumWinBufferSize=%zu, MAXWINBUFFERSIZE=%zu", (size_t)sumWinBufferSize,
         (size_t)MAXWINBUFFERSIZE);
Deike Kleberg's avatar
Deike Kleberg committed
421
  xassert ( sumWinBufferSize <= MAXWINBUFFERSIZE );
422
  xdebug("%s", "RETURN");
Deike Kleberg's avatar
Deike Kleberg committed
423
}
424

Deike Kleberg's avatar
Deike Kleberg committed
425
426
427
428

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


429
static
Deike Kleberg's avatar
Deike Kleberg committed
430
  void modelWinFlushBuffer ( int collID )
Deike Kleberg's avatar
Deike Kleberg committed
431
{
Deike Kleberg's avatar
Deike Kleberg committed
432
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
433

Deike Kleberg's avatar
Deike Kleberg committed
434
435
  xassert ( collID                >= 0         &&
            collID                < nProcsColl &&
436
            txWin[collID].buffer     != NULL      &&
437
438
            txWin != NULL      &&
            txWin[collID].size >= 0         &&
439
            txWin[collID].size <= MAXWINBUFFERSIZE);
440
  memset(txWin[collID].buffer, 0, txWin[collID].size);
441
  txWin[collID].head = txWin[collID].buffer
442
    + txWin[collID].dictSize * sizeof (struct winHeaderEntry);
443
  txWin[collID].refuseFuncCall = 0;
444
445
  txWin[collID].dictDataUsed = 1;
  txWin[collID].dictRPCUsed = 0;
Deike Kleberg's avatar
Deike Kleberg committed
446
447
448
}


Deike Kleberg's avatar
Deike Kleberg committed
449
450
451
/************************************************************************/


Deike Kleberg's avatar
Deike Kleberg committed
452
453
454
static
void modelWinCreate ( void )
{
Deike Kleberg's avatar
Deike Kleberg committed
455
  int collID, ranks[1];
456
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
457

458
  xdebug("%s", "START");
459
  txWin = xmalloc(nProcsColl * sizeof (txWin[0]));
Deike Kleberg's avatar
Deike Kleberg committed
460

Deike Kleberg's avatar
Deike Kleberg committed
461
  modelWinDefBufferSizes ();
462
  ranks[0] = commInqNProcsModel ();
Deike Kleberg's avatar
Deike Kleberg committed
463

Deike Kleberg's avatar
Deike Kleberg committed
464
  for ( collID = 0; collID < nProcsColl; collID ++ )
Deike Kleberg's avatar
Deike Kleberg committed
465
    {
466
      xassert(txWin[collID].size > 0);
467
      txWin[collID].buffer = NULL;
468
      xmpi(MPI_Alloc_mem((MPI_Aint)txWin[collID].size, MPI_INFO_NULL,
469
470
                         &txWin[collID].buffer));
      xassert ( txWin[collID].buffer != NULL );
471
      txWin[collID].head = txWin[collID].buffer
472
        + txWin[collID].dictSize * sizeof (struct winHeaderEntry);
473
      xmpi(MPI_Win_create(txWin[collID].buffer, (MPI_Aint)txWin[collID].size, 1,
474
                          MPI_INFO_NULL, commInqCommsIO(collID),
475
                          &txWin[collID].win));
476
477
478
      xmpi(MPI_Comm_group(commInqCommsIO(collID), &txWin[collID].ioGroup));
      xmpi(MPI_Group_incl(txWin[collID].ioGroup, 1, ranks,
                          &txWin[collID].ioGroup ));
Deike Kleberg's avatar
Deike Kleberg committed
479
    }
480
  xdebug("%s", "RETURN, CREATED MPI_WIN'S");
Deike Kleberg's avatar
Deike Kleberg committed
481
482
483
484
}

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

485
static void
486
modelWinEnqueue(int collID,
487
488
                struct winHeaderEntry header, const void *data, size_t size,
                valPackFunc packFunc)
Deike Kleberg's avatar
Deike Kleberg committed
489
{
490
491
  struct winHeaderEntry *winDict
    = (struct winHeaderEntry *)txWin[collID].buffer;
492
  int targetEntry;
493
  if (header.id > 0)
494
    {
495
      targetEntry = (txWin[collID].dictDataUsed)++;
496
      int offset = header.offset
497
498
        = (int)roundUpToMultiple(txWin[collID].head - txWin[collID].buffer,
                                 sizeof (double));
499
500
501
502
      MPI_Comm comm = commInqCommsIO(collID);
      packFunc((void *)data, txWin[collID].buffer, (int)txWin[collID].size,
               &offset, &comm);
      txWin[collID].head = txWin[collID].buffer + offset;
503
    }
504
  else if (header.id == PARTDESCMARKER)
505
506
    {
      targetEntry = (txWin[collID].dictDataUsed)++;
507
      Xt_uid uid = header.specific.partDesc.uid;
508
509
510
511
      int offset = -1;
      /* search if same uid entry has already been enqueued */
      for (int entry = 2; entry < targetEntry; entry += 2)
        {
512
          xassert(winDict[entry].id == PARTDESCMARKER);
513
          if (winDict[entry].specific.partDesc.uid == uid)
514
            {
515
              offset = winDict[entry].offset;
516
517
518
519
520
521
522
523
524
              break;
            }
        }
      if (offset == -1)
        {
          /* not yet used partition descriptor, serialize at
           * current position */
          int position = 0;
          MPI_Comm comm = commInqCommsIO(collID);
525
          header.offset
526
527
528
529
530
531
532
533
534
535
536
            = (int)(txWin[collID].head - txWin[collID].buffer);
          size_t size = xt_idxlist_get_pack_size((Xt_idxlist)data, comm);
          size_t remaining_size = txWin[collID].size
            - (txWin[collID].head - txWin[collID].buffer);
          xassert(size <= remaining_size);
          xt_idxlist_pack((Xt_idxlist)data, txWin[collID].head,
                          (int)remaining_size, &position, comm);
          txWin[collID].head += position;
        }
      else
        /* duplicate entries are copied only once per timestep */
537
        header.offset = offset;
538
    }
539
  else
540
    {
541
      targetEntry = txWin[collID].dictSize - ++(txWin[collID].dictRPCUsed);
542
      if (header.id == STREAMOPEN)
543
        {
544
          header.offset
545
546
547
548
            = (int)(txWin[collID].head - txWin[collID].buffer);
          memcpy(txWin[collID].head, data, size);
          txWin[collID].head += size;
        }
549
      else if (header.id == STREAMDEFTIMESTEP)
550
        {
551
          header.offset
552
553
554
555
            = (int)(txWin[collID].head - txWin[collID].buffer);
          memcpy(txWin[collID].head, data, size);
          txWin[collID].head += size;
        }
556
    }
557
  winDict[targetEntry] = header;
Deike Kleberg's avatar
Deike Kleberg committed
558
559
}

560
561
562
563
564
565
void
pioBufferPartData(int streamID, int varID, const double *data,
                  int nmiss, Xt_idxlist partDesc)
{
  int vlistID, collID = CDI_UNDEFID;

Deike Kleberg's avatar
Deike Kleberg committed
566
567
  vlistID  = streamInqVlist ( streamID );
  collID   = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
568
569
  xassert ( collID         >= 0                    &&
            collID         <  commInqNProcsColl () &&
570
            txWin != NULL);
Deike Kleberg's avatar
Deike Kleberg committed
571

572
  if (txWin[collID].postSet)
573
    {
574
      xmpi(MPI_Win_wait(txWin[collID].win));
575
      txWin[collID].postSet = 0;
Deike Kleberg's avatar
Deike Kleberg committed
576
      modelWinFlushBuffer ( collID );
577
    }
578

579
580
581
  Xt_int chunk = xt_idxlist_get_num_indices(partDesc);
  xassert(chunk <= INT_MAX);

582
  struct winHeaderEntry dataHeader
583
    = { .id = streamID, .specific.dataRecord = { varID, nmiss }, .offset = -1 };
584
585
586
  modelWinEnqueue(collID, dataHeader,
                  &(struct memCpyDataDesc){data, chunk * sizeof (data[0])},
                  0, memcpyPackFunc);
587
  {
588
    struct winHeaderEntry partHeader
589
590
      = { .id = PARTDESCMARKER,
          .specific.partDesc = { .uid = xt_idxlist_get_uid(partDesc) },
591
          .offset = 0 };
592
    modelWinEnqueue(collID, partHeader, partDesc, 0, memcpyPackFunc);
593
  }
594

595
  txWin[collID].refuseFuncCall = 1;
Deike Kleberg's avatar
Deike Kleberg committed
596
597
}

598
599
/************************************************************************/

600
void pioBufferFuncCall(struct winHeaderEntry header,
601
602
                       const void *data, size_t data_len,
                       valPackFunc dataPackFunc)
603
604
605
606
{
  int rankGlob = commInqRankGlob ();
  int root = commInqRootGlob ();
  int collID, nProcsColl = commInqNProcsColl ();
607
  int funcID = header.id;
608

609
610
  xassert(funcID >= MINFUNCID && funcID <= MAXFUNCID);
  xdebug("%s, func: %s", "START", funcMap[(-1 - funcID)]);
611

Deike Kleberg's avatar
Deike Kleberg committed
612
613
  if ( rankGlob != root ) return;

614
  xassert(txWin != NULL);
615

Thomas Jahns's avatar
Thomas Jahns committed
616
617
618
619
620
621
622
623
  for (collID = 0; collID < nProcsColl; ++collID)
    {
      if (txWin[collID].postSet)
        {
          xmpi(MPI_Win_wait(txWin[collID].win));
          txWin[collID].postSet = 0;
          modelWinFlushBuffer ( collID );
        }
624
625
      xassert(txWin[collID].dictRPCUsed + txWin[collID].dictDataUsed
              < txWin[collID].dictSize);
Thomas Jahns's avatar
Thomas Jahns committed
626
      xassert(txWin[collID].refuseFuncCall == 0);
627
      modelWinEnqueue(collID, header, data, data_len, dataPackFunc);
628
    }
629

630
  xdebug("%s", "RETURN");
631
632
}

633
634
635
636
#endif

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

637
638
639
640
/* pioInit definition must currently compile even in non-MPI configurations */
#ifndef MPI_VERSION
#  define MPI_Comm int
#endif
641
642
643
/**
   @brief initializes the MPI_Communicators needed for the
  communication between the calculator PEs and the I/O PEs and within the
644
645
646
647
648
649
650
651
652
  group of I/O PEs.

  commGlob: all PEs

  commPIO: I/O PEs, PEs with highest ranks in commGlob

  commModel: calculating PEs, no I/O PEs

  commsIO[i]:
653
654

  Collective call
655
656
657

  @param comm MPI_Communicator of all calling PEs
  @param nIOP number of I/O PEs
658
659
660
  @param partInflate allow for array partitions on comute
  PE that are at most sized \f$ partInflate * \lceil arraySize /
  numComputePEs \rceil \f$
661
662
663
  @return int indicating wether the calling PE is a calcutator (1) or not (0)
*/

Thomas Jahns's avatar
Thomas Jahns committed
664
665
#ifdef USE_MPI
static int pioNamespace_ = -1;
666
static int xtInitByCDI = 0;
Thomas Jahns's avatar
Thomas Jahns committed
667
668
#endif

669
670
MPI_Comm
pioInit(MPI_Comm commGlob, int nProcsIO, int IOMode,
Thomas Jahns's avatar
Thomas Jahns committed
671
        int *pioNamespace, float partInflate)
672
{
673
#ifdef USE_MPI
Thomas Jahns's avatar
Thomas Jahns committed
674
  int sizeGlob;
Deike Kleberg's avatar
Deike Kleberg committed
675

Deike Kleberg's avatar
Deike Kleberg committed
676
  if ( IOMode < PIO_MINIOMODE || IOMode > PIO_MAXIOMODE )
Deike Kleberg's avatar
Deike Kleberg committed
677
    xabort ( "IOMODE IS NOT VALID." );
678

679
#ifdef _SX
Deike Kleberg's avatar
Deike Kleberg committed
680
681
  if ( IOMode ==  PIO_ASYNCH )
    xabort ( "PIO_ASYNCH DOES NOT WORK ON SX." );
682
683
#endif

684
685
686
687
  if ((xtInitByCDI = (!xt_initialized() || xt_finalized())))
    {
      xt_initialize(commGlob);
    }
688
689
  commInit ();
  commDefCommGlob ( commGlob );
690
  sizeGlob = commInqSizeGlob ();
691

692
693
  if (((IOMode != PIO_NONE && (nProcsIO <= 0 || nProcsIO > sizeGlob - 1)))
      || (IOMode == PIO_NONE && nProcsIO != 1))
694
695
    xabort("DISTRIBUTION OF TASKS ON PROCS IS NOT VALID.\n"
           "nProcsIO=%d, sizeGlob=%d\n", nProcsIO, sizeGlob);
Deike Kleberg's avatar
Deike Kleberg committed
696
697

  commDefNProcsIO ( nProcsIO );
Deike Kleberg's avatar
Deike Kleberg committed
698
  commDefIOMode   ( IOMode, PIO_MAXIOMODE, PIO_MINIOMODEWITHSPECIALPROCS );
Deike Kleberg's avatar
Deike Kleberg committed
699
  commDefCommPio  ();
700

701
702
703
  xassert(partInflate >= 1.0);
  cdiPIOpartInflate_ = partInflate;

Deike Kleberg's avatar
Deike Kleberg committed
704
  // JUST FOR TEST CASES WITH ONLY ONE MPI TASK
705
  if ( commInqSizeGlob () == 1 )
706
    {
707
      pioNamespace_ = *pioNamespace = namespaceNew();
Deike Kleberg's avatar
Deike Kleberg committed
708
709
      return commInqCommGlob ();
    }
710

711
712
  if ( commInqIsProcIO ())
    {
713
      serializeSetMPI();
714
715
716
      namespaceSwitchSet(NSSWITCH_ABORT, NSSW_FUNC(cdiAbortC_MPI));
      namespaceSwitchSet(NSSWITCH_FILE_OPEN, NSSW_FUNC(pioFileOpen));
      namespaceSwitchSet(NSSWITCH_FILE_CLOSE, NSSW_FUNC(pioFileClose));
717
      IOServer ();
Thomas Jahns's avatar
Thomas Jahns committed
718
      namespaceDelete(0);
719
      commDestroy ();
720
      xt_finalize();
721
      MPI_Finalize ();
722
      exit ( EXIT_SUCCESS );
723
724
    }
  else
725
    cdiPioClientSetup(&pioNamespace_, pioNamespace);
726

727
  xdebug ( "nProcsGlob=%d, RETURN", sizeGlob );
728
  return commInqCommModel ();
729
#else
730
  abort();
731
#endif
732
}
733

734
735
#ifndef MPI_VERSION
#  undef MPI_Comm
Deike Kleberg's avatar
Deike Kleberg committed
736
737
#endif

738
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
739

740
void  pioEndDef ( void )
741
742
{
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
743
  char   * buffer;
744
  int bufferSize;
745
  int rankGlob = commInqRankGlob ();
746

747
  xdebug("%s", "START");
748

Deike Kleberg's avatar
Deike Kleberg committed
749
  varsMapNDeco ( commInqNNodes (), commInqNodeSizes ());
Deike Kleberg's avatar
Deike Kleberg committed
750

751
  if ( rankGlob < commInqNProcsColl ())
Deike Kleberg's avatar
Deike Kleberg committed
752
    {
753
754
      MPI_Comm comm = commInqCommsIO ( rankGlob );
      reshPackBufferCreate(&buffer, &bufferSize, &comm);
755
756

      xmpi ( MPI_Send ( buffer, bufferSize, MPI_PACKED, commInqNProcsModel (),
757
                        RESOURCES, commInqCommsIO ( rankGlob )));
758

759
      xdebug("%s", "SENT MESSAGE WITH TAG \"RESOURCES\"");
760

Deike Kleberg's avatar
Deike Kleberg committed
761
      reshPackBufferDestroy ( &buffer );
Deike Kleberg's avatar
Deike Kleberg committed
762
    }
Deike Kleberg's avatar
Deike Kleberg committed
763
764

  modelWinCreate ();
765
  namespaceDefResStatus ( STAGE_TIMELOOP );
766
  xdebug("%s", "RETURN");
767
768
769
770
771
772
773
774
#endif
}

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

void  pioEndTimestepping ( void )
{
#ifdef USE_MPI
775
  xdebug("%s", "START");
776
  namespaceDefResStatus ( STAGE_CLEANUP );
777
  xdebug("%s", "RETURN");
778
#endif
779
780
}

Deike Kleberg's avatar
Deike Kleberg committed
781

782
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
783

Deike Kleberg's avatar
Deike Kleberg committed
784

Deike Kleberg's avatar
Deike Kleberg committed
785
786
787
788
789
790
791
792
793
/**
  @brief is invoked by the calculator PEs, to inform
  the I/O PEs that no more data will be written.

  @param

  @return
*/

Deike Kleberg's avatar
Deike Kleberg committed
794
void pioFinalize ( void )
Deike Kleberg's avatar
Deike Kleberg committed
795
{
796
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
797
  int collID, ibuffer = 1111;
798
  xdebug("%s", "START");
Thomas Jahns's avatar
Thomas Jahns committed
799
  namespaceDelete(pioNamespace_);
Deike Kleberg's avatar
Deike Kleberg committed
800
  for ( collID = 0; collID < commInqNProcsColl (); collID++ )
801
    {
802
      xmpi ( MPI_Send ( &ibuffer, 1, MPI_INT, commInqNProcsModel (),
Deike Kleberg's avatar
Deike Kleberg committed
803
                        FINALIZE, commInqCommsIO ( collID )));
804
      xdebug("%s", "SENT MESSAGE WITH TAG \"FINALIZE\"");
805
    }
Deike Kleberg's avatar
Deike Kleberg committed
806
  modelWinCleanup ();
807
  commDestroy ();
808
809
  if (xtInitByCDI)
    xt_finalize();
810
  xdebug("%s", "RETURN");
811
#endif
812
813
}

814
 /************************************************************************/
815

816
void pioWriteTimestep()
817
818
{
#ifdef USE_MPI
819
  int collID, iAssert = 0;
820
<