xref: /petsc/src/sys/classes/viewer/impls/adios/adios.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
14c02969dSBarry Smith #include <petsc/private/viewerimpl.h> /*I   "petscsys.h"   I*/
24c02969dSBarry Smith #include <adios.h>
322580e64SBarry Smith #include <adios_read.h>
44c02969dSBarry Smith 
58a976931SBarry Smith #include <petsc/private/vieweradiosimpl.h>
64c02969dSBarry Smith 
79371c9d4SSatish Balay static PetscErrorCode PetscViewerSetFromOptions_ADIOS(PetscViewer v, PetscOptionItems *PetscOptionsObject) {
84c02969dSBarry Smith   PetscFunctionBegin;
9d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "ADIOS PetscViewer Options");
10d0609cedSBarry Smith   PetscOptionsHeadEnd();
114c02969dSBarry Smith   PetscFunctionReturn(0);
124c02969dSBarry Smith }
134c02969dSBarry Smith 
149371c9d4SSatish Balay static PetscErrorCode PetscViewerFileClose_ADIOS(PetscViewer viewer) {
154c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
164c02969dSBarry Smith 
174c02969dSBarry Smith   PetscFunctionBegin;
1822580e64SBarry Smith   switch (adios->btype) {
199371c9d4SSatish Balay   case FILE_MODE_READ: PetscCall(adios_read_close(adios->adios_fp)); break;
209371c9d4SSatish Balay   case FILE_MODE_WRITE: PetscCall(adios_close(adios->adios_handle)); break;
219371c9d4SSatish Balay   default: break;
2222580e64SBarry Smith   }
239566063dSJacob Faibussowitsch   PetscCall(PetscFree(adios->filename));
244c02969dSBarry Smith   PetscFunctionReturn(0);
254c02969dSBarry Smith }
264c02969dSBarry Smith 
279371c9d4SSatish Balay PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer) {
284c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
294c02969dSBarry Smith 
304c02969dSBarry Smith   PetscFunctionBegin;
319566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_ADIOS(viewer));
329566063dSJacob Faibussowitsch   PetscCall(PetscFree(adios));
339566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
349566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
359566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
364c02969dSBarry Smith   PetscFunctionReturn(0);
374c02969dSBarry Smith }
384c02969dSBarry Smith 
399371c9d4SSatish Balay PetscErrorCode PetscViewerFileSetMode_ADIOS(PetscViewer viewer, PetscFileMode type) {
404c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
414c02969dSBarry Smith 
424c02969dSBarry Smith   PetscFunctionBegin;
434c02969dSBarry Smith   adios->btype = type;
444c02969dSBarry Smith   PetscFunctionReturn(0);
454c02969dSBarry Smith }
464c02969dSBarry Smith 
479371c9d4SSatish Balay PetscErrorCode PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[]) {
48a56f64adSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
49a56f64adSBarry Smith 
504c02969dSBarry Smith   PetscFunctionBegin;
519566063dSJacob Faibussowitsch   if (adios->filename) PetscCall(PetscFree(adios->filename));
529566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &adios->filename));
53a56f64adSBarry Smith   /* Create or open the file collectively */
54a56f64adSBarry Smith   switch (adios->btype) {
559371c9d4SSatish Balay   case FILE_MODE_READ: adios->adios_fp = adios_read_open_file(adios->filename, ADIOS_READ_METHOD_BP, PetscObjectComm((PetscObject)viewer)); break;
569371c9d4SSatish Balay   case FILE_MODE_WRITE: adios_open(&adios->adios_handle, "PETSc", adios->filename, "w", PetscObjectComm((PetscObject)viewer)); break;
579371c9d4SSatish Balay   case FILE_MODE_UNDEFINED: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
589371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[adios->btype]);
59a56f64adSBarry Smith   }
604c02969dSBarry Smith   PetscFunctionReturn(0);
614c02969dSBarry Smith }
624c02969dSBarry Smith 
639371c9d4SSatish Balay static PetscErrorCode PetscViewerFileGetName_ADIOS(PetscViewer viewer, const char **name) {
644c02969dSBarry Smith   PetscViewer_ADIOS *vadios = (PetscViewer_ADIOS *)viewer->data;
654c02969dSBarry Smith 
664c02969dSBarry Smith   PetscFunctionBegin;
674c02969dSBarry Smith   *name = vadios->filename;
684c02969dSBarry Smith   PetscFunctionReturn(0);
694c02969dSBarry Smith }
704c02969dSBarry Smith 
714c02969dSBarry Smith /*MC
724c02969dSBarry Smith    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file
734c02969dSBarry Smith 
74811af0c4SBarry Smith   Level: beginner
75811af0c4SBarry Smith 
76db781477SPatrick Sanan .seealso: `PetscViewerADIOSOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
77db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
78db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
79db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
804c02969dSBarry Smith M*/
814c02969dSBarry Smith 
829371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v) {
834c02969dSBarry Smith   PetscViewer_ADIOS *adios;
844c02969dSBarry Smith 
854c02969dSBarry Smith   PetscFunctionBegin;
86*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&adios));
874c02969dSBarry Smith 
884c02969dSBarry Smith   v->data                = (void *)adios;
894c02969dSBarry Smith   v->ops->destroy        = PetscViewerDestroy_ADIOS;
904c02969dSBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_ADIOS;
917e4fd573SVaclav Hapla   v->ops->flush          = NULL;
927e4fd573SVaclav Hapla   adios->btype           = FILE_MODE_UNDEFINED;
937e4fd573SVaclav Hapla   adios->filename        = NULL;
944c02969dSBarry Smith   adios->timestep        = -1;
954c02969dSBarry Smith 
969566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_ADIOS));
979566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_ADIOS));
989566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_ADIOS));
994c02969dSBarry Smith   PetscFunctionReturn(0);
1004c02969dSBarry Smith }
1014c02969dSBarry Smith 
1024c02969dSBarry Smith /*@C
1034c02969dSBarry Smith    PetscViewerADIOSOpen - Opens a file for ADIOS input/output.
1044c02969dSBarry Smith 
105d083f849SBarry Smith    Collective
1064c02969dSBarry Smith 
1074c02969dSBarry Smith    Input Parameters:
1084c02969dSBarry Smith +  comm - MPI communicator
1094c02969dSBarry Smith .  name - name of file
1104c02969dSBarry Smith -  type - type of file
1114c02969dSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
1124c02969dSBarry Smith $    FILE_MODE_READ - open existing file for binary input
1134c02969dSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
1144c02969dSBarry Smith 
1154c02969dSBarry Smith    Output Parameter:
116811af0c4SBarry Smith .  adiosv - `PetscViewer` for ADIOS input/output to use with the specified file
1174c02969dSBarry Smith 
1184c02969dSBarry Smith    Level: beginner
1194c02969dSBarry Smith 
1204c02969dSBarry Smith    Note:
121811af0c4SBarry Smith    This PetscViewer should be destroyed with `PetscViewerDestroy()`.
1224c02969dSBarry Smith 
123db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerHDF5Open()`,
124db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
125db781477SPatrick Sanan           `MatLoad()`, `PetscFileMode`, `PetscViewer`
1264c02969dSBarry Smith @*/
1279371c9d4SSatish Balay PetscErrorCode PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv) {
1284c02969dSBarry Smith   PetscFunctionBegin;
1299566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, adiosv));
1309566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*adiosv, PETSCVIEWERADIOS));
1319566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*adiosv, type));
1329566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*adiosv, name));
1334c02969dSBarry Smith   PetscFunctionReturn(0);
1344c02969dSBarry Smith }
1354c02969dSBarry Smith 
1364c02969dSBarry Smith /*@C
1374c02969dSBarry Smith   PetscDataTypeToADIOSDataType - Converts the PETSc name of a datatype to its ADIOS name.
1384c02969dSBarry Smith 
1394c02969dSBarry Smith   Not collective
1404c02969dSBarry Smith 
1414c02969dSBarry Smith   Input Parameter:
142811af0c4SBarry Smith . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
1434c02969dSBarry Smith 
1444c02969dSBarry Smith   Output Parameter:
145811af0c4SBarry Smith . mtype - the ADIOS datatype (for example MPI_DOUBLE, ...)
1464c02969dSBarry Smith 
1474c02969dSBarry Smith   Level: advanced
1484c02969dSBarry Smith 
149db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
1504c02969dSBarry Smith @*/
1519371c9d4SSatish Balay PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype) {
1524c02969dSBarry Smith   PetscFunctionBegin;
1534c02969dSBarry Smith   if (ptype == PETSC_INT)
1544c02969dSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
1554c02969dSBarry Smith     *htype = adios_long;
1564c02969dSBarry Smith #else
1574c02969dSBarry Smith     *htype = adios_integer;
1584c02969dSBarry Smith #endif
1594c02969dSBarry Smith   else if (ptype == PETSC_ENUM) *htype = adios_integer;
1604c02969dSBarry Smith   else if (ptype == PETSC_DOUBLE) *htype = adios_double;
1614c02969dSBarry Smith   else if (ptype == PETSC_LONG) *htype = adios_long;
1624c02969dSBarry Smith   else if (ptype == PETSC_SHORT) *htype = adios_short;
1634c02969dSBarry Smith   else if (ptype == PETSC_FLOAT) *htype = adios_real;
1644c02969dSBarry Smith   else if (ptype == PETSC_CHAR) *htype = adios_string_array;
1654c02969dSBarry Smith   else if (ptype == PETSC_STRING) *htype = adios_string;
1664c02969dSBarry Smith   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
1674c02969dSBarry Smith   PetscFunctionReturn(0);
1684c02969dSBarry Smith }
1694c02969dSBarry Smith 
1704c02969dSBarry Smith /*@C
1714c02969dSBarry Smith   PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name
1724c02969dSBarry Smith 
1734c02969dSBarry Smith   Not collective
1744c02969dSBarry Smith 
1754c02969dSBarry Smith   Input Parameter:
1764c02969dSBarry Smith . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)
1774c02969dSBarry Smith 
1784c02969dSBarry Smith   Output Parameter:
179811af0c4SBarry Smith . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
1804c02969dSBarry Smith 
1814c02969dSBarry Smith   Level: advanced
1824c02969dSBarry Smith 
183db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
1844c02969dSBarry Smith @*/
1859371c9d4SSatish Balay PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype) {
1864c02969dSBarry Smith   PetscFunctionBegin;
1874c02969dSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
1884c02969dSBarry Smith   if (htype == adios_integer) *ptype = PETSC_ENUM;
1894c02969dSBarry Smith   else if (htype == adios_long) *ptype = PETSC_INT;
1904c02969dSBarry Smith #else
1914c02969dSBarry Smith   if (htype == adios_integer) *ptype = PETSC_INT;
1924c02969dSBarry Smith #endif
1934c02969dSBarry Smith   else if (htype == adios_double) *ptype = PETSC_DOUBLE;
1944c02969dSBarry Smith   else if (htype == adios_long) *ptype = PETSC_LONG;
1954c02969dSBarry Smith   else if (htype == adios_short) *ptype = PETSC_SHORT;
1964c02969dSBarry Smith   else if (htype == adios_real) *ptype = PETSC_FLOAT;
1974c02969dSBarry Smith   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
1984c02969dSBarry Smith   else if (htype == adios_string) *ptype = PETSC_STRING;
1994c02969dSBarry Smith   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
2004c02969dSBarry Smith   PetscFunctionReturn(0);
2014c02969dSBarry Smith }
202