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

Deike Kleberg's avatar
Deike Kleberg committed
5
#include <stdlib.h>
6
#include <stdio.h>
7
#include <stdarg.h>
8
#include "cdi.h"
9
#include "limits.h"
Deike Kleberg's avatar
Deike Kleberg committed
10
#include "pio_util.h"
Deike Kleberg's avatar
Deike Kleberg committed
11
#include "vlist_var.h"
12
13

#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
14
#include "namespace.h"
15
#include "pio_interface.h"
Deike Kleberg's avatar
Deike Kleberg committed
16
#include "pio_comm.h"
Deike Kleberg's avatar
Deike Kleberg committed
17
#include "pio_rpc.h"
Deike Kleberg's avatar
Deike Kleberg committed
18
#include "pio_server.h"
19
#include "resource_handle.h"
Deike Kleberg's avatar
Deike Kleberg committed
20
#include "stream_int.h"
Deike Kleberg's avatar
Deike Kleberg committed
21
#include "vlist.h"
Deike Kleberg's avatar
Deike Kleberg committed
22
extern resOps streamOps;
Deike Kleberg's avatar
Deike Kleberg committed
23

24

25
static struct rdmaWin
26
27
{
  size_t size;
28
  unsigned char *buffer, *head;
29
  MPI_Win win;
30
  int postSet, refuseFuncCall;
31
  MPI_Group ioGroup;
32
  int dictSize, dictDataUsed, dictRPCUsed;
33
34
} *txWin = NULL;

Deike Kleberg's avatar
Deike Kleberg committed
35

Deike Kleberg's avatar
Deike Kleberg committed
36
char * funcMap[nFuncs] = {"streamOpen", "streamDefVlist", "streamClose" };
Deike Kleberg's avatar
Deike Kleberg committed
37
38


Deike Kleberg's avatar
Deike Kleberg committed
39
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
40

41
static int cmp ( const void * va, const void * vb )
Deike Kleberg's avatar
Deike Kleberg committed
42
{
43
44
    const int ** a, ** b;

Deike Kleberg's avatar
Deike Kleberg committed
45
46
  a = ( const int ** ) va;
  b = ( const int ** ) vb;
47

Deike Kleberg's avatar
Deike Kleberg committed
48
49
50
51
  return (( **a < **b ) - ( **a > **b ));
}

/****************************************************/
52

53
54
55
static void
mapProblems(int problemSizes[], int * problemMapping, int nProblems,
            int nWriter, double * w)
Deike Kleberg's avatar
Deike Kleberg committed
56
57
58
59
60
61
62
{
  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
63
  int writerIdx = 0;
Deike Kleberg's avatar
Deike Kleberg committed
64
  int i, j;
65

Deike Kleberg's avatar
Deike Kleberg committed
66
67
68
69
70
71
72
73
74
75
76

  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;
77
78

  for ( j = 0; j < nWriter; j++ )
Deike Kleberg's avatar
Deike Kleberg committed
79
80
81
82
83
    meanBucket[j] = ( double ) sum * ( * ( w + j ));

  for ( i = 0; i < nProblems; i++ )
    {
      currCapacity = INT_MIN;
84

Deike Kleberg's avatar
Deike Kleberg committed
85
      for ( j = 0; j < nWriter; j++ )
86
87
88
	{
	  if ( !i ) buckets[j] = 0.0;
	  nextCapacity = meanBucket[j] - ( buckets[j] + ( *ip[i] ));
89

90
91
92
93
94
95
	  if ( nextCapacity > currCapacity )
	    {
	      currCapacity = nextCapacity;
	      writerIdx = j;
	    }
	}
Thomas Jahns's avatar
Thomas Jahns committed
96
      problemMapping[ dummy[i] ] = writerIdx;
Deike Kleberg's avatar
Deike Kleberg committed
97
98
      buckets[writerIdx] +=  *ip[i];
    }
Deike Kleberg's avatar
Deike Kleberg committed
99

100
101
102
  xprintArray3 (  "problemSizes = ", problemSizes, nProblems, DATATYPE_INT );
  xprintArray3 ( "vector of indices, qsort of problemSizes", dummy,
                nProblems, DATATYPE_INT );
Deike Kleberg's avatar
Deike Kleberg committed
103
104
  xprintArray3 ( "problemMapping", problemMapping, nProblems, DATATYPE_INT );
  xprintArray3 ( "meanBucket", meanBucket, nWriter, DATATYPE_FLT );
105
  xprintArray3 ( "actual buckets", buckets, nWriter, DATATYPE_INT );
Deike Kleberg's avatar
Deike Kleberg committed
106
107
}

