ENH: simplify construction of 'one-sided' globalIndex

- uses globalIndex::gatherOnly / globalIndex::gatherNone dispatch tags

  Eg,
      globalIndex(send.size(), globalIndex::gatherOnly{});

  vs.

      globalIndex
      (
          UPstream::listGatherValues(send.size()),
          globalIndex::SIZES
      );
This commit is contained in:
Mark Olesen
2022-02-01 15:17:51 +01:00
parent ddcc04dadc
commit 4f8b8258fc
11 changed files with 184 additions and 151 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,8 +40,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef UPstream_H
#define UPstream_H
#ifndef Foam_UPstream_H
#define Foam_UPstream_H
#include "labelList.H"
#include "DynamicList.H"
@ -645,8 +645,10 @@ public:
// Gather single, contiguous value(s)
//- Individual values into list locations.
// On master list length == nProcs, otherwise zero length
//- Gather individual values into list locations.
// On master list length == nProcs, otherwise zero length.
// If called in non-parallel mode,
// the returned list length is 1 with localValue.
template<class T>
static List<T> listGatherValues
(
@ -654,8 +656,10 @@ public:
const label communicator = worldComm
);
//- Individual values into list locations.
// On master list length == nProcs, otherwise zero length
//- Scatter individual values from list locations.
// On master input list length == nProcs, ignored on other procs.
// If called in non-parallel mode,
// returns the first list element (or zero).
template<class T>
static T listScatterValues
(

View File

@ -31,6 +31,21 @@ License
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::globalIndex::reportOverflowAndExit
(
const label idx,
const labelUList& localSizes
)
{
FatalErrorInFunction
<< "Overflow : sum of sizes exceeds labelMax ("
<< labelMax << ") after index " << idx << " of "
<< flatOutput(localSizes) << nl
<< "Please recompile with larger datatype for label." << nl
<< exit(FatalError);
}
Foam::labelList
Foam::globalIndex::calcOffsets
(
@ -54,12 +69,7 @@ Foam::globalIndex::calcOffsets
if (checkOverflow && start < values[i])
{
FatalErrorInFunction
<< "Overflow : sum of sizes exceeds labelMax ("
<< labelMax << ") after index " << i << " of "
<< flatOutput(localSizes) << nl
<< "Please recompile with larger datatype for label." << nl
<< exit(FatalError);
reportOverflowAndExit(i, localSizes);
}
}
values[len] = start;
@ -92,12 +102,7 @@ Foam::globalIndex::calcRanges
if (checkOverflow && start < values[i].start())
{
FatalErrorInFunction
<< "Overflow : sum of sizes exceeds labelMax ("
<< labelMax << ") after index " << i << " of "
<< flatOutput(localSizes) << nl
<< "Please recompile with larger datatype for label." << nl
<< exit(FatalError);
reportOverflowAndExit(i, localSizes);
}
}
}
@ -182,6 +187,18 @@ void Foam::globalIndex::reset(const label localSize)
}
void Foam::globalIndex::reset
(
const label localSize,
const globalIndex::gatherOnly,
const label comm
)
{
// Gather sizes (one-sided)
reset(UPstream::listGatherValues(localSize, comm));
}
void Foam::globalIndex::reset
(
const label localSize,
@ -236,12 +253,7 @@ void Foam::globalIndex::reset
if (checkOverflow && start < offsets_[i])
{
FatalErrorInFunction
<< "Overflow : sum of sizes exceeds labelMax ("
<< labelMax << ") after index " << i << " of "
<< flatOutput(localSizes) << nl
<< "Please recompile with larger datatype for label." << nl
<< exit(FatalError);
reportOverflowAndExit(i, localSizes);
}
}
offsets_[len] = start;

View File

