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: {
      9:   PetscOptionsHead(PetscOptionsObject,"ADIOS PetscViewer Options");
     10:   PetscOptionsTail();
     11:   return 0;
     12: }

     14: static PetscErrorCode PetscViewerFileClose_ADIOS(PetscViewer viewer)
     15: {
     16:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*)viewer->data;

     18:   switch (adios->btype) {
     19:   case FILE_MODE_READ:
     20:     adios_read_close(adios->adios_fp);
     21:     break;
     22:   case FILE_MODE_WRITE:
     23:      adios_close(adios->adios_handle);
     24:     break;
     25:   default:
     26:     break;
     27:   }
     28:   PetscFree(adios->filename);
     29:   return 0;
     30: }

     32: PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer)
     33: {
     34:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;

     36:   PetscViewerFileClose_ADIOS(viewer);
     37:   PetscFree(adios);
     38:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
     39:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL);
     40:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);
     41:   return 0;
     42: }

     44: PetscErrorCode  PetscViewerFileSetMode_ADIOS(PetscViewer viewer, PetscFileMode type)
     45: {
     46:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;

     48:   adios->btype = type;
     49:   return 0;
     50: }

     52: PetscErrorCode  PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[])
     53: {
     54:   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS*) viewer->data;

     56:   if (adios->filename) PetscFree(adios->filename);
     57:   PetscStrallocpy(name, &adios->filename);
     58:   /* Create or open the file collectively */
     59:   switch (adios->btype) {
     60:   case FILE_MODE_READ:
     61:     adios->adios_fp = adios_read_open_file(adios->filename,ADIOS_READ_METHOD_BP,PetscObjectComm((PetscObject)viewer));
     62:     break;
     63:   case FILE_MODE_WRITE:
     64:     adios_open(&adios->adios_handle,"PETSc",adios->filename,"w",PetscObjectComm((PetscObject)viewer));
     65:     break;
     66:   case FILE_MODE_UNDEFINED:
     67:     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
     68:   default:
     69:     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[adios->btype]);
     70:   }
     71:   return 0;
     72: }

     74: static PetscErrorCode PetscViewerFileGetName_ADIOS(PetscViewer viewer,const char **name)
     75: {
     76:   PetscViewer_ADIOS *vadios = (PetscViewer_ADIOS*)viewer->data;

     78:   *name = vadios->filename;
     79:   return 0;
     80: }

     82: /*MC
     83:    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file

     85: .seealso:  PetscViewerADIOSOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
     86:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
     87:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
     88:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

     90:   Level: beginner
     91: M*/

     93: PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v)
     94: {
     95:   PetscViewer_ADIOS *adios;

     97:   PetscNewLog(v,&adios);

     99:   v->data                = (void*) adios;
    100:   v->ops->destroy        = PetscViewerDestroy_ADIOS;
    101:   v->ops->setfromoptions = PetscViewerSetFromOptions_ADIOS;
    102:   v->ops->flush          = NULL;
    103:   adios->btype           = FILE_MODE_UNDEFINED;
    104:   adios->filename        = NULL;
    105:   adios->timestep        = -1;

    107:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_ADIOS);
    108:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_ADIOS);
    109:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ADIOS);
    110:   return 0;
    111: }

    113: /*@C
    114:    PetscViewerADIOSOpen - Opens a file for ADIOS input/output.

    116:    Collective

    118:    Input Parameters:
    119: +  comm - MPI communicator
    120: .  name - name of file
    121: -  type - type of file
    122: $    FILE_MODE_WRITE - create new file for binary output
    123: $    FILE_MODE_READ - open existing file for binary input
    124: $    FILE_MODE_APPEND - open existing file for binary output

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

    129:    Level: beginner

    131:    Note:
    132:    This PetscViewer should be destroyed with PetscViewerDestroy().

    134: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), PetscViewerHDF5Open(),
    135:           VecView(), MatView(), VecLoad(), PetscViewerSetType(), PetscViewerFileSetMode(), PetscViewerFileSetName()
    136:           MatLoad(), PetscFileMode, PetscViewer
    137: @*/
    138: PetscErrorCode  PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv)
    139: {
    140:   PetscViewerCreate(comm, adiosv);
    141:   PetscViewerSetType(*adiosv, PETSCVIEWERADIOS);
    142:   PetscViewerFileSetMode(*adiosv, type);
    143:   PetscViewerFileSetName(*adiosv, name);
    144:   return 0;
    145: }

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

    150:   Not collective

    152:   Input Parameter:
    153: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

    155:   Output Parameter:
    156: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)

    158:   Level: advanced

    160:   Developer Notes: These have not been verified

    162: .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
    163: @*/
    164: PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype)
    165: {
    166:   if (ptype == PETSC_INT)
    167: #if defined(PETSC_USE_64BIT_INDICES)
    168:                                        *htype = adios_long;
    169: #else
    170:                                        *htype = adios_integer;
    171: #endif
    172:   else if (ptype == PETSC_ENUM)        *htype = adios_integer;
    173:   else if (ptype == PETSC_DOUBLE)      *htype = adios_double;
    174:   else if (ptype == PETSC_LONG)        *htype = adios_long;
    175:   else if (ptype == PETSC_SHORT)       *htype = adios_short;
    176:   else if (ptype == PETSC_FLOAT)       *htype = adios_real;
    177:   else if (ptype == PETSC_CHAR)        *htype = adios_string_array;
    178:   else if (ptype == PETSC_STRING)      *htype = adios_string;
    179:   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
    180:   return 0;
    181: }

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

    186:   Not collective

    188:   Input Parameter:
    189: . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)

    191:   Output Parameter:
    192: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)

    194:   Level: advanced

    196:   Developer Notes: These have not been verified

    198: .seealso: PetscDataType, PetscADIOSDataTypeToPetscDataType()
    199: @*/
    200: PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype)
    201: {
    202: #if defined(PETSC_USE_64BIT_INDICES)
    203:   if      (htype == adios_integer)     *ptype = PETSC_ENUM;
    204:   else if (htype == adios_long)        *ptype = PETSC_INT;
    205: #else
    206:   if      (htype == adios_integer)     *ptype = PETSC_INT;
    207: #endif
    208:   else if (htype == adios_double)      *ptype = PETSC_DOUBLE;
    209:   else if (htype == adios_long)        *ptype = PETSC_LONG;
    210:   else if (htype == adios_short)       *ptype = PETSC_SHORT;
    211:   else if (htype == adios_real)        *ptype = PETSC_FLOAT;
    212:   else if (htype == adios_string_array) *ptype = PETSC_CHAR;
    213:   else if (htype == adios_string)       *ptype = PETSC_STRING;
    214:   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
    215:   return 0;
    216: }