pio_interface.c 25.3 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 != NULL      &&
Thomas Jahns's avatar
Thomas Jahns committed
437
            txWin[collID].buffer     != NULL      &&
438
            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

464
465
466
  MPI_Info no_locks_info;
  xmpi(MPI_Info_create(&no_locks_info));
  xmpi(MPI_Info_set(no_locks_info, "no_locks", "true"));
Deike Kleberg's avatar
Deike Kleberg committed
467
  for ( collID = 0; collID < nProcsColl; collID ++ )
Deike Kleberg's avatar
Deike Kleberg committed
468
    {
469
      xassert(txWin[collID].size > 0);
470
      txWin[collID].buffer = NULL;
471
      xmpi(MPI_Alloc_mem((MPI_Aint)txWin[collID].size, MPI_INFO_NULL,
472
473
                         &txWin[collID].buffer));
      xassert ( txWin[collID].buffer != NULL );
474
      txWin[collID].head = txWin[collID].buffer
475
        + txWin[collID].dictSize * sizeof (struct winHeaderEntry);
476
      xmpi(MPI_Win_create(txWin[collID].buffer, (MPI_Aint)txWin[collID].size, 1,
477
                          no_locks_info, commInqCommsIO(collID),
478
                          &txWin[collID].win));
Thomas Jahns's avatar
Thomas Jahns committed
479
480
481
482
      MPI_Group commGroup;
      xmpi(MPI_Comm_group(commInqCommsIO(collID), &commGroup));
      xmpi(MPI_Group_incl(commGroup, 1, ranks, &txWin[collID].ioGroup));
      xmpi(MPI_Group_free(&commGroup));
Deike Kleberg's avatar
Deike Kleberg committed
483
    }
484
485
486

  xmpi(MPI_Info_free(&no_locks_info));

487
  xdebug("%s", "RETURN, CREATED MPI_WIN'S");
Deike Kleberg's avatar
Deike Kleberg committed
488
489
490
491
}

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

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

555
556
557
558
559
560
561
562
563
564
565
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);
}

566
567
568
569
570
571
572
573
574
575
576
static inline void
collWait(int collID)
{
  if (txWin[collID].postSet)
    {
      xmpi(MPI_Win_wait(txWin[collID].win));
      txWin[collID].postSet = 0;
      modelWinFlushBuffer(collID);
    }
}

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
static inline void
collProbe(int collID)
{
  if (txWin[collID].postSet)
    {
      int flag;
      xmpi(MPI_Win_test(txWin[collID].win, &flag));
      if (flag)
        {
          txWin[collID].postSet = 0;
          modelWinFlushBuffer(collID);
        }
    }
}

void
cdiPioRDMAProgress()
{
  int nProcsColl = commInqNProcsColl();
  for (int collID = 0; collID < nProcsColl; collID++)
    collProbe(collID);
}


601
602
603
604
605
606
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
607
608
  vlistID  = streamInqVlist ( streamID );
  collID   = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
609
610
  xassert ( collID         >= 0                    &&
            collID         <  commInqNProcsColl () &&
611
            txWin != NULL);
Deike Kleberg's avatar
Deike Kleberg committed
612

613
  collWait(collID);
614

615
616
617
  Xt_int chunk = xt_idxlist_get_num_indices(partDesc);
  xassert(chunk <= INT_MAX);

618
  struct winHeaderEntry dataHeader
619
    = { .id = streamID, .specific.dataRecord = { varID, nmiss }, .offset = -1 };
620
621
  modelWinEnqueue(collID, dataHeader,
                  &(struct memCpyDataDesc){data, chunk * sizeof (data[0])},
622
                  memcpyPackFunc);
623
  {
624
    struct winHeaderEntry partHeader
625
626
      = { .id = PARTDESCMARKER,
          .specific.partDesc = { .uid = xt_idxlist_get_uid(partDesc) },
627
          .offset = 0 };
628
    modelWinEnqueue(collID, partHeader, partDesc, cdiPio_xt_idxlist_pack_wrap);
629
  }
630

631
  txWin[collID].refuseFuncCall = 1;
Deike Kleberg's avatar
Deike Kleberg committed
632
633
}

634
635
/************************************************************************/

636
void pioBufferFuncCall(struct winHeaderEntry header,
637
                       const void *data, valPackFunc dataPackFunc)
