aboutsummaryrefslogtreecommitdiff
path: root/src/Startup.c
blob: 0e612d1be45cda538ac97171a98b30df38e73a7d (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
 /*@@
   @file      Startup.c
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc 
              Startup and termination routines for IOStreamedHDF5.
   @enddesc 
   @version   $Id$
 @@*/


#include <stdlib.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "util_Network.h"
#include "BetaThorns/Socket/src/SocketUtils.h"
#include "CactusBase/IOUtil/src/ioutil_AdvertisedFiles.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioStreamedHDF5GH.h"

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

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


/* prototypes of routines defined in this source file */
void IOStreamedHDF5_Startup (void);
void IOStreamedHDF5_Terminate (cGH *GH);
static void *IOStreamedHDF5_SetupGH (tFleshConfig *config,
                                     int convergence_level,
                                     cGH *GH);


 /*@@
   @routine   IOStreamedHDF5_Startup
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc 
              The startup registration routine for IOStreamedHDF5.
              Registers the GH extensions needed for IOStreamedHDF5
              along with its setup routine.
   @enddesc 

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

  /* FIXME: this check can go as soon as the Stream VFD
            comes with the default HDF5 installation */
#ifndef H5_HAVE_STREAM
  CCTK_WARN (1, "The Stream VFD was not configured in your HDF5 installation. "
                "No HDF5 streaming IO will be available !");
#else
  CCTK_RegisterGHExtensionSetupGH (CCTK_RegisterGHExtension ("IOStreamedHDF5"),
                                   IOStreamedHDF5_SetupGH);
#endif
}


 /*@@
   @routine   IOStreamedHDF5_Terminate
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc 
              IOStreamedHDF5's termination routine
              Closes all open sockets and destroys the timers.
   @enddesc 

   @calls     CCTK_TimerDestroyI

   @var       GH
   @vdesc     Pointer to CCTK grid hierarchy
   @vtype     cGH *
   @vio       in
   @endvar
@@*/
void IOStreamedHDF5_Terminate (cGH *GH)
{
  int i;
  ioStreamedHDF5GH *myGH;


  myGH = (ioStreamedHDF5GH *) CCTK_GHExtension (GH, "IOStreamedHDF5");
  if (myGH)
  {
    /* close the data and checkpoint output sockets */
    if (myGH->data_socket >= 0)
    {
      close (myGH->data_socket);
    }
    if (myGH->checkpoint_socket >= 0)
    {
      close (myGH->checkpoint_socket);
    }

    /* release allocated timers */
    if (myGH->print_timing_info)
    {
      for (i = 0; i < IOHDF5_NUM_TIMERS; i++)
      {
        CCTK_TimerDestroyI (myGH->timers[i]);
      }
    }

    /* remove advertised file and free filename */
    if (myGH->advertised_filename)
    {
      remove (myGH->advertised_filename);
      free (myGH->advertised_filename);
    }
  }
}


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

   @calls     CCTK_RegisterIOMethod
              CCTK_RegisterIOMethodOutputGH
              CCTK_RegisterIOMethodOutputVarAs
              CCTK_RegisterIOMethodTimeToOutput
              CCTK_RegisterIOMethodTriggerOutput
              Socket_TCPOpenServerSock
              Socket_SetNonBlocking
              IOUtil_AdvertiseFile
              Util_GetHostName
              CCTK_TimerCreateI
              CCTK_TimerDestroyI
              CCTK_TimerResetI

   @var       config
   @vdesc     the CCTK configuration as provided by the flesh
   @vtype     tFleshConfig *
   @vio       unused
   @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 *IOStreamedHDF5_SetupGH (tFleshConfig *config,
                                     int convergence_level,
                                     cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i;
  int myproc;
  int numvars;
  char hostname[256];
  ioStreamedHDF5GH *myGH;
  FILE *advertised_file_fd;
  ioAdvertisedFileDesc advertised_file;


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

  myproc = CCTK_MyProc (GH);

  /* Register IOStreamedHDF5 routines as output methods  */
  i = CCTK_RegisterIOMethod ("IOStreamedHDF5");
  CCTK_RegisterIOMethodOutputGH (i, IOStreamedHDF5_OutputGH);
  CCTK_RegisterIOMethodOutputVarAs (i, IOStreamedHDF5_OutputVarAs);
  CCTK_RegisterIOMethodTimeToOutput (i, IOStreamedHDF5_TimeFor);
  CCTK_RegisterIOMethodTriggerOutput (i, IOStreamedHDF5_TriggerOutput);

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

  numvars = CCTK_NumVars ();
  myGH = (ioStreamedHDF5GH *) malloc (sizeof (ioStreamedHDF5GH));
  myGH->geo_output = (ioHDF5Geo_t **) calloc (numvars, sizeof (ioHDF5Geo_t *));
  myGH->out_last = (int *) malloc (numvars * sizeof (int));
  myGH->advertised_filename = NULL;

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

  /* only processor 0 is doing socket I/O */
  myGH->data_socket = -1;
  if (myproc == 0)
  {
    myGH->data_socket = Socket_TCPOpenServerSock (data_port);
    if (myGH->data_socket < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Couldn't open TCP server socket on output port %d. "
                  "No HDF5 streaming output will be available !", data_port);
    }
    else if (Socket_SetNonBlocking (myGH->data_socket) < 0)
    {
      CCTK_WARN (1, "Couldn't set output socket into non-blocking mode. "
                    "No HDF5 streaming output will be available !");
      close (myGH->data_socket);
      myGH->data_socket = -1;
    }
    else
    {
      Util_GetHostName (hostname, sizeof (hostname));
      CCTK_VInfo (CCTK_THORNSTRING,
                  "HDF5 data streaming service started on\n"
                  "                         %s:%d",
                  hostname, data_port);

      /* write the hostname/portnumber information in a temporary file
         which gets advertised and then can be downloaded */
      myGH->advertised_filename = (char *) malloc (L_tmpnam);
      if (myGH->advertised_filename && tmpnam (myGH->advertised_filename))
      {
        advertised_file_fd = fopen (myGH->advertised_filename, "w");

        if (advertised_file_fd)
        {
          fprintf (advertised_file_fd, "Hostname:  %s\n", hostname);
          fprintf (advertised_file_fd, "Data port: %d", data_port);
          fclose (advertised_file_fd);

          advertised_file.slice = "";
          advertised_file.thorn = CCTK_THORNSTRING;
          advertised_file.varname = "All variables";
          advertised_file.description = "Streamed HDF5 data";
          advertised_file.mimetype = "data/streamed-hdf5";

          IOUtil_AdvertiseFile (GH, myGH->advertised_filename,&advertised_file);
        }
        else
        {
          CCTK_WARN (2, "Couldn't create unique temporary file ! "
                        "HDF5 data streaming was not advertised.");
        }
      }
      else
      {
        CCTK_WARN (2, "Couldn't create unique temporary filename "
                      "for advertising the hostname/portnumber information !");
        free (myGH->advertised_filename);
        myGH->advertised_filename = 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]);
    }
  }

  /* FIXME: this check can go as soon as the Stream VFD
            comes with the default HDF5 installation */
