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(PetscOptionItems *PetscOptionsObject,PetscViewer v) 8 { 9 PetscFunctionBegin; 10 CHKERRQ(PetscOptionsHead(PetscOptionsObject,"ADIOS PetscViewer Options")); 11 CHKERRQ(PetscOptionsTail()); 12 PetscFunctionReturn(0); 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 CHKERRQ(adios_read_close(adios->adios_fp)); 23 break; 24 case FILE_MODE_WRITE: 25 CHKERRQ(adios_close(adios->adios_handle)); 26 break; 27 default: 28 break; 29 } 30 CHKERRQ(PetscFree(adios->filename)); 31 PetscFunctionReturn(0); 32 } 33 34 PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer) 35 { 36 PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data; 37 38 PetscFunctionBegin; 39 CHKERRQ(PetscViewerFileClose_ADIOS(viewer)); 40 CHKERRQ(PetscFree(adios)); 41 CHKERRQ(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL)); 42 CHKERRQ(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL)); 43 CHKERRQ(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL)); 44 PetscFunctionReturn(0); 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(0); 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) CHKERRQ(PetscFree(adios->filename)); 62 CHKERRQ(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(0); 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(0); 86 } 87 88 /*MC 89 PETSCVIEWERADIOS - A viewer that writes to an ADIOS file 90 91 .seealso: PetscViewerADIOSOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET, 92 PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING, 93 PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, 94 PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType() 95 96 Level: beginner 97 M*/ 98 99 PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v) 100 { 101 PetscViewer_ADIOS *adios; 102 103 PetscFunctionBegin; 104 CHKERRQ(PetscNewLog(v,&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 CHKERRQ(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_ADIOS)); 115 CHKERRQ(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_ADIOS)); 116 CHKERRQ(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ADIOS)); 117 PetscFunctionReturn(0); 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 CHKERRQ(PetscViewerCreate(comm, adiosv)); 149 CHKERRQ(PetscViewerSetType(*adiosv, PETSCVIEWERADIOS)); 150 CHKERRQ(PetscViewerFileSetMode(*adiosv, type)); 151 CHKERRQ(PetscViewerFileSetName(*adiosv, name)); 152 PetscFunctionReturn(0); 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 MPI datatype (for example MPI_DOUBLE, ...) 165 166 Level: advanced 167 168 Developer Notes: These have not been verified 169 170 .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType() 171 @*/ 172 PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype) 173 { 174 PetscFunctionBegin; 175 if (ptype == PETSC_INT) 176 #if defined(PETSC_USE_64BIT_INDICES) 177 *htype = adios_long; 178 #else 179 *htype = adios_integer; 180 #endif 181 else if (ptype == PETSC_ENUM) *htype = adios_integer; 182 else if (ptype == PETSC_DOUBLE) *htype = adios_double; 183 else if (ptype == PETSC_LONG) *htype = adios_long; 184 else if (ptype == PETSC_SHORT) *htype = adios_short; 185 else if (ptype == PETSC_FLOAT) *htype = adios_real; 186 else if (ptype == PETSC_CHAR) *htype = adios_string_array; 187 else if (ptype == PETSC_STRING) *htype = adios_string; 188 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype"); 189 PetscFunctionReturn(0); 190 } 191 192 /*@C 193 PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name 194 195 Not collective 196 197 Input Parameter: 198 . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...) 199 200 Output Parameter: 201 . ptype - the PETSc datatype name (for example PETSC_DOUBLE) 202 203 Level: advanced 204 205 Developer Notes: These have not been verified 206 207 .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType() 208 @*/ 209 PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype) 210 { 211 PetscFunctionBegin; 212 #if defined(PETSC_USE_64BIT_INDICES) 213 if (htype == adios_integer) *ptype = PETSC_ENUM; 214 else if (htype == adios_long) *ptype = PETSC_INT; 215 #else 216 if (htype == adios_integer) *ptype = PETSC_INT; 217 #endif 218 else if (htype == adios_double) *ptype = PETSC_DOUBLE; 219 else if (htype == adios_long) *ptype = PETSC_LONG; 220 else if (htype == adios_short) *ptype = PETSC_SHORT; 221 else if (htype == adios_real) *ptype = PETSC_FLOAT; 222 else if (htype == adios_string_array) *ptype = PETSC_CHAR; 223 else if (htype == adios_string) *ptype = PETSC_STRING; 224 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype"); 225 PetscFunctionReturn(0); 226 } 227