pio_interface.c 25.7 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"
16
#include "cdipio.h"
Deike Kleberg's avatar
Deike Kleberg committed
17
#include "pio_util.h"
Deike Kleberg's avatar
Deike Kleberg committed
18
#include "vlist_var.h"
19
20

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

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

35

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

Deike Kleberg's avatar
Deike Kleberg committed
46

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

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

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

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

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

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

68
69
70
71
72
73
74
75
76
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
77
/****************************************************/
78

79
80
81
static void
mapProblems(int problemSizes[], int * problemMapping, int nProblems,
            int nWriter, double * w)
Deike Kleberg's avatar
Deike Kleberg committed
82
83
84
85
86
87
88
{
  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
89
  int writerIdx = 0;
Deike Kleberg's avatar
Deike Kleberg committed
90
  int i, j;
91

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

  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;
103
104

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

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

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

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

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

127
128
129
130
131
132
133
134
135
  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
136
137
}

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

/**
   @brief is encapsulated in CDI library.

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

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

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

   @param nStreams number of stream_t 's
151
152

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

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

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

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

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

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

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

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

Deike Kleberg's avatar
Deike Kleberg committed
186
  w = ( double * ) xmalloc ( nNodes * sizeof ( double ));
Deike Kleberg's avatar
Deike Kleberg committed
187
188
189
190
191
192
193
194
195
196
197
198
199
  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++ )
200
	if ( * ( streamMapping + j ) == i )
201
202
	  nVarsNode += * ( sSizes + j );

Deike Kleberg's avatar
Deike Kleberg committed
203
204
205
      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
206
207
208
209
      offset = 0;
      offsetN = 0;

      for ( j = 0; j < nStreams; j++ )
210
211
212
213
214
	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
215
216

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

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

      offset = 0;
      offsetN = 0;

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

      summandRank += * ( nodeSizes + i );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

321
  xdebug("%s", "START");
322
  if (txWin != NULL)
323
324
325
326
327
328
329
330
331
332
333
    {
      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
334

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

/************************************************************************/
339

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

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

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

Deike Kleberg's avatar
Deike Kleberg committed
358
  nstreams = reshCountType ( &streamOps );
359
  streamIndexList = xmalloc ( nstreams * sizeof ( streamIndexList[0] ));
360
  collIndex = xcalloc(nProcsColl, sizeof (collIndex[0]));
361
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
Deike Kleberg's avatar
Deike Kleberg committed
362
363
  for ( streamNo = 0; streamNo < nstreams; streamNo++ )
    {
364
365
366
      // memory required for data
      int streamID = streamIndexList[streamNo];
      int vlistID = streamInqVlist(streamID);
Deike Kleberg's avatar
Deike Kleberg committed
367
368
369
      nvars = vlistNvars ( vlistID );
      for ( varID = 0; varID < nvars; varID++ )
        {
370
371
372
373
374
375
376
377
          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
378
          xassert ( collID != CDI_UNDEFID && collIDchunk > 0 );
379
380
381
382
383
384
          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*/
385
            + 2 * sizeof (struct winHeaderEntry)
386
387
            /* FIXME: heuristic for size of packed Xt_idxlist */
            + sizeof (Xt_int) * collIDchunk * 3;
Deike Kleberg's avatar
Deike Kleberg committed
388
        }
389

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

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

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

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


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

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


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


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

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

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

465
466
467
  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
468
  for ( collID = 0; collID < nProcsColl; collID ++ )
Deike Kleberg's avatar
Deike Kleberg committed
469
    {
470
      xassert(txWin[collID].size > 0);
471
      txWin[collID].buffer = NULL;
472
      xmpi(MPI_Alloc_mem((MPI_Aint)txWin[collID].size, MPI_INFO_NULL,
473
474
                         &txWin[collID].buffer));
      xassert ( txWin[collID].buffer != NULL );
475
      txWin[collID].head = txWin[collID].buffer
476
        + txWin[collID].dictSize * sizeof (struct winHeaderEntry);
477
      xmpi(MPI_Win_create(txWin[collID].buffer, (MPI_Aint)txWin[collID].size, 1,
478
                          no_locks_info, commInqCommsIO(collID),
479
                          &txWin[collID].win));
Thomas Jahns's avatar
Thomas Jahns committed
480
481
482
483
      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
484
    }
485
486
487

  xmpi(MPI_Info_free(&no_locks_info));

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

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

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

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

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

578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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);
}


602
603
604
605
static void
pioBufferPartData_(int streamID, int varID,
                   const void *packData, valPackFunc packDataFunc,
                   int nmiss, Xt_idxlist partDesc)
