Actual source code: vecio.c

  1: /*
  2:    This file contains simple binary input routines for vectors.  The
  3:    analogous output routines are within each vector implementation's
  4:    VecView (with viewer types PETSCVIEWERBINARY)
  5:  */

  7: #include <petscvec.h>
  8: #include <petsc/private/vecimpl.h>
  9: #include <petsc/private/viewerimpl.h>
 10: #include <petsclayouthdf5.h>

 12: PetscErrorCode VecView_Binary(Vec vec, PetscViewer viewer)
 13: {
 14:   PetscBool          skipHeader;
 15:   PetscLayout        map;
 16:   PetscInt           tr[2], n, s, N;
 17:   const PetscScalar *xarray;

 19:   PetscFunctionBegin;
 20:   PetscCheckSameComm(vec, 1, viewer, 2);
 21:   PetscCall(PetscViewerSetUp(viewer));
 22:   PetscCall(PetscViewerBinaryGetSkipHeader(viewer, &skipHeader));

 24:   PetscCall(VecGetLayout(vec, &map));
 25:   PetscCall(PetscLayoutGetLocalSize(map, &n));
 26:   PetscCall(PetscLayoutGetRange(map, &s, NULL));
 27:   PetscCall(PetscLayoutGetSize(map, &N));

 29:   tr[0] = VEC_FILE_CLASSID;
 30:   tr[1] = N;
 31:   if (!skipHeader) PetscCall(PetscViewerBinaryWrite(viewer, tr, 2, PETSC_INT));

 33:   PetscCall(VecGetArrayRead(vec, &xarray));
 34:   PetscCall(PetscViewerBinaryWriteAll(viewer, xarray, n, s, N, PETSC_SCALAR));
 35:   PetscCall(VecRestoreArrayRead(vec, &xarray));

 37:   { /* write to the viewer's .info file */
 38:     FILE             *info;
 39:     PetscMPIInt       rank;
 40:     PetscViewerFormat format;
 41:     const char       *name, *pre;

 43:     PetscCall(PetscViewerBinaryGetInfoPointer(viewer, &info));
 44:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)vec), &rank));

 46:     PetscCall(PetscViewerGetFormat(viewer, &format));
 47:     if (format == PETSC_VIEWER_BINARY_MATLAB) {
 48:       PetscCall(PetscObjectGetName((PetscObject)vec, &name));
 49:       if (rank == 0 && info) {
 50:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "#--- begin code written by PetscViewerBinary for MATLAB format ---#\n"));
 51:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "#$$ Set.%s = PetscBinaryRead(fd);\n", name));
 52:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n"));
 53:       }
 54:     }

 56:     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)vec, &pre));
 57:     if (rank == 0 && info) PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "-%svecload_block_size %" PetscInt_FMT "\n", pre ? pre : "", vec->map->bs));
 58:   }
 59:   PetscFunctionReturn(PETSC_SUCCESS);
 60: }

 62: static PetscErrorCode VecLoad_Binary(Vec vec, PetscViewer viewer)
 63: {
 64:   PetscBool    skipHeader, flg;
 65:   uint32_t     tr[2];
 66:   PetscInt     token, rows, N, n, s, bs;
 67:   PetscScalar *array;
 68:   PetscLayout  map;

 70:   PetscFunctionBegin;
 71:   PetscCall(PetscViewerSetUp(viewer));
 72:   PetscCall(PetscViewerBinaryGetSkipHeader(viewer, &skipHeader));

 74:   PetscCall(VecGetLayout(vec, &map));
 75:   PetscCall(PetscLayoutGetSize(map, &N));

 77:   /* read vector header */
 78:   if (!skipHeader) {
 79:     PetscCall(PetscViewerBinaryRead(viewer, tr, 2, NULL, PETSC_INT32));
 80:     if (tr[0] == VEC_FILE_CLASSID) { // File was written with 32-bit ints
 81:       rows = tr[1];
 82:     } else { // Assume file was written with 64-bit ints so reconstruct token and read number of rows
 83:       PetscInt64 rows64;
 84:       token = (PetscInt)((uint64_t)tr[0] << 32) + tr[1];
 85:       PetscCheck(token == VEC_FILE_CLASSID, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Not a vector next in file");
 86:       PetscCall(PetscViewerBinaryRead(viewer, &rows64, 1, NULL, PETSC_INT64));
 87:       PetscCall(PetscIntCast(rows64, &rows));
 88:     }
 89:     PetscCheck(rows >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Vector size (%" PetscInt_FMT ") in file is negative", rows);
 90:     PetscCheck(N < 0 || N == rows, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Vector in file different size (%" PetscInt_FMT ") than input vector (%" PetscInt_FMT ")", rows, N);
 91:   } else {
 92:     PetscCheck(N >= 0, PETSC_COMM_SELF, PETSC_ERR_USER, "Vector binary file header was skipped, thus the user must specify the global size of input vector");
 93:     rows = N;
 94:   }

 96:   /* set vector size, blocksize, and type if not already set; block size first so that local sizes will be compatible. */
 97:   PetscCall(PetscLayoutGetBlockSize(map, &bs));
 98:   PetscCall(PetscOptionsGetInt(((PetscObject)viewer)->options, ((PetscObject)vec)->prefix, "-vecload_block_size", &bs, &flg));
 99:   if (flg) PetscCall(VecSetBlockSize(vec, bs));
100:   PetscCall(PetscLayoutGetLocalSize(map, &n));
101:   if (N < 0) PetscCall(VecSetSizes(vec, n, rows));
102:   PetscCall(VecSetUp(vec));

104:   /* get vector sizes and check global size */
105:   PetscCall(VecGetSize(vec, &N));
106:   PetscCall(VecGetLocalSize(vec, &n));
107:   PetscCall(VecGetOwnershipRange(vec, &s, NULL));
108:   PetscCheck(N == rows, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Vector in file different size (%" PetscInt_FMT ") than input vector (%" PetscInt_FMT ")", rows, N);

110:   /* read vector values */
111:   PetscCall(VecGetArray(vec, &array));
112:   PetscCall(PetscViewerBinaryReadAll(viewer, array, n, s, N, PETSC_SCALAR));
113:   PetscCall(VecRestoreArray(vec, &array));
114:   PetscFunctionReturn(PETSC_SUCCESS);
115: }

117: #if PetscDefined(HAVE_HDF5)
118: static PetscErrorCode VecLoad_HDF5(Vec xin, PetscViewer viewer)
119: {
120:   hid_t        scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */
121:   PetscScalar *x, *arr;
122:   const char  *vecname;

124:   PetscFunctionBegin;
125:   PetscCheck(((PetscObject)xin)->name, PetscObjectComm((PetscObject)xin), PETSC_ERR_SUP, "Vec name must be set with PetscObjectSetName() before VecLoad()");
126:   #if PetscDefined(USE_REAL_SINGLE)
127:   scalartype = H5T_NATIVE_FLOAT;
128:   #elif PetscDefined(USE_REAL___FLOAT128)
129:     #error "HDF5 output with 128 bit floats not supported."
130:   #elif PetscDefined(USE_REAL___FP16)
131:     #error "HDF5 output with 16 bit floats not supported."
132:   #else
133:   scalartype = H5T_NATIVE_DOUBLE;
134:   #endif
135:   PetscCall(PetscObjectGetName((PetscObject)xin, &vecname));
136:   PetscCall(PetscViewerHDF5Load(viewer, vecname, xin->map, scalartype, (void **)&x));
137:   PetscCall(VecSetUp(xin)); /* VecSetSizes might have not been called so ensure ops->create has been called */
138:   if (!xin->ops->replacearray) {
139:     PetscCall(VecGetArray(xin, &arr));
140:     PetscCall(PetscArraycpy(arr, x, xin->map->n));
141:     PetscCall(PetscFree(x));
142:     PetscCall(VecRestoreArray(xin, &arr));
143:   } else {
144:     PetscCall(VecReplaceArray(xin, x));
145:   }
146:   PetscFunctionReturn(PETSC_SUCCESS);
147: }
148: #endif

150: #if PetscDefined(HAVE_ADIOS)
151:   #include <adios.h>
152:   #include <adios_read.h>
153: #include <petsc/private/vieweradiosimpl.h>
154: #include <petsc/private/viewerimpl.h>

156: static PetscErrorCode VecLoad_ADIOS(Vec xin, PetscViewer viewer)
157: {
158:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
159:   PetscScalar       *x;
160:   PetscInt           Nfile, N, rstart, n;
161:   uint64_t           N_t, rstart_t;
162:   const char        *vecname;
163:   ADIOS_VARINFO     *v;
164:   ADIOS_SELECTION   *sel;

166:   PetscFunctionBegin;
167:   PetscCall(PetscObjectGetName((PetscObject)xin, &vecname));

169:   v = adios_inq_var(adios->adios_fp, vecname);
170:   PetscCheck(v->ndim == 1, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Vector in file is not of dimension 1 (%" PetscInt_FMT ")", v->ndim);
171:   Nfile = (PetscInt)v->dims[0];

173:   /* Set Vec sizes,blocksize,and type if not already set */
174:   if ((xin)->map->n < 0 && (xin)->map->N < 0) PetscCall(VecSetSizes(xin, PETSC_DECIDE, Nfile));
175:   /* If sizes and type already set,check if the vector global size is correct */
176:   PetscCall(VecGetSize(xin, &N));
177:   PetscCall(VecGetLocalSize(xin, &n));
178:   PetscCheck(N == Nfile, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Vector in file different length (%" PetscInt_FMT ") then input vector (%" PetscInt_FMT ")", Nfile, N);

180:   PetscCall(VecGetOwnershipRange(xin, &rstart, NULL));
181:   rstart_t = rstart;
182:   N_t      = n;
183:   sel      = adios_selection_boundingbox(v->ndim, &rstart_t, &N_t);
184:   PetscCall(VecGetArray(xin, &x));
185:   adios_schedule_read(adios->adios_fp, sel, vecname, 0, 1, x);
186:   adios_perform_reads(adios->adios_fp, 1);
187:   PetscCall(VecRestoreArray(xin, &x));
188:   adios_selection_delete(sel);
189:   PetscFunctionReturn(PETSC_SUCCESS);
190: }
191: #endif

193: PetscErrorCode VecLoad_Default(Vec newvec, PetscViewer viewer)
194: {
195:   PetscBool isbinary;
196: #if PetscDefined(HAVE_HDF5)
197:   PetscBool ishdf5;
198: #endif
199: #if PetscDefined(HAVE_ADIOS)
200:   PetscBool isadios;
201: #endif

203:   PetscFunctionBegin;
204:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
205: #if PetscDefined(HAVE_HDF5)
206:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
207: #endif
208: #if PetscDefined(HAVE_ADIOS)
209:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERADIOS, &isadios));
210: #endif

212:   if (isbinary) {
213:     PetscCall(VecLoad_Binary(newvec, viewer));
214: #if PetscDefined(HAVE_HDF5)
215:   } else if (ishdf5) {
216:     if (!((PetscObject)newvec)->name) {
217:       PetscCall(PetscLogEventEnd(VEC_Load, viewer, 0, 0, 0));
218:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Since HDF5 format gives ASCII name for each object in file; must use VecLoad() after setting name of Vec with PetscObjectSetName()");
219:     }
220:     PetscCall(VecLoad_HDF5(newvec, viewer));
221: #endif
222: #if PetscDefined(HAVE_ADIOS)
223:   } else if (isadios) {
224:     if (!((PetscObject)newvec)->name) {
225:       PetscCall(PetscLogEventEnd(VEC_Load, viewer, 0, 0, 0));
226:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Since ADIOS format gives ASCII name for each object in file; must use VecLoad() after setting name of Vec with PetscObjectSetName()");
227:     }
228:     PetscCall(VecLoad_ADIOS(newvec, viewer));
229: #endif
230:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name);
231:   PetscFunctionReturn(PETSC_SUCCESS);
232: }

234: /*@
235:   VecFilter - Set all values in the vector with an absolute value less than or equal to the tolerance to zero

237:   Input Parameters:
238: + v   - The vector
239: - tol - The zero tolerance

241:   Output Parameter:
242: . v - The filtered vector

244:   Level: intermediate

246: .seealso: `VecCreate()`, `VecSet()`, `MatFilter()`
247: @*/
248: PetscErrorCode VecFilter(Vec v, PetscReal tol)
249: {
250:   PetscScalar *a;
251:   PetscInt     n;

253:   PetscFunctionBegin;
254:   PetscCall(VecGetLocalSize(v, &n));
255:   PetscCall(VecGetArray(v, &a));
256:   for (PetscInt i = 0; i < n; ++i) {
257:     if (PetscAbsScalar(a[i]) < tol) a[i] = 0.0;
258:   }
259:   PetscCall(VecRestoreArray(v, &a));
260:   PetscFunctionReturn(PETSC_SUCCESS);
261: }