aboutsummaryrefslogtreecommitdiff
path: root/src/GHExtension.c
blob: ce7f0e26d53c41d972d89c1bbbd694e39071e16b (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
 /*@@
   @file      GHExtension.c
   @date      Tue 9th Feb 1999
   @author    Gabrielle Allen
   @desc 
   IOUtil GH extension stuff.
   @enddesc 
   @history
   @hauthor Thomas Radke @hdate 16 Mar 1999
   @hdesc Added parameters for 2D and 3D output
   @hauthor Thomas Radke @hdate 01 Apr 1999
   @hdesc check parameter symmetry instead of grid in Automatic1DLines()
   @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) {
      char *command = (char *) malloc (1024 * sizeof (char));

      sprintf (command, "mkdir -p %s", checkpoint_dir);
      if (system (command) < 0)
        CCTK_WARN (1,"Problem creating checkpoint directory");
      free (command);
    }
  }

  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_x = out3D_downsample_x;
  myGH->downsample_y = out3D_downsample_y;
  myGH->downsample_z = out3D_downsample_z;

  /* 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;
}

int IOUtil_rfrTraverseGH(cGH *GH, int rfrpoint)
{
  return 0;
}


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

int InitIONum(int *array, char *string)
{
  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++)
    array[i] = 0;

  splitstring=string;

  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++)
          array[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++)
              array[index] = 1;
          }
        } else
          array[varnum] = 1;
      }
    }
    splitstring = after;
  }

  if (strlen(splitstring)>0) {

    /* Special cases */
    if (CCTK_Equals(splitstring,"all")) {
      int ilab;
      for (ilab=0;ilab<CCTK_NumVars();ilab++)
        array[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++)
            array[index] = 1;
        }
      } else
        array[varnum] = 1;
    }
  }

  if (before) free(before);
  if (after) free(after);

  return 0;
}