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(PetscViewer v, PetscOptionItems *PetscOptionsObject)
8: {
9: PetscOptionsHeadBegin(PetscOptionsObject, "ADIOS PetscViewer Options");
10: PetscOptionsHeadEnd();
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: Level: beginner
87: .seealso: `PetscViewerADIOSOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
88: `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
89: `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
90: `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
91: M*/
93: PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v)
94: {
95: PetscViewer_ADIOS *adios;
97: PetscNew(&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 ADIOS datatype (for example MPI_DOUBLE, ...)
158: Level: advanced
160: .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
161: @*/
162: PetscErrorCode PetscDataTypeToADIOSDataType(PetscDataType ptype, enum ADIOS_DATATYPES *htype)
163: {
164: if (ptype == PETSC_INT)
165: #if defined(PETSC_USE_64BIT_INDICES)
166: *htype = adios_long;
167: #else
168: *htype = adios_integer;
169: #endif
170: else if (ptype == PETSC_ENUM) *htype = adios_integer;
171: else if (ptype == PETSC_DOUBLE) *htype = adios_double;
172: else if (ptype == PETSC_LONG) *htype = adios_long;
173: else if (ptype == PETSC_SHORT) *htype = adios_short;
174: else if (ptype == PETSC_FLOAT) *htype = adios_real;
175: else if (ptype == PETSC_CHAR) *htype = adios_string_array;
176: else if (ptype == PETSC_STRING) *htype = adios_string;
177: else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
178: return 0;
179: }
181: /*@C
182: PetscADIOSDataTypeToPetscDataType - Finds the PETSc name of a datatype from its ADIOS name
184: Not collective
186: Input Parameter:
187: . htype - the ADIOS datatype (for example H5T_NATIVE_DOUBLE, ...)
189: Output Parameter:
190: . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
192: Level: advanced
194: .seealso: `PetscDataType`, `PetscADIOSDataTypeToPetscDataType()`
195: @*/
196: PetscErrorCode PetscADIOSDataTypeToPetscDataType(enum ADIOS_DATATYPES htype, PetscDataType *ptype)
197: {
198: #if defined(PETSC_USE_64BIT_INDICES)
199: if (htype == adios_integer) *ptype = PETSC_ENUM;
200: else if (htype == adios_long) *ptype = PETSC_INT;
201: #else
202: if (htype == adios_integer) *ptype = PETSC_INT;
203: #endif
204: else if (htype == adios_double) *ptype = PETSC_DOUBLE;
205: else if (htype == adios_long) *ptype = PETSC_LONG;
206: else if (htype == adios_short) *ptype = PETSC_SHORT;
207: else if (htype == adios_real) *ptype = PETSC_FLOAT;
208: else if (htype == adios_string_array) *ptype = PETSC_CHAR;
209: else if (htype == adios_string) *ptype = PETSC_STRING;
210: else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported ADIOS datatype");
211: return 0;
212: }