aboutsummaryrefslogtreecommitdiff
path: root/src/GHExtension.c
blob: 876c230692d0c5e16b22a190c6f3fdf92649f906 (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
 /*@@
   @file      GHExtension.c
   @date      Thu Feb  4 10:47:14 1999
   @author    Tom Goodale
   @desc 
   Pugh GH extension stuff.
   @enddesc 
   @version $Header$
 @@*/

/*#define DEBUG_PUGH*/

#include <stdio.h>
#include <stdlib.h>

#include "cctk.h"
#include "cctk_Schedule.h"
#include "cctk_Parameters.h"

#include "pugh.h"
#include "pugh_extension.h"



static char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusPUGH_pugh_GHExtension_c)

 /*@@
   @routine    PUGH_SetupGH
   @date       Wed Feb  3 23:10:19 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void *PUGH_SetupGH(tFleshConfig *config, 
                   int           convergence_level, 
                   cGH          *GH)
{
  DECLARE_CCTK_PARAMETERS

  int    group;
  int    dim;
  int   *nsize;
  int   *ghostsize;
  int   *perme;
  int   *groupghostsize;
  int   *groupsize;
  cGroup pgroup;
  pGH    *newGH;

  /* Set up the GH */

  dim = CCTK_MaxDim();

  nsize     = (int *)malloc(dim*sizeof(int));
  PUGH_GFSize(dim,nsize);

  ghostsize = (int *)malloc(dim*sizeof(int));
  PUGH_GFGhostsize(dim,ghostsize);

  perme     = (int *)malloc(dim*sizeof(int));
  PUGH_GFPeriodic(dim,perme);

  newGH = PUGH_SetupPGH(GH, 
                        dim, 
                        nsize, 
                        ghostsize, 
                        CCTK_StaggerVars() ? PUGH_STAGGER : PUGH_NO_STAGGER,
                        perme);
  if (!newGH) 
  {
    CCTK_WARN(0,"Failed to allocate memory for a pGH");
  }

  /* Set the identity for this pGH. For now this is an empty string. */
  newGH->identity_string = (char *) calloc (1, sizeof (char));

  /* Set up groups on the GH */

  for (group = 0; group < CCTK_NumGroups(); group++)
  {
    CCTK_INT **size;
    int i;

    CCTK_GroupData(group,&pgroup);

    /* Set the global size of the variables in the group */
    size=CCTK_GroupSizesI(group);

    if (size) 
    {
      groupsize = (int *)malloc(pgroup.dim*sizeof(int));
      for (i=0;i<pgroup.dim;i++)
      {
        groupsize[i]=*size[i];
        /* Check distribution type */
        if (pgroup.disttype == CCTK_DISTRIB_CONSTANT)
        {
          groupsize[i] = -groupsize[i];
        }
      }
    }
    else
    {
      groupsize = NULL;
    }

    /* Set the ghostzone size of the variables in the group */
    size=CCTK_GroupGhostsizesI(group);

    if (size) 
    {
      groupghostsize = (int *)malloc(pgroup.dim*sizeof(int));
      for (i=0;i<pgroup.dim;i++)
      {
        groupghostsize[i]=*size[i];
      }
    }
    else if (pgroup.grouptype == CCTK_ARRAY)
    {
      groupghostsize = (int *)malloc(pgroup.dim*sizeof(int));
      for (i=0;i<pgroup.dim;i++)
      {
        groupghostsize[i]=0;
      }      
    }
    else
    {
      groupghostsize = NULL;
    }

    PUGH_SetupGroup(newGH, 
                    groupsize,
                    groupghostsize,
                    pgroup.grouptype, 
                    pgroup.vartype, 
                    pgroup.dim, 
                    pgroup.numvars, 
                    pgroup.stagtype,
                    pgroup.numtimelevels);

    if (groupsize) free(groupsize);
    if (groupghostsize) free(groupghostsize);
  }

  free(nsize);
  free(ghostsize);
  free(perme);

  return newGH;

}


 /*@@
   @routine    PUGH_InitGHBasics
   @date       Thu May 11 2000
   @author     Thomas Radke
   @desc 
               Initializes the basic fields of a cGH structure
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static void PUGH_InitGHBasics (cGH *GH)
{
  int  idir;
  int  istag;
  pGH *mypGH;
  pGExtras *GFExtras;

  mypGH = PUGH_pGH(GH);
  GFExtras = mypGH->GFExtras[GH->cctk_dim-1];

  GH->cctk_convlevel = 0;

  for (idir=0;idir<GH->cctk_dim;idir++)
  {
    GH->cctk_levfac[idir] = 1;
    GH->cctk_nghostzones[idir] = GFExtras->nghostzones[idir];
    GH->cctk_lsh[idir]         = GFExtras->lnsize[idir];
    GH->cctk_gsh[idir]         = GFExtras->nsize[idir];
    GH->cctk_bbox[2*idir]      = GFExtras->bbox[2*idir];
    GH->cctk_bbox[2*idir+1]    = GFExtras->bbox[2*idir+1];
    GH->cctk_lbnd[idir]        = GFExtras->lb[mypGH->myproc][idir];
    GH->cctk_ubnd[idir]        = GFExtras->ub[mypGH->myproc][idir];
    
    for (istag=0;istag<CCTK_NSTAGGER;istag++) 
    {
      GH->cctk_lssh[CCTK_LSSH_IDX(istag,idir)] = GH->cctk_lsh[idir];
      if ((GH->cctk_bbox[2*idir+1]==1) && (istag>0))
        GH->cctk_lssh[CCTK_LSSH_IDX(istag,idir)]--;
    }
  }
}


 /*@@
   @routine    PUGH_InitGHVariables
   @date       Thu May 11 2000
   @author     Thomas Radke
   @desc 
               Initializes the variables of a cGH structure
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static void PUGH_InitGHVariables (cGH *GH)
{
  int  var;
  int  gtype;
  int  ntimelevels;
  int  level;
  pGH *mypGH;


  mypGH = PUGH_pGH(GH);

  for(var = 0; var < mypGH->nvariables; var++)
  {
    gtype = CCTK_GroupTypeFromVarI(var);
    ntimelevels = CCTK_NumTimeLevelsFromVarI(var);

    for(level = 0; level < ntimelevels; level++)
    {
      switch(gtype)
      { 
        case CCTK_SCALAR :
          GH->data[var][level] = mypGH->variables[var][level]; 
          break;
        case CCTK_GF     :
        case CCTK_ARRAY  :
          GH->data[var][level] = 
            ((pGA ***)(mypGH->variables))[var][level]->data; 
          break;
        default:
          CCTK_WARN(1,"Unknown group type in PUGH_InitGHVariables");
      }
    }
  }
}

 /*@@
   @routine    PUGH_InitGH
   @date       Wed Feb  3 23:10:19 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int PUGH_InitGH(cGH *GH)
{
  PUGH_InitGHBasics (GH);

  return 0;
}


 /*@@
   @routine    PUGH_ScheduleTraverseGH
   @date       Thu Jan 27 15:14:09 2000
   @author     Tom Goodale
   @desc 
   Fills out the cGH and then calls schedule traverse.
   @enddesc 
   @calls     
   @calledby   
   @history 
   @hdate Thu Jan 27 15:15:35 2000 @hauthor Tom Goodale
   @hdesc Was originally PUGH_rfrTraverse 
   @hdate Thu May 11 2000 @hauthor Thomas Radke
   @hdesc Splitted pGH setup into PUGH_InitGHBasics() and PUGH_InitGHVariables()
   @endhistory 

@@*/
int PUGH_ScheduleTraverseGH(cGH *GH, 
                            const char *where)
{
  if (strcmp ("CCTK_SHUTDOWN", where) != 0)
  {
    PUGH_InitGHBasics (GH);
    PUGH_InitGHVariables (GH);
  }

  return CCTK_ScheduleTraverse(where, GH, NULL);
}

