aboutsummaryrefslogtreecommitdiff
path: root/src/GHExtension.c
blob: 5d923c62340181b65594f51197553defcfb92613 (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
 /*@@
   @file      GHExtension.c
   @date      Tue 9th Feb 1999
   @author    Gabrielle Allen
   @desc 
              IOUtil GH extension stuff.
   @enddesc 
   @history
   @endhistory
 @@*/

/*#define DEBUG_IO*/

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

#include "cctk.h"
#include "util_String.h"
#include "cctk_Parameters.h"
#include "ioGH.h"


/* local function prototypes */
static void SetupSliceCenter (cGH *GH);


 /*@@
   @routine   IOUtil_SetupGH
   @date      Tue May 09 2000
   @author    Thomas Radke
   @desc
   Allocates the IOUtil GH extension structure.
   @enddesc
   @calledby   CCTK scheduler at CCTK_INITIALIZE
   @var        config
   @vdesc      flesh configuration structure (unused)
   @vtype      tFleshConfig *
   @vio        in
   @endvar
   @var        convergence_level
   @vdesc      convergence level (unused)
   @vtype      int
   @vio        in
   @endvar
   @var        GH
   @vdesc      pointer to grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @history

   @endhistory
@@*/
void *IOUtil_SetupGH (tFleshConfig *config, int convergence_level, cGH *GH)
{
  return (malloc (sizeof (ioGH)));
}


 /*@@
   @routine   IOUtil_InitGH
   @date      Tue May 09 2000
   @author    Thomas Radke
   @desc
   The GH initialization routine for IOUtil.
   Necessary output dirs are created.
   For 2D output the slice center is set up.
   Checkpoint/recovery timers are created if timing information was requested.
   @enddesc
   @calledby   CCTK scheduler at CCTK_INITIALIZE
   @var        GH
   @vdesc      pointer to grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @history

   @endhistory
@@*/
int IOUtil_InitGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i;
  ioGH *myGH;


  myGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];

  /* Create the checkpoint directory if it is different to outdir */
  if (checkpoint_every > 0 && ! CCTK_Equals (checkpoint_dir, outdir)) {
    if (CCTK_MyProc (GH) == 0)
      if (CCTK_CreateDirectory (0755,checkpoint_dir) < 0)
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Problem creating checkpoint directory '%s'", checkpoint_dir);
  }

  if (CCTK_Equals (out3D_mode, "proc")) {
    myGH->ioproc = CCTK_MyProc (GH);
    myGH->nioprocs = CCTK_nProcs (GH);
    myGH->ioproc_every = 1;
  } else if (CCTK_Equals (out3D_mode, "np")) {
    if (out3D_procs > CCTK_nProcs (GH)) {
      myGH->ioproc_every = CCTK_nProcs (GH);
      printf ("Reducing ioproc_every to %d\n", myGH->ioproc_every);
    } else
      myGH->ioproc_every  = out3D_procs;

    myGH->nioprocs = CCTK_nProcs (GH) / myGH->ioproc_every +
                      (CCTK_nProcs (GH) % myGH->ioproc_every ? 1 : 0);
    myGH->ioproc   = CCTK_MyProc (GH) -
                      CCTK_MyProc (GH) % myGH->ioproc_every;
  } else if (CCTK_Equals (out3D_mode, "onefile")) {
    myGH->ioproc   = 0;
    myGH->nioprocs = 1;
    myGH->ioproc_every = CCTK_nProcs (GH);
  } else {
    printf ("I don't understand out3D_mode setting. Using onefile.\n");
    myGH->ioproc   = 0;
    myGH->nioprocs = 1;
    myGH->ioproc_every = CCTK_nProcs (GH);
  }

  /* At the moment only have unchunked for a single output file */
  if (out3D_unchunked || CCTK_nProcs (GH) == 1) {
    if (myGH->ioproc_every >= CCTK_nProcs (GH))
      myGH->unchunked = 1;
    else {
      printf ("Unchunked output not currently supported for multiple\n");
      printf ("output files. Output will be chunked.\n");
      myGH->unchunked = 0;
    }
  } else
    myGH->unchunked = 0;

  /* save downsampling parameters in ioUtilGH because they are temporarily
     reset during checkpointing */
  myGH->downsample = (int *) malloc (CCTK_MaxDim () * sizeof (int));
  /* for now we have only parameters for the first 3 dimensions
     the rest is constantly initialized to 1 */
  myGH->downsample [0] = out3D_downsample_x;
  myGH->downsample [1] = out3D_downsample_y;
  myGH->downsample [2] = out3D_downsample_z;
  for (i = 3; i < CCTK_MaxDim (); i++)
    myGH->downsample [i] = 1;

  /* evaluate the out_single parameter only for Cactus compiled with
     double precision */
