aboutsummaryrefslogtreecommitdiff
path: root/src/Startup.c
blob: f54a04dbd54ccbf3b6106fbfcf87eea50f35bf84 (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
 /*@@
   @file      Startup.c
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
              Startup and termination routines for IOHDF5.
   @enddesc 
   @version   $Id$
 @@*/


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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "BetaThorns/Socket/src/SocketUtils.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioHDF5GH.h"

/* the rcs ID and its dummy function to use it */
static char *rcsid = "$Id$";
CCTK_FILEVERSION(AlphaThorns_IOHDF5_Startup_c)


/* local function prototypes */
static void *IOHDF5_SetupGH (tFleshConfig *config,
                             int convergence_level,
                             cGH *GH);


 /*@@
   @routine   IOHDF5_Startup
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
              The startup registration routine for IOHDF5.
              Registers the GH extensions needed for IOHDF5
              along with its setup routine.
   @enddesc 

   @calls     CCTK_RegisterGHExtensionSetupGH
@@*/
void IOHDF5_Startup (void)
{
  /* check that thorn IOHDF5Util was activated */
  if (CCTK_GHExtensionHandle ("IOHDF5Util") < 0)
  {
    CCTK_WARN (1, "Thorn IOHDF5Util was not activated. "
                  "No IOHDF5 IO methods will be registered.");
    return;
  }

  CCTK_RegisterGHExtensionSetupGH (CCTK_RegisterGHExtension ("IOHDF5"),
                                   IOHDF5_SetupGH);
}


/****************************************************************************/
/*                           local routines                                 */
/****************************************************************************/
 /*@@
   @routine   IOHDF5_SetupGH
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc
              Allocates and sets up IOHDF5's GH extension structure.
   @enddesc

   @calls     CCTK_RegisterIOMethod
              CCTK_RegisterIOMethodOutputGH
              CCTK_RegisterIOMethodOutputVarAs
              CCTK_RegisterIOMethodTimeToOutput
              CCTK_RegisterIOMethodTriggerOutput
              CCTK_TimerCreateI
              CCTK_TimerDestroyI
              CCTK_TimerResetI
              CCTK_CreateDirectory

   @var       config
   @vdesc     the CCTK configuration as provided by the flesh
   @vtype     tFleshConfig *
   @vio       usused
   @endvar
   @var       convergence_level
   @vdesc     the convergence level
   @vtype     int
   @vio       unused
   @endvar
   @var       GH
   @vdesc     Pointer to CCTK grid hierarchy
   @vtype     cGH *
   @vio       in
   @endvar

   @returntype  void *
   @returndesc
                pointer to the allocated GH extension structure
   @endreturndesc
@@*/
static void *IOHDF5_SetupGH (tFleshConfig *config,
                             int convergence_level,
                             cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i;
  int numvars;
  ioHDF5GH *myGH;
 

  /* suppress compiler warnings about unused variables */
  config = config;
  convergence_level = convergence_level;

  /* Register the IOHDF5 routines as a new IO method */
  i = CCTK_RegisterIOMethod ("IOHDF5");
  CCTK_RegisterIOMethodOutputGH (i, IOHDF5_OutputGH);
  CCTK_RegisterIOMethodOutputVarAs (i, IOHDF5_OutputVarAs);
  CCTK_RegisterIOMethodTimeToOutput (i, IOHDF5_TimeFor);
  CCTK_RegisterIOMethodTriggerOutput (i, IOHDF5_TriggerOutput);

  /* Register the IOHDF5 recovery routine to thorn IOUtil */
  if (IOUtil_RegisterRecover ("IOHDF5 recovery", IOHDF5_Recover) < 0)
  {
    CCTK_WARN (1, "Failed to register IOHDF5 recovery routine");
  }

  numvars = CCTK_NumVars ();
  myGH            = (ioHDF5GH *) malloc (sizeof (ioHDF5GH));
  myGH->out_last  = (int *) malloc (numvars * sizeof (int));
  myGH->out_geo   = (ioHDF5Geo_t **) calloc (numvars, sizeof (ioHDF5Geo_t *));
  myGH->check_exisiting_objects = (char *) calloc (numvars, 1);
  myGH->cp_filename_list = (char **) calloc (checkpoint_keep, sizeof (char *));
  myGH->cp_filename_index = 0;

  /* Check whether "IOHDF5::outdir_HDF5" was set.
     If so take this directory otherwise default to "IO::outdir" */
  if (CCTK_ParameterQueryTimesSet ("outdir_HDF5", CCTK_THORNSTRING) > 0)
  {
    myGH->outdir = strdup (outdir_HDF5);
  }
  else
  {
    myGH->outdir = strdup (outdir);
  }

  /* Create the output directory */
  if (CCTK_MyProc (GH) == 0)
  {
    i = CCTK_CreateDirectory (0755, myGH->outdir);
    if (i < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Problem creating HDF5 output directory '%s'", myGH->outdir);
    }
    else if (i > 0)
    {
      CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "HDF5 output directory '%s' already exists", myGH->outdir);
    }
  }

  for (i = 0; i < numvars; i++) 
  {
    myGH->out_last [i] = -1;
  }

  myGH->open_output_files = NULL;

  /* create timers if timing info was requested */
  myGH->print_timing_info = print_timing_info;
  if (myGH->print_timing_info)
  {
    for (i = 0; i < IOHDF5_NUM_TIMERS; i++)
    {
      if ((myGH->timers[i] = CCTK_TimerCreateI ()) < 0)
      {
        break;
      }
    }
    if (i != IOHDF5_NUM_TIMERS)
    {
      CCTK_WARN (1, "Could not create timers for checkpoint/recovery ! "
                    "No timer information will be available.");
      while (--i >= 0)
      {
        CCTK_TimerDestroyI (myGH->timers[i]);
      }
      myGH->print_timing_info = 0;
    }   
    else
    {
      CCTK_TimerResetI (myGH->timers[CP_TOTAL_TIMER]);
      CCTK_TimerResetI (myGH->timers[RECOVERY_TIMER]);
    }
  }

  return (myGH);
}