108
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
109
110
111
112
113

/**
   @brief is encapsulated in CDI library.

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

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

117
   @param varMapping return value, array with ranks of I/O PEs assigned to var_t 's
Deike Kleberg's avatar
Deike Kleberg committed
118
119
120
in order of vSizes

   @param nStreams number of stream_t 's
121
122

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

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

Deike Kleberg's avatar
Deike Kleberg committed
127
128
   @return
*/
129

130
131
132
static void
varMapGen(int *vSizes, int *sSizes, int *varMapping,
          int nStreams, int *nodeSizes, int nNodes)
Deike Kleberg's avatar
Deike Kleberg committed
133
{
134

Deike Kleberg's avatar
Deike Kleberg committed
135
136
137
138
139
  int weightsStreams[nStreams];
  int streamMapping[nStreams];
  int nPEs = 0, nVars = 0;
  int i, j, k, offset = 0, offsetN = 0;
  double * w;
140

Deike Kleberg's avatar
Deike Kleberg committed
141
142
143
  int * weightsVarsNode;
  int * varMappingNode;
  int nVarsNode, summandRank = 0;
144
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
145

146
  int buckets[nProcsColl];
Deike Kleberg's avatar
Deike Kleberg committed
147
148
149
150
151
152

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

Deike Kleberg's avatar
Deike Kleberg committed
156
  w = ( double * ) xmalloc ( nNodes * sizeof ( double ));
Deike Kleberg's avatar
Deike Kleberg committed
157
158
159
160
161
162
163
164
165
166
167
168
169
  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++ )
170
	if ( * ( streamMapping + j ) == i )
171
172
	  nVarsNode += * ( sSizes + j );

Deike Kleberg's avatar
Deike Kleberg committed
173
174
175
      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
176
177
178
179
      offset = 0;
      offsetN = 0;

      for ( j = 0; j < nStreams; j++ )
180
181
182
183
184
	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
185
186

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

189
      mapProblems ( weightsVarsNode, varMappingNode, nVarsNode,
190
		    * ( nodeSizes + i ),  w );
Deike Kleberg's avatar
Deike Kleberg committed
191
192
193
194
195

      offset = 0;
      offsetN = 0;

      for ( j = 0; j < nStreams; j++ )
196
197
	if ( * ( streamMapping + j ) == i )
	  for ( k = 0; k < * ( sSizes + j ); k ++ )
198
	    * ( varMapping + offset ++ ) =
199
              commCollID2RankGlob ( * ( varMappingNode + offsetN ++ ) +
200
                                    summandRank );
201
202
	else
	  offset += * ( sSizes + j );
Deike Kleberg's avatar
Deike Kleberg committed
203
204
205
206
207
208
209

      summandRank += * ( nodeSizes + i );

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

211
  if ( ddebug )
Deike Kleberg's avatar
Deike Kleberg committed
212
    {
Deike Kleberg's avatar
Deike Kleberg committed
213
      xprintArray ( "varMapping", varMapping, nVars, DATATYPE_INT  );
214
      for ( i = 0; i < nProcsColl; i++ )
215
	buckets[i] = 0;
Deike Kleberg's avatar
Deike Kleberg committed
216
      for ( i = 0; i < nVars; i ++ )
217
	buckets[commRankGlob2CollID ( *(varMapping + i ))] += * ( vSizes + i );
218
      xprintArray ( "buckets", buckets, nProcsColl, DATATYPE_INT );
Deike Kleberg's avatar
Deike Kleberg committed
219
    }
220
}
Deike Kleberg's avatar
Deike Kleberg committed
221

