xref: /petsc/src/sys/classes/viewer/impls/adios/adios.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
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(PetscViewer v, PetscOptionItems *PetscOptionsObject)
8 {
9   PetscFunctionBegin;
10   PetscOptionsHeadBegin(PetscOptionsObject, "ADIOS PetscViewer Options");
11   PetscOptionsHeadEnd();
12   PetscFunctionReturn(PETSC_SUCCESS);
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     PetscCallExternal(adios_read_close, adios->adios_fp);
23     break;
24   case FILE_MODE_WRITE:
25     PetscCallExternal(adios_close, adios->adios_handle);
26     break;
27   default:
28     break;
29   }
30   PetscCall(PetscFree(adios->filename));
31   PetscFunctionReturn(PETSC_SUCCESS);
32 }
33 
34 static PetscErrorCode PetscViewerDestroy_ADIOS(PetscViewer viewer)
35 {
36   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
37 
38   PetscFunctionBegin;
39   PetscCall(PetscViewerFileClose_ADIOS(viewer));
40   PetscCall(PetscFree(adios));
41   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
42   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
43   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
44   PetscFunctionReturn(PETSC_SUCCESS);
45 }
46 
47 static 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(PETSC_SUCCESS);
54 }
55 
56 static PetscErrorCode PetscViewerFileSetName_ADIOS(PetscViewer viewer, const char name[])
57 {
58   PetscViewer_ADIOS *adios = (PetscViewer_ADIOS *)viewer->data;
59 
60   PetscFunctionBegin;
61   if (adios->filename) PetscCall(PetscFree(adios->filename));
62   PetscCall(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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
86 }
87 
88 /*MC
89    PETSCVIEWERADIOS - A viewer that writes to an ADIOS file
90 
91   Level: beginner
92 
93 .seealso: `PetscViewerADIOSOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
94           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
95           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
96           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
97 M*/
98 
99 PETSC_EXTERN PetscErrorCode PetscViewerCreate_ADIOS(PetscViewer v)
100 {
101   PetscViewer_ADIOS *adios;
102 
103   PetscFunctionBegin;
104   PetscCall(PetscNew(&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   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_ADIOS));
115   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_ADIOS));
116   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_ADIOS));
117   PetscFunctionReturn(PETSC_SUCCESS);
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 .vb
130     FILE_MODE_WRITE - create new file for binary output
131     FILE_MODE_READ - open existing file for binary input
132     FILE_MODE_APPEND - open existing file for binary output
133 .ve
134 
135   Output Parameter:
136 . adiosv - `PetscViewer` for ADIOS input/output to use with the specified file
137 
138   Level: beginner
139 
140   Note:
141   This `PetscViewer` should be destroyed with `PetscViewerDestroy()`.
142 
143 .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerHDF5Open()`,
144           `VecView()`, `MatView()`, `VecLoad()`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
145           `MatLoad()`, `PetscFileMode`, `PetscViewer`
146 @*/
147 PetscErrorCode PetscViewerADIOSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *adiosv)
148 {
149   PetscFunctionBegin;
150   PetscCall(PetscViewerCreate(comm, adiosv));
151   PetscCall(PetscViewerSetType(*adiosv, PETSCVIEWERADIOS));
152   PetscCall(PetscViewerFileSetMode(*adiosv, type));
153   PetscCall(PetscViewerFileSetName(*adiosv, name));
154   PetscFunctionReturn(PETSC_SUCCESS);
155 }
156