@ -54,7 +54,6 @@ namespace Foam
{
// Forward Declarations
class globalIndex;
class labelRange;
@ -86,6 +85,13 @@ class globalIndex
DynamicList<label>& validBins
);
//- Report overflow at specified index
static void reportOverflowAndExit
(
const label idx,
const labelUList& localSizes
);
public:
// Public Data Types
@ -93,6 +99,12 @@ public:
//- Disambiguation tag (list construction dispatch)
enum accessType : char { OFFSETS, SIZES };
//- Dispatch tag
struct gatherOnly{};
//- Dispatch tag
struct gatherNone{};
// Constructors
@ -119,6 +131,26 @@ public:
// Communication with default communicator and message tag.
inline explicit globalIndex(const label localSize);
//- Construct by gathering local sizes without rescattering.
//- This 'one-sided' globalIndex will be empty on non-master processes.
//
// \note can be used when Pstream::parRun() is false.
inline globalIndex
(
const label localSize,
const globalIndex::gatherOnly,
const label comm = UPstream::worldComm //!< communicator
);
//- Construct with a single size entry.
//- No communication required
inline globalIndex
(
const label localSize,
const globalIndex::gatherNone,
const label comm = -1 //!< dummy communicator
);
//- Construct from local size.
// Communication with given communicator and message tag,
// unless parallel == false
@ -140,9 +172,12 @@ public:
//- Check for default constructed or global sum == 0
inline bool empty() const;
//- Global sum of localSizes
//- Global sum of localSizes. Same as totalSize()
inline label size() const;
//- Global sum of localSizes. Same as size()
inline label totalSize() const;
//- The local sizes
labelList sizes() const;
@ -177,6 +212,17 @@ public:
// Does communication with default communicator and message tag.
void reset(const label localSize);
//- Reset by gathering local sizes without rescattering.
//- This 'one-sided' globalIndex will be empty on non-master processes.
//
// \note can be used when Pstream::parRun() is false.
void reset
(
const label localSize,
const globalIndex::gatherOnly,
const label comm = UPstream::worldComm //!< communicator
);
//- Reset from local size.
// Does communication with given communicator and message tag,
// unless parallel == false

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2021 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -83,6 +83,32 @@ inline Foam::globalIndex::globalIndex(const label localSize)
}
inline Foam::globalIndex::globalIndex
(
const label localSize,
const globalIndex::gatherOnly,
const label comm
)
{
// Gather sizes (one-sided)
reset(UPstream::listGatherValues(localSize, comm));
}
inline Foam::globalIndex::globalIndex
(
const label localSize,
const globalIndex::gatherNone,
const label /* comm (ignored) */
)
:
offsets_(2)
{
offsets_[0] = 0;
offsets_[1] = localSize;
}
inline Foam::globalIndex::globalIndex
(
const label localSize,
@ -103,6 +129,19 @@ inline bool Foam::globalIndex::empty() const
}
inline Foam::label Foam::globalIndex::totalSize() const
{
const label len = (offsets_.size() - 1);
return (len < 1) ? static_cast<label>(0) : offsets_[len];
}
inline Foam::label Foam::globalIndex::size() const
{
return totalSize();
}
inline Foam::label Foam::globalIndex::nProcs() const noexcept
{
const label len = (offsets_.size() - 1);
@ -148,12 +187,6 @@ inline const Foam::labelUList Foam::globalIndex::localStarts() const
}
inline Foam::label Foam::globalIndex::size() const
{
return offsets_.empty() ? static_cast<label>(0) : offsets_.last();
}
inline Foam::label Foam::globalIndex::offset(const label proci) const
{
return offsets_[proci];

View File

@ -519,11 +519,8 @@ void Foam::globalIndex::mpiGatherOp
if (UPstream::parRun())
{
// Gather sizes - only needed on master
globalIndex
(
UPstream::listGatherValues(sendData.size(), comm),
accessType::SIZES
).mpiGather(sendData, allData, comm);
globalIndex(sendData.size(), globalIndex::gatherOnly{}, comm)
.mpiGather(sendData, allData, comm);
}
else
{
@ -584,11 +581,8 @@ void Foam::globalIndex::gatherOp
if (UPstream::parRun())
{
// Gather sizes - only needed on master
globalIndex
(
UPstream::listGatherValues(sendData.size(), comm),
accessType::SIZES
).gather(sendData, allData, tag, commsType, comm);
globalIndex(sendData.size(), globalIndex::gatherOnly{}, comm)
.gather(sendData, allData, tag, commsType, comm);
}
else
{
@ -611,11 +605,8 @@ void Foam::globalIndex::gatherOp
if (UPstream::parRun())
{
// Gather sizes - only needed on master
globalIndex
(
UPstream::listGatherValues(sendData.size(), comm),
accessType::SIZES
).gather(sendData, allData, tag, commsType, comm);
globalIndex(sendData.size(), globalIndex::gatherOnly{}, comm)
.gather(sendData, allData, tag, commsType, comm);
}
else
{
@ -667,11 +658,8 @@ void Foam::globalIndex::gatherInplaceOp
if (UPstream::parRun())
{
// Gather sizes - only needed on master
globalIndex
(
UPstream::listGatherValues(fld.size(), comm),
accessType::SIZES
).gather(fld, tag, commsType, comm);
globalIndex(fld.size(), globalIndex::gatherOnly{}, comm)
.gather(fld, tag, commsType, comm);
}
// Serial: (no-op)
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -68,17 +68,13 @@ void Foam::ensightOutput::Detail::writeFieldContent
// already checked prior to calling, but extra safety
parallel = parallel && Pstream::parRun();
// Size information (offsets are irrelevant)
globalIndex procAddr;
if (parallel)
{
procAddr.reset(UPstream::listGatherValues<label>(fld.size()));
}
else
{
// Master size
procAddr.reset(labelList(Foam::one{}, fld.size()));
}
// Gather sizes (offsets irrelevant)
const globalIndex procAddr
(
parallel
? globalIndex(fld.size(), globalIndex::gatherOnly{})
: globalIndex(fld.size(), globalIndex::gatherNone{})
);
if (Pstream::master())
@ -122,7 +118,7 @@ void Foam::ensightOutput::Detail::writeFieldContent
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
cmptBuffer.cdata_bytes(),
cmptBuffer.size_bytes()
);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2021 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -565,14 +565,14 @@ bool Foam::vtk::fileWriter::writeProcIDs(const label nValues)
}
const globalIndex procSizes
const globalIndex procAddr
(
parallel_
? globalIndex(nValues)
: globalIndex()
? globalIndex(nValues, globalIndex::gatherOnly{})
: globalIndex(nValues, globalIndex::gatherNone{})
);
const label totalCount = (parallel_ ? procSizes.size() : nValues);
const label totalCount = procAddr.totalSize();
this->beginDataArray<label>("procID", totalCount);
@ -583,9 +583,9 @@ bool Foam::vtk::fileWriter::writeProcIDs(const label nValues)
if (Pstream::master())
{
// Per-processor ids
for (const int proci : Pstream::allProcs())
for (const label proci : procAddr.allProcs())
{
vtk::write(format(), label(proci), procSizes.localSize(proci));
vtk::write(format(), proci, procAddr.localSize(proci));
}
good = true;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -129,13 +129,8 @@ void Foam::vtk::writeListParallel
const globalIndex& procOffset
)
{
// Gather sizes - master information, offsets are irrelevant
const globalIndex procAddr
(
UPstream::listGatherValues<label>(values.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(values.size(), globalIndex::gatherOnly{});
if (Pstream::master())
{
@ -174,7 +169,7 @@ void Foam::vtk::writeListParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
values.cdata_bytes(),
values.size_bytes()
);

View File

@ -170,12 +170,8 @@ void Foam::vtk::writeListParallel
}
// Gather sizes - master information, offsets are irrelevant
const globalIndex procAddr
(
UPstream::listGatherValues<label>(values.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(values.size(), globalIndex::gatherOnly{});
if (Pstream::master())
@ -205,7 +201,7 @@ void Foam::vtk::writeListParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
values.cdata_bytes(),
values.size_bytes()
);
@ -236,12 +232,8 @@ void Foam::vtk::writeListParallel
sendData = UIndirectList<Type>(values, addressing);
}
// Gather sizes - master information, offsets are irrelevant
const globalIndex procAddr
(
UPstream::listGatherValues<label>(sendData.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(sendData.size(), globalIndex::gatherOnly{});
if (Pstream::master())
@ -270,7 +262,7 @@ void Foam::vtk::writeListParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
sendData.cdata_bytes(),
sendData.size_bytes()
);
@ -301,12 +293,8 @@ void Foam::vtk::writeListParallel
sendData = subset(selected, values);
}
// Gather sizes - master information, offsets are irrelevant
const globalIndex procAddr
(
UPstream::listGatherValues<label>(sendData.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(sendData.size(), globalIndex::gatherOnly{});
if (Pstream::master())
@ -335,7 +323,7 @@ void Foam::vtk::writeListParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
sendData.cdata_bytes(),
sendData.size_bytes()
);
@ -360,17 +348,9 @@ void Foam::vtk::writeListsParallel
}
// Gather sizes - master information and offsets are irrelevant
const globalIndex procAddr1
(
UPstream::listGatherValues<label>(values1.size()),
globalIndex::SIZES
);
const globalIndex procAddr2
(
UPstream::listGatherValues<label>(values2.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr1(values1.size(), globalIndex::gatherOnly{});
const globalIndex procAddr2(values2.size(), globalIndex::gatherOnly{});
if (Pstream::master())
@ -415,7 +395,7 @@ void Foam::vtk::writeListsParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
values1.cdata_bytes(),
values1.size_bytes()
);
@ -423,7 +403,7 @@ void Foam::vtk::writeListsParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
values2.cdata_bytes(),
values2.size_bytes()
);
@ -456,17 +436,9 @@ void Foam::vtk::writeListsParallel
}
// Gather sizes - master information, offsets are irrelevant
const globalIndex procAddr1
(
UPstream::listGatherValues<label>(values1.size()),
globalIndex::SIZES
);
const globalIndex procAddr2
(
UPstream::listGatherValues<label>(sendData2.size()),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr1(values1.size(), globalIndex::gatherOnly{});
const globalIndex procAddr2(sendData2.size(), globalIndex::gatherOnly{});
if (Pstream::master())
@ -512,7 +484,7 @@ void Foam::vtk::writeListsParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
values1.cdata_bytes(),
values1.size_bytes()
);
@ -520,7 +492,7 @@ void Foam::vtk::writeListsParallel
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
sendData2.cdata_bytes(),
sendData2.size_bytes()
);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -107,12 +107,8 @@ bool Foam::ensightOutput::writeCloudPositions
}
// Size information (offsets are irrelevant)
const globalIndex procAddr
(
UPstream::listGatherValues<label>(nLocalParcels),
globalIndex::SIZES
);
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(nLocalParcels, globalIndex::gatherOnly{});
DynamicList<point> positions;
@ -193,7 +189,7 @@ bool Foam::ensightOutput::writeCloudPositions
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
positions.cdata_bytes(),
positions.size_bytes()
);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -78,17 +78,8 @@ bool Foam::ensightOutput::writeCloudField
return false;
}
// Size information (offsets are irrelevant)
globalIndex procAddr;
if (Pstream::parRun())
{
procAddr.reset(UPstream::listGatherValues<label>(field.size()));
}
else
{
procAddr.reset(labelList(Foam::one{}, field.size()));
}
// Gather sizes (offsets irrelevant)
const globalIndex procAddr(field.size(), globalIndex::gatherOnly{});
if (Pstream::master())
{
@ -137,7 +128,7 @@ bool Foam::ensightOutput::writeCloudField
UOPstream::write
(
UPstream::commsTypes::scheduled,
Pstream::masterNo(),
UPstream::masterNo(),
field.cdata_bytes(),
field.size_bytes()
);