aboutsummaryrefslogtreecommitdiff
path: root/src/Write.c
blob: 232a4f4986cbcd5e8a1665ad0540eee132595065 (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
/*@@
   @file      Write.c
   @date      Fri Feb 21 15:27:03 1997
   @author    John Shalf, Gabrielle Allen, Tom Goodale, Thomas Radke
   @desc
              File handling routines for IOFlexIO.
   @enddesc
   @version   $Id$
 @@*/


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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "CactusBase/IOUtil/src/ioutil_Utils.h"
#include "ioFlexGH.h"

/* the rcs ID and its dummy funtion to use it */
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusPUGHIO_IOFlexIO_Write_c)


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static IEEEfile_t *OpenFile (const cGH *GH, int vindex, const char *alias,
                             int *is_new_file);


/*@@
   @routine    IOFlexIO_Write
   @author     Paul Walker
   @date       Feb 1997
   @desc
               Opens the IOFlexIO output file, calls the dump routine,
               and closes it again.
   @enddesc

   @calls      OpenFile
               IOFlexIO_DumpVar

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      const 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        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine IOFlexIO_DumpVar, or<BR>
               -1 if no storage was assigned to variable<BR>
               -2 if file couldn't be opened
   @endreturndesc
@@*/
int IOFlexIO_Write (const cGH *GH, int vindex, const char *alias)
{
  const flexioGH *myGH;
  const ioGH *ioUtilGH;
  int myproc, len, is_new_file, retval;
  char buffer[128];
  char *fullname;
  IEEEfile_t *file;
  DECLARE_CCTK_PARAMETERS


  /* 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 IOFlexIO output for '%s' (no storage)", fullname);
    free (fullname);
    return (-1);
  }

  if (CCTK_Equals (verbose, "full"))
  {
    CCTK_VInfo (CCTK_THORNSTRING, "IOFlexIO output of variable '%s', "
                "alias '%s'", CCTK_VarName (vindex), alias);
  }

  myGH = CCTK_GHExtension (GH, "IOFlexIO");
  ioUtilGH = CCTK_GHExtension (GH, "IO");

  /* get the filename and descriptor for output */
  file = OpenFile (GH, vindex, alias, &is_new_file);

  /* nothing to do for an already opened file which is kept open all the time */
  myproc = CCTK_MyProc (GH);
  if (myproc == ioUtilGH->ioproc &&
      (CCTK_GroupTypeFromVarI (vindex) != CCTK_SCALAR || myproc == 0))
  {
    if (is_new_file || reuse_filehandles)
    {
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "%s IEEEIO output file '%s'",
                    is_new_file ? "Opening" : "Resuming", file->filename);
      }

      /* first time through or one file per slice: open anew */
      if (is_new_file)
      {
        file->iofile = IEEEopen (file->filename, "w");
        if (! IOisValid (file->iofile))
        {
          CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, "Couldn't "
                      "create IEEEIO output file '%s'", file->filename);
        }

        /* add the parameter filename and the creation date
           as file identification attributes */
        if (CCTK_Equals (out_fileinfo, "parameter filename") ||
            CCTK_Equals (out_fileinfo, "all"))
        {
          buffer[0] = 0;
          CCTK_ParameterFilename (sizeof (buffer), buffer);
          FLEXIO_ERROR (IOwriteAttribute (file->iofile, "parameter file",
                                          CHAR,
                                          strlen (buffer) + 1, buffer));
        }
        if (CCTK_Equals (out_fileinfo, "creation date") ||
            CCTK_Equals (out_fileinfo, "all"))
        {
          buffer[0] = 0;
          Util_CurrentDate (sizeof (buffer), buffer);
          len = strlen (buffer) + 1;
          buffer[len-1] = ' ';
          Util_CurrentTime (sizeof (buffer) - len, buffer + len);
          FLEXIO_ERROR (IOwriteAttribute (file->iofile, "creation date",
                                          CHAR,
                                          strlen (buffer) + 1, buffer));
        }
      }
      else
      {
        /* resume the file (reopen descriptor)
           This allows descriptor reuse without requiring expensive destruction
           and reallocation of the associated datastructures */
        FLEXIO_ERROR (IOresume (file->iofile));
      }

      /* turn on buffer cache (with default size chosen by IEEEIO lib) */
      if (file->iofile)
      {
        IEEEbufferOn (file->iofile, 0);
      }
    }
    if (! file->iofile)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Invalid file descriptor for IEEEIO ouput file '%s'",
                  file->filename);
      return (-2);
    }
  }
  else
  {
    file->iofile = (IOFile) 0;
  }

  /* output the data */
  retval = IOFlexIO_DumpVar (GH, myGH->requests[vindex], file->iofile);

  /* output the GH extensions and parameters */
  if (file->iofile)
  {
    /* output parameters necessary for filereader datafiles */
    if (is_new_file)
    {
      IOFlexIOi_DumpGHExtensions (GH, file->iofile);
      if (! CCTK_Equals (out_save_parameters, "no"))
      {
        IOFlexIOi_DumpParameters (GH, CCTK_Equals (out_save_parameters, "all"),
                                  file->iofile);
      }
    }

    /* close the file */
    if (out_timesteps_per_file > 0 || reuse_filehandles)
    {
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "%s IEEEIO output file '%s'",
                    out_timesteps_per_file > 0 ? "Closing" : "Pausing",
                    file->filename);
      }

      IEEEbufferOff (file->iofile);
      if (out_timesteps_per_file > 0)
      {
        FLEXIO_ERROR (IOclose (file->iofile));
      }
      else
      {
        FLEXIO_ERROR (IOpause (file->iofile));
      }
    }
  }
  if (out_timesteps_per_file > 0)
  {
    free (file->filename);
    free (file);
  }

  return (retval);
}


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
/* generate the filename for the given alias and creates/opens the file */
static IEEEfile_t *OpenFile (const cGH *GH, int vindex, const char *alias,
                             int *is_new_file)
{
  const ioGH *ioUtilGH;
  flexioGH *myGH;
  int myproc, nprocs, vdim, result;
  char extra[256], extradir[256];
  char *outputdir, *tmp;
  IEEEfile_t *file;
  DECLARE_CCTK_PARAMETERS


  /* get the GH extensions for IOUtil, and IOFlexIO */
  ioUtilGH = CCTK_GHExtension (GH, "IO");
  myGH = CCTK_GHExtension (GH, "IOFlexIO");

  file = GetNamedData (myGH->fileList, alias);
  if (file != NULL)
  {
    /* set flag to indicate that file should be opened in append mode */
    *is_new_file = 0;
    return (file);
  }

  extra[0] = extradir[0] = '\0';

  nprocs = CCTK_nProcs (GH);
  myproc = CCTK_MyProc (GH);
  vdim   = CCTK_GroupDimFromVarI (vindex);

  if (out_timesteps_per_file > 0)
  {
    tmp = extra;
    sprintf (extra, ".time_%7.3f", GH->cctk_time);

    /* be sure to replace any spaces in the filename with an _ */
    do
    {
      if (*tmp == ' ')
      {
        *tmp = '_';
      }
    } while (*++tmp);
  }

  /*
   *  OUTPUT ONE FILE FOR EACH N PROCESSORS
   *  -------------------------------------
   *
   *  If only one output file, the single file for each GV is written
   *  in the default output directory (ie. extradir = ""). Otherwise
   *  a directory is created for each output GV to hold the multiple files.
   */
  if (ioUtilGH->ioproc_every < nprocs &&
      CCTK_GroupTypeFromVarI (vindex) != CCTK_SCALAR)
  {
    /* add the output processor number to the extra string */
    sprintf (extra, "%s.file_%d", extra, myproc / ioUtilGH->ioproc_every);

    /* if necessary create the output directory */
    outputdir = malloc (strlen (myGH->out_dir) + strlen (alias) + 5);
    sprintf (outputdir, "%s/%s_%dd", myGH->out_dir, alias, vdim);

    result = IOUtil_CreateDirectory (GH, outputdir,
                                     ! CCTK_Equals (out_mode, "onefile"),
                                     ioUtilGH->ioproc);
    if (result < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Problem creating IOFlexIO output directory '%s'", outputdir);
    }
    else if (result > 0 && CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING,
                  "IOFlexIO output directory '%s' already exists", outputdir);
    }

    free (outputdir);

#ifdef CCTK_MPI
    /* wait for all processors to catch up */
    CCTK_Barrier (GH);
#endif

    /* extradir is the relative output directory */
    sprintf (extradir, "%s_%dd/", alias, vdim);
  }

  /* CREATE THE COMPLETE OUTPUT FILENAME
     ----------------------------------- */

  file = malloc (sizeof (IEEEfile_t));

  file->filename = malloc (strlen (myGH->out_dir) + strlen (extradir) +
                           strlen (alias) + strlen (extra) +
                           strlen (GH->identity) + 10);
  sprintf (file->filename, "%s/%s%s%s%s.ieee", myGH->out_dir,
           extradir, alias, extra, GH->identity);

  /* no need to store file info if used only once */
  *is_new_file = 1;
  if (out_timesteps_per_file < 0)
  {
    if (myproc == ioUtilGH->ioproc)
    {
      if (! IO_TruncateOutputFiles (GH))
      {
        file->iofile = IEEEopen (file->filename, "a");
        *is_new_file = ! IOisValid (file->iofile);
      }
    }
    StoreNamedData (&myGH->fileList, alias, file);
  }

  return (file);
}