222
/************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
223

224
225
static void
defVarDeco(int vlistID, int varID)
Deike Kleberg's avatar
Deike Kleberg committed
226
227
{
  int varSize, cRank, lChunk, rem, lOffset;
228
229
  int nProcsModel = commInqNProcsModel ();
  deco_t deco[nProcsModel];
230

Deike Kleberg's avatar
Deike Kleberg committed
231
232
  varSize = vlistInqVarSize ( vlistID, varID );

233
  for ( cRank = 0; cRank < nProcsModel; cRank++ )
Deike Kleberg's avatar
Deike Kleberg committed
234
    {
235
      lChunk = varSize / nProcsModel;
Deike Kleberg's avatar
Deike Kleberg committed
236
      lOffset = cRank * lChunk;
237
      rem = varSize % nProcsModel;
238
      if ( cRank < rem )
Deike Kleberg's avatar
Deike Kleberg committed
239
240
241
242
243
244
        {
          lChunk++;
          lOffset += cRank;
        }
      else
        lOffset += rem;
245

Deike Kleberg's avatar
Deike Kleberg committed
246
247
248
249
      deco[cRank].rank   = cRank;
      deco[cRank].offset = lOffset;
      deco[cRank].chunk  = lChunk;
    }
250
  vlistDefVarDeco ( vlistID, varID, nProcsModel, &deco[0] );
Deike Kleberg's avatar
Deike Kleberg committed
251
252
}

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

Deike Kleberg's avatar
Deike Kleberg committed
255

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

265
266
  xdebug ( "START, nProcsColl=%d", nProcsColl );

Deike Kleberg's avatar
Deike Kleberg committed
267
  nStreams = streamSize ();
268

269
270
  resHs       = xmalloc ( nStreams * sizeof ( resHs[0] ));
  streamSizes = xmalloc ( nStreams * sizeof ( streamSizes[0] ));
271
  collectsData = xmalloc ( nProcsColl * sizeof ( collectsData[0] ));
272
  streamGetIndexList ( nStreams, resHs );
273

Deike Kleberg's avatar
Deike Kleberg committed
274
  for ( i = 0; i < nStreams; i++ )
275
    streamSizes[i] = streamInqNvars ( * ( resHs + i ));
276

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

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

Deike Kleberg's avatar
Deike Kleberg committed
285
  xassert ( k == nVars );
286

287
  varMapGen ( varSizes, streamSizes, varMapping,
Deike Kleberg's avatar
Deike Kleberg committed
288
	      nStreams, nodeSizes, nNodes );
289
290
291

  k = 0;
  for ( i = 0; i < nStreams; i++ )
292
    for ( j = 0; j < * ( streamSizes + i ); j++ )
293
      {
294
295
296
        defVarDeco ( streamInqVlist ( *( resHs + i )), j );
        defVarDeco ( streamInqVlistIDorig ( * ( resHs + i )), j );
        vlistDefVarIOrank ( streamInqVlist ( * ( resHs + i )), j,
297
                            * ( varMapping + k ));
298
        vlistDefVarIOrank ( streamInqVlistIDorig ( * ( resHs + i )), j,
299
300
                            * ( varMapping + k ));
        collectsData[commRankGlob2CollID ( varMapping[k++] )] = 1;
301
      }
302
303
304
305

  for ( j = 0; j < nProcsColl; j++ )
    if ( collectsData[j] == 0 )
      {
306
        sprintf ( text,
307
308
309
310
311
312
                  "\nAT LEAST ONE COLLECTOR PROCESS IDLES, "
                  "CURRENTLY NOT COVERED: "
                  "PE%d collects no data",
                  commCollID2RankGlob ( j ));
        xabort ( text );
      }
313

314
315
316
317
318
  if ( varMapping )   free ( varMapping );
  if ( varSizes )     free ( varSizes );
  if ( collectsData ) free ( collectsData );
  if ( streamSizes )  free ( streamSizes );
  if ( resHs )        free ( resHs );
319

320
  xdebug("%s", "RETURN");
Deike Kleberg's avatar
Deike Kleberg committed
321
322
323
324
}

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

325
static
Deike Kleberg's avatar
Deike Kleberg committed
326
327
void modelWinCleanup ( void )
{
Deike Kleberg's avatar
Deike Kleberg committed
328
  int collID;
Deike Kleberg's avatar
Deike Kleberg committed
329

330
  xdebug("%s", "START");
331
  if (txWin != NULL)
332
333
334
335
336
337
338
339
340
341
342
    {
      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
343

344
  xdebug("%s", "RETURN. CLEANED UP MPI_WIN'S");
Deike Kleberg's avatar
Deike Kleberg committed
345
346
347
}

/************************************************************************/
348

349
350
struct collDesc
{
351
  int numDataRecords, numRPCRecords;
352
353
};

354
355
static void
modelWinDefBufferSizes(void)
Deike Kleberg's avatar
Deike Kleberg committed
356
{
357
  int collID, nstreams, * streamIndexList, streamNo, nvars, varID;
Deike Kleberg's avatar
Deike Kleberg committed
358
  int collIDchunk = 0, sumWinBufferSize = 0;
359
360
  int nProcsColl  = commInqNProcsColl ();
  int rankGlob    = commInqRankGlob ();
361
  int rankModel   = commInqRankModel ();
362
  int root = commInqRootGlob ();
363
  struct collDesc *collIndex;
Deike Kleberg's avatar
Deike Kleberg committed
364

365
  xdebug("%s", "START");
366
  xassert(txWin != NULL);
367

Deike Kleberg's avatar
Deike Kleberg committed
368
  nstreams = reshCountType ( &streamOps );
369
  streamIndexList = xmalloc ( nstreams * sizeof ( streamIndexList[0] ));
370
  collIndex = xcalloc(nProcsColl, sizeof (collIndex[0]));
371
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
Deike Kleberg's avatar
Deike Kleberg committed
372
373
  for ( streamNo = 0; streamNo < nstreams; streamNo++ )
    {
374
375
376
      // memory required for data
      int streamID = streamIndexList[streamNo];
      int vlistID = streamInqVlist(streamID);
Deike Kleberg's avatar
Deike Kleberg committed
377
378
379
      nvars = vlistNvars ( vlistID );
      for ( varID = 0; varID < nvars; varID++ )
        {
Deike Kleberg's avatar
Deike Kleberg committed
380
381
          collID = CDI_UNDEFID;
          collID = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
382
          collIDchunk = vlistInqVarDecoChunk ( vlistID, varID, rankModel );
Deike Kleberg's avatar
Deike Kleberg committed
383
          xassert ( collID != CDI_UNDEFID && collIDchunk > 0 );
384
          ++(collIndex[collID].numDataRecords);
385
          txWin[collID].size += collIDchunk * sizeof (double) +
386
            sizeof (union winHeaderEntry);
Deike Kleberg's avatar
Deike Kleberg committed
387
        }
388

389
      // memory required for the 3 function calls streamOpen, streamDefVlist, streamClose
Deike Kleberg's avatar
Deike Kleberg committed
390
      // once per stream and timestep for all collprocs only on the modelproc root
391
392
      if ( rankGlob == root )
        for ( collID = 0; collID < nProcsColl; collID++ )
393
          {
394
            collIndex[collID].numRPCRecords += 3;
395
396
397
398
            txWin[collID].size +=
              3 * sizeof (union winHeaderEntry)
              + MAXDATAFILENAME;
          }
Deike Kleberg's avatar
Deike Kleberg committed
399
    }
400
  for (collID = 0; collID < nProcsColl; ++collID)
401
    {
402
403
      int numRecords = 1 + collIndex[collID].numDataRecords
        + collIndex[collID].numRPCRecords;
404
      txWin[collID].dictSize = numRecords;
405
406
      txWin[collID].dictDataUsed = 1;
      txWin[collID].dictRPCUsed = 0;
407
      txWin[collID].size += sizeof (union winHeaderEntry);
408
      sumWinBufferSize += txWin[collID].size;
409
    }
410
411
412
  free(collIndex);
  free ( streamIndexList );

413
414
  xdebug("sumWinBufferSize=%zu, MAXWINBUFFERSIZE=%zu", (size_t)sumWinBufferSize,
         (size_t)MAXWINBUFFERSIZE);
Deike Kleberg's avatar
Deike Kleberg committed
415
  xassert ( sumWinBufferSize <= MAXWINBUFFERSIZE );
416
  xdebug("%s", "RETURN");
Deike Kleberg's avatar
Deike Kleberg committed
417
}
418

Deike Kleberg's avatar
Deike Kleberg committed
419
420
421
422

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


423
static
Deike Kleberg's avatar
Deike Kleberg committed
424
  void modelWinFlushBuffer ( int collID )
Deike Kleberg's avatar
Deike Kleberg committed
425
{
Deike Kleberg's avatar
Deike Kleberg committed
426
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
427

Deike Kleberg's avatar
Deike Kleberg committed
428
429
  xassert ( collID                >= 0         &&
            collID                < nProcsColl &&
430
            txWin[collID].buffer     != NULL      &&
431
432
            txWin != NULL      &&
            txWin[collID].size >= 0         &&
433
            txWin[collID].size <= MAXWINBUFFERSIZE);
434
  memset(txWin[collID].buffer, 0, txWin[collID].size);
435
436
  txWin[collID].head = txWin[collID].buffer
    + txWin[collID].dictSize * sizeof (union winHeaderEntry);
437
  txWin[collID].refuseFuncCall = 0;
438
439
  txWin[collID].dictDataUsed = 1;
  txWin[collID].dictRPCUsed = 0;
Deike Kleberg's avatar
Deike Kleberg committed
440
441
442
}


Deike Kleberg's avatar
Deike Kleberg committed
443
444
445
/************************************************************************/


Deike Kleberg's avatar
Deike Kleberg committed
446
447
448
static
void modelWinCreate ( void )
{
Deike Kleberg's avatar
Deike Kleberg committed
449
  int collID, ranks[1];
450
  int nProcsColl = commInqNProcsColl ();
Deike Kleberg's avatar
Deike Kleberg committed
451

452
  xdebug("%s", "START");
453
  txWin = xmalloc(nProcsColl * sizeof (txWin[0]));
Deike Kleberg's avatar
Deike Kleberg committed
454

Deike Kleberg's avatar
Deike Kleberg committed
455
  modelWinDefBufferSizes ();
456
  ranks[0] = commInqNProcsModel ();
Deike Kleberg's avatar
Deike Kleberg committed
457

Deike Kleberg's avatar
Deike Kleberg committed
458
  for ( collID = 0; collID < nProcsColl; collID ++ )
Deike Kleberg's avatar
Deike Kleberg committed
459
    {
460
      xassert(txWin[collID].size > 0);
461
      txWin[collID].buffer = NULL;
462
      xmpi(MPI_Alloc_mem((MPI_Aint)txWin[collID].size, MPI_INFO_NULL,
463
464
                         &txWin[collID].buffer));
      xassert ( txWin[collID].buffer != NULL );
465
466
      txWin[collID].head = txWin[collID].buffer
        + txWin[collID].dictSize * sizeof (union winHeaderEntry);
467
      xmpi(MPI_Win_create(txWin[collID].buffer, (MPI_Aint)txWin[collID].size, 1,
468
                          MPI_INFO_NULL, commInqCommsIO(collID),
469
                          &txWin[collID].win));
470
471
472
      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
473
    }
474
  xdebug("%s", "RETURN, CREATED MPI_WIN'S");
Deike Kleberg's avatar
Deike Kleberg committed
475
476
477
478
}

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

479
static void
480
481
modelWinEnqueue(int collID,
                union winHeaderEntry header, const void *data, size_t size)
Deike Kleberg's avatar
Deike Kleberg committed
482
{
483
484
  union winHeaderEntry *winDict
    = (union winHeaderEntry *)txWin[collID].buffer;
485
  int targetEntry;
486
487
  if (header.dataRecord.streamID > 0)
    {
488
      targetEntry = (txWin[collID].dictDataUsed)++;
489
490
491
492
493
      header.dataRecord.offset
        = (int)(txWin[collID].head - txWin[collID].buffer);
      memcpy(txWin[collID].head, data, size);
      txWin[collID].head += size;
    }
494
  else
495
    {
496
497
498
499
500
501
502
503
      targetEntry = txWin[collID].dictSize - ++(txWin[collID].dictRPCUsed);
      if (header.funcCall.funcID == STREAMOPEN)
        {
          header.funcCall.funcArgs.newFile.offset
            = (int)(txWin[collID].head - txWin[collID].buffer);
          memcpy(txWin[collID].head, data, size);
          txWin[collID].head += size;
        }
504
    }
505
  winDict[targetEntry] = header;
Deike Kleberg's avatar
Deike Kleberg committed
506
507
}

