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