ENH: simplify Pstream broadcast stream construction

- the rootProcNo is usually == UPstream::masterNo()
This commit is contained in:
Mark Olesen 2024-04-10 14:05:52 +02:00
parent bca093d89c
commit 7fd962926d
8 changed files with 112 additions and 70 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,7 +34,7 @@ License
Foam::UIPBstream::UIPBstream
(
const UPstream::commsTypes commsType,
const int fromProcNo,
const int rootProcNo,
DynamicList<char>& receiveBuf,
label& receiveBufPosition,
const int tag,
@ -45,11 +45,11 @@ Foam::UIPBstream::UIPBstream
:
UIPstreamBase
(
commsType,
fromProcNo,
commsType, // irrelevant
rootProcNo, // normally UPstream::masterNo()
receiveBuf,
receiveBufPosition,
tag,
tag, // irrelevant
comm,
clearAtEnd,
fmt
@ -62,7 +62,7 @@ Foam::UIPBstream::UIPBstream
Foam::IPBstream::IPBstream
(
const UPstream::commsTypes commsType,
const int fromProcNo,
const int rootProcNo,
const label bufSize,
const int tag,
const label comm,
@ -72,11 +72,11 @@ Foam::IPBstream::IPBstream
Pstream(commsType, bufSize),
UIPBstream
(
commsType,
fromProcNo,
commsType, // irrelevant
rootProcNo, // normally UPstream::masterNo()
Pstream::transferBuf_,
UIPstreamBase::storedRecvBufPos_, // Internal only
tag,
tag, // irrelevant
comm,
false, // Do not clear Pstream::transferBuf_ if at end
fmt
@ -86,7 +86,7 @@ Foam::IPBstream::IPBstream
Foam::IPBstream::IPBstream
(
const int fromProcNo,
const int rootProcNo,
const label comm,
IOstreamOption::streamFormat fmt
)
@ -94,7 +94,25 @@ Foam::IPBstream::IPBstream
IPBstream
(
UPstream::commsTypes::scheduled, // irrelevant
fromProcNo,
rootProcNo,
label(0), // bufSize
UPstream::msgType(), // irrelevant
comm,
fmt
)
{}
Foam::IPBstream::IPBstream
(
const label comm,
IOstreamOption::streamFormat fmt
)
:
IPBstream
(
UPstream::commsTypes::scheduled, // irrelevant
UPstream::masterNo(), // rootProcNo
label(0), // bufSize
UPstream::msgType(), // irrelevant
comm,

View File

@ -127,11 +127,19 @@ public:
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);
//- Construct for broadcast root with optional communicator,
//- write format
explicit IPBstream
//- Construct for broadcast root and communicator,
//- with optional read format
IPBstream
(
const int rootProcNo, //!< normally UPstream::masterNo()
const label comm,
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);
//- Construct with optional communicator and read format.
//- Uses UPstream::masterNo() root
explicit IPBstream
(
const label comm = UPstream::worldComm,
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,7 +34,7 @@ License
Foam::UOPBstream::UOPBstream
(
const UPstream::commsTypes commsType,
const int toProcNo,
const int rootProcNo,
DynamicList<char>& sendBuf,
const int tag,
const label comm,
@ -42,14 +42,23 @@ Foam::UOPBstream::UOPBstream
IOstreamOption::streamFormat fmt
)
:
UOPstreamBase(commsType, toProcNo, sendBuf, tag, comm, sendAtDestruct, fmt)
UOPstreamBase
(
commsType, // irrelevant
rootProcNo, // normally UPstream::masterNo()
sendBuf,
tag, // irrelevant
comm,
sendAtDestruct,
fmt
)
{}
Foam::OPBstream::OPBstream
(
const UPstream::commsTypes commsType,
const int toProcNo,
const int rootProcNo,
const label bufSize,
const int tag,
const label comm,
@ -59,10 +68,10 @@ Foam::OPBstream::OPBstream
Pstream(commsType, bufSize),
UOPBstream
(
commsType,
toProcNo,
commsType, // irrelevant
rootProcNo, // normally UPstream::masterNo()
Pstream::transferBuf_,
tag,
tag, // irrelevant
comm,
true, // sendAtDestruct
fmt
@ -72,7 +81,7 @@ Foam::OPBstream::OPBstream
Foam::OPBstream::OPBstream
(
const int toProcNo,
const int rootProcNo,
const label comm,
IOstreamOption::streamFormat fmt
)
@ -80,7 +89,25 @@ Foam::OPBstream::OPBstream
OPBstream
(
UPstream::commsTypes::scheduled, // irrelevant
toProcNo,
rootProcNo,
label(0), // bufSize
UPstream::msgType(), // irrelevant
comm,
fmt
)
{}
Foam::OPBstream::OPBstream
(
const label comm,
IOstreamOption::streamFormat fmt
)
:
OPBstream
(
UPstream::commsTypes::scheduled, // irrelevant
UPstream::masterNo(), // rootProcNo
label(0), // bufSize
UPstream::msgType(), // irrelevant
comm,

View File

@ -144,11 +144,19 @@ public:
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);
//- Construct for broadcast root with optional communicator,
//- write format
explicit OPBstream
//- Construct for broadcast root and communicator,
//- with optional write format
OPBstream
(
const int rootProcNo, //!< normally UPstream::masterNo()
const label comm,
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);
//- Construct with optional communicator and write format.
//- Uses UPstream::masterNo() root
explicit OPBstream
(
const label comm = UPstream::worldComm,
IOstreamOption::streamFormat fmt = IOstreamOption::BINARY
);

View File

@ -41,20 +41,19 @@ void Foam::Pstream::broadcast(Type& value, const label comm)
(
reinterpret_cast<char*>(&value),
sizeof(Type),
comm,
UPstream::masterNo()
comm
);
}
else if (UPstream::is_parallel(comm))
{
if (UPstream::master(comm))
{
OPBstream os(UPstream::masterNo(), comm);
OPBstream os(comm);
os << value;
}
else // UPstream::is_subrank(comm)
{
IPBstream is(UPstream::masterNo(), comm);
IPBstream is(comm);
is >> value;
}
}
@ -68,12 +67,12 @@ void Foam::Pstream::broadcasts(const label comm, Type& arg1, Args&&... args)
{
if (UPstream::master(comm))
{
OPBstream os(UPstream::masterNo(), comm);
OPBstream os(comm);
Detail::outputLoop(os, arg1, std::forward<Args>(args)...);
}
else // UPstream::is_subrank(comm)
{
IPBstream is(UPstream::masterNo(), comm);
IPBstream is(comm);
Detail::inputLoop(is, arg1, std::forward<Args>(args)...);
}
}
@ -98,8 +97,7 @@ void Foam::Pstream::broadcastList(ListType& list, const label comm)
(
reinterpret_cast<char*>(&len),
sizeof(label),
comm,
UPstream::masterNo()
comm
);
if (UPstream::is_subrank(comm))
@ -113,8 +111,7 @@ void Foam::Pstream::broadcastList(ListType& list, const label comm)
(
list.data_bytes(),
list.size_bytes(),
comm,
UPstream::masterNo()
comm
);
}
}
@ -125,12 +122,12 @@ void Foam::Pstream::broadcastList(ListType& list, const label comm)
if (UPstream::master(comm))
{
OPBstream os(UPstream::masterNo(), comm);
OPBstream os(comm);
os << list;
}
else // UPstream::is_subrank(comm)
{
IPBstream is(UPstream::masterNo(), comm);
IPBstream is(comm);
is >> list;
}
}

View File

@ -2195,26 +2195,18 @@ bool Foam::fileOperations::masterUncollatedFileOperation::read
io.note()
);
if (Pstream::master(UPstream::worldComm))
if (UPstream::master(UPstream::worldComm))
{
OPBstream toAll
(
UPstream::masterNo(),
UPstream::worldComm,
format
);
bool okWrite = io.writeData(toAll);
OPBstream os(UPstream::worldComm, format);
bool okWrite = io.writeData(os);
ok = ok && okWrite;
}
else
{
IPBstream fromMaster
(
UPstream::masterNo(),
UPstream::worldComm,
format
);
ok = io.readData(fromMaster);
IPBstream is(UPstream::worldComm, format);
ok = io.readData(is);
}
}
}

