Commit 3e941ae4 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: additional methods for globalIndex

- const version of offsets().

- empty() method to test for empty offsets, or zero overall size

- reset() methods for reseting the sizes. For example, when the context
  has shifted slightly.

- localStart() methods, similar to localSize() methods

STYLE: make globalIndex single parameter constructors explicit
parent 296bdb61
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -27,18 +27,27 @@ License ...@@ -27,18 +27,27 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::globalIndex::globalIndex Foam::globalIndex::globalIndex(Istream& is)
{
is >> offsets_;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::globalIndex::reset
( (
const label localSize, const label localSize,
const int tag, const int tag,
const label comm, const label comm,
const bool parallel const bool parallel
) )
:
offsets_(Pstream::nProcs(comm)+1)
{ {
labelList localSizes(Pstream::nProcs(comm), 0); offsets_.resize(Pstream::nProcs(comm)+1);
labelList localSizes(Pstream::nProcs(comm), Zero);
localSizes[Pstream::myProcNo(comm)] = localSize; localSizes[Pstream::myProcNo(comm)] = localSize;
if (parallel) if (parallel)
{ {
Pstream::gatherList(localSizes, tag, comm); Pstream::gatherList(localSizes, tag, comm);
...@@ -47,9 +56,9 @@ Foam::globalIndex::globalIndex ...@@ -47,9 +56,9 @@ Foam::globalIndex::globalIndex
label offset = 0; label offset = 0;
offsets_[0] = 0; offsets_[0] = 0;
for (label proci = 0; proci < Pstream::nProcs(comm); proci++) for (label proci = 0; proci < Pstream::nProcs(comm); ++proci)
{ {
label oldOffset = offset; const label oldOffset = offset;
offset += localSizes[proci]; offset += localSizes[proci];
if (offset < oldOffset) if (offset < oldOffset)
...@@ -65,20 +74,21 @@ Foam::globalIndex::globalIndex ...@@ -65,20 +74,21 @@ Foam::globalIndex::globalIndex
} }
Foam::globalIndex::globalIndex(const label localSize) void Foam::globalIndex::reset(const label localSize)
:
offsets_(Pstream::nProcs()+1)
{ {
labelList localSizes(Pstream::nProcs(), 0); offsets_.resize(Pstream::nProcs()+1);
labelList localSizes(Pstream::nProcs(), Zero);
localSizes[Pstream::myProcNo()] = localSize; localSizes[Pstream::myProcNo()] = localSize;
Pstream::gatherList(localSizes, Pstream::msgType()); Pstream::gatherList(localSizes, Pstream::msgType());
Pstream::scatterList(localSizes, Pstream::msgType()); Pstream::scatterList(localSizes, Pstream::msgType());
label offset = 0; label offset = 0;
offsets_[0] = 0; offsets_[0] = 0;
for (label proci = 0; proci < Pstream::nProcs(); proci++) for (label proci = 0; proci < Pstream::nProcs(); ++proci)
{ {
label oldOffset = offset; const label oldOffset = offset;
offset += localSizes[proci]; offset += localSizes[proci];
if (offset < oldOffset) if (offset < oldOffset)
...@@ -94,12 +104,6 @@ Foam::globalIndex::globalIndex(const label localSize) ...@@ -94,12 +104,6 @@ Foam::globalIndex::globalIndex(const label localSize)
} }
Foam::globalIndex::globalIndex(Istream& is)
{
is >> offsets_;
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, globalIndex& gi) Foam::Istream& Foam::operator>>(Istream& is, globalIndex& gi)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -78,11 +78,11 @@ public: ...@@ -78,11 +78,11 @@ public:
//- Construct from local max size. //- Construct from local max size.
// Does communication with default communicator and message tag. // Does communication with default communicator and message tag.
globalIndex(const label localSize); inline explicit globalIndex(const label localSize);
//- Construct from local max size. //- Construct from local max size.
// Does communication with given communicator and message tag // Does communication with given communicator and message tag
globalIndex inline globalIndex
( (
const label localSize, const label localSize,
const int tag, const int tag,
...@@ -91,10 +91,10 @@ public: ...@@ -91,10 +91,10 @@ public:
); );
//- Copy construct from list of labels //- Copy construct from list of labels
inline globalIndex(const labelUList& offsets); inline explicit globalIndex(const labelUList& offsets);
//- Move construct from list of labels //- Move construct from list of labels
inline globalIndex(labelList&& offsets); inline explicit globalIndex(labelList&& offsets);
//- Construct from Istream //- Construct from Istream
globalIndex(Istream& is); globalIndex(Istream& is);
...@@ -102,14 +102,38 @@ public: ...@@ -102,14 +102,38 @@ public:
// Member Functions // Member Functions
// Edit //- Check for null constructed or global sum == 0
inline bool empty() const;
//- Change after construction //- Const-access to the offsets
inline labelList& offsets(); inline const labelList& offsets() const;
// Edit
//- Write-access to the offsets, for changing after construction
inline labelList& offsets();
//- Reset from local size.
// Does communication with default communicator and message tag.
void reset(const label localSize);
//- Reset from local size.
// Does communication with given communicator and message tag
void reset
(
const label localSize,
const int tag,
const label comm,
const bool parallel // use parallel comms
);
// Queries relating to my processor (using world communicator) // Queries relating to my processor (using world communicator)
//- My local start
inline label localStart() const;
//- My local size //- My local size
inline label localSize() const; inline label localSize() const;
...@@ -132,6 +156,12 @@ public: ...@@ -132,6 +156,12 @@ public:
//- Global sum of localSizes //- Global sum of localSizes
inline label size() const; inline label size() const;
//- Start of proci data
inline label offset(const label proci) const;
//- Start of proci data
inline label localStart(const label proci) const;
//- Size of proci data //- Size of proci data
inline label localSize(const label proci) const; inline label localSize(const label proci) const;
...@@ -150,9 +180,6 @@ public: ...@@ -150,9 +180,6 @@ public:
//- Which processor does global come from? Binary search. //- Which processor does global come from? Binary search.
inline label whichProcID(const label i) const; inline label whichProcID(const label i) const;
//- Start of proci data
inline label offset(const label proci) const;
// Other // Other
......
...@@ -28,6 +28,28 @@ License ...@@ -28,6 +28,28 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::globalIndex::globalIndex(const label localSize)
:
globalIndex()
{
reset(localSize);
}
inline Foam::globalIndex::globalIndex
(
const label localSize,
const int tag,
const label comm,
const bool parallel
)
:
globalIndex()
{
reset(localSize, tag, comm, parallel);
}
inline Foam::globalIndex::globalIndex(const labelUList& offsets) inline Foam::globalIndex::globalIndex(const labelUList& offsets)
: :
offsets_(offsets) offsets_(offsets)
...@@ -42,6 +64,18 @@ inline Foam::globalIndex::globalIndex(labelList&& offsets) ...@@ -42,6 +64,18 @@ inline Foam::globalIndex::globalIndex(labelList&& offsets)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::globalIndex::empty() const
{
return offsets_.empty() || offsets_.last() == 0;
}
inline const Foam::labelList& Foam::globalIndex::offsets() const
{
return offsets_;
}
inline Foam::labelList& Foam::globalIndex::offsets() inline Foam::labelList& Foam::globalIndex::offsets()
{ {
return offsets_; return offsets_;
...@@ -54,6 +88,18 @@ inline Foam::label Foam::globalIndex::offset(const label proci) const ...@@ -54,6 +88,18 @@ inline Foam::label Foam::globalIndex::offset(const label proci) const
} }
inline Foam::label Foam::globalIndex::localStart(const label proci) const
{
return offsets_[proci];
}
inline Foam::label Foam::globalIndex::localStart() const
{
return localStart(Pstream::myProcNo());
}
inline Foam::label Foam::globalIndex::localSize(const label proci) const inline Foam::label Foam::globalIndex::localSize(const label proci) const
{ {
return offsets_[proci+1] - offsets_[proci]; return offsets_[proci+1] - offsets_[proci];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment