aboutsummaryrefslogtreecommitdiff
path: root/src/SetupGroup.c
blob: 346da2a4c51479cbb2e370dcc81182739b3611a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
 /*@@
   @file      SetupGroup.c
   @date      Mon Feb  8 19:31:45 1999
   @author    Tom Goodale
   @desc
              Subroutines for setting up a group on a pGH.
   @enddesc
   @version   $Id$
 @@*/

#include <stdlib.h>
#include <string.h>

#include "pugh.h"
#include "pughi.h"

#include "cctk_Parameters.h"

static const char *rcsid = "$Header$";
CCTK_FILEVERSION (CactusPUGH_PUGH_SetupGroup_c)


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static int PUGH_SetupScalarGroup (pGH *newGH,
                                  int vtype,
                                  int n_variables,
                                  int n_timelevels,
                                  int vectorgroup);

static int PUGH_SetupGAGroup (pGH *newGH,
                              int *nsize,
                              int *ghostsize,
                              int  gtype,
                              int  vtype,
                              int  dim,
                              int  n_variables,
                              int  staggercode,
                              int  n_timelevels,
                              int vectorgroup);


 /*@@
   @routine    PUGH_SetupScalarGroup
   @date       Wed Feb 17 04:45:49 1999
   @author     Tom Goodale
   @desc
               Set up a group of scalar variables on a pGH.
               For efficiency reasons, PUGH allocates storage for scalars
               only once when the group is created.
               The current state of storage allocation (which is toggled by
               Enable/DisableGroupStorage) is stored in a byte-sized flag
               immediately after the scalar data.
   @enddesc

   @var        newGH
   @vdesc      Pointer to PUGH grid hierarchy
   @vtype      pGH *
   @vio        in
   @endvar
   @var        vtype
   @vdesc      CCTK data type for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_variables
   @vdesc      number of variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_timelevels
   @vdesc      number of timelevels in this group
   @vtype      int
   @vio        in
   @endvar
   @var        vectorgroup
   @vdesc      is this a vector group ?
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               PUGH_SUCCESS (0) if successful, or
               PUGH_ERRORMEMORY (negative) if memory allocation failed
   @endreturndesc
@@*/
static int PUGH_SetupScalarGroup (pGH *newGH,
                                  int vtype,
                                  int n_variables,
                                  int n_timelevels,
                                  int vectorgroup)
{
  DECLARE_CCTK_PARAMETERS
  int variable, level, vtypesize, retval;
  void *temp;


  (void) (vectorgroup + 0);

  retval = 0;  /* PUGH_SUCCESS */

  temp = realloc (newGH->variables,
                  (newGH->nvariables + n_variables) * sizeof (void **));
  if (temp)
  {
    newGH->variables = (void ***) temp;
    vtypesize = CCTK_VarTypeSize (vtype);

    for (variable = 0; variable < n_variables && retval == 0; variable++)
    {
      temp = malloc (n_timelevels * sizeof (void *));
      if (temp)
      {
        newGH->variables[newGH->nvariables + variable] = (void **) temp;
        for (level = 0; level < n_timelevels; level++)
        {
          /* allocate one more byte for the query_storage flag */
          temp = malloc (vtypesize + 1);
          if (temp)
          {
            newGH->variables[newGH->nvariables + variable][level] = temp;

            /* reset the query_storage flag */
            ((char *) temp)[vtypesize] = PUGH_NOSTORAGE;
          }
          else
          {
            retval = PUGH_ERRORMEMORY;
            break;
          }
        }
      }
      else
      {
        retval = PUGH_ERRORMEMORY;
      }
    }
  }
  else
  {
    retval = PUGH_ERRORMEMORY;
  }

  if (! retval)
  {
    newGH->nvariables += n_variables;
  }
  else
  {
    CCTK_WARN (1, "Memory allocation error in PUGH_SetupScalarGroup");
  }

  return (retval);
}


 /*@@
   @routine    PUGH_SetupGAGroup
   @date       January 19 2000
   @author     Gabrielle Allen
   @desc
               Set up a group of grid array variables (CCTK_GF or CCTK_ARRAY
               types) on a pGH.
   @enddesc
   @calls      CCTK_VarTypeSize
               PUGH_SetupConnectivity
               PUGH_SetupPGExtras
               PUGH_SetupGArrayGroupComm
               PUGH_SetupGArray

   @var        newGH
   @vdesc      Pointer to PUGH grid hierarchy
   @vtype      pGH *
   @vio        in
   @endvar
   @var        nsize
   @vdesc      size dimensions for variables in this group
   @vtype      int *
   @vio        in
   @endvar
   @var        ghostsize
   @vdesc      ghostsize dimensions for variables in this group
   @vtype      int *
   @vio        in
   @endvar
   @var        gtype
   @vdesc      group type
   @vtype      int
   @vio        in
   @endvar
   @var        vtype
   @vdesc      CCTK data type for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        dim
   @vdesc      number of dimensions for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_variables
   @vdesc      number of variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        staggercode
   @vdesc      stagger code for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_timelevels
   @vdesc      number of timelevels in this group
   @vtype      int
   @vio        in
   @endvar
   @var        vectorgroup
   @vdesc      is this a vector group ?
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               PUGH_SUCCESS (0) if successful, or
               PUGH_ERRORMEMORY (negative) if memory allocation failed
   @endreturndesc
@@*/
static int PUGH_SetupGAGroup (pGH *newGH,
                              int *nsize,
                              int *ghostsize,
                              int  gtype,
                              int  vtype,
                              int  dim,
                              int  n_variables,
                              int  staggercode,
                              int  n_timelevels,
                              int  vectorgroup)
{
  int i;
  int           variable;
  int           var_size;
  int           retval;
  int           level;
  void       ***temp;
  pConnectivity *connectivity;
  pGExtras      *extras;
  pComm         *groupcomm;
  int *perme;
  int *nprocs;


  retval = PUGH_SUCCESS;
  var_size = CCTK_VarTypeSize (vtype);

  if (gtype == CCTK_ARRAY)
  {
    /* Arrays can't (yet) have periodicity and manual setup,
       so initialize them to zero */
    perme = calloc (dim, sizeof (int));
    nprocs = calloc (dim, sizeof (int));
    if (! (perme && nprocs))
    {
      CCTK_WARN (0, "Memory allocation error in PUGH_SetupGAGroup");
    }

    /* Check that there are enough grid points in this dimension
     * to make parallelising it worthwhile
     */
    for (i = 0 ; i < dim; i++)
    {
      if (abs (nsize[i]) <= 2*ghostsize[i])
      {
        nprocs[i] = 1;
      }
    }

    connectivity = PUGH_SetupConnectivity (dim, newGH->nprocs, nprocs, perme);

    extras = PUGH_SetupPGExtras (dim,
                                 perme,
                                 staggercode,
                                 nsize,
                                 ghostsize,
                                 newGH->nprocs,
                                 connectivity->nprocs,
                                 newGH->myproc);

    free (nprocs);
    free (perme);
  }
  else
  {
    /* this is for CCTK_GF variables */
    connectivity = newGH->Connectivity[dim-1];
    extras = newGH->GFExtras[dim-1];
  }

  /* Set up the communication buffer used for all variables within this group.
     Note: only with allocated buffers we can have group communication. */
  if(newGH->commmodel == PUGH_ALLOCATEDBUFFERS)
  {
    groupcomm = PUGH_SetupGArrayGroupComm (newGH,
                                           dim,
                                           newGH->nvariables,
                                           n_variables,
                                           0,
                                           vtype,
                                           extras);
  }
  else
  {
    groupcomm = NULL;
  }

  temp = (void ***) realloc (newGH->variables, (newGH->nvariables+n_variables) *
                             sizeof (void **));

  if(temp)
  {
    newGH->variables = temp;

    for (variable = 0; variable < n_variables; variable++)
    {
      newGH->variables[newGH->nvariables] = (void **) malloc (n_timelevels *
                                                              sizeof (void *));

      if (newGH->variables[newGH->nvariables])
      {
        for (level = 0; level < n_timelevels; level++)
        {
          newGH->variables[newGH->nvariables][level] =
            PUGH_SetupGArray (newGH,
                              extras,
                              connectivity,
                              groupcomm,
                              CCTK_VarName (newGH->nvariables),
                              newGH->nvariables,
                              newGH->narrays,
                              var_size,
                              vtype,
                              staggercode,
                              vectorgroup ? n_variables : 1,
                              variable,
                              variable > 0 ? newGH->variables[newGH->nvariables-variable][level] : NULL);
          newGH->narrays++;
        }
        newGH->nvariables++;
      }
      else
      {
        retval = PUGH_ERRORMEMORY;
        break;
      }
    }
  }
  else
  {
    retval = PUGH_ERRORMEMORY;
  }

  if (retval)
  {
    CCTK_WARN (1, "Memory allocation error in PUGH_SetupGAGroup");
  }

  return (retval);
}


 /*@@
   @routine    PUGH_SetupGroup
   @date       Mon Feb  8 19:37:55 1999
   @author     Tom Goodale
   @desc
               Sets up a group on a pGH
   @enddesc
   @calls      PUGH_SetupScalarGroup
               PUGH_SetupGAGroup

   @var        newGH
   @vdesc      Pointer to PUGH grid hierarchy
   @vtype      pGH *
   @vio        in
   @endvar
   @var        nsize
   @vdesc      size dimensions for variables in this group
   @vtype      int *
   @vio        in
   @endvar
   @var        ghostsize
   @vdesc      ghostsize dimensions for variables in this group
   @vtype      int *
   @vio        in
   @endvar
   @var        gtype
   @vdesc      group type
   @vtype      int
   @vio        in
   @endvar
   @var        vtype
   @vdesc      CCTK data type for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        dim
   @vdesc      number of dimensions for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_variables
   @vdesc      number of variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        staggercode
   @vdesc      stagger code for variables in this group
   @vtype      int
   @vio        in
   @endvar
   @var        n_timelevels
   @vdesc      number of timelevels in this group
   @vtype      int
   @vio        in
   @endvar
   @var        vectorgroup
   @vdesc      is this a vector group ?
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine PUGH_SetupScalarGroup or
               @seeroutine PUGH_SetupGAGroup, or
               PUGH_ERROR if invalid group type was passed in
   @endreturndesc
@@*/
int PUGH_SetupGroup (pGH *newGH,
                     int *nsize,
                     int *nghostsize,
                     int  gtype,
                     int  vtype,
                     int  dim,
                     int  n_variables,
                     int  staggercode,
                     int  n_timelevels,
                     int  vectorgroup)
{
  int retval;

  if (gtype == CCTK_SCALAR)
  {
    retval = PUGH_SetupScalarGroup (newGH, vtype, n_variables, n_timelevels, vectorgroup);
  }
  else if (gtype == CCTK_ARRAY || gtype == CCTK_GF)
  {
    retval = PUGH_SetupGAGroup (newGH, nsize, nghostsize, gtype, vtype, dim,
                                n_variables, staggercode, n_timelevels,vectorgroup);
  }
  else
  {
    CCTK_WARN (0, "Unknown group type in PUGH_SetupGroup");
    retval = PUGH_ERROR;
  }

  return (retval);
}