View File

@ -752,24 +752,16 @@ bool Foam::fileOperations::uncollatedFileOperation::read
if (UPstream::master(UPstream::worldComm))
{
OPBstream toAll
(
UPstream::masterNo(),
UPstream::worldComm,
format
);
bool okWrite = io.writeData(toAll);
OPBstream os(UPstream::worldComm, format);
bool okWrite = io.writeData(os);
ok = ok && okWrite;
}
else
{
IPBstream fromMaster
(
UPstream::masterNo(),
UPstream::worldComm,
format
);
ok = io.readData(fromMaster);
IPBstream is(UPstream::worldComm, format);
ok = io.readData(is);
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -270,7 +270,7 @@ void Foam::fieldsDistributor::readFieldsImpl
// Broadcast zero sized fields everywhere (if needed)
// Send like a list of dictionaries
OPBstream toProcs(UPstream::masterNo()); // worldComm
OPBstream toProcs(UPstream::worldComm);
const label nDicts = (subsetter ? fields.size() : label(0));
@ -299,7 +299,7 @@ void Foam::fieldsDistributor::readFieldsImpl
else
{
// Receive the broadcast...
IPBstream fromMaster(UPstream::masterNo()); // worldComm
IPBstream fromMaster(UPstream::worldComm);
// But only consume where needed...
if (!haveMeshOnProc.test(UPstream::myProcNo()))