#ifdef H5_HAVE_STREAM
  /* only processor 0 is doing socket I/O */
  myGH->checkpoint_socket = -1;
  myGH->checkpoint_fapl = -1;
  if (myproc == 0)
  {
    myGH->checkpoint_socket = Socket_TCPOpenServerSock (checkpoint_port);
    if (myGH->checkpoint_socket < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Couldn't open TCP server socket on checkpoint port %d. "
                  "No IOStreamedHDF5 checkpointing will be available !",
                  checkpoint_port);
    }
    else if (Socket_SetNonBlocking (myGH->checkpoint_socket) < 0)
    {
      CCTK_WARN (1, "Couldn't set checkpoint socket into non-blocking mode. "
                    "No IOStreamedHDF5 checkpointing will be available !");
      close (myGH->checkpoint_socket);
      myGH->checkpoint_socket = -1;
    }
    else
    {
      H5FD_stream_fapl_t fapl;


      /* setup file access property list and select Stream VFD */
      fapl.increment = 0;
      fapl.socket = myGH->checkpoint_socket;
      fapl.do_socket_io = 1;
      fapl.backlog = 5;
      fapl.broadcast_fn  = NULL;
      fapl.broadcast_arg = NULL;
      IOHDF5_ERROR (myGH->checkpoint_fapl = H5Pcreate (H5P_FILE_ACCESS));
      IOHDF5_ERROR (H5Pset_fapl_stream (myGH->checkpoint_fapl, &fapl));
     
      Util_GetHostName (hostname, sizeof (hostname));
      CCTK_VInfo (CCTK_THORNSTRING,
                  "HDF5 checkpoint streaming service started on\n"
                  "                         %s:%d",
                  hostname, checkpoint_port);
    }
  }
#endif

  return (myGH);
}