#ifdef SINGLE_PRECISION
  myGH->out_single = 0;
#else
  myGH->out_single = out3D_single;
#endif

  /* reset the flag incidicating restart from recovery */
  myGH->recovered = 0;

  /* reset the flags array for the file reader */
  myGH->do_inVars = NULL;

  /* set up 2D planes to plot */
  myGH->sp2xyz = (int *) calloc (GH->cctk_dim, sizeof (int));
  SetupSliceCenter (GH);

  /* initialize the file advertising listeners database */
  myGH->listener_DB = NULL;

  return 0;
}


/* =============================================================== 
   utility routines used by other IO thorns
   ===============================================================*/

 /*@@
   @routine    ParseVarsForOutput
   @date       Sat March 6 1999
   @author     Gabrielle Allen
   @desc 
               Sets each flag in the do_output[] do_output to true 
               if var[i] could be found in the list of variable names.
               var_list my also contain group names of variables as well as the
               special keyword "all" which indicates that output is requested
               on all variables.
   @enddesc 
   @history 
   @endhistory 
   @var        var_list
   @vdesc      list of variables and/or group names
   @vtype      const char *
   @vio        in
   @endvar 
   @var        do_output
   @vdesc      do_output of flags indicating output was requested for var[i]
   @vtype      char []
   @vio        out
   @endvar 
@@*/

void ParseVarsForOutput (const char *var_list, char do_output [])
{
  char *before=NULL;
  char *after=NULL;
  char *splitstring;
  int first,groupnum,i,last,index,varnum;

  /* First initialise every variable to no output */
  for (i=0; i<CCTK_NumVars(); i++)
    do_output[i] = 0;

  splitstring = (char *) var_list;

  while(Util_SplitString(&before,&after,splitstring," ")==0) {

#ifdef DEBUG_IO
    printf("   String is -%s-\n",splitstring);
    printf("   Split is -%s- and -%s-\n",before,after);
#endif

    if (CCTK_Equals(before," ")) continue;

    if (strlen(before) > 0) {

      /* Look for any special tokens */
      if (CCTK_Equals(before,"all")) {
        int ilab;
        for (ilab=0;ilab<CCTK_NumVars();ilab++)
          do_output[ilab]=1;
      } else {

        /* See if this name is implementation::variable */
        varnum = CCTK_VarIndex(before);
        if ( varnum < 0 ) {

          /* See if this name is implementation::group */
          groupnum = CCTK_GroupIndex(before);
          if (groupnum < 0) {
            char *msg;
            msg = (char *)malloc((100+strlen(before))*sizeof(char));
            sprintf(msg,"Ignoring %s in IO string (invalid token)",before);
            CCTK_WARN(2,msg);
            free(msg);
          } else {
            /* We have a group so now need all the variables in the group */
            first = CCTK_FirstVarIndexI(groupnum);
            last = first+CCTK_NumVarsInGroupI(groupnum)-1;
            for (index=first;index<=last;index++)
              do_output[index] = 1;
          }
        } else
          do_output[varnum] = 1;
      }
    }
    if(before) 
    {
      free(before);
      before = NULL;
    }
    if(splitstring != var_list)
    {
      free(splitstring);
    }
     
    splitstring = after;

  }

  if (strlen(splitstring)>0) {

    /* Special cases */
    if (CCTK_Equals(splitstring,"all")) {
      int ilab;
      for (ilab=0;ilab<CCTK_NumVars();ilab++)
        do_output[ilab]=1;
    } else {

      varnum = CCTK_VarIndex(splitstring);
      if (varnum < 0) {
        groupnum = CCTK_GroupIndex(splitstring);
        if (groupnum < 0) {
          char *msg;
          msg = (char *)malloc((100+strlen(splitstring))*sizeof(char));
          sprintf(msg,"Ignoring %s in IO string (invalid token)",splitstring);
          CCTK_WARN(2,msg);
          free(msg);
        } else {
          /* We have a group so now need all the variables in the group */
          first = CCTK_FirstVarIndexI(groupnum);
          last = first+CCTK_NumVarsInGroupI(groupnum)-1;
          for (index=first;index<=last;index++)
            do_output[index] = 1;
        }
      } else
        do_output[varnum] = 1;
    }
  }

  if (before) free(before);
  if (after) free(after);
  if(splitstring != var_list)
  {
    free(splitstring);
  }

}


 /*@@
   @routine    IOUtil_RegisterAdvertisedFileListener
   @date       Wed May 17 2000
   @author     Thomas Radke
   @desc 
               Registers a listener with its callbacks for
               advertised file handling
   @enddesc 
   @history 
   @endhistory 
   @var        GH
   @vdesc      pointer to grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        listener
   @vdesc      identification of the listener to be registered
   @vtype      const char *
   @vio        in
   @endvar 
   @var        callbacks
   @vdesc      structure containing the listener's callback routines
   @vtype      const IOUtil_AdvertisedFileListenerCallbacks_t *
   @vio        in
   @endvar 
   @returntype int
   @returndesc This routine returns
                  0  on success
                  -1 if memory allocation failed or NULL pointers were passed
   @endreturndesc
@@*/