508
void pioBufferData ( const int streamID, const int varID, const double *data, int nmiss )
Deike Kleberg's avatar
Deike Kleberg committed
509
{
510
  int chunk, vlistID, collID = CDI_UNDEFID;
511
  int rankModel = commInqRankModel ();
Deike Kleberg's avatar
Deike Kleberg committed
512
513
514

  vlistID  = streamInqVlist ( streamID );
  collID   = commRankGlob2CollID ( vlistInqVarIOrank ( vlistID, varID ));
515
  chunk    = vlistInqVarDecoChunk ( vlistID, varID, rankModel );
516
517
518
  xassert ( collID         >= 0                    &&
            collID         <  commInqNProcsColl () &&
            chunk          >= 0                    &&
519
            txWin != NULL);
Deike Kleberg's avatar
Deike Kleberg committed
520

521
  if (txWin[collID].postSet)
522
    {
523
      xmpi(MPI_Win_wait(txWin[collID].win));
524
      txWin[collID].postSet = 0;
Deike Kleberg's avatar
Deike Kleberg committed
525
      modelWinFlushBuffer ( collID );
526
    }
527

528
529
530
  union winHeaderEntry header
    = { .dataRecord = { streamID, varID, -1, nmiss } };
  modelWinEnqueue(collID, header, data, chunk * sizeof (data[0]));
531

532
  txWin[collID].refuseFuncCall = 1;
Deike Kleberg's avatar
Deike Kleberg committed
533
534
}

535
536
/************************************************************************/

537
void pioBufferFuncCall(int funcID, int argc, ... )
538
539
540
541
542
{
  va_list ap;
  int rankGlob = commInqRankGlob ();
  int root = commInqRootGlob ();
  int collID, nProcsColl = commInqNProcsColl ();
543

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

Deike Kleberg's avatar
Deike Kleberg committed
547
548
  if ( rankGlob != root ) return;

549
550
551
  xassert (argc          >= 1                    &&
           argc          <= 2                    &&
           txWin != NULL);
552

Deike Kleberg's avatar
Deike Kleberg committed
553
  va_start ( ap, argc );
554

Thomas Jahns's avatar
Thomas Jahns committed
555
556
557
558
559
  union winHeaderEntry header;
  char * filename = NULL;
  size_t filenamesz = 0;
  int streamID = 0, vlistID = 0, filetype = 0;

560
  switch ( funcID )
Deike Kleberg's avatar
Deike Kleberg committed
561
    {
562
    case STREAMCLOSE:
Thomas Jahns's avatar
Thomas Jahns committed
563
564
      xassert ( argc == 1 );
    case STREAMDEFVLIST:
565
      {
Thomas Jahns's avatar
Thomas Jahns committed
566
567
568
        streamID = va_arg(ap, int);
        vlistID = CDI_UNDEFID;
        if (funcID == STREAMDEFVLIST)
569
          {
Thomas Jahns's avatar
Thomas Jahns committed
570
571
            xassert ( argc == 2 );
            vlistID = va_arg(ap, int);
572
          }
Thomas Jahns's avatar
Thomas Jahns committed
573
574
575
576
        struct funcCallDesc f = { .funcID = funcID,
                                  .funcArgs.streamChange = { streamID,
                                                             vlistID } };
        header.funcCall = f;
577
      }
578
      break;
Deike Kleberg's avatar
Deike Kleberg committed
579
580
    case STREAMOPEN:
      {
Deike Kleberg's avatar
Deike Kleberg committed
581
        xassert ( argc == 2 );
Thomas Jahns's avatar
Thomas Jahns committed
582
583
        filename = va_arg(ap, char *);
        filenamesz = strlen(filename);
Deike Kleberg's avatar
Deike Kleberg committed
584
585
        xassert ( filenamesz > 0 &&
                  filenamesz < MAXDATAFILENAME );
Thomas Jahns's avatar
Thomas Jahns committed
586
587
588
589
590
591
592
        filetype = va_arg(ap, int);
        struct funcCallDesc f = { .funcID = STREAMOPEN,
                                  .funcArgs.newFile
                                  = { .fnamelen = filenamesz,
                                      .filetype = filetype } };
        header.funcCall = f;
        ++filenamesz;
Deike Kleberg's avatar
Deike Kleberg committed
593
      }
594
      break;
Thomas Jahns's avatar
Thomas Jahns committed
595
596
597
    default:
      xabort("FUNCTION NOT MAPPED!");
    }
598

Thomas Jahns's avatar
Thomas Jahns committed
599
600
  for (collID = 0; collID < nProcsColl; ++collID)
    {
601
602
      xassert((txWin[collID].dictSize - txWin[collID].dictRPCUsed)
              > txWin[collID].dictDataUsed);
Thomas Jahns's avatar
Thomas Jahns committed
603
604
605
606
607
608
609
610
611
      if (txWin[collID].postSet)
        {
          xmpi(MPI_Win_wait(txWin[collID].win));
          txWin[collID].postSet = 0;
          modelWinFlushBuffer ( collID );
        }
      xassert(txWin[collID].refuseFuncCall == 0);
      modelWinEnqueue(collID, header, filename, filenamesz);
    }
612

Thomas Jahns's avatar
Thomas Jahns committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  switch ( funcID )
    {
    case STREAMCLOSE:
      xdebug("WROTE FUNCTION CALL IN BUFFER OF WINS:  %s, streamID=%d",
             funcMap[(-1 - funcID)], streamID);
      break;
    case STREAMDEFVLIST:
      xdebug("WROTE FUNCTION CALL IN BUFFER OF WINS:  %s, streamID=%d,"
             " vlistID=%d", funcMap[(-1 - funcID)], streamID, vlistID);
      break;
    case STREAMOPEN:
      xdebug("WROTE FUNCTION CALL IN BUFFER OF WINS:  %s, filenamesz=%zu,"
             " filename=%s, filetype=%d",
             funcMap[(-1 - funcID)], filenamesz, filename, filetype);
627
      break;
628
    }
629

Thomas Jahns's avatar
Thomas Jahns committed
630
  va_end(ap);
631
  xdebug("%s", "RETURN");
632
633
}