int PUGH_GFSize(int dim, int *nsize)
{
  DECLARE_CCTK_PARAMETERS

  int dir;

  if (global_nsize > 0) 
  {
    for (dir=0;dir<dim;dir++)
    {
      nsize[dir] = global_nsize;
    }
  }
  else
  {
    if (local_nsize > 0)
    {
      for (dir=0;dir<dim;dir++)
      {
        nsize[dir] = -local_nsize;
      }
    }
    else
    {
      if (dim>0) 
      {
        if (local_nx > 0)
        {
          nsize[0] = -local_nx;
        }
        else
        {
          nsize[0] = global_nx;
        }
      }

      if (dim>1)
      {
        if (local_ny > 0)
        {
          nsize[1] = -local_ny;
        }
        else
        {
          nsize[1] = global_ny;
        }
      }

      if (dim>2)
      {
        if (local_nz > 0)
        {
          nsize[2] = -local_nz;
        }
        else
        {
          nsize[2] = global_nz;
        }
      }
    }
  }

  return 0;
}

int PUGH_GFGhostsize(int dim, int *ghostsize)
{

  DECLARE_CCTK_PARAMETERS

  int dir;

  if (ghost_size>-1)
  {
    for (dir=0;dir<dim;dir++)
    {
      ghostsize[dir] = ghost_size;
    }
  }
  else
  {
    switch(dim)
    {
      case 3:
        ghostsize[2] = ghost_size_z;
      case 2:
        ghostsize[1] = ghost_size_y;
      case 1:
        ghostsize[0] = ghost_size_x;
      default:
        ;
    }
  }
    
  return 0;
}


