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


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

102:   Level: beginner
103: M*/

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

111:   PetscNewLog(v,&adios);

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

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

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

130:    Collective

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

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

143:    Level: beginner

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


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

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

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

168:   Not collective

170:   Input Parameter:
171: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

173:   Output Parameter:
174: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)

176:   Level: advanced

178:   Developer Notes: These have not been verified

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

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

205:   Not collective

207:   Input Parameter:
208: . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)

210:   Output Parameter:
211: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

213:   Level: advanced

215:   Developer Notes: These have not been verified

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