aboutsummaryrefslogtreecommitdiff
path: root/src/Write.c
blob: 6a72c007cbfc2f484aeb98f5ef4d83d5cd726354 (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
/*@@
   @file      Write.c
   @date      Tue Oct 10 2000
   @author    Thomas Radke
   @desc 
              File handling routines for IOStreamedHDF5.
   @enddesc 
   @version   $Id$
 @@*/


#include <stdlib.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "StoreNamedData.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "BetaThorns/IOHDF5Util/src/ioHDF5UtilGH.h"
#include "ioStreamedHDF5GH.h"

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


/*@@
   @routine    IOStreamedHDF5_Write
   @date       Tue Oct 10 2000
   @author     Thomas Radke
   @desc
               Opens the HDF5 output file, calls the dump routine,
               and closes it again.
   @enddesc

   @calls      IOHDF5Util_DumpVar

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      cGH *
   @vio        in
   @endvar
   @var        vindex
   @vdesc      index of variable
   @vtype      int
   @vio        in
   @endvar
   @var        alias
   @vdesc      alias name of variable to output
   @vtype      const char *
   @vio        unused
   @endvar
@@*/
void IOStreamedHDF5_Write (cGH *GH,
                           int vindex,
                           const char *alias)
{
  DECLARE_CCTK_PARAMETERS
  int timelevel;
  int old_ioproc;
  int old_nioprocs;
  int old_ioproc_every;
  char *fullname;
  ioGH *ioUtilGH;
  ioStreamedHDF5GH *myGH;
  hid_t file, plist;
  H5FD_stream_fapl_t fapl;


  /* suppress compiler warnings about unused variables */
  alias = alias;

  /* first, check if variable has storage assigned */
  if (! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (vindex)))
  {
    fullname = CCTK_FullName (vindex);
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "No IOStreamedHDF5 output for '%s' (no storage)", fullname);
    free (fullname);
    return;
  }

  /* Get the handles for IO and IOStreamedHDF5 extensions */
  ioUtilGH = (ioGH *) CCTK_GHExtension (GH, "IO");
  myGH = (ioStreamedHDF5GH *) CCTK_GHExtension (GH, "IOStreamedHDF5");

  /* for now we can only have IO mode "oneproc" */
  if (ioUtilGH->nioprocs != 1)
  {
    CCTK_WARN (2, "Output into multiple chunked files not yet implemented. "
                  "Switching to IO mode \"oneproc\".");
  }
  old_ioproc = ioUtilGH->ioproc;
  ioUtilGH->ioproc = 0;
  old_nioprocs = ioUtilGH->nioprocs;
  ioUtilGH->nioprocs = 1;
  old_ioproc_every = ioUtilGH->ioproc_every;
  ioUtilGH->ioproc_every = CCTK_nProcs (GH);

  file = -1;
  if (CCTK_MyProc (GH) == 0 && myGH->data_socket >= 0)
  {
    if (verbose)
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Opening HDF5 output file "
                                    "on data output port %d", data_port);
    }

    /* set file access property list to use the Stream VFD
       and open the file */
    fapl.increment = 0;
    fapl.socket = myGH->data_socket;
    fapl.do_socket_io = 1;
    fapl.backlog = 5;
    fapl.broadcast_fn = NULL;
    fapl.broadcast_arg = NULL;

    IOHDF5_ERROR (plist = H5Pcreate (H5P_FILE_ACCESS));
    IOHDF5_ERROR (H5Pset_fapl_stream (plist, &fapl));

    /* filename is not used if we pass a plist
       but it must not be NULL or an empty string */
    IOHDF5_ERROR (file = H5Fcreate ("unused", H5F_ACC_TRUNC, H5P_DEFAULT,
                                    plist));
    IOHDF5_ERROR (H5Pclose (plist));
  }

  /* get the current timelevel */
  timelevel = CCTK_NumTimeLevelsFromVarI (vindex) - 1;

  /* output the data */
  IOHDF5Util_DumpVar (GH, vindex, timelevel, myGH->geo_output[vindex], file, 0);

  /* close the file */
  if (file >= 0)
  {
    if (verbose)
    {
      CCTK_INFO ("Closing HDF5 output file from this iteration");
    }
    IOHDF5_ERROR (H5Fclose (file));
  }

  /* restore original IO mode */
  ioUtilGH->ioproc = old_ioproc;
  ioUtilGH->nioprocs = old_nioprocs;
  ioUtilGH->ioproc_every = old_ioproc_every;
}