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

548
549
550
551
552
553
554
555
556
557
558
static void
cdiPio_xt_idxlist_pack_wrap(void *data, void *buf, int size, int *pos,
                            void *context)
{
  MPI_Comm comm = *(MPI_Comm *)context;
  int pack_size = xt_idxlist_get_pack_size((Xt_idxlist)data, comm);
  xassert(size >= *pos && pack_size <= size - *pos);
  xt_idxlist_pack((Xt_idxlist)data, (unsigned char *)buf,
                  size, pos, comm);
}

559
560
561
562
563
564
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
565
566
  vlistID  = streamInqVlist ( streamID );
  collID   = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
567
568
  xassert ( collID         >= 0                    &&
            collID         <  commInqNProcsColl () &&
569
            txWin != NULL);
Deike Kleberg's avatar
Deike Kleberg committed
570

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

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

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

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

597
598
/************************************************************************/

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

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

Deike Kleberg's avatar
Deike Kleberg committed
610
611
  if ( rankGlob != root ) return;

612
  xassert(txWin != NULL);
613

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

628
  xdebug("%s", "RETURN");
629
630
}

631
632
633
634
#endif

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

635
636
637
638
/* pioInit definition must currently compile even in non-MPI configurations */
#ifndef MPI_VERSION
#  define MPI_Comm int
#endif
639
640
641
/**
   @brief initializes the MPI_Communicators needed for the
  communication between the calculator PEs and the I/O PEs and within the
642
643
644
645
646
647
648
649
650
  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]:
651
652

  Collective call
653
654
655

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

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

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

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

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

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

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

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

699
700
701
  xassert(partInflate >= 1.0);
  cdiPIOpartInflate_ = partInflate;

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

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

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

732
733
#ifndef MPI_VERSION
#  undef MPI_Comm
Deike Kleberg's avatar
Deike Kleberg committed
734
735
#endif

736
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
737

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

745
  xdebug("%s", "START");
746

Deike Kleberg's avatar
Deike Kleberg committed
747
  varsMapNDeco ( commInqNNodes (), commInqNodeSizes ());
Deike Kleberg's avatar
Deike Kleberg committed
748

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

      xmpi ( MPI_Send ( buffer, bufferSize, MPI_PACKED, commInqNProcsModel (),
755
                        RESOURCES, commInqCommsIO ( rankGlob )));
756

757
      xdebug("%s", "SENT MESSAGE WITH TAG \"RESOURCES\"");
758

Deike Kleberg's avatar
Deike Kleberg committed
759
      reshPackBufferDestroy ( &buffer );
Deike Kleberg's avatar
Deike Kleberg committed
760
    }
Deike Kleberg's avatar
Deike Kleberg committed
761
762

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

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

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

Deike Kleberg's avatar
Deike Kleberg committed
779

780
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
781

Deike Kleberg's avatar
Deike Kleberg committed
782

Deike Kleberg's avatar
Deike Kleberg committed
783
784
785
786
787
788
789
790
791
/**
  @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
792
void pioFinalize ( void )
Deike Kleberg's avatar
Deike Kleberg committed
793
{
794
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
795
  int collID, ibuffer = 1111;
796
  xdebug("%s", "START");
Thomas Jahns's avatar
Thomas Jahns committed
797
  namespaceDelete(pioNamespace_);
Deike Kleberg's avatar
Deike Kleberg committed
798
  for ( collID = 0; collID < commInqNProcsColl (); collID++ )
799
    {
800
      xmpi ( MPI_Send ( &ibuffer, 1, MPI_INT, commInqNProcsModel (),
Deike Kleberg's avatar
Deike Kleberg committed
801
                        FINALIZE, commInqCommsIO ( collID )));
802
      xdebug("%s", "SENT MESSAGE WITH TAG \"FINALIZE\"");
803
    }
Deike Kleberg's avatar
Deike Kleberg committed
804
  modelWinCleanup ();
805
  commDestroy ();
806
807
  if (xtInitByCDI)
    xt_finalize();
808
  xdebug("%s", "RETURN");
809
#endif
810
811
}

812
 /************************************************************************/
813

814
void pioWriteTimestep()
815
816
{
#ifdef USE_MPI
817
  int collID, iAssert = 0;
818
  /* int tokenEnd = END; */
819
  int rankGlob = commInqRankGlob ();
820
821
  int nProcsColl = commInqNProcsColl ();
  int nProcsModel = commInqNProcsModel ();