/* DMLabelMakeInvalid_Private - Transfer stratum data from the sorted list format to the hash format Input parameter: + label - The DMLabel - v - The stratum value Output parameter: . label - The DMLabel with stratum in hash format Level: developer .seealso: DMLabelCreate() */ static PetscErrorCode DMLabelMakeInvalid_Private(DMLabel label, PetscInt v) { PETSC_UNUSED PetscHashIIter ret, iter; PetscInt p; PetscErrorCode ierr; PetscFunctionBegin; if (!label->arrayValid[v]) PetscFunctionReturn(0); for (p = 0; p < label->stratumSizes[v]; ++p) PetscHashIPut(label->ht[v], label->points[v][p], ret, iter); ierr = PetscFree(label->points[v]);CHKERRQ(ierr); label->arrayValid[v] = PETSC_FALSE; PetscFunctionReturn(0); }
/* DMLabelMakeInvalid_Private - Transfer stratum data from the sorted list format to the hash format Input parameter: + label - The DMLabel - v - The stratum value Output parameter: . label - The DMLabel with stratum in hash format Level: developer .seealso: DMLabelCreate() */ static PetscErrorCode DMLabelMakeInvalid_Private(DMLabel label, PetscInt v) { PETSC_UNUSED PetscHashIIter ret, iter; PetscInt p; const PetscInt *points; PetscErrorCode ierr; PetscFunctionBegin; if (!label->validIS[v]) PetscFunctionReturn(0); if (label->points[v]) { ierr = ISGetIndices(label->points[v],&points);CHKERRQ(ierr); for (p = 0; p < label->stratumSizes[v]; ++p) PetscHashIPut(label->ht[v], points[p], ret, iter); ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr); ierr = ISDestroy(&(label->points[v]));CHKERRQ(ierr); } label->validIS[v] = PETSC_FALSE; PetscFunctionReturn(0); }
/*@ DMLabelSetValue - Set the value a label assigns to a point. If the value is the same as the label's default value (which is initially -1, and can be changed with DMLabelSetDefaultValue() to somethingg different), then this function will do nothing. Input Parameters: + label - the DMLabel . point - the point - value - The point value Level: intermediate .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue() @*/ PetscErrorCode DMLabelSetValue(DMLabel label, PetscInt point, PetscInt value) { PETSC_UNUSED PetscHashIIter iter, ret; PetscInt v; PetscErrorCode ierr; PetscFunctionBegin; /* Find, or add, label value */ if (value == label->defaultValue) PetscFunctionReturn(0); for (v = 0; v < label->numStrata; ++v) { if (label->stratumValues[v] == value) break; } /* Create new table */ if (v >= label->numStrata) {ierr = DMLabelAddStratum(label, value);CHKERRQ(ierr);} ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr); /* Set key */ PetscHashIPut(label->ht[v], point, ret, iter); PetscFunctionReturn(0); }
PetscErrorCode DMLabelDistribute(DMLabel label, PetscSF sf, DMLabel *labelNew) { MPI_Comm comm; PetscSection leafSection; PetscInt p, pStart, pEnd, s, size, dof, offset, stratum; PetscInt *leafStrata, *strataIdx; char *name; PetscInt nameSize; PetscHashI stratumHash; PETSC_UNUSED PetscHashIIter ret, iter; size_t len = 0; PetscMPIInt rank; PetscErrorCode ierr; PetscFunctionBegin; if (label) {ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);} ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); /* Bcast name */ if (!rank) {ierr = PetscStrlen(label->name, &len);CHKERRQ(ierr);} nameSize = len; ierr = MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);CHKERRQ(ierr); ierr = PetscMalloc1(nameSize+1, &name);CHKERRQ(ierr); if (!rank) {ierr = PetscMemcpy(name, label->name, nameSize+1);CHKERRQ(ierr);} ierr = MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);CHKERRQ(ierr); ierr = DMLabelCreate(name, labelNew);CHKERRQ(ierr); ierr = PetscFree(name);CHKERRQ(ierr); /* Bcast defaultValue */ if (!rank) (*labelNew)->defaultValue = label->defaultValue; ierr = MPI_Bcast(&(*labelNew)->defaultValue, 1, MPIU_INT, 0, comm);CHKERRQ(ierr); /* Distribute stratum values over the SF and get the point mapping on the receiver */ ierr = DMLabelDistribute_Internal(label, sf, &leafSection, &leafStrata);CHKERRQ(ierr); /* Determine received stratum values and initialise new label*/ PetscHashICreate(stratumHash); ierr = PetscSectionGetStorageSize(leafSection, &size);CHKERRQ(ierr); for (p = 0; p < size; ++p) PetscHashIPut(stratumHash, leafStrata[p], ret, iter); PetscHashISize(stratumHash, (*labelNew)->numStrata); ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->arrayValid);CHKERRQ(ierr); for (s = 0; s < (*labelNew)->numStrata; ++s) (*labelNew)->arrayValid[s] = PETSC_TRUE; ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->stratumValues);CHKERRQ(ierr); /* Turn leafStrata into indices rather than stratum values */ offset = 0; ierr = PetscHashIGetKeys(stratumHash, &offset, (*labelNew)->stratumValues);CHKERRQ(ierr); for (s = 0; s < (*labelNew)->numStrata; ++s) { for (p = 0; p < size; ++p) { if (leafStrata[p] == (*labelNew)->stratumValues[s]) leafStrata[p] = s; } } /* Rebuild the point strata on the receiver */ ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->stratumSizes);CHKERRQ(ierr); ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr); for (p=pStart; p<pEnd; p++) { ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr); ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr); for (s=0; s<dof; s++) { (*labelNew)->stratumSizes[leafStrata[offset+s]]++; } } ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->ht);CHKERRQ(ierr); ierr = PetscMalloc1((*labelNew)->numStrata,&(*labelNew)->points);CHKERRQ(ierr); for (s = 0; s < (*labelNew)->numStrata; ++s) { PetscHashICreate((*labelNew)->ht[s]); ierr = PetscMalloc1((*labelNew)->stratumSizes[s], &(*labelNew)->points[s]);CHKERRQ(ierr); } /* Insert points into new strata */ ierr = PetscCalloc1((*labelNew)->numStrata, &strataIdx);CHKERRQ(ierr); ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr); for (p=pStart; p<pEnd; p++) { ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr); ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr); for (s=0; s<dof; s++) { stratum = leafStrata[offset+s]; (*labelNew)->points[stratum][strataIdx[stratum]++] = p; } } PetscHashIDestroy(stratumHash); ierr = PetscFree(leafStrata);CHKERRQ(ierr); ierr = PetscFree(strataIdx);CHKERRQ(ierr); ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr); PetscFunctionReturn(0); }