638
639
640
641
{
  int rankGlob = commInqRankGlob ();
  int root = commInqRootGlob ();
  int collID, nProcsColl = commInqNProcsColl ();
642
  int funcID = header.id;
643

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

Deike Kleberg's avatar
Deike Kleberg committed
647
648
  if ( rankGlob != root ) return;

649
  xassert(txWin != NULL);
650

Thomas Jahns's avatar
Thomas Jahns committed
651
652
  for (collID = 0; collID < nProcsColl; ++collID)
    {
653
      collWait(collID);
654
655
      xassert(txWin[collID].dictRPCUsed + txWin[collID].dictDataUsed
              < txWin[collID].dictSize);
Thomas Jahns's avatar
Thomas Jahns committed
656
      xassert(txWin[collID].refuseFuncCall == 0);
657
      modelWinEnqueue(collID, header, data, dataPackFunc);
658
    }
659

660
  xdebug("%s", "RETURN");
661
662
}

663
664
665
666
#endif

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

667
668
669
670
/* pioInit definition must currently compile even in non-MPI configurations */
#ifndef MPI_VERSION
#  define MPI_Comm int
#endif
671
672
673
/**
   @brief initializes the MPI_Communicators needed for the
  communication between the calculator PEs and the I/O PEs and within the
674
675
676
677
678
679
680
681
682
  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]:
683
684

  Collective call
685
686
687

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

Thomas Jahns's avatar
Thomas Jahns committed
694
695
#ifdef USE_MPI
static int pioNamespace_ = -1;
696
static int xtInitByCDI = 0;
Thomas Jahns's avatar
Thomas Jahns committed
697
698
#endif

699
700
MPI_Comm
pioInit(MPI_Comm commGlob, int nProcsIO, int IOMode,
Thomas Jahns's avatar
Thomas Jahns committed
701
        int *pioNamespace, float partInflate)
702
{
703
#ifdef USE_MPI
Thomas Jahns's avatar
Thomas Jahns committed
704
  int sizeGlob;
Deike Kleberg's avatar
Deike Kleberg committed
705

Deike Kleberg's avatar
Deike Kleberg committed
706
  if ( IOMode < PIO_MINIOMODE || IOMode > PIO_MAXIOMODE )
Deike Kleberg's avatar
Deike Kleberg committed
707
    xabort ( "IOMODE IS NOT VALID." );
708

709
#ifdef _SX
Deike Kleberg's avatar
Deike Kleberg committed
710
711
  if ( IOMode ==  PIO_ASYNCH )
    xabort ( "PIO_ASYNCH DOES NOT WORK ON SX." );
712
713
#endif

714
715
716
717
  if ((xtInitByCDI = (!xt_initialized() || xt_finalized())))
    {
      xt_initialize(commGlob);
    }
718
719
  commInit ();
  commDefCommGlob ( commGlob );
720
  sizeGlob = commInqSizeGlob ();
721

722
723
  if (((IOMode != PIO_NONE && (nProcsIO <= 0 || nProcsIO > sizeGlob - 1)))
      || (IOMode == PIO_NONE && nProcsIO != 1))
724
725
    xabort("DISTRIBUTION OF TASKS ON PROCS IS NOT VALID.\n"
           "nProcsIO=%d, sizeGlob=%d\n", nProcsIO, sizeGlob);
Deike Kleberg's avatar
Deike Kleberg committed
726
727

  commDefNProcsIO ( nProcsIO );
Deike Kleberg's avatar
Deike Kleberg committed
728
  commDefIOMode   ( IOMode, PIO_MAXIOMODE, PIO_MINIOMODEWITHSPECIALPROCS );
Deike Kleberg's avatar
Deike Kleberg committed
729
  commDefCommPio  ();
730

731
732
733
  xassert(partInflate >= 1.0);
  cdiPIOpartInflate_ = partInflate;

Deike Kleberg's avatar
Deike Kleberg committed
734
  // JUST FOR TEST CASES WITH ONLY ONE MPI TASK
735
  if ( commInqSizeGlob () == 1 )
736
    {
737
      pioNamespace_ = *pioNamespace = namespaceNew();
Deike Kleberg's avatar
Deike Kleberg committed
738
739
      return commInqCommGlob ();
    }
740

741
742
  if ( commInqIsProcIO ())
    {
743
      serializeSetMPI();
744
745
746
      namespaceSwitchSet(NSSWITCH_ABORT, NSSW_FUNC(cdiAbortC_MPI));
      namespaceSwitchSet(NSSWITCH_FILE_OPEN, NSSW_FUNC(pioFileOpen));
      namespaceSwitchSet(NSSWITCH_FILE_CLOSE, NSSW_FUNC(pioFileClose));
747
      IOServer ();
Thomas Jahns's avatar
Thomas Jahns committed
748
      namespaceDelete(0);
749
      commDestroy ();
750
      xt_finalize();
751
      MPI_Finalize ();
752
      exit ( EXIT_SUCCESS );
753
754
    }
  else
755
    cdiPioClientSetup(&pioNamespace_, pioNamespace);
756

757
  xdebug ( "nProcsGlob=%d, RETURN", sizeGlob );
758
  return commInqCommModel ();
759
#else
760
  abort();
761
#endif
762
}
763

764
765
#ifndef MPI_VERSION
#  undef MPI_Comm
Deike Kleberg's avatar
Deike Kleberg committed
766
767
#endif

768
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
769

770
void  pioEndDef ( void )
771
772
{
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
773
  char   * buffer;
774
  int bufferSize;
775
  int rankGlob = commInqRankGlob ();
776

777
  xdebug("%s", "START");
778

Deike Kleberg's avatar
Deike Kleberg committed
779
  varsMapNDeco ( commInqNNodes (), commInqNodeSizes ());
Deike Kleberg's avatar
Deike Kleberg committed
780

781
  if ( rankGlob < commInqNProcsColl ())
Deike Kleberg's avatar
Deike Kleberg committed
782
    {
783
784
      MPI_Comm comm = commInqCommsIO ( rankGlob );
      reshPackBufferCreate(&buffer, &bufferSize, &comm);
785
786

      xmpi ( MPI_Send ( buffer, bufferSize, MPI_PACKED, commInqNProcsModel (),
787
                        RESOURCES, commInqCommsIO ( rankGlob )));
788

789
      xdebug("%s", "SENT MESSAGE WITH TAG \"RESOURCES\"");
790

Deike Kleberg's avatar
Deike Kleberg committed
791
      reshPackBufferDestroy ( &buffer );
Deike Kleberg's avatar
Deike Kleberg committed
792
    }
Deike Kleberg's avatar
Deike Kleberg committed
793
794

  modelWinCreate ();
795
  namespaceDefResStatus ( STAGE_TIMELOOP );
796
  xdebug("%s", "RETURN");
797
798
799
800
801
802
803
804
#endif
}

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

void  pioEndTimestepping ( void )
{
#ifdef USE_MPI
805
  xdebug("%s", "START");
806
  namespaceDefResStatus ( STAGE_CLEANUP );
807
  xdebug("%s", "RETURN");
808
#endif
809
810
}

Deike Kleberg's avatar
Deike Kleberg committed
811

812
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
813

Deike Kleberg's avatar
Deike Kleberg committed
814

Deike Kleberg's avatar
Deike Kleberg committed
815
816
817
818
819
820
821
822
823
/**
  @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
824
void pioFinalize ( void )
Deike Kleberg's avatar
Deike Kleberg committed
825
{
826
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
827
  int collID, ibuffer = 1111;
828
  xdebug("%s", "START");
Thomas Jahns's avatar
Thomas Jahns committed
829
  namespaceDelete(pioNamespace_);
Deike Kleberg's avatar
Deike Kleberg committed
830
  for ( collID = 0; collID < commInqNProcsColl (); collID++ )
831
    {
832
      xmpi ( MPI_Send ( &ibuffer, 1, MPI_INT, commInqNProcsModel (),
Deike Kleberg's avatar
Deike Kleberg committed
833
                        FINALIZE, commInqCommsIO ( collID )));
834
      xdebug("%s", "SENT MESSAGE WITH TAG \"FINALIZE\"");
835
    }
Deike Kleberg's avatar
Deike Kleberg committed
836
  modelWinCleanup ();
837
  commDestroy ();
838
839
  if (xtInitByCDI)
    xt_finalize();
840
  xdebug("%s", "RETURN");
841
#endif
842
843
}

844
 /************************************************************************/
845