Actual source code: adios.c

  1: #include <petsc/private/viewerimpl.h>
  2: #include <adios.h>
  3: #include <adios_read.h>

  5: #include <petsc/private/vieweradiosimpl.h>

  7: static PetscErrorCode PetscViewerSetFromOptions_ADIOS(PetscOptionItems *PetscOptionsObject,PetscViewer v)
  8: {

 12:   PetscOptionsHead(PetscOptionsObject,"ADIOS PetscViewer Options");
 13:   PetscOptionsTail();
 14:   return(0);
 15: }

 17: static PetscErrorCode PetscViewerFileClose_ADIOS(PetscViewer viewer)
 18: {
 19:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*)viewer->data;
 20:   PetscErrorCode    ierr;

 23:   switch (adios->btype) {
 24:   case FILE_MODE_READ:
 25:     adios_read_close(adios->adios_fp);
 26:     break;
 27:   case FILE_MODE_WRITE:
 28:      adios_close(adios->adios_handle);
 29:     break;
 30:   default:
 31:     break;
 32:   }
 33:   PetscFree(adios->filename);
 34:   return(0);
 35: }

 37: PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer)
 38: {
 39:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;
 40:   PetscErrorCode    ierr;

 43:   PetscViewerFileClose_ADIOS(viewer);
 44:   PetscFree(adios);
 45:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
 46:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL);
 47:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);
 48:   return(0);
 49: }

 51: PetscErrorCode  PetscViewerFileSetMode_ADIOS(PetscViewer viewer, PetscFileMode type)
 52: {
 53:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;

 56:   adios->btype = type;
 57:   return(0);
 58: }

 60: PetscErrorCode  PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[])
 61: {
 62:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;
 63:   PetscErrorCode    ierr;

 66:   if (adios->filename) {PetscFree(adios->filename);}
 67:   PetscStrallocpy(name, &adios->filename);
 68:   /* Create or open the file collectively */
 69:   switch (adios->btype) {
 70:   case FILE_MODE_READ:
 71:     adios->adios_fp = adios_read_open_file(adios->filename,ADIOS_READ_METHOD_BP,PetscObjectComm((PetscObject)viewer));
 72:     break;
 73:   case FILE_MODE_WRITE:
 74:     adios_open(&adios->adios_handle,"PETSc",adios->filename,"w",PetscObjectComm((PetscObject)viewer));
 75:     break;
 76:   case FILE_MODE_UNDEFINED:
 77:     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
 78:   default:
 79:     SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[adios->btype]);
 80:   }
 81:   return(0);
 82: }

 84: static PetscErrorCode PetscViewerFileGetName_ADIOS(PetscViewer viewer,const char **name)
 85: {
 86:   PetscViewer_ADIOS *vadios = (PetscViewer_ADIOS*)viewer->data;

 89:   *name = vadios->filename;
 90:   return(0);
 91: }

 93: /*MC
 94:    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file

 96: .seealso:  PetscViewerADIOSOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
 97:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
 98:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
 99:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

101:   Level: beginner
102: M*/

104: PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v)
105: {
106:   PetscViewer_ADIOS *adios;
107:   PetscErrorCode    ierr;

110:   PetscNewLog(v,&adios);

112:   v->data                = (void*) adios;
113:   v->ops->destroy        = PetscViewerDestroy_ADIOS;
114:   v->ops->setfromoptions = PetscViewerSetFromOptions_ADIOS;
115:   v->ops->flush          = NULL;
116:   adios->btype           = FILE_MODE_UNDEFINED;
117:   adios->filename        = NULL;
118:   adios->timestep        = -1;

120:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_ADIOS);
121:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_ADIOS);
122:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ADIOS);
123:   return(0);
124: }

126: /*@C
127:    PetscViewerADIOSOpen - Opens a file for ADIOS input/output.

129:    Collective

131:    Input Parameters:
132: +  comm - MPI communicator
133: .  name - name of file
134: -  type - type of file
135: $    FILE_MODE_WRITE - create new file for binary output
136: $    FILE_MODE_READ - open existing file for binary input
137: $    FILE_MODE_APPEND - open existing file for binary output

139:    Output Parameter:
140: .  adiosv - PetscViewer for ADIOS input/output to use with the specified file

142:    Level: beginner

144:    Note:
145:    This PetscViewer should be destroyed with PetscViewerDestroy().

147: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), PetscViewerHDF5Open(),
148:           VecView(), MatView(), VecLoad(), PetscViewerSetType(), PetscViewerFileSetMode(), PetscViewerFileSetName()
149:           MatLoad(), PetscFileMode, PetscViewer
150: @*/
151: PetscErrorCode  PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv)
152: {

156:   PetscViewerCreate(comm, adiosv);
157:   PetscViewerSetType(*adiosv, PETSCVIEWERADIOS);
158:   PetscViewerFileSetMode(*adiosv, type);
159:   PetscViewerFileSetName(*adiosv, name);
160:   return(0);
161: }

163: /*@C
164:   PetscDataTypeToADIOSDataType - Converts the PETSc name of a datatype to its ADIOS name.

166:   Not collective

168:   Input Parameter:
169: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

171:   Output Parameter:
172: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)

174:   Level: advanced

176:   Developer Notes: These have not been verified

178: .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
179: @*/
180: PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype)
181: {
183:   if (ptype == PETSC_INT)
184: #if defined(PETSC_USE_64BIT_INDICES)
185:                                        *htype = adios_long;
186: #else
187:                                        *htype = adios_integer;
188: #endif
189:   else if (ptype == PETSC_ENUM)        *htype = adios_integer;
190:   else if (ptype == PETSC_DOUBLE)      *htype = adios_double;
191:   else if (ptype == PETSC_LONG)        *htype = adios_long;
192:   else if (ptype == PETSC_SHORT)       *htype = adios_short;
193:   else if (ptype == PETSC_FLOAT)       *htype = adios_real;
194:   else if (ptype == PETSC_CHAR)        *htype = adios_string_array;
195:   else if (ptype == PETSC_STRING)      *htype = adios_string;
196:   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
197:   return(0);
198: }

200: /*@C
201:   PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name

203:   Not collective

205:   Input Parameter:
206: . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)

208:   Output Parameter:
209: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

211:   Level: advanced

213:   Developer Notes: These have not been verified

215: .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
216: @*/
217: PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype)
218: {
220: #if defined(PETSC_USE_64BIT_INDICES)
221:   if      (htype == adios_integer)     *ptype = PETSC_ENUM;
222:   else if (htype == adios_long)        *ptype = PETSC_INT;
223: #else
224:   if      (htype == adios_integer)     *ptype = PETSC_INT;
225: #endif
226:   else if (htype == adios_double)      *ptype = PETSC_DOUBLE;
227:   else if (htype == adios_long)        *ptype = PETSC_LONG;
228:   else if (htype == adios_short)       *ptype = PETSC_SHORT;
229:   else if (htype == adios_real)        *ptype = PETSC_FLOAT;
230:   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
231:   else if (htype == adios_string)       *ptype = PETSC_STRING;
232:   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
233:   return(0);
234: }