xref: /petsc/src/sys/classes/viewer/impls/adios/adios.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
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 .seealso: `PetscViewerADIOSOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
75           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
76           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
77           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
78 
79   Level: beginner
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 MPI datatype (for example MPI_DOUBLE, ...)
146 
147   Level: advanced
148 
149   Developer Notes: These have not been verified
150 
151 .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
152 @*/
153 PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype) {
154   PetscFunctionBegin;
155   if (ptype == PETSC_INT)
156 #if defined(PETSC_USE_64BIT_INDICES)
157     *htype = adios_long;
158 #else
159     *htype = adios_integer;
160 #endif
161   else if (ptype == PETSC_ENUM) *htype = adios_integer;
162   else if (ptype == PETSC_DOUBLE) *htype = adios_double;
163   else if (ptype == PETSC_LONG) *htype = adios_long;
164   else if (ptype == PETSC_SHORT) *htype = adios_short;
165   else if (ptype == PETSC_FLOAT) *htype = adios_real;
166   else if (ptype == PETSC_CHAR) *htype = adios_string_array;
167   else if (ptype == PETSC_STRING) *htype = adios_string;
168   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
169   PetscFunctionReturn(0);
170 }
171 
172 /*@C
173   PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name
174 
175   Not collective
176 
177   Input Parameter:
178 . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)
179 
180   Output Parameter:
181 . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
182 
183   Level: advanced
184 
185   Developer Notes: These have not been verified
186 
187 .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
188 @*/
189 PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype) {
190   PetscFunctionBegin;
191 #if defined(PETSC_USE_64BIT_INDICES)
192   if (htype == adios_integer) *ptype = PETSC_ENUM;
193   else if (htype == adios_long) *ptype = PETSC_INT;
194 #else
195   if (htype == adios_integer) *ptype = PETSC_INT;
196 #endif
197   else if (htype == adios_double) *ptype = PETSC_DOUBLE;
198   else if (htype == adios_long) *ptype = PETSC_LONG;
199   else if (htype == adios_short) *ptype = PETSC_SHORT;
200   else if (htype == adios_real) *ptype = PETSC_FLOAT;
201   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
202   else if (htype == adios_string) *ptype = PETSC_STRING;
203   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
204   PetscFunctionReturn(0);
205 }
206