xref: /petsc/src/sys/classes/viewer/impls/adios/adios.c (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
1 #include <petsc/private/viewerimpl.h> /*I   "petscsys.h"   I*/
2 #include <adios.h>
3 #include <adios_read.h>
4 
5 #include <petsc/private/vieweradiosimpl.h>
6 
7 static PetscErrorCode PetscViewerSetFromOptions_ADIOS(PetscViewer v, PetscOptionItems *PetscOptionsObject) {
8   PetscFunctionBegin;
9   PetscOptionsHeadBegin(PetscOptionsObject, "ADIOS PetscViewer Options");
10   PetscOptionsHeadEnd();
11   PetscFunctionReturn(0);
12 }
13 
14 static PetscErrorCode PetscViewerFileClose_ADIOS(PetscViewer viewer) {
15   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
16 
17   PetscFunctionBegin;
18   switch (adios->btype) {
19   case FILE_MODE_READ: PetscCall(adios_read_close(adios->adios_fp)); break;
20   case FILE_MODE_WRITE: PetscCall(adios_close(adios->adios_handle)); break;
21   default: break;
22   }
23   PetscCall(PetscFree(adios->filename));
24   PetscFunctionReturn(0);
25 }
26 
27 PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer) {
28   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
29 
30   PetscFunctionBegin;
31   PetscCall(PetscViewerFileClose_ADIOS(viewer));
32   PetscCall(PetscFree(adios));
33   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
34   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
35   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
36   PetscFunctionReturn(0);
37 }
38 
39 PetscErrorCode PetscViewerFileSetMode_ADIOS(PetscViewer viewer, PetscFileMode type) {
40   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
41 
42   PetscFunctionBegin;
43   adios->btype = type;
44   PetscFunctionReturn(0);
45 }
46 
47 PetscErrorCode PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[]) {
48   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
49 
50   PetscFunctionBegin;
51   if (adios->filename) PetscCall(PetscFree(adios->filename));
52   PetscCall(PetscStrallocpy(name, &adios->filename));
53   /* Create or open the file collectively */
54   switch (adios->btype) {
55   case FILE_MODE_READ: adios->adios_fp = adios_read_open_file(adios->filename, ADIOS_READ_METHOD_BP, PetscObjectComm((PetscObject)viewer)); break;
56   case FILE_MODE_WRITE: adios_open(&adios->adios_handle, "PETSc", adios->filename, "w", PetscObjectComm((PetscObject)viewer)); break;
57   case FILE_MODE_UNDEFINED: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
58   default: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[adios->btype]);
59   }
60   PetscFunctionReturn(0);
61 }
62 
63 static PetscErrorCode PetscViewerFileGetName_ADIOS(PetscViewer viewer, const char **name) {
64   PetscViewer_ADIOS *vadios = (PetscViewer_ADIOS *)viewer->data;
65 
66   PetscFunctionBegin;
67   *name = vadios->filename;
68   PetscFunctionReturn(0);
69 }
70 
71 /*MC
72    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file
73 
74   Level: beginner
75 
76 .seealso: `PetscViewerADIOSOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
77           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
78           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
79           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
80 M*/
81 
82 PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v) {
83   PetscViewer_ADIOS *adios;
84 
85   PetscFunctionBegin;
86   PetscCall(PetscNewLog(v, &adios));
87 
88   v->data                = (void *)adios;
89   v->ops->destroy        = PetscViewerDestroy_ADIOS;
90   v->ops->setfromoptions = PetscViewerSetFromOptions_ADIOS;
91   v->ops->flush          = NULL;
92   adios->btype           = FILE_MODE_UNDEFINED;
93   adios->filename        = NULL;
94   adios->timestep        = -1;
95 
96   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_ADIOS));
97   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_ADIOS));
98   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_ADIOS));
99   PetscFunctionReturn(0);
100 }
101 
102 /*@C
103    PetscViewerADIOSOpen - Opens a file for ADIOS input/output.
104 
105    Collective
106 
107    Input Parameters:
108 +  comm - MPI communicator
109 .  name - name of file
110 -  type - type of file
111 $    FILE_MODE_WRITE - create new file for binary output
112 $    FILE_MODE_READ - open existing file for binary input
113 $    FILE_MODE_APPEND - open existing file for binary output
114 
115    Output Parameter:
116 .  adiosv - `PetscViewer` for ADIOS input/output to use with the specified file
117 
118    Level: beginner
119 
120    Note:
121    This PetscViewer should be destroyed with `PetscViewerDestroy()`.
122 
123 .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerHDF5Open()`,
124           `VecView()`, `MatView()`, `VecLoad()`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
125           `MatLoad()`, `PetscFileMode`, `PetscViewer`
126 @*/
127 PetscErrorCode PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv) {
128   PetscFunctionBegin;
129   PetscCall(PetscViewerCreate(comm, adiosv));
130   PetscCall(PetscViewerSetType(*adiosv, PETSCVIEWERADIOS));
131   PetscCall(PetscViewerFileSetMode(*adiosv, type));
132   PetscCall(PetscViewerFileSetName(*adiosv, name));
133   PetscFunctionReturn(0);
134 }
135 
136 /*@C
137   PetscDataTypeToADIOSDataType - Converts the PETSc name of a datatype to its ADIOS name.
138 
139   Not collective
140 
141   Input Parameter:
142 . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
143 
144   Output Parameter:
145 . mtype - the ADIOS datatype (for example MPI_DOUBLE, ...)
146 
147   Level: advanced
148 
149 .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
150 @*/
151 PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype) {
152   PetscFunctionBegin;
153   if (ptype == PETSC_INT)
154 #if defined(PETSC_USE_64BIT_INDICES)
155     *htype = adios_long;
156 #else
157     *htype = adios_integer;
158 #endif
159   else if (ptype == PETSC_ENUM) *htype = adios_integer;
160   else if (ptype == PETSC_DOUBLE) *htype = adios_double;
161   else if (ptype == PETSC_LONG) *htype = adios_long;
162   else if (ptype == PETSC_SHORT) *htype = adios_short;
163   else if (ptype == PETSC_FLOAT) *htype = adios_real;
164   else if (ptype == PETSC_CHAR) *htype = adios_string_array;
165   else if (ptype == PETSC_STRING) *htype = adios_string;
166   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
167   PetscFunctionReturn(0);
168 }
169 
170 /*@C
171   PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name
172 
173   Not collective
174 
175   Input Parameter:
176 . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)
177 
178   Output Parameter:
179 . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
180 
181   Level: advanced
182 
183 .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
184 @*/
185 PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype) {
186   PetscFunctionBegin;
187 #if defined(PETSC_USE_64BIT_INDICES)
188   if (htype == adios_integer) *ptype = PETSC_ENUM;
189   else if (htype == adios_long) *ptype = PETSC_INT;
190 #else
191   if (htype == adios_integer) *ptype = PETSC_INT;
192 #endif
193   else if (htype == adios_double) *ptype = PETSC_DOUBLE;
194   else if (htype == adios_long) *ptype = PETSC_LONG;
195   else if (htype == adios_short) *ptype = PETSC_SHORT;
196   else if (htype == adios_real) *ptype = PETSC_FLOAT;
197   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
198   else if (htype == adios_string) *ptype = PETSC_STRING;
199   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
200   PetscFunctionReturn(0);
201 }
202