int PUGH_GFPeriodic(int dim, int *perme)
{

  DECLARE_CCTK_PARAMETERS

  if (periodic)
  {
    switch(dim)
    {
      case 3:
        perme[2] = periodic_z;
      case 2:
        perme[1] = periodic_y;
      case 1:
        perme[0] = periodic_x;
      default:
        ;
    }
  }
  else
  {
    switch(dim)
    {
      case 3:
        perme[2] = 0;
      case 2:
        perme[1] = 0;
      case 1:
        perme[0] = 0;
      default:
        ;
    }
  }    
    
  return 0;
}


int PUGH_PrintTimingInfo(cGH *GH)
{
  int i;
  cTimerData *info;
  pGH *pughGH = PUGH_pGH (GH);


  if (pughGH->comm_time < 0)
  {
    return 0;
  }

  info = CCTK_TimerCreateData();
  if (info)
  {
    CCTK_INFO("Elapsed time spent in communication:");
    CCTK_TimerI(pughGH->comm_time, info);
    for (i = 0; i < info->n_vals; i++)
    {
      switch (info->vals[i].type)
      {
        case val_int:
          CCTK_VInfo(CCTK_THORNSTRING, "  %s: %d %s", info->vals[i].heading,
                     info->vals[i].val.i, info->vals[i].units);
          break;

        case val_long:
          CCTK_VInfo(CCTK_THORNSTRING, "  %s: %d %s", info->vals[i].heading,
                     (int) info->vals[i].val.l, info->vals[i].units);
          break;

        case val_double:
          CCTK_VInfo(CCTK_THORNSTRING, "  %s: %.3f %s", info->vals[i].heading,
                     info->vals[i].val.d, info->vals[i].units);
          break;

        default:
          CCTK_WARN(1, "Unknown data type for timer info");
          break;
      }
    }

    CCTK_TimerDestroyData(info);
  }
  else
  {
    CCTK_WARN(1, "Couldn't create timer info structure ! "
                 "No timing output available.");
  }

  return 0;
}