From c1cdacc0b450ca85d9a46e31519b39ff2c810b81 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 26 Jan 2023 10:53:42 +0100 Subject: [PATCH] COMP: missing default parameters for UIPstream::read ENH: support UIPstream::read, UOPstream::write with UList, SubList --- applications/test/FixedList/Test-FixedList.C | 43 +++--- .../db/IOstreams/Pstreams/UIPstream.H | 126 ++++++++++++++++-- .../db/IOstreams/Pstreams/UOPstream.H | 61 ++++++++- 3 files changed, 203 insertions(+), 27 deletions(-) diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C index 7e510d8c78..b1976a2bfd 100644 --- a/applications/test/FixedList/Test-FixedList.C +++ b/applications/test/FixedList/Test-FixedList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -309,32 +309,45 @@ int main(int argc, char *argv[]) if (Pstream::parRun()) { + // Fixed buffer would also work, but want to test using UList + List buffer; + + const label startOfRequests = UPstream::nRequests(); + if (Pstream::master()) { + buffer.resize(UPstream::nProcs()); + buffer[0] = labelPair(0, UPstream::myProcNo()); + for (const int proci : Pstream::subProcs()) { - IPstream fromSlave(Pstream::commsTypes::blocking, proci); - FixedList list3(fromSlave); - - Serr<< "Receiving from " << proci - << " : " << list3 << endl; + UIPstream::read + ( + UPstream::commsTypes::nonBlocking, + proci, + buffer.slice(proci, 1) + ); } } else { - Perr<< "Sending to master" << endl; + buffer.resize(1); + buffer[0] = labelPair(0, UPstream::myProcNo()); - OPstream toMaster + Perr<< "Sending to master: " << buffer << endl; + + UOPstream::write ( - Pstream::commsTypes::blocking, - Pstream::masterNo() + UPstream::commsTypes::nonBlocking, + UPstream::masterNo(), + buffer.slice(0, 1) // OK + /// buffer // Also OK ); - - FixedList list3; - list3[0] = 0; - list3[1] = Pstream::myProcNo(); - toMaster << list3; } + + UPstream::waitRequests(startOfRequests); + + Info<< "Gathered: " << buffer << endl; } return 0; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H index b29042280f..bcb989cf3a 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H @@ -251,8 +251,8 @@ public: // Static Functions - //- Read buffer contents from given processor - // \return the message size + //- Read buffer contents from given processor. + // \return the message size (bytes read) static label read ( const UPstream::commsTypes commsType, @@ -265,8 +265,8 @@ public: UPstream::Request* req = nullptr ); - //- Read buffer contents (non-blocking) from given processor - // \return the message size + //- Read buffer contents (non-blocking) from given processor. + // \return the message size (bytes read) inline static label read ( //! [out] request information @@ -274,8 +274,8 @@ public: const int fromProcNo, char* buf, const std::streamsize bufSize, - const int tag, - const label communicator + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm ) { return UIPstream::read @@ -285,7 +285,113 @@ public: buf, bufSize, tag, - communicator, + comm, + &req + ); + } + + //- Read into UList storage from given processor. + // Only valid for contiguous data types. + // \return the message size (bytes read). May change in the future + template + inline static label read + ( + const UPstream::commsTypes commsType, + const int fromProcNo, + UList& buffer, + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm, + //! [out] request information (for non-blocking) + UPstream::Request* req = nullptr + ) + { + return UIPstream::read + ( + commsType, + fromProcNo, + buffer.data_bytes(), + buffer.size_bytes(), + tag, + comm, + req + ); + } + + //- Read into SubList storage from given processor. + // Only valid for contiguous data types. + // \return the message size (bytes read). May change in the future + template + inline static label read + ( + const UPstream::commsTypes commsType, + const int fromProcNo, + SubList buffer, // passed by shallow copy + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm, + //! [out] request information (for non-blocking) + UPstream::Request* req = nullptr + ) + { + return UIPstream::read + ( + commsType, + fromProcNo, + buffer.data_bytes(), + buffer.size_bytes(), + tag, + comm, + req + ); + } + + //- Read into UList storage (non-blocking) from given processor. + // Only valid for contiguous data types. + // \return the message size (bytes read). May change in the future + template + inline static label read + ( + //! [out] request information + UPstream::Request& req, + const int fromProcNo, + UList& buffer, + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm + ) + { + return UIPstream::read + ( + UPstream::commsTypes::nonBlocking, + fromProcNo, + buffer.data_bytes(), + buffer.size_bytes(), + tag, + comm, + &req + ); + } + + //- Read into SubList storage (non-blocking) from given processor. + // Only valid for contiguous data types. + // \return the message size (bytes read). May change in the future + template + inline static label read + ( + //! [out] request information + UPstream::Request& req, + const int fromProcNo, + SubList buffer, // passed by shallow copy + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm + ) + { + return UIPstream::read + ( + UPstream::commsTypes::nonBlocking, + fromProcNo, + buffer.data_bytes(), + buffer.size_bytes(), + tag, + comm, &req ); } @@ -317,11 +423,11 @@ public: //- and IO format UIPBstream ( - const UPstream::commsTypes, //!< ignored + const UPstream::commsTypes, //!< irrelevant const int rootProcNo, //!< normally UPstream::masterNo() DynamicList& receiveBuf, label& receiveBufPosition, - const int tag = UPstream::msgType(), //!< ignored + const int tag = UPstream::msgType(), //!< irrelevant const label comm = UPstream::worldComm, const bool clearAtEnd = false, //!< destroy receiveBuf if at end IOstreamOption::streamFormat fmt = IOstreamOption::BINARY @@ -341,7 +447,7 @@ public: // Static Functions //- Wrapped version of UPstream::broadcast - // \return the message size + // \return the message size (bytes read) static label read ( const int rootProcNo, //!< normally UPstream::masterNo() diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index d2c344da43..a3f86d3281 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -364,6 +364,63 @@ public: sendMode ); } + + //- Write UList contents to given processor. + // Only valid for contiguous data types. + // \return True on success + template + inline static bool write + ( + const UPstream::commsTypes commsType, + const int toProcNo, + const UList& buffer, + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm, + //! [out] request information (for non-blocking) + UPstream::Request* req = nullptr, + const UPstream::sendModes sendMode = UPstream::sendModes::normal + ) + { + return UOPstream::write + ( + commsType, + toProcNo, + buffer.cdata_bytes(), + buffer.size_bytes(), + tag, + comm, + req, + sendMode + ); + } + + //- Write UList contents (non-blocking) to given processor. + // Only valid for contiguous data types. + // \return True on success + template + inline static bool write + ( + //! [out] request information + UPstream::Request& req, + const int toProcNo, + const UList& buffer, + const int tag = UPstream::msgType(), + const label comm = UPstream::worldComm, + const UPstream::sendModes sendMode = UPstream::sendModes::normal + ) + { + return UOPstream::write + ( + UPstream::commsTypes::nonBlocking, + toProcNo, + buffer.cdata_bytes(), + buffer.size_bytes(), + tag, + comm, + &req, + sendMode + ); + } }; @@ -394,10 +451,10 @@ public: //- and IO format UOPBstream ( - const UPstream::commsTypes, //!< ignored + const UPstream::commsTypes, //!< irrelevant const int toProcNo, //!< normally UPstream::masterNo() DynamicList& sendBuf, - const int tag = UPstream::msgType(), //!< ignored + const int tag = UPstream::msgType(), //!< irrelevant const label comm = UPstream::worldComm, const bool sendAtDestruct = true, IOstreamOption::streamFormat fmt = IOstreamOption::BINARY