xref: /petsc/src/sys/classes/viewer/impls/adios/adios.c (revision d0609ced746bc51b019815ca91d747429db24893)
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 
74c02969dSBarry Smith static PetscErrorCode PetscViewerSetFromOptions_ADIOS(PetscOptionItems *PetscOptionsObject,PetscViewer v)
84c02969dSBarry Smith {
94c02969dSBarry Smith   PetscFunctionBegin;
10*d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject,"ADIOS PetscViewer Options");
11*d0609cedSBarry Smith   PetscOptionsHeadEnd();
124c02969dSBarry Smith   PetscFunctionReturn(0);
134c02969dSBarry Smith }
144c02969dSBarry Smith 
154c02969dSBarry Smith static PetscErrorCode PetscViewerFileClose_ADIOS(PetscViewer viewer)
164c02969dSBarry Smith {
174c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*)viewer->data;
184c02969dSBarry Smith 
194c02969dSBarry Smith   PetscFunctionBegin;
2022580e64SBarry Smith   switch (adios->btype) {
2122580e64SBarry Smith   case FILE_MODE_READ:
229566063dSJacob Faibussowitsch     PetscCall(adios_read_close(adios->adios_fp));
2322580e64SBarry Smith     break;
2422580e64SBarry Smith   case FILE_MODE_WRITE:
259566063dSJacob Faibussowitsch      PetscCall(adios_close(adios->adios_handle));
2622580e64SBarry Smith     break;
2722580e64SBarry Smith   default:
2822580e64SBarry Smith     break;
2922580e64SBarry Smith   }
309566063dSJacob Faibussowitsch   PetscCall(PetscFree(adios->filename));
314c02969dSBarry Smith   PetscFunctionReturn(0);
324c02969dSBarry Smith }
334c02969dSBarry Smith 
344c02969dSBarry Smith PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer)
354c02969dSBarry Smith {
364c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;
374c02969dSBarry Smith 
384c02969dSBarry Smith   PetscFunctionBegin;
399566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_ADIOS(viewer));
409566063dSJacob Faibussowitsch   PetscCall(PetscFree(adios));
419566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL));
429566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL));
439566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL));
444c02969dSBarry Smith   PetscFunctionReturn(0);
454c02969dSBarry Smith }
464c02969dSBarry Smith 
474c02969dSBarry Smith PetscErrorCode  PetscViewerFileSetMode_ADIOS(PetscViewer viewer, PetscFileMode type)
484c02969dSBarry Smith {
494c02969dSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;
504c02969dSBarry Smith 
514c02969dSBarry Smith   PetscFunctionBegin;
524c02969dSBarry Smith   adios->btype = type;
534c02969dSBarry Smith   PetscFunctionReturn(0);
544c02969dSBarry Smith }
554c02969dSBarry Smith 
564c02969dSBarry Smith PetscErrorCode  PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[])
574c02969dSBarry Smith {
58a56f64adSBarry Smith   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;
59a56f64adSBarry Smith 
604c02969dSBarry Smith   PetscFunctionBegin;
619566063dSJacob Faibussowitsch   if (adios->filename) PetscCall(PetscFree(adios->filename));
629566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &adios->filename));
63a56f64adSBarry Smith   /* Create or open the file collectively */
64a56f64adSBarry Smith   switch (adios->btype) {
65a56f64adSBarry Smith   case FILE_MODE_READ:
6622580e64SBarry Smith     adios->adios_fp = adios_read_open_file(adios->filename,ADIOS_READ_METHOD_BP,PetscObjectComm((PetscObject)viewer));
67a56f64adSBarry Smith     break;
68a56f64adSBarry Smith   case FILE_MODE_WRITE:
69a56f64adSBarry Smith     adios_open(&adios->adios_handle,"PETSc",adios->filename,"w",PetscObjectComm((PetscObject)viewer));
70a56f64adSBarry Smith     break;
717e4fd573SVaclav Hapla   case FILE_MODE_UNDEFINED:
727e4fd573SVaclav Hapla     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
73a56f64adSBarry Smith   default:
7498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[adios->btype]);
75a56f64adSBarry Smith   }
764c02969dSBarry Smith   PetscFunctionReturn(0);
774c02969dSBarry Smith }
784c02969dSBarry Smith 
794c02969dSBarry Smith static PetscErrorCode PetscViewerFileGetName_ADIOS(PetscViewer viewer,const char **name)
804c02969dSBarry Smith {
814c02969dSBarry Smith   PetscViewer_ADIOS *vadios = (PetscViewer_ADIOS*)viewer->data;
824c02969dSBarry Smith 
834c02969dSBarry Smith   PetscFunctionBegin;
844c02969dSBarry Smith   *name = vadios->filename;
854c02969dSBarry Smith   PetscFunctionReturn(0);
864c02969dSBarry Smith }
874c02969dSBarry Smith 
884c02969dSBarry Smith /*MC
894c02969dSBarry Smith    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file
904c02969dSBarry Smith 
914c02969dSBarry Smith .seealso:  PetscViewerADIOSOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
924c02969dSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
934c02969dSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
944c02969dSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
954c02969dSBarry Smith 
964c02969dSBarry Smith   Level: beginner
974c02969dSBarry Smith M*/
984c02969dSBarry Smith 
994c02969dSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v)
1004c02969dSBarry Smith {
1014c02969dSBarry Smith   PetscViewer_ADIOS *adios;
1024c02969dSBarry Smith 
1034c02969dSBarry Smith   PetscFunctionBegin;
1049566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(v,&adios));
1054c02969dSBarry Smith 
1064c02969dSBarry Smith   v->data                = (void*) adios;
1074c02969dSBarry Smith   v->ops->destroy        = PetscViewerDestroy_ADIOS;
1084c02969dSBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_ADIOS;
1097e4fd573SVaclav Hapla   v->ops->flush          = NULL;
1107e4fd573SVaclav Hapla   adios->btype           = FILE_MODE_UNDEFINED;
1117e4fd573SVaclav Hapla   adios->filename        = NULL;
1124c02969dSBarry Smith   adios->timestep        = -1;
1134c02969dSBarry Smith 
1149566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_ADIOS));
1159566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_ADIOS));
1169566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ADIOS));
1174c02969dSBarry Smith   PetscFunctionReturn(0);
1184c02969dSBarry Smith }
1194c02969dSBarry Smith 
1204c02969dSBarry Smith /*@C
1214c02969dSBarry Smith    PetscViewerADIOSOpen - Opens a file for ADIOS input/output.
1224c02969dSBarry Smith 
123d083f849SBarry Smith    Collective
1244c02969dSBarry Smith 
1254c02969dSBarry Smith    Input Parameters:
1264c02969dSBarry Smith +  comm - MPI communicator
1274c02969dSBarry Smith .  name - name of file
1284c02969dSBarry Smith -  type - type of file
1294c02969dSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
1304c02969dSBarry Smith $    FILE_MODE_READ - open existing file for binary input
1314c02969dSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
1324c02969dSBarry Smith 
1334c02969dSBarry Smith    Output Parameter:
1344c02969dSBarry Smith .  adiosv - PetscViewer for ADIOS input/output to use with the specified file
1354c02969dSBarry Smith 
1364c02969dSBarry Smith    Level: beginner
1374c02969dSBarry Smith 
1384c02969dSBarry Smith    Note:
1394c02969dSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
1404c02969dSBarry Smith 
141a56f64adSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), PetscViewerHDF5Open(),
142a56f64adSBarry Smith           VecView(), MatView(), VecLoad(), PetscViewerSetType(), PetscViewerFileSetMode(), PetscViewerFileSetName()
1434c02969dSBarry Smith           MatLoad(), PetscFileMode, PetscViewer
1444c02969dSBarry Smith @*/
1454c02969dSBarry Smith PetscErrorCode  PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv)
1464c02969dSBarry Smith {
1474c02969dSBarry Smith   PetscFunctionBegin;
1489566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, adiosv));
1499566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*adiosv, PETSCVIEWERADIOS));
1509566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*adiosv, type));
1519566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*adiosv, name));
1524c02969dSBarry Smith   PetscFunctionReturn(0);
1534c02969dSBarry Smith }
1544c02969dSBarry Smith 
1554c02969dSBarry Smith /*@C
1564c02969dSBarry Smith   PetscDataTypeToADIOSDataType - Converts the PETSc name of a datatype to its ADIOS name.
1574c02969dSBarry Smith 
1584c02969dSBarry Smith   Not collective
1594c02969dSBarry Smith 
1604c02969dSBarry Smith   Input Parameter:
1614c02969dSBarry Smith . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
1624c02969dSBarry Smith 
1634c02969dSBarry Smith   Output Parameter:
1644c02969dSBarry Smith . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
1654c02969dSBarry Smith 
1664c02969dSBarry Smith   Level: advanced
1674c02969dSBarry Smith 
1684c02969dSBarry Smith   Developer Notes: These have not been verified
1694c02969dSBarry Smith 
1704c02969dSBarry Smith .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
1714c02969dSBarry Smith @*/
1724c02969dSBarry Smith PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype)
1734c02969dSBarry Smith {
1744c02969dSBarry Smith   PetscFunctionBegin;
1754c02969dSBarry Smith   if (ptype == PETSC_INT)
1764c02969dSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
1774c02969dSBarry Smith                                        *htype = adios_long;
1784c02969dSBarry Smith #else
1794c02969dSBarry Smith                                        *htype = adios_integer;
1804c02969dSBarry Smith #endif
1814c02969dSBarry Smith   else if (ptype == PETSC_ENUM)        *htype = adios_integer;
1824c02969dSBarry Smith   else if (ptype == PETSC_DOUBLE)      *htype = adios_double;
1834c02969dSBarry Smith   else if (ptype == PETSC_LONG)        *htype = adios_long;
1844c02969dSBarry Smith   else if (ptype == PETSC_SHORT)       *htype = adios_short;
1854c02969dSBarry Smith   else if (ptype == PETSC_FLOAT)       *htype = adios_real;
1864c02969dSBarry Smith   else if (ptype == PETSC_CHAR)        *htype = adios_string_array;
1874c02969dSBarry Smith   else if (ptype == PETSC_STRING)      *htype = adios_string;
1884c02969dSBarry Smith   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
1894c02969dSBarry Smith   PetscFunctionReturn(0);
1904c02969dSBarry Smith }
1914c02969dSBarry Smith 
1924c02969dSBarry Smith /*@C
1934c02969dSBarry Smith   PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name
1944c02969dSBarry Smith 
1954c02969dSBarry Smith   Not collective
1964c02969dSBarry Smith 
1974c02969dSBarry Smith   Input Parameter:
1984c02969dSBarry Smith . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)
1994c02969dSBarry Smith 
2004c02969dSBarry Smith   Output Parameter:
2014c02969dSBarry Smith . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
2024c02969dSBarry Smith 
2034c02969dSBarry Smith   Level: advanced
2044c02969dSBarry Smith 
2054c02969dSBarry Smith   Developer Notes: These have not been verified
2064c02969dSBarry Smith 
2074c02969dSBarry Smith .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
2084c02969dSBarry Smith @*/
2094c02969dSBarry Smith PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype)
2104c02969dSBarry Smith {
2114c02969dSBarry Smith   PetscFunctionBegin;
2124c02969dSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
2134c02969dSBarry Smith   if      (htype == adios_integer)     *ptype = PETSC_ENUM;
2144c02969dSBarry Smith   else if (htype == adios_long)        *ptype = PETSC_INT;
2154c02969dSBarry Smith #else
2164c02969dSBarry Smith   if      (htype == adios_integer)     *ptype = PETSC_INT;
2174c02969dSBarry Smith #endif
2184c02969dSBarry Smith   else if (htype == adios_double)      *ptype = PETSC_DOUBLE;
2194c02969dSBarry Smith   else if (htype == adios_long)        *ptype = PETSC_LONG;
2204c02969dSBarry Smith   else if (htype == adios_short)       *ptype = PETSC_SHORT;
2214c02969dSBarry Smith   else if (htype == adios_real)        *ptype = PETSC_FLOAT;
2224c02969dSBarry Smith   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
2234c02969dSBarry Smith   else if (htype == adios_string)       *ptype = PETSC_STRING;
2244c02969dSBarry Smith   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
2254c02969dSBarry Smith   PetscFunctionReturn(0);
2264c02969dSBarry Smith }
227