aboutsummaryrefslogtreecommitdiff
path: root/src/GHExtension.c
blob: 5d107215a728a605a45a9dd1f2fd669bb1795013 (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
 /*@@
   @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 "cctk_Parameters.h"
#include "ioGH.h"


void *IOUtil_SetupGH (tFleshConfig *config, int convergence_level, cGH *GH)
{
  /* nothing else to allocate but the extension */

  return (malloc (sizeof (ioGH)));
}

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)
      CCTK_mkdir (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

  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);
  }

}