606
607
608
{
  int vlistID, collID = CDI_UNDEFID;

Deike Kleberg's avatar
Deike Kleberg committed
609
610
  vlistID  = streamInqVlist ( streamID );
  collID   = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
611
612
  xassert ( collID         >= 0                    &&
            collID         <  commInqNProcsColl () &&
613
            txWin != NULL);
Deike Kleberg's avatar
Deike Kleberg committed
614

615
  collWait(collID);
616

617

618
  struct winHeaderEntry dataHeader
619
    = { .id = streamID, .specific.dataRecord = { varID, nmiss }, .offset = -1 };
620
  modelWinEnqueue(collID, dataHeader, packData, packDataFunc);
621
  {
622
    struct winHeaderEntry partHeader
623
624
      = { .id = PARTDESCMARKER,
          .specific.partDesc = { .uid = xt_idxlist_get_uid(partDesc) },
625
          .offset = 0 };
626
    modelWinEnqueue(collID, partHeader, partDesc, cdiPio_xt_idxlist_pack_wrap);
627
  }
628

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

632
633
634
635
636
637
638
639
640
641
642
643
void
pioBufferPartData(int streamID, int varID, const double *data,
                  int nmiss, Xt_idxlist partDesc)
{
  int chunk = xt_idxlist_get_num_indices(partDesc);
  xassert(chunk <= INT_MAX);
  pioBufferPartData_(streamID, varID,
                     &(struct memCpyDataDesc){data, chunk * sizeof (data[0])},
                     memcpyPackFunc,
                     nmiss, partDesc);
}

644
645
/************************************************************************/

646
void pioBufferFuncCall(struct winHeaderEntry header,
647
                       const void *data, valPackFunc dataPackFunc)
648
649
650
651
{
  int rankGlob = commInqRankGlob ();
  int root = commInqRootGlob ();
  int collID, nProcsColl = commInqNProcsColl ();
652
  int funcID = header.id;
653

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

Deike Kleberg's avatar
Deike Kleberg committed
657
658
  if ( rankGlob != root ) return;

659
  xassert(txWin != NULL);
660

Thomas Jahns's avatar
Thomas Jahns committed
661
662
  for (collID = 0; collID < nProcsColl; ++collID)
    {
663
      collWait(collID);
664
665
      xassert(txWin[collID].dictRPCUsed + txWin[collID].dictDataUsed
              < txWin[collID].dictSize);
Thomas Jahns's avatar
Thomas Jahns committed
666
      xassert(txWin[collID].refuseFuncCall == 0);
667
      modelWinEnqueue(collID, header, data, dataPackFunc);
668
    }
669

670
  xdebug("%s", "RETURN");
671
672
}

673
674
675
676
#endif

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

677
678
679
680
/* pioInit definition must currently compile even in non-MPI configurations */
#ifndef MPI_VERSION
#  define MPI_Comm int
#endif
681
682
683
/**
   @brief initializes the MPI_Communicators needed for the
  communication between the calculator PEs and the I/O PEs and within the
684
685
686
687
688
689
690
691
692
  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]:
693
694

  Collective call
695
696
697

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

Thomas Jahns's avatar
Thomas Jahns committed
704
705
#ifdef USE_MPI
static int pioNamespace_ = -1;
706
static int xtInitByCDI = 0;
Thomas Jahns's avatar
Thomas Jahns committed
707
708
#endif

709
710
MPI_Comm
pioInit(MPI_Comm commGlob, int nProcsIO, int IOMode,
Thomas Jahns's avatar
Thomas Jahns committed
711
        int *pioNamespace, float partInflate)
712
{
713
#ifdef USE_MPI
Thomas Jahns's avatar
Thomas Jahns committed
714
  int sizeGlob;
Deike Kleberg's avatar
Deike Kleberg committed
715

Deike Kleberg's avatar
Deike Kleberg committed
716
  if ( IOMode < PIO_MINIOMODE || IOMode > PIO_MAXIOMODE )
Deike Kleberg's avatar
Deike Kleberg committed
717
    xabort ( "IOMODE IS NOT VALID." );
718

719
#ifdef _SX
Deike Kleberg's avatar
Deike Kleberg committed
720
721
  if ( IOMode ==  PIO_ASYNCH )
    xabort ( "PIO_ASYNCH DOES NOT WORK ON SX." );
722
723
#endif

724
725
726
727
  if ((xtInitByCDI = (!xt_initialized() || xt_finalized())))
    {
      xt_initialize(commGlob);
    }
728
729
  commInit ();
  commDefCommGlob ( commGlob );
730
  sizeGlob = commInqSizeGlob ();
731

732
733
  if (((IOMode != PIO_NONE && (nProcsIO <= 0 || nProcsIO > sizeGlob - 1)))
      || (IOMode == PIO_NONE && nProcsIO != 1))
734
735
    xabort("DISTRIBUTION OF TASKS ON PROCS IS NOT VALID.\n"
           "nProcsIO=%d, sizeGlob=%d\n", nProcsIO, sizeGlob);
Deike Kleberg's avatar
Deike Kleberg committed
736
737

  commDefNProcsIO ( nProcsIO );
Deike Kleberg's avatar
Deike Kleberg committed
738
  commDefIOMode   ( IOMode, PIO_MAXIOMODE, PIO_MINIOMODEWITHSPECIALPROCS );
Deike Kleberg's avatar
Deike Kleberg committed
739
  commDefCommPio  ();
740

741
742
743
  xassert(partInflate >= 1.0);
  cdiPIOpartInflate_ = partInflate;

Deike Kleberg's avatar
Deike Kleberg committed
744
  // JUST FOR TEST CASES WITH ONLY ONE MPI TASK
745
  if ( commInqSizeGlob () == 1 )
746
    {
747
      pioNamespace_ = *pioNamespace = namespaceNew();
Deike Kleberg's avatar
Deike Kleberg committed
748
749
      return commInqCommGlob ();
    }
750

751
752
  if ( commInqIsProcIO ())
    {
753
      serializeSetMPI();
754
755
756
      namespaceSwitchSet(NSSWITCH_ABORT, NSSW_FUNC(cdiAbortC_MPI));
      namespaceSwitchSet(NSSWITCH_FILE_OPEN, NSSW_FUNC(pioFileOpen));
      namespaceSwitchSet(NSSWITCH_FILE_CLOSE, NSSW_FUNC(pioFileClose));
757
      IOServer ();
Thomas Jahns's avatar
Thomas Jahns committed
758
      namespaceDelete(0);
759
      commDestroy ();
760
      xt_finalize();
761
      MPI_Finalize ();
762
      exit ( EXIT_SUCCESS );
763
764
    }
  else
765
    cdiPioClientSetup(&pioNamespace_, pioNamespace);
766

767
  xdebug ( "nProcsGlob=%d, RETURN", sizeGlob );
768
  return commInqCommModel ();
769
#else
770
  abort();
771
#endif
772
}
773

774
775
#ifndef MPI_VERSION
#  undef MPI_Comm
Deike Kleberg's avatar
Deike Kleberg committed
776
777
#endif

778
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
779

780
void  pioEndDef ( void )
781
782
{
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
783
  char   * buffer;
784
  int bufferSize;
785
  int rankGlob = commInqRankGlob ();
786

787
  xdebug("%s", "START");
788

Deike Kleberg's avatar
Deike Kleberg committed
789
  varsMapNDeco ( commInqNNodes (), commInqNodeSizes ());
Deike Kleberg's avatar
Deike Kleberg committed
790

791
  if ( rankGlob < commInqNProcsColl ())
Deike Kleberg's avatar
Deike Kleberg committed
792
    {
793
794
      MPI_Comm comm = commInqCommsIO ( rankGlob );
      reshPackBufferCreate(&buffer, &bufferSize, &comm);
795
796

      xmpi ( MPI_Send ( buffer, bufferSize, MPI_PACKED, commInqNProcsModel (),
797
                        RESOURCES, commInqCommsIO ( rankGlob )));
798

799
      xdebug("%s", "SENT MESSAGE WITH TAG \"RESOURCES\"");
800

Deike Kleberg's avatar
Deike Kleberg committed
801
      reshPackBufferDestroy ( &buffer );
Deike Kleberg's avatar
Deike Kleberg committed
802
    }
Deike Kleberg's avatar
Deike Kleberg committed
803
804

  modelWinCreate ();
805
  namespaceDefResStatus ( STAGE_TIMELOOP );
806
  xdebug("%s", "RETURN");
807
808
809
810
811
812
813
814
#endif
}

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

void  pioEndTimestepping ( void )
{
#ifdef USE_MPI
815
  xdebug("%s", "START");
816
  namespaceDefResStatus ( STAGE_CLEANUP );
817
  xdebug("%s", "RETURN");
818
#endif
819
820
}

Deike Kleberg's avatar
Deike Kleberg committed
821

822
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
823

Deike Kleberg's avatar
Deike Kleberg committed
824

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