Actual source code: sectionhdf5.c
1: #include <petsc/private/sectionimpl.h>
2: #include <petscsf.h>
3: #include <petscis.h>
4: #include <petscviewerhdf5.h>
5: #include <petsclayouthdf5.h>
7: static PetscErrorCode PetscSectionView_HDF5_SingleField(PetscSection s, PetscViewer viewer)
8: {
9: MPI_Comm comm;
10: PetscInt pStart, pEnd, p, n;
11: PetscBool hasConstraints, includesConstraints;
12: IS dofIS, offIS, cdofIS, coffIS, cindIS;
13: PetscInt *dofs, *offs, *cdofs, *coffs, *cinds, dof, cdof, m, moff, i;
15: PetscFunctionBegin;
16: PetscCall(PetscObjectGetComm((PetscObject)s, &comm));
17: PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
18: hasConstraints = (s->bc) ? PETSC_TRUE : PETSC_FALSE;
19: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &hasConstraints, 1, MPI_C_BOOL, MPI_LOR, comm));
20: for (p = pStart, n = 0, m = 0; p < pEnd; ++p) {
21: PetscCall(PetscSectionGetDof(s, p, &dof));
22: if (dof >= 0) {
23: if (hasConstraints) {
24: PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
25: m += cdof;
26: }
27: n++;
28: }
29: }
30: PetscCall(PetscMalloc1(n, &dofs));
31: PetscCall(PetscMalloc1(n, &offs));
32: if (hasConstraints) {
33: PetscCall(PetscMalloc1(n, &cdofs));
34: PetscCall(PetscMalloc1(n, &coffs));
35: PetscCall(PetscMalloc1(m, &cinds));
36: }
37: for (p = pStart, n = 0, m = 0; p < pEnd; ++p) {
38: PetscCall(PetscSectionGetDof(s, p, &dof));
39: if (dof >= 0) {
40: dofs[n] = dof;
41: PetscCall(PetscSectionGetOffset(s, p, &offs[n]));
42: if (hasConstraints) {
43: const PetscInt *cpinds;
45: PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
46: PetscCall(PetscSectionGetConstraintIndices(s, p, &cpinds));
47: cdofs[n] = cdof;
48: coffs[n] = m;
49: for (i = 0; i < cdof; ++i) cinds[m++] = cpinds[i];
50: }
51: n++;
52: }
53: }
54: if (hasConstraints) {
55: PetscCallMPI(MPI_Scan(&m, &moff, 1, MPIU_INT, MPI_SUM, comm));
56: moff -= m;
57: for (p = 0; p < n; ++p) coffs[p] += moff;
58: }
59: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "hasConstraints", PETSC_BOOL, (void *)&hasConstraints));
60: PetscCall(PetscSectionGetIncludesConstraints(s, &includesConstraints));
61: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "includesConstraints", PETSC_BOOL, (void *)&includesConstraints));
62: PetscCall(ISCreateGeneral(comm, n, dofs, PETSC_OWN_POINTER, &dofIS));
63: PetscCall(PetscObjectSetName((PetscObject)dofIS, "atlasDof"));
64: PetscCall(ISView(dofIS, viewer));
65: PetscCall(ISDestroy(&dofIS));
66: PetscCall(ISCreateGeneral(comm, n, offs, PETSC_OWN_POINTER, &offIS));
67: PetscCall(PetscObjectSetName((PetscObject)offIS, "atlasOff"));
68: PetscCall(ISView(offIS, viewer));
69: PetscCall(ISDestroy(&offIS));
70: if (hasConstraints) {
71: PetscCall(PetscViewerHDF5PushGroup(viewer, "bc"));
72: PetscCall(ISCreateGeneral(comm, n, cdofs, PETSC_OWN_POINTER, &cdofIS));
73: PetscCall(PetscObjectSetName((PetscObject)cdofIS, "atlasDof"));
74: PetscCall(ISView(cdofIS, viewer));
75: PetscCall(ISDestroy(&cdofIS));
76: PetscCall(ISCreateGeneral(comm, n, coffs, PETSC_OWN_POINTER, &coffIS));
77: PetscCall(PetscObjectSetName((PetscObject)coffIS, "atlasOff"));
78: PetscCall(ISView(coffIS, viewer));
79: PetscCall(ISDestroy(&coffIS));
80: PetscCall(PetscViewerHDF5PopGroup(viewer));
81: PetscCall(ISCreateGeneral(comm, m, cinds, PETSC_OWN_POINTER, &cindIS));
82: PetscCall(PetscObjectSetName((PetscObject)cindIS, "bcIndices"));
83: PetscCall(ISView(cindIS, viewer));
84: PetscCall(ISDestroy(&cindIS));
85: }
86: PetscFunctionReturn(PETSC_SUCCESS);
87: }
89: PetscErrorCode PetscSectionView_HDF5_Internal(PetscSection s, PetscViewer viewer)
90: {
91: PetscInt numFields, f;
93: PetscFunctionBegin;
94: PetscCall(PetscViewerHDF5PushGroup(viewer, "section"));
95: PetscCall(PetscSectionGetNumFields(s, &numFields));
96: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "numFields", PETSC_INT, (void *)&numFields));
97: PetscCall(PetscSectionView_HDF5_SingleField(s, viewer));
98: for (f = 0; f < numFields; ++f) {
99: char fname[PETSC_MAX_PATH_LEN];
100: const char *fieldName;
101: PetscInt fieldComponents;
103: PetscCall(PetscSNPrintf(fname, sizeof(fname), "field%" PetscInt_FMT, f));
104: PetscCall(PetscViewerHDF5PushGroup(viewer, fname));
105: PetscCall(PetscSectionGetFieldName(s, f, &fieldName));
106: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "fieldName", PETSC_STRING, fieldName));
107: PetscCall(PetscSectionGetFieldComponents(s, f, &fieldComponents));
108: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "fieldComponents", PETSC_INT, (void *)&fieldComponents));
109: for (PetscInt c = 0; c < fieldComponents; ++c) {
110: char cname[PETSC_MAX_PATH_LEN];
111: const char *componentName;
113: PetscCall(PetscSNPrintf(cname, sizeof(cname), "component%" PetscInt_FMT, c));
114: PetscCall(PetscViewerHDF5PushGroup(viewer, cname));
115: PetscCall(PetscSectionGetComponentName(s, f, c, &componentName));
116: PetscCall(PetscViewerHDF5WriteAttribute(viewer, NULL, "componentName", PETSC_STRING, componentName));
117: PetscCall(PetscViewerHDF5PopGroup(viewer));
118: }
119: PetscCall(PetscSectionView_HDF5_SingleField(s->field[f], viewer));
120: PetscCall(PetscViewerHDF5PopGroup(viewer));
121: }
122: PetscCall(PetscViewerHDF5PopGroup(viewer));
123: PetscFunctionReturn(PETSC_SUCCESS);
124: }
126: static PetscErrorCode PetscSectionLoad_HDF5_SingleField_SetConstraintIndices(PetscSection s, IS cindIS, IS coffIS)
127: {
128: MPI_Comm comm;
129: PetscInt pStart, pEnd, p, M, m, i, cdof;
130: const PetscInt *data;
131: PetscInt *cinds;
132: const PetscInt *coffs;
133: PetscInt *coffsets;
134: PetscSF sf;
135: PetscLayout layout;
137: PetscFunctionBegin;
138: PetscCall(PetscObjectGetComm((PetscObject)s, &comm));
139: PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
140: PetscCall(ISGetSize(cindIS, &M));
141: PetscCall(ISGetLocalSize(cindIS, &m));
142: PetscCall(PetscMalloc1(m, &coffsets));
143: PetscCall(ISGetIndices(coffIS, &coffs));
144: for (p = pStart, m = 0; p < pEnd; ++p) {
145: PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
146: for (i = 0; i < cdof; ++i) coffsets[m++] = coffs[p - pStart] + i;
147: }
148: PetscCall(ISRestoreIndices(coffIS, &coffs));
149: PetscCall(PetscSFCreate(comm, &sf));
150: PetscCall(PetscLayoutCreate(comm, &layout));
151: PetscCall(PetscLayoutSetSize(layout, M));
152: PetscCall(PetscLayoutSetLocalSize(layout, m));
153: PetscCall(PetscLayoutSetBlockSize(layout, 1));
154: PetscCall(PetscLayoutSetUp(layout));
155: PetscCall(PetscSFSetGraphLayout(sf, layout, m, NULL, PETSC_OWN_POINTER, coffsets));
156: PetscCall(PetscLayoutDestroy(&layout));
157: PetscCall(PetscFree(coffsets));
158: PetscCall(PetscMalloc1(m, &cinds));
159: PetscCall(ISGetIndices(cindIS, &data));
160: PetscCall(PetscSFBcastBegin(sf, MPIU_INT, data, cinds, MPI_REPLACE));
161: PetscCall(PetscSFBcastEnd(sf, MPIU_INT, data, cinds, MPI_REPLACE));
162: PetscCall(ISRestoreIndices(cindIS, &data));
163: PetscCall(PetscSFDestroy(&sf));
164: PetscCall(PetscSectionSetUpBC(s));
165: for (p = pStart, m = 0; p < pEnd; ++p) {
166: PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
167: PetscCall(PetscSectionSetConstraintIndices(s, p, PetscSafePointerPlusOffset(cinds, m)));
168: m += cdof;
169: }
170: PetscCall(PetscFree(cinds));
171: PetscFunctionReturn(PETSC_SUCCESS);
172: }
174: static PetscErrorCode PetscSectionLoad_HDF5_SingleField(PetscSection s, PetscViewer viewer)
175: {
176: MPI_Comm comm;
177: PetscInt pStart, pEnd, p, N, n, M, m, N1, M1;
178: PetscBool hasConstraints, includesConstraints;
179: IS dofIS, offIS, cdofIS, coffIS, cindIS;
180: const PetscInt *dofs, *offs, *cdofs;
181: PetscLayout map;
183: PetscFunctionBegin;
184: PetscCall(PetscObjectGetComm((PetscObject)s, &comm));
185: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "includesConstraints", PETSC_BOOL, NULL, (void *)&includesConstraints));
186: PetscCall(PetscSectionSetIncludesConstraints(s, includesConstraints));
187: PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
188: n = pEnd - pStart;
189: if (PetscDefined(USE_DEBUG)) PetscCallMPI(MPIU_Allreduce(&n, &N1, 1, MPIU_INT, MPI_SUM, comm));
190: PetscCall(ISCreate(comm, &dofIS));
191: PetscCall(PetscObjectSetName((PetscObject)dofIS, "atlasDof"));
192: PetscCall(PetscViewerHDF5ReadSizes(viewer, "atlasDof", NULL, &N));
193: if (PetscDefined(USE_DEBUG)) PetscCheck(N1 == N, comm, PETSC_ERR_ARG_SIZ, "Unable to load s->atlasDof: sum of local sizes (%" PetscInt_FMT ") != global size (%" PetscInt_FMT "): local size on this process is %" PetscInt_FMT, N1, N, n);
194: PetscCall(ISGetLayout(dofIS, &map));
195: PetscCall(PetscLayoutSetSize(map, N));
196: PetscCall(PetscLayoutSetLocalSize(map, n));
197: PetscCall(ISLoad(dofIS, viewer));
198: PetscCall(ISCreate(comm, &offIS));
199: PetscCall(PetscObjectSetName((PetscObject)offIS, "atlasOff"));
200: PetscCall(PetscViewerHDF5ReadSizes(viewer, "atlasOff", NULL, &N));
201: if (PetscDefined(USE_DEBUG)) PetscCheck(N1 == N, comm, PETSC_ERR_ARG_SIZ, "Unable to load s->atlasOff: sum of local sizes (%" PetscInt_FMT ") != global size (%" PetscInt_FMT "): local size on this process is %" PetscInt_FMT, N1, N, n);
202: PetscCall(ISGetLayout(offIS, &map));
203: PetscCall(PetscLayoutSetSize(map, N));
204: PetscCall(PetscLayoutSetLocalSize(map, n));
205: PetscCall(ISLoad(offIS, viewer));
206: PetscCall(ISGetIndices(dofIS, &dofs));
207: PetscCall(ISGetIndices(offIS, &offs));
208: for (p = pStart, n = 0; p < pEnd; ++p, ++n) {
209: PetscCall(PetscSectionSetDof(s, p, dofs[n]));
210: PetscCall(PetscSectionSetOffset(s, p, offs[n]));
211: }
212: PetscCall(ISRestoreIndices(dofIS, &dofs));
213: PetscCall(ISRestoreIndices(offIS, &offs));
214: PetscCall(ISDestroy(&dofIS));
215: PetscCall(ISDestroy(&offIS));
216: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "hasConstraints", PETSC_BOOL, NULL, (void *)&hasConstraints));
217: if (hasConstraints) {
218: PetscCall(PetscViewerHDF5PushGroup(viewer, "bc"));
219: PetscCall(ISCreate(comm, &cdofIS));
220: PetscCall(PetscObjectSetName((PetscObject)cdofIS, "atlasDof"));
221: PetscCall(PetscViewerHDF5ReadSizes(viewer, "atlasDof", NULL, &N));
222: if (PetscDefined(USE_DEBUG)) PetscCheck(N1 == N, comm, PETSC_ERR_ARG_SIZ, "Unable to load s->bc->atlasDof: sum of local sizes (%" PetscInt_FMT ") != global size (%" PetscInt_FMT "): local size on this process is %" PetscInt_FMT, N1, N, n);
223: PetscCall(ISGetLayout(cdofIS, &map));
224: PetscCall(PetscLayoutSetSize(map, N));
225: PetscCall(PetscLayoutSetLocalSize(map, n));
226: PetscCall(ISLoad(cdofIS, viewer));
227: PetscCall(ISGetIndices(cdofIS, &cdofs));
228: for (p = pStart, n = 0; p < pEnd; ++p, ++n) PetscCall(PetscSectionSetConstraintDof(s, p, cdofs[n]));
229: PetscCall(ISRestoreIndices(cdofIS, &cdofs));
230: PetscCall(ISDestroy(&cdofIS));
231: PetscCall(ISCreate(comm, &coffIS));
232: PetscCall(PetscObjectSetName((PetscObject)coffIS, "atlasOff"));
233: PetscCall(PetscViewerHDF5ReadSizes(viewer, "atlasOff", NULL, &N));
234: if (PetscDefined(USE_DEBUG)) PetscCheck(N1 == N, comm, PETSC_ERR_ARG_SIZ, "Unable to load s->bc->atlasOff: sum of local sizes (%" PetscInt_FMT ") != global size (%" PetscInt_FMT "): local size on this process is %" PetscInt_FMT, N1, N, n);
235: PetscCall(ISGetLayout(coffIS, &map));
236: PetscCall(PetscLayoutSetSize(map, N));
237: PetscCall(PetscLayoutSetLocalSize(map, n));
238: PetscCall(ISLoad(coffIS, viewer));
239: PetscCall(PetscViewerHDF5PopGroup(viewer));
240: PetscCall(ISCreate(comm, &cindIS));
241: PetscCall(PetscObjectSetName((PetscObject)cindIS, "bcIndices"));
242: PetscCall(PetscViewerHDF5ReadSizes(viewer, "bcIndices", NULL, &M));
243: if (!s->bc) m = 0;
244: else PetscCall(PetscSectionGetStorageSize(s->bc, &m));
245: if (PetscDefined(USE_DEBUG)) {
246: PetscCallMPI(MPIU_Allreduce(&m, &M1, 1, MPIU_INT, MPI_SUM, comm));
247: PetscCheck(M1 == M, comm, PETSC_ERR_ARG_SIZ, "Unable to load s->bcIndices: sum of local sizes (%" PetscInt_FMT ") != global size (%" PetscInt_FMT "): local size on this process is %" PetscInt_FMT, M1, M, m);
248: }
249: PetscCall(ISGetLayout(cindIS, &map));
250: PetscCall(PetscLayoutSetSize(map, M));
251: PetscCall(PetscLayoutSetLocalSize(map, m));
252: PetscCall(ISLoad(cindIS, viewer));
253: PetscCall(PetscSectionLoad_HDF5_SingleField_SetConstraintIndices(s, cindIS, coffIS));
254: PetscCall(ISDestroy(&coffIS));
255: PetscCall(ISDestroy(&cindIS));
256: }
257: PetscFunctionReturn(PETSC_SUCCESS);
258: }
260: PetscErrorCode PetscSectionLoad_HDF5_Internal(PetscSection s, PetscViewer viewer)
261: {
262: MPI_Comm comm;
263: PetscInt N, n, numFields, f;
265: PetscFunctionBegin;
266: PetscCall(PetscObjectGetComm((PetscObject)s, &comm));
267: PetscCall(PetscViewerHDF5PushGroup(viewer, "section"));
268: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "numFields", PETSC_INT, NULL, (void *)&numFields));
269: if (s->pStart < 0 && s->pEnd < 0) n = PETSC_DECIDE;
270: else {
271: PetscCheck(s->pStart == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "s->pStart must be 0 (got %" PetscInt_FMT ")", s->pStart);
272: PetscCheck(s->pEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "s->pEnd must be >= 0, (got %" PetscInt_FMT ")", s->pEnd);
273: n = s->pEnd;
274: }
275: if (numFields > 0) PetscCall(PetscSectionSetNumFields(s, numFields));
276: PetscCall(PetscViewerHDF5ReadSizes(viewer, "atlasDof", NULL, &N));
277: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnership(comm, &n, &N));
278: PetscCall(PetscSectionSetChart(s, 0, n));
279: PetscCall(PetscSectionLoad_HDF5_SingleField(s, viewer));
280: for (f = 0; f < numFields; ++f) {
281: char fname[PETSC_MAX_PATH_LEN];
282: char *fieldName;
283: PetscInt fieldComponents;
285: PetscCall(PetscSNPrintf(fname, sizeof(fname), "field%" PetscInt_FMT, f));
286: PetscCall(PetscViewerHDF5PushGroup(viewer, fname));
287: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "fieldName", PETSC_STRING, NULL, &fieldName));
288: PetscCall(PetscSectionSetFieldName(s, f, fieldName));
289: PetscCall(PetscFree(fieldName));
290: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "fieldComponents", PETSC_INT, NULL, (void *)&fieldComponents));
291: PetscCall(PetscSectionSetFieldComponents(s, f, fieldComponents));
292: for (PetscInt c = 0; c < fieldComponents; ++c) {
293: char cname[PETSC_MAX_PATH_LEN];
294: char *componentName;
296: PetscCall(PetscSNPrintf(cname, sizeof(cname), "component%" PetscInt_FMT, c));
297: PetscCall(PetscViewerHDF5PushGroup(viewer, cname));
298: PetscCall(PetscViewerHDF5ReadAttribute(viewer, NULL, "componentName", PETSC_STRING, NULL, &componentName));
299: PetscCall(PetscSectionSetComponentName(s, f, c, componentName));
300: PetscCall(PetscFree(componentName));
301: PetscCall(PetscViewerHDF5PopGroup(viewer));
302: }
303: PetscCall(PetscSectionLoad_HDF5_SingleField(s->field[f], viewer));
304: PetscCall(PetscViewerHDF5PopGroup(viewer));
305: }
306: PetscCall(PetscViewerHDF5PopGroup(viewer));
307: PetscFunctionReturn(PETSC_SUCCESS);
308: }