1 #include <petsc/private/dmimpl.h> /*I "petscdm.h" I*/ 2 #include <petscds.h> 3 4 PetscErrorCode DMCreateGlobalVector_Section_Private(DM dm,Vec *vec) 5 { 6 PetscSection gSection; 7 PetscInt localSize, bs, blockSize = -1, pStart, pEnd, p; 8 PetscErrorCode ierr; 9 PetscInt in[2],out[2]; 10 11 PetscFunctionBegin; 12 ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr); 13 ierr = PetscSectionGetChart(gSection, &pStart, &pEnd);CHKERRQ(ierr); 14 for (p = pStart; p < pEnd; ++p) { 15 PetscInt dof, cdof; 16 17 ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr); 18 ierr = PetscSectionGetConstraintDof(gSection, p, &cdof);CHKERRQ(ierr); 19 20 if (dof > 0) { 21 if (blockSize < 0 && dof-cdof > 0) { 22 /* set blockSize */ 23 blockSize = dof-cdof; 24 } else if (dof-cdof != blockSize) { 25 /* non-identical blockSize, set it as 1 */ 26 blockSize = 1; 27 break; 28 } 29 } 30 } 31 32 in[0] = blockSize < 0 ? PETSC_MIN_INT : -blockSize; 33 in[1] = blockSize; 34 ierr = MPIU_Allreduce(in,out,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 35 /* -out[0] = min(blockSize), out[1] = max(blockSize) */ 36 if (-out[0] == out[1]) { 37 bs = out[1]; 38 } else bs = 1; 39 40 if (bs < 0) { /* Everyone was empty */ 41 blockSize = 1; 42 bs = 1; 43 } 44 45 ierr = PetscSectionGetConstrainedStorageSize(gSection, &localSize);CHKERRQ(ierr); 46 if (localSize%blockSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize); 47 ierr = VecCreate(PetscObjectComm((PetscObject)dm), vec);CHKERRQ(ierr); 48 ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr); 49 ierr = VecSetBlockSize(*vec, bs);CHKERRQ(ierr); 50 ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr); 51 ierr = VecSetDM(*vec, dm);CHKERRQ(ierr); 52 /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */ 53 PetscFunctionReturn(0); 54 } 55 56 PetscErrorCode DMCreateLocalVector_Section_Private(DM dm,Vec *vec) 57 { 58 PetscSection section; 59 PetscInt localSize, blockSize = -1, pStart, pEnd, p; 60 PetscErrorCode ierr; 61 62 PetscFunctionBegin; 63 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 64 ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 65 for (p = pStart; p < pEnd; ++p) { 66 PetscInt dof; 67 68 ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 69 if ((blockSize < 0) && (dof > 0)) blockSize = dof; 70 if ((dof > 0) && (dof != blockSize)) { 71 blockSize = 1; 72 break; 73 } 74 } 75 ierr = PetscSectionGetStorageSize(section, &localSize);CHKERRQ(ierr); 76 ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr); 77 ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr); 78 ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr); 79 ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr); 80 ierr = VecSetDM(*vec, dm);CHKERRQ(ierr); 81 PetscFunctionReturn(0); 82 } 83 84 /* This assumes that the DM has been cloned prior to the call */ 85 PetscErrorCode DMCreateSubDM_Section_Private(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm) 86 { 87 PetscSection section, sectionGlobal; 88 PetscInt *subIndices; 89 PetscInt subSize = 0, subOff = 0, nF, f, pStart, pEnd, p; 90 PetscErrorCode ierr; 91 92 PetscFunctionBegin; 93 if (!numFields) PetscFunctionReturn(0); 94 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 95 ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 96 if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting fields"); 97 if (!sectionGlobal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default global section for DM before splitting fields"); 98 ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 99 if (numFields > nF) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Number of requested fields %d greater than number of DM fields %d", numFields, nF); 100 if (is) { 101 PetscInt bs = -1, bsLocal[2], bsMinMax[2]; 102 ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 103 for (p = pStart; p < pEnd; ++p) { 104 PetscInt gdof, pSubSize = 0; 105 106 ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 107 if (gdof > 0) { 108 for (f = 0; f < numFields; ++f) { 109 PetscInt fdof, fcdof; 110 111 ierr = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr); 112 ierr = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr); 113 pSubSize += fdof-fcdof; 114 } 115 subSize += pSubSize; 116 if (pSubSize) { 117 if (bs < 0) { 118 bs = pSubSize; 119 } else if (bs != pSubSize) { 120 /* Layout does not admit a pointwise block size */ 121 bs = 1; 122 } 123 } 124 } 125 } 126 /* Must have same blocksize on all procs (some might have no points) */ 127 bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs; 128 ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr); 129 if (bsMinMax[0] != bsMinMax[1]) {bs = 1;} 130 else {bs = bsMinMax[0];} 131 ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr); 132 for (p = pStart; p < pEnd; ++p) { 133 PetscInt gdof, goff; 134 135 ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 136 if (gdof > 0) { 137 ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 138 for (f = 0; f < numFields; ++f) { 139 PetscInt fdof, fcdof, fc, f2, poff = 0; 140 141 /* Can get rid of this loop by storing field information in the global section */ 142 for (f2 = 0; f2 < fields[f]; ++f2) { 143 ierr = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr); 144 ierr = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr); 145 poff += fdof-fcdof; 146 } 147 ierr = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr); 148 ierr = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr); 149 for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) { 150 subIndices[subOff] = goff+poff+fc; 151 } 152 } 153 } 154 } 155 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 156 if (bs > 1) { 157 /* We need to check that the block size does not come from non-contiguous fields */ 158 PetscInt i, j, set = 1; 159 for (i = 0; i < subSize; i += bs) { 160 for (j = 0; j < bs; ++j) { 161 if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;} 162 } 163 } 164 if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);} 165 } 166 } 167 if (subdm) { 168 PetscSection subsection; 169 PetscBool haveNull = PETSC_FALSE; 170 PetscInt f, nf = 0; 171 172 ierr = PetscSectionCreateSubsection(section, numFields, fields, &subsection);CHKERRQ(ierr); 173 ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr); 174 ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr); 175 for (f = 0; f < numFields; ++f) { 176 (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[fields[f]]; 177 if ((*subdm)->nullspaceConstructors[f]) { 178 haveNull = PETSC_TRUE; 179 nf = f; 180 } 181 } 182 if (haveNull && is) { 183 MatNullSpace nullSpace; 184 185 ierr = (*(*subdm)->nullspaceConstructors[nf])(*subdm, nf, &nullSpace);CHKERRQ(ierr); 186 ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr); 187 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 188 } 189 if (dm->prob) { 190 PetscInt Nf; 191 192 ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr); 193 if (nF != Nf) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "The number of DM fields %d does not match the number of Section fields %d", Nf, nF); 194 ierr = DMSetNumFields(*subdm, numFields);CHKERRQ(ierr); 195 for (f = 0; f < numFields; ++f) { 196 PetscObject disc; 197 198 ierr = DMGetField(dm, fields[f], &disc);CHKERRQ(ierr); 199 ierr = DMSetField(*subdm, f, disc);CHKERRQ(ierr); 200 } 201 if (numFields == 1 && is) { 202 PetscObject disc, space, pmat; 203 204 ierr = DMGetField(*subdm, 0, &disc);CHKERRQ(ierr); 205 ierr = PetscObjectQuery(disc, "nullspace", &space);CHKERRQ(ierr); 206 if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nullspace", space);CHKERRQ(ierr);} 207 ierr = PetscObjectQuery(disc, "nearnullspace", &space);CHKERRQ(ierr); 208 if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nearnullspace", space);CHKERRQ(ierr);} 209 ierr = PetscObjectQuery(disc, "pmat", &pmat);CHKERRQ(ierr); 210 if (pmat) {ierr = PetscObjectCompose((PetscObject) *is, "pmat", pmat);CHKERRQ(ierr);} 211 } 212 } 213 } 214 #if 0 215 /* We need a way to filter the original SF for given fields: 216 - Keeping the original section around it too much I think 217 - We could keep the distributed section, and subset it 218 */ 219 if (dm->sfNatural) { 220 PetscSF sfNatural; 221 222 ierr = PetscSectionCreateSubsection(dm->originalSection, numFields, fields, &(*subdm)->originalSection);CHKERRQ(ierr); 223 ierr = DMPlexCreateGlobalToNaturalPetscSF(*subdm, &sfNatural);CHKERRQ(ierr); 224 ierr = DMPlexSetGlobalToNaturalPetscSF(*subdm, sfNatural);CHKERRQ(ierr); 225 } 226 #endif 227 PetscFunctionReturn(0); 228 } 229 230 /* This assumes that the DM has been cloned prior to the call */ 231 PetscErrorCode DMCreateSuperDM_Section_Private(DM dms[], PetscInt len, IS **is, DM *superdm) 232 { 233 PetscSection *sections, *sectionGlobals; 234 PetscInt *Nfs, Nf = 0, *subIndices, i; 235 PetscErrorCode ierr; 236 237 PetscFunctionBegin; 238 ierr = PetscMalloc3(len, &Nfs, len, §ions, len, §ionGlobals);CHKERRQ(ierr); 239 for (i = 0 ; i < len; ++i) { 240 ierr = DMGetDefaultSection(dms[i], §ions[i]);CHKERRQ(ierr); 241 ierr = DMGetDefaultGlobalSection(dms[i], §ionGlobals[i]);CHKERRQ(ierr); 242 if (!sections[i]) SETERRQ(PetscObjectComm((PetscObject)dms[0]), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting fields"); 243 if (!sectionGlobals[i]) SETERRQ(PetscObjectComm((PetscObject)dms[0]), PETSC_ERR_ARG_WRONG, "Must set default global section for DM before splitting fields"); 244 ierr = PetscSectionGetNumFields(sections[i], &Nfs[i]);CHKERRQ(ierr); 245 Nf += Nfs[i]; 246 } 247 if (is) { 248 PetscInt *offs, *globalOffs, iOff = 0; 249 250 ierr = PetscMalloc1(len, is);CHKERRQ(ierr); 251 ierr = PetscCalloc2(len+1, &offs, len+1, &globalOffs);CHKERRQ(ierr); 252 for (i = 0 ; i < len; ++i) { 253 ierr = PetscSectionGetConstrainedStorageSize(sectionGlobals[i], &offs[i]);CHKERRQ(ierr); 254 offs[len] += offs[i]; 255 } 256 ierr = MPI_Scan(offs, globalOffs, len+1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) dms[0]));CHKERRQ(ierr); 257 for (i = 0 ; i <= len; ++i) globalOffs[i] -= offs[i]; 258 for (i = 0 ; i < len; ++i, iOff += offs[i-1]) { 259 PetscInt bs = -1, bsLocal[2], bsMinMax[2]; 260 PetscInt subSize = 0, subOff = 0, gtoff = globalOffs[len] - globalOffs[i], pStart, pEnd, p; 261 262 ierr = PetscSectionGetChart(sectionGlobals[i], &pStart, &pEnd);CHKERRQ(ierr); 263 ierr = PetscSectionGetConstrainedStorageSize(sectionGlobals[i], &subSize);CHKERRQ(ierr); 264 ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr); 265 for (p = pStart; p < pEnd; ++p) { 266 PetscInt gdof, gcdof, gtdof, goff, d; 267 268 ierr = PetscSectionGetDof(sectionGlobals[i], p, &gdof);CHKERRQ(ierr); 269 ierr = PetscSectionGetConstraintDof(sections[i], p, &gcdof);CHKERRQ(ierr); 270 gtdof = gdof-gcdof; 271 if (gdof > 0 && gtdof) { 272 if (bs < 0) {bs = gtdof;} 273 else if (bs != gtdof) {bs = 1;} 274 ierr = PetscSectionGetOffset(sectionGlobals[i], p, &goff);CHKERRQ(ierr); 275 for (d = 0; d < gtdof; ++d, ++subOff) { 276 subIndices[subOff] = goff+gtoff+d+iOff; 277 } 278 } 279 } 280 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dms[0]), subSize, subIndices, PETSC_OWN_POINTER, &(*is)[i]);CHKERRQ(ierr); 281 /* Must have same blocksize on all procs (some might have no points) */ 282 bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs; 283 ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dms[0]), bsLocal, bsMinMax);CHKERRQ(ierr); 284 if (bsMinMax[0] != bsMinMax[1]) {bs = 1;} 285 else {bs = bsMinMax[0];} 286 ierr = ISSetBlockSize((*is)[i], bs);CHKERRQ(ierr); 287 } 288 ierr = PetscFree2(offs, globalOffs);CHKERRQ(ierr); 289 } 290 if (superdm) { 291 PetscSection supersection; 292 PetscBool haveNull = PETSC_FALSE; 293 PetscInt field, f, nf = 0; 294 295 ierr = PetscSectionCreateSupersection(sections, len, &supersection);CHKERRQ(ierr); 296 ierr = DMSetDefaultSection(*superdm, supersection);CHKERRQ(ierr); 297 ierr = PetscSectionDestroy(&supersection);CHKERRQ(ierr); 298 for (i = 0, field = 0; i < len; ++i) { 299 for (f = 0; f < Nfs[i]; ++f, ++field) { 300 (*superdm)->nullspaceConstructors[field] = dms[i]->nullspaceConstructors[f]; 301 if ((*superdm)->nullspaceConstructors[field]) { 302 haveNull = PETSC_TRUE; 303 nf = field; 304 } 305 } 306 } 307 if (haveNull && is) { 308 MatNullSpace nullSpace; 309 310 ierr = (*(*superdm)->nullspaceConstructors[nf])(*superdm, nf, &nullSpace);CHKERRQ(ierr); 311 ierr = PetscObjectCompose((PetscObject) (*is)[nf], "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr); 312 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 313 } 314 if (len && dms[0]->prob) { 315 ierr = DMSetNumFields(*superdm, Nf);CHKERRQ(ierr); 316 for (i = 0, field = 0; i < len; ++i) { 317 for (f = 0; f < Nfs[i]; ++f, ++field) { 318 PetscObject disc; 319 320 ierr = DMGetField(dms[i], f, &disc);CHKERRQ(ierr); 321 ierr = DMSetField(*superdm, field, disc);CHKERRQ(ierr); 322 } 323 } 324 } 325 } 326 #if 0 327 /* We need a way to filter the original SF for given fields: 328 - Keeping the original section around is too much I think 329 - We could keep the distributed section, and subset it 330 */ 331 if (len && dms[0]->sfNatural) { 332 PetscSF sfNatural; 333 334 ierr = PetscSectionCreateSupersection(originalSections, len, &(*superdm)->originalSection);CHKERRQ(ierr); 335 ierr = DMPlexCreateGlobalToNaturalPetscSF(*superdm, &sfNatural);CHKERRQ(ierr); 336 ierr = DMPlexSetGlobalToNaturalPetscSF(*superdm, sfNatural);CHKERRQ(ierr); 337 } 338 #endif 339 ierr = PetscFree3(Nfs, sections, sectionGlobals);CHKERRQ(ierr); 340 PetscFunctionReturn(0); 341 } 342