634
635
636
637
#endif

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

638
639
640
641
int pioInqVarDecoChunk ( int vlistID, int varID )
{
#ifdef USE_MPI
   int rankModel = commInqRankModel ();
Deike Kleberg's avatar
Deike Kleberg committed
642
   xassert ( rankModel != CDI_UNDEFID );
643
644
645
646
647
648
649
650
651
652
653
   return vlistInqVarDecoChunk ( vlistID, varID, rankModel );
#endif
   return vlistInqVarDecoChunk ( vlistID, varID, CDI_UNDEFID );
}

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

int pioInqVarDecoOff ( int vlistID, int varID )
{
#ifdef USE_MPI
   int rankModel = commInqRankModel ();
Deike Kleberg's avatar
Deike Kleberg committed
654
   xassert ( rankModel != CDI_UNDEFID );
655
   return vlistInqVarDecoOff ( vlistID, varID, rankModel );
Thomas Jahns's avatar
Thomas Jahns committed
656
#else
657
   return vlistInqVarDecoOff ( vlistID, varID, CDI_UNDEFID );
Thomas Jahns's avatar
Thomas Jahns committed
658
#endif
659
660
}

661
/*****************************************************************************/
662
663
664
/**
   @brief initializes the MPI_Communicators needed for the
  communication between the calculator PEs and the I/O PEs and within the
665
666
667
668
669
670
671
672
673
  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]:
674
675

  Collective call
676
677
678
679
680
681
682

  @param comm MPI_Communicator of all calling PEs
  @param nIOP number of I/O PEs
  @return int indicating wether the calling PE is a calcutator (1) or not (0)
*/

#ifdef USE_MPI
683
MPI_Comm pioInit_c ( MPI_Comm commGlob, int nProcsIO, int IOMode,
Deike Kleberg's avatar
Deike Kleberg committed
684
                     int nNamespaces, int * hasLocalFile )