int IOUtil_RegisterAdvertisedFileListener (cGH *GH, const char *listener,
                     const IOUtil_AdvertisedFileListenerCallbacks_t *callbacks)
{
  ioGH *myGH;
  int retval = -1;
  IOUtil_AdvertisedFileListenerCallbacks_t *new_callbacks;


  if (listener && callbacks)
  {
    new_callbacks = (IOUtil_AdvertisedFileListenerCallbacks_t *)
                     malloc (sizeof (IOUtil_AdvertisedFileListenerCallbacks_t));
    if (new_callbacks)
    {
      *new_callbacks = *callbacks;
      myGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];
      retval = StoreNamedData (&myGH->listener_DB, listener, new_callbacks);
      if (retval)
        free (new_callbacks);
    }
  }

  return (retval);
}


 /*@@
   @routine    IOUtil_AdvertiseFile
   @date       Wed May 17 2000
   @author     Thomas Radke
   @desc 
               Adds a filename to the list of advertised files for downloading.
               The routine calls all listeners which are currently registered
               for advertised file handling.
   @enddesc 
   @history 
   @endhistory 
   @var        GH
   @vdesc      pointer to grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        filename
   @vdesc      name of the file to advertise
   @vtype      const char *
   @vio        in
   @endvar 
   @var        description
   @vdesc      structure which describes the file
   @vtype      const IOUtil_AdvertisedFileDesc_t *
   @vio        in
   @endvar 
   @returntype int
   @returndesc This routine returns the logically OR'ed return values
               of all listeners' advertise callbacks called.
   @endreturndesc
@@*/

int IOUtil_AdvertiseFile (cGH *GH, const char *filename,
                          const IOUtil_AdvertisedFileDesc_t *description)
{
  int retval = 0;
  pNamedData *listener;
  IOUtil_AdvertisedFileListenerCallbacks_t *callbacks;


  /* get the listener database from IOUtil's GH extension */
  listener = ((ioGH *)
              GH->extensions [CCTK_GHExtensionHandle ("IO")])->listener_DB;

  /* loop through all listeners' advertise() callbacks */
  while (listener)
  {
    callbacks = (IOUtil_AdvertisedFileListenerCallbacks_t *) listener->data;
    if (callbacks && callbacks->advertise)
      retval |= (callbacks->advertise) (GH, filename, description);
    listener = listener->next;
  }

  return (retval);
}


/****************************************************************************/
/* local routines                                                           */
/****************************************************************************/
static void SetupSliceCenter (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int dim;
  int slice_center;
  ioGH *myGH;


  /* Get the handle for IOUtil extensions */
  myGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];

  if (   CCTK_Equals (domain, "octant")
      || CCTK_Equals (domain, "quadrant")
      || CCTK_Equals (domain, "bitant"))
  {
    slice_center = 0;
  }
  else
  {
    slice_center = 1;
  }

  if (slice_center)
  {
    /* For NONE octant mode: the center for slicings */
    /* is moved to the center index */
    /* (unless nx,ny,nz=1) */

    for (dim = 0; dim < GH->cctk_dim; dim++)
    {
      myGH->sp2xyz [dim] = (GH->cctk_lsh [dim] == 1) ?
                            0 : GH->cctk_lsh [dim]/2 - GH->cctk_lbnd [dim];
    }

  }

  /* for octant mode, the slicing center is the index 1 or zero*/
  else

  {
    for (dim = 0; dim < GH->cctk_dim; dim++)
    {
      myGH->sp2xyz [dim] = (GH->cctk_lsh [dim] == 1) ?
                            0 : 1 - GH->cctk_lbnd [dim];
    }
  }

  /* In quadrant mode x and y are like full, but z is like octant */
  if (CCTK_Equals (domain, "quadrant"))
  {
    myGH->sp2xyz [2] = (GH->cctk_lsh [2] == 1) ?
                        0 : GH->cctk_lsh [2]/2 - GH->cctk_lbnd [2];
  }

  if (CCTK_Equals (domain, "bitant"))
  {
    myGH->sp2xyz [0] = (GH->cctk_lsh [0] == 1) ?
                        0 : myGH->sp2xyz [0]/2 - GH->cctk_lbnd [0];

    myGH->sp2xyz [1] = (GH->cctk_lsh [1] == 1) ?
                        0 : GH->cctk_lsh [1]/2 - GH->cctk_lbnd [1];
  }
}