aboutsummaryrefslogtreecommitdiff
path: root/src/Startup.c
blob: 779a05144a44dc5694b39832952c1d5ab5c3af24 (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
 /*@@
   @file      Startup.c
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
   Startup routines for StreamedHDF5.
   @enddesc 
   @history
   @hauthor Thomas Radke @hdate May 21 1999
   @hdesc Just copied from thorn FlexIO.
   @endhistory
 @@*/

#include <stdio.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "BetaThorns/Socket/src/SocketUtils.h"
#include "StreamedHDF5GH.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif


/* prototypes of functions to be registered */
int StreamedHDF5_OutputGH(cGH *GH);
int StreamedHDF5_TriggerOutput(cGH *GH, int);
int StreamedHDF5_TimeFor(cGH *GH, int);
int StreamedHDF5_OutputVarAs(cGH *GH, const char *var, const char *alias);

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


 /*@@
   @routine   StreamedHDF5_Startup
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
   The startup registration routine for StreamedHDF5.
   Registers the GH extensions needed for StreamedHDF5 and
   the registerable routines used for each method of StreamedHDF5.
   StreamedHDF5 does not overload any functions.
   @enddesc 
   @calls     
   @calledby   CCTK scheduler at STARTUP
   @history 
 
   @endhistory 
@@*/
void StreamedHDF5_Startup (void)
{
  int IO_GHExtension;


  if (CCTK_GHExtensionHandle ("IO") < 0)
  {
    CCTK_WARN (1, "Thorn IOUtil was not activated. "
                  "No StreamedHDF5 IO methods will be enabled.");
    return;
  }

  /* this check can go as soon as the Stream VFD
     comes with the default HDF5 installation */
#ifndef H5FDstream_H
  CCTK_WARN (1, "The Stream VFD is not included in the configured HDF5 "
                "installation. No HDF5 stream output will be available !");
#else
  IO_GHExtension = CCTK_RegisterGHExtension ("StreamedHDF5");
  CCTK_RegisterGHExtensionSetupGH (IO_GHExtension, StreamedHDF5_SetupGH);
  CCTK_RegisterGHExtensionInitGH (IO_GHExtension, StreamedHDF5_InitGH);
#endif
}


 /*@@
   @routine   StreamedHDF5_Terminate
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc 
              StreamedHDF5's termination routine
              Closes the socket if it was opened before.
   @enddesc 
   @calls     
   @calledby   CCTK scheduler at TERMINATE
   @history 
 
   @endhistory 
@@*/
void StreamedHDF5_Terminate (cGH *GH)
{
  int handle;
  StreamedHDF5GH *myGH;


  handle = CCTK_GHExtensionHandle ("StreamedHDF5");
  myGH = handle >= 0 ? (StreamedHDF5GH *) GH->extensions [handle] : NULL;
  if (myGH)
  {
    if (myGH->socket >= 0)
    {
      close (myGH->socket);
    }
  }
}


/*************************** local routines *********************************/
static void *StreamedHDF5_SetupGH (tFleshConfig *config, int convergence_level, cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int IOMethod;
  int numvars, socket;
  StreamedHDF5GH *newGH;


  if (CCTK_MyProc (GH) == 0)
  {
    socket = Socket_TCPOpenServerSock (port);
    if (socket < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Couldn't open TCP server socket on port %d. No HDF5 stream "
                  "output available !", port);
      return (NULL);
    }

    if (Socket_SetNonBlocking (socket) < 0)
    {
      CCTK_WARN (1, "Couldn't set socket into non-blocking mode. No HDF5 "
                    "stream output available !");
      close (socket);
      return (NULL);
    }
  }
  else
  {
    socket = -1;
  }

  /* Register StreamedHDF5 routines as output methods  */
  IOMethod = CCTK_RegisterIOMethod ("StreamedHDF5");
  CCTK_RegisterIOMethodOutputGH (IOMethod, StreamedHDF5_OutputGH);
  CCTK_RegisterIOMethodOutputVarAs (IOMethod, StreamedHDF5_OutputVarAs);
  CCTK_RegisterIOMethodTimeToOutput (IOMethod, StreamedHDF5_TimeFor);
  CCTK_RegisterIOMethodTriggerOutput (IOMethod, StreamedHDF5_TriggerOutput);

  numvars = CCTK_NumVars ();

  newGH = (StreamedHDF5GH *) malloc (sizeof (StreamedHDF5GH));
  newGH->do_output  = (char *) malloc (numvars * sizeof (char));
  newGH->geo_output = (StreamGeo_t*) malloc (numvars * sizeof (StreamGeo_t));
  newGH->out_last   = (int *) malloc (numvars * sizeof (int));
  newGH->socket     = socket;

  /* save the original error printing routine and its argument */
  CACTUS_IOHDF5_ERROR (H5Eget_auto (&newGH->printErrorFn,
                                    &newGH->printErrorFnArg));

  /* predefine dataspaces for writing scalar and array attributes */
  /* the dimension of the array dataspace is set when used */
  CACTUS_IOHDF5_ERROR (newGH->scalarDataspace = H5Screate (H5S_SCALAR));
  CACTUS_IOHDF5_ERROR (newGH->arrayDataspace = H5Screate (H5S_SIMPLE));

  /* predefine a IOHDF5_COMPLEX datatype */
  CACTUS_IOHDF5_ERROR (newGH->IOHDF5_COMPLEX =
                       H5Tcreate (H5T_COMPOUND, sizeof (CCTK_COMPLEX)));
  CACTUS_IOHDF5_ERROR (H5Tinsert (newGH->IOHDF5_COMPLEX, "real",
                       offsetof (CCTK_COMPLEX, Re), IOHDF5_REAL));
  CACTUS_IOHDF5_ERROR (H5Tinsert (newGH->IOHDF5_COMPLEX, "imag",
                       offsetof (CCTK_COMPLEX, Im), IOHDF5_REAL));

  /* predefine a C string datatype */
  CACTUS_IOHDF5_ERROR (newGH->IOHDF5_STRING = H5Tcopy (H5T_C_S1));

  return (newGH);
}


static int StreamedHDF5_InitGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i, handle;
  StreamedHDF5GH *myGH;


  handle = CCTK_GHExtensionHandle ("StreamedHDF5");
  myGH = handle >= 0 ? (StreamedHDF5GH *) GH->extensions [handle] : NULL;
  if (handle < 0 || myGH == NULL)
  { 
    CCTK_WARN (1, "Couldn't access GH extension handle\n");
    return (-1);
  }

  /* How often to output */
  myGH->out_every = out_every > 0 ? out_every : -1;
  if (outHDF5_every > 0)
  {
    myGH->out_every = outHDF5_every;
  }

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

  return (0);
}