685
{
Thomas Jahns's avatar
Thomas Jahns committed
686
  int sizeGlob;
Deike Kleberg's avatar
Deike Kleberg committed
687

Deike Kleberg's avatar
Deike Kleberg committed
688
  if ( IOMode < PIO_MINIOMODE || IOMode > PIO_MAXIOMODE )
Deike Kleberg's avatar
Deike Kleberg committed
689
    xabort ( "IOMODE IS NOT VALID." );
690

691
#ifdef _SX
Deike Kleberg's avatar
Deike Kleberg committed
692
693
  if ( IOMode ==  PIO_ASYNCH )
    xabort ( "PIO_ASYNCH DOES NOT WORK ON SX." );
694
695
696
697
#endif

  commInit ();
  commDefCommGlob ( commGlob );
698
  sizeGlob = commInqSizeGlob ();
699

700
  if ( nProcsIO <= 0 || nProcsIO > sizeGlob - 1 )
701
702
    xabort("DISTRIBUTION OF TASKS ON PROCS IS NOT VALID.\n"
           "nProcsIO=%d, sizeGlob=%d\n", nProcsIO, sizeGlob);
Deike Kleberg's avatar
Deike Kleberg committed
703
704

  commDefNProcsIO ( nProcsIO );
Deike Kleberg's avatar
Deike Kleberg committed
705
  commDefIOMode   ( IOMode, PIO_MAXIOMODE, PIO_MINIOMODEWITHSPECIALPROCS );
Deike Kleberg's avatar
Deike Kleberg committed
706
  commDefCommPio  ();
707

Deike Kleberg's avatar
Deike Kleberg committed
708
  // JUST FOR TEST CASES WITH ONLY ONE MPI TASK
709
  if ( commInqSizeGlob () == 1 )
710
711
    {
      namespaceInit ( nNamespaces, hasLocalFile );
Deike Kleberg's avatar
Deike Kleberg committed
712
713
      return commInqCommGlob ();
    }
714

715
716
  if ( commInqIsProcIO ())
    {
717
718
719
      IOServer ();
      commDestroy ();
      MPI_Finalize ();
720
      exit ( EXIT_SUCCESS );
721
722
    }
  else
723
    {
724
      commEvalPhysNodes ();
725
726
      commDefCommsIO ();
      namespaceInit ( nNamespaces, hasLocalFile );
727
    }
728

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

Deike Kleberg's avatar
Deike Kleberg committed
734
735
736
737
738
/*****************************************************************************/

int pioInit ( int commGlobArg, int nProcsIO, int IOMode, int nNamespaces,
              int * hasLocalFile )
{
739
#ifdef USE_MPI
740
741
742
  xdebug("START: %s, nProcsIO=%d, IOMode=%d, nNamespaces=%d",
         "cdi parallel",
         nProcsIO, IOMode, nNamespaces );
743
#else
744
745
746
  xdebug("START: %s, nProcsIO=%d, IOMode=%d, nNamespaces=%d",
         "cdi serial",
         nProcsIO, IOMode, nNamespaces );
747
748
#endif

Deike Kleberg's avatar
Deike Kleberg committed
749
750
751
752
753
754
#ifdef USE_MPI
  MPI_Comm commGlob;

  commGlob = MPI_COMM_NULL;
  commGlob = MPI_Comm_f2c (( MPI_Fint ) commGlobArg );
  xassert ( commGlob != MPI_COMM_NULL );
755
756

  return MPI_Comm_c2f ( pioInit_c ( commGlob, nProcsIO, IOMode, nNamespaces,
Deike Kleberg's avatar
Deike Kleberg committed
757
758
                                    hasLocalFile ));
#endif
759
  xdebug("%s", "RETURN" );
Deike Kleberg's avatar
Deike Kleberg committed
760
761
762
  return 0;
}

Deike Kleberg's avatar
Deike Kleberg committed
763
/************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
764

765
void  pioEndDef ( void )
766
767
{
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
768
  char   * buffer;
769
  int bufferSize;
770
  int rankGlob = commInqRankGlob ();
771

772
  xdebug("%s", "START");
773

Deike Kleberg's avatar
Deike Kleberg committed
774
  varsMapNDeco ( commInqNNodes (), commInqNodeSizes ());
Deike Kleberg's avatar
Deike Kleberg committed
775

776
  reshListPrint ( "reshListModel" );
777
  
778
  if ( rankGlob < commInqNProcsColl ())
Deike Kleberg's avatar
Deike Kleberg committed
779
    {
780
      reshPackBufferCreate ( &buffer, &bufferSize, commInqCommsIO ( rankGlob ));
781
782

      xmpi ( MPI_Send ( buffer, bufferSize, MPI_PACKED, commInqNProcsModel (),
783
                        RESOURCES, commInqCommsIO ( rankGlob )));
784

785
      xdebug("%s", "SENT MESSAGE WITH TAG \"RESOURCES\"");
786

Deike Kleberg's avatar
Deike Kleberg committed
787
      reshPackBufferDestroy ( &buffer );
Deike Kleberg's avatar
Deike Kleberg committed
788
    }
Deike Kleberg's avatar
Deike Kleberg committed
789
790

  modelWinCreate ();
791
  namespaceDefResStatus ( STAGE_TIMELOOP );
792
  xdebug("%s", "RETURN");
793
794
795
796
797
798
799
800
#endif
}

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

void  pioEndTimestepping ( void )
{
#ifdef USE_MPI
801
  xdebug("%s", "START");
802
  namespaceDefResStatus ( STAGE_CLEANUP );
803
  xdebug("%s", "RETURN");
804
#endif
805
806
}

Deike Kleberg's avatar
Deike Kleberg committed
807

808
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
809

Deike Kleberg's avatar
Deike Kleberg committed
810

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