From b34793c3924ccb4265224a403b7a5578e2d370c5 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 25 Oct 2023 09:06:32 +0200 Subject: [PATCH] ENH: additional globalIndex methods and helpers - single() method : simply tests if the globalIndex has nProcs == 1, which is typically from a gatherNone invocation. For example, globalIndex gi; if (...) gi.reset(localSize); else gi.reset(globalIndex::gatherNone{}, localSize); // later... const label begin = (gi.single() ? 0 : gi.localStart()); const label count = (gi.single() ? gi.totalSize() : gi.localSize()); - add front() and back() methods to return the begin/end ranges, and begin_value(), end_value() - as per labelRange. - make more methods noexcept - calcOffset(), calcRange() helper functions to determine the processor-local of a numbering range without the overhead of creating a list of offsets. For example, label myOffset = globalIndex::calcOffset(mesh.nCells()); labelRange mySlice = globalIndex::calcRange(mesh.nCells()); - add globalIndex localEnd() as per CompactListList method STYLE: align looping constructs in CompactListList with List - make more methods noexcept --- .../test/globalIndex/Test-globalIndex.C | 10 ++ .../CompactListList/CompactListList.C | 26 +-- .../CompactListList/CompactListList.H | 32 ++-- .../CompactListList/CompactListListI.H | 45 ++--- .../parallel/globalIndex/globalIndex.C | 162 +++++++++++++++++- .../parallel/globalIndex/globalIndex.H | 60 ++++++- .../parallel/globalIndex/globalIndexI.H | 50 +++++- 7 files changed, 315 insertions(+), 70 deletions(-) diff --git a/applications/test/globalIndex/Test-globalIndex.C b/applications/test/globalIndex/Test-globalIndex.C index e976cb5036..a33bccc6d1 100644 --- a/applications/test/globalIndex/Test-globalIndex.C +++ b/applications/test/globalIndex/Test-globalIndex.C @@ -55,6 +55,16 @@ int main(int argc, char *argv[]) // Global numbering of cells (proc0 elements first, then proc1, etc.) globalIndex globalNumbering(mesh.nCells()); + Pout<< "local-offset: " << globalIndex::calcOffset(mesh.nCells()) << nl; + Pout<< "local-range: " << globalIndex::calcRange(mesh.nCells()) << nl; + + Info<< "cells from:" << globalNumbering.begin_value() + << " to:" << globalNumbering.end_value() + << " span:" << globalNumbering.span() << nl; + + Info<< "front: " << globalNumbering.front() + << " back: " << globalNumbering.back() << nl; + if (globalNumbering.localSize() != mesh.nCells()) { FatalErrorInFunction diff --git a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.C b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.C index 4a0df991db..484deb275d 100644 --- a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.C +++ b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.C @@ -56,7 +56,7 @@ void Foam::CompactListList::reportOverflowAndExit template template -Foam::CompactListList Foam::CompactListList::packImpl +Foam::CompactListList Foam::CompactListList::pack_impl ( const ListListType& lists, const bool checkOverflow @@ -89,17 +89,21 @@ Foam::CompactListList Foam::CompactListList::packImpl if (total) { - // Copy in the data + // Make a deepCopy of data newValues.resize(total); - auto outIter = newValues.begin(); + auto iter = newValues.begin(); + + // NB: operator[] for sub-list read access (eg, an indirect list) + // cannot replace with std::copy for (const auto& list : lists) { - forAll(list, i) + const label sublen = list.size(); + + for (label i = 0; i < sublen; (void)++i, (void)++iter) { - *outIter = list[i]; - ++outIter; + *iter = list[i]; } } } @@ -116,7 +120,7 @@ Foam::CompactListList Foam::CompactListList::pack const bool checkOverflow ) { - return CompactListList::packImpl> + return CompactListList::pack_impl> ( lists, checkOverflow @@ -132,7 +136,7 @@ Foam::CompactListList Foam::CompactListList::pack const bool checkOverflow ) { - return CompactListList::packImpl> + return CompactListList::pack_impl> ( lists, checkOverflow @@ -404,12 +408,12 @@ Foam::CompactListList::unpack(const labelRange& range) const { List lists(range.size()); - auto outIter = lists.begin(); + auto iter = lists.begin(); for (const label i : range) { - *outIter = SubListType(this->localList(i)); - ++outIter; + *iter = SubListType(this->localList(i)); + ++iter; } return lists; diff --git a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H index 67160465a4..91c68b103a 100644 --- a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H +++ b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H @@ -28,7 +28,7 @@ Class Foam::CompactListList Description - A packed storage unstructured matrix of objects of type \ + A packed storage of objects of type \ using an offset table for access. The offset table is the size of the number of rows+1 @@ -91,7 +91,7 @@ class CompactListList //- Construct by packing together the list of lists template - static CompactListList packImpl + static CompactListList pack_impl ( const ListListType& lists, const bool checkOverflow = false @@ -197,23 +197,29 @@ public: //- True if the number of rows/sublists is zero inline bool empty() const noexcept; + //- True if content is a single row/sublist only. + //- Such content could be flattened out into a straight list + //- (for example). + inline bool single() const noexcept; + //- The primary size (the number of rows/sublists) inline label size() const noexcept; - //- The total addressed size - inline label totalSize() const; + //- The total addressed size, which corresponds to the + //- end (back) offset and also the sum of all localSizes. + inline label totalSize() const noexcept; //- Return the offset table (= size()+1) - inline const labelList& offsets() const noexcept; + const labelList& offsets() const noexcept { return offsets_; } //- Return non-const access to the offset table - inline labelList& offsets() noexcept; + labelList& offsets() noexcept { return offsets_; } - //- Return the packed matrix of values - inline const List& values() const noexcept; + //- Return the packed values + const List& values() const noexcept { return values_; } - //- Return non-const access to the packed matrix of values - inline List& values() noexcept; + //- Return non-const access to the packed values + List& values() noexcept { return values_; } //- Return const pointer to the first data in values() @@ -414,10 +420,12 @@ public: // Housekeeping - //- Const access to the packed matrix of values + //- Const access to the packed values + //FOAM_DEPRECATED_STRICT(2022-03, "values()") const List& m() const noexcept { return values_; } - //- Non-const access to the packed matrix of values + //- Non-const access to the packed values + //FOAM_DEPRECATED_STRICT(2022-03, "values()") List& m() noexcept { return values_; } //- Return flat index into packed values diff --git a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H index 03a353cd14..5a7733fb4c 100644 --- a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H +++ b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019-2022 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -145,34 +145,6 @@ Foam::CompactListList::clone() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline const Foam::labelList& Foam::CompactListList::offsets() const noexcept -{ - return offsets_; -} - - -template -inline Foam::labelList& Foam::CompactListList::offsets() noexcept -{ - return offsets_; -} - - -template -inline const Foam::List& Foam::CompactListList::values() const noexcept -{ - return values_; -} - - -template -inline Foam::List& Foam::CompactListList::values() noexcept -{ - return values_; -} - - template inline const T* Foam::CompactListList::cdata() const noexcept { @@ -211,10 +183,20 @@ inline std::streamsize Foam::CompactListList::size_bytes() const noexcept template inline bool Foam::CompactListList::empty() const noexcept { + // Note: could (should?) also check (offsets_.back() == 0) + // return offsets_.empty() + // || (*(offsets_.cdata() + offsets_.size()-1) == 0); return (offsets_.size() <= 1); } +template +inline bool Foam::CompactListList::single() const noexcept +{ + return (offsets_.size() == 2); +} + + template inline Foam::label Foam::CompactListList::size() const noexcept { @@ -231,10 +213,9 @@ inline Foam::labelList Foam::CompactListList::sizes() const template -inline Foam::label Foam::CompactListList::totalSize() const +inline Foam::label Foam::CompactListList::totalSize() const noexcept { - const label len = (offsets_.size() - 1); - return (len < 1) ? static_cast