From 9fbc484cdf419714e605d96b5f74ecfdce110db6 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 11 Apr 2023 11:50:17 +0200
Subject: [PATCH] ENH: use bundled MPI_Test when checking processor interface
 ready()

- fewer calls, potentially more consistent

ENH: update sendRequest state after recvRequest wait

- previously had this type of code:

    // Treat send as finished when recv is done
    UPstream::waitRequest(recvRequest_);
    recvRequest_ = -1;
    sendRequest_ = -1;

  Now refined as follows:

    // Require receive data. Update the send request state.
    UPstream::waitRequest(recvRequest_);
    recvRequest_ = -1;
    if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;

  Can potentially investigate with requiring both,
  but this may be over-contrained.
  Example,

    // Require receive data, but also wait for sends too
    UPstream::waitRequestPair(recvRequest_, sendRequest_);
---
 .../lduCalculatedProcessorField.C             | 18 ++++-----
 .../processorGAMGInterfaceField.C             |  9 +++--
 .../processor/processorFaPatchField.C         | 39 +++++++++---------
 .../calculatedProcessorFvPatchField.C         | 33 +++++++--------
 .../processor/processorFvPatchField.C         | 40 +++++++++----------
 .../calculatedProcessorGAMGInterfaceField.C   |  9 +++--
 6 files changed, 69 insertions(+), 79 deletions(-)

diff --git a/src/OpenFOAM/matrices/lduMatrix/lduAddressing/lduInterface/lduCalculatedProcessorField/lduCalculatedProcessorField.C b/src/OpenFOAM/matrices/lduMatrix/lduAddressing/lduInterface/lduCalculatedProcessorField/lduCalculatedProcessorField.C
index 11e837c5303..d60692571d1 100644
--- a/src/OpenFOAM/matrices/lduMatrix/lduAddressing/lduInterface/lduCalculatedProcessorField/lduCalculatedProcessorField.C
+++ b/src/OpenFOAM/matrices/lduMatrix/lduAddressing/lduInterface/lduCalculatedProcessorField/lduCalculatedProcessorField.C
@@ -60,13 +60,7 @@ Foam::lduCalculatedProcessorField<Type>::lduCalculatedProcessorField
 template<class Type>
 bool Foam::lduCalculatedProcessorField<Type>::ready() const
 {
-    if (!UPstream::finishedRequest(this->sendRequest_)) return false;
-    this->sendRequest_ = -1;
-
-    if (!UPstream::finishedRequest(this->recvRequest_)) return false;
-    this->recvRequest_ = -1;
-
-    return true;
+    return UPstream::finishedRequestPair(recvRequest_, sendRequest_);
 }
 
 
@@ -173,10 +167,12 @@ void Foam::lduCalculatedProcessorField<Type>::updateInterfaceMatrix
         return;
     }
 
-    // Treat send as finished when recv is done
-    UPstream::waitRequest(recvRequest_);
-    recvRequest_ = -1;
-    sendRequest_ = -1;
+    // Require receive data. Update the send request state.
+    // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+    UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+    if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
+
 
     // Consume straight from receive buffer. Note use of our own
     // helper to avoid using fvPatch addressing
diff --git a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorGAMGInterfaceField/processorGAMGInterfaceField.C b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorGAMGInterfaceField/processorGAMGInterfaceField.C
index 8309c1b31a5..0e4b2215352 100644
--- a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorGAMGInterfaceField/processorGAMGInterfaceField.C
+++ b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorGAMGInterfaceField/processorGAMGInterfaceField.C
@@ -171,10 +171,11 @@ void Foam::processorGAMGInterfaceField::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
diff --git a/src/finiteArea/fields/faPatchFields/constraint/processor/processorFaPatchField.C b/src/finiteArea/fields/faPatchFields/constraint/processor/processorFaPatchField.C
index d65ea2637d4..70935bcf792 100644
--- a/src/finiteArea/fields/faPatchFields/constraint/processor/processorFaPatchField.C
+++ b/src/finiteArea/fields/faPatchFields/constraint/processor/processorFaPatchField.C
@@ -176,13 +176,7 @@ Foam::processorFaPatchField<Type>::processorFaPatchField
 template<class Type>
 bool Foam::processorFaPatchField<Type>::ready() const
 {
-    if (!UPstream::finishedRequest(sendRequest_)) return false;
-    sendRequest_ = -1;
-
-    if (!UPstream::finishedRequest(recvRequest_)) return false;
-    recvRequest_ = -1;
-
-    return true;
+    return UPstream::finishedRequestPair(recvRequest_, sendRequest_);
 }
 
 
@@ -207,7 +201,7 @@ void Foam::processorFaPatchField<Type>::initEvaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
         this->patchInternalField(sendBuf_);
 
@@ -259,16 +253,17 @@ void Foam::processorFaPatchField<Type>::evaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
         if (commsType == UPstream::commsTypes::nonBlocking)
         {
             // Fast path. Received into *this
 
-            // Treat send as finished when recv is done
-            UPstream::waitRequest(recvRequest_);
-            recvRequest_ = -1;
-            sendRequest_ = -1;
+            // Require receive data. Update the send request state.
+            // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+            UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+            if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
         }
         else
         {
@@ -373,10 +368,11 @@ void Foam::processorFaPatchField<Type>::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
@@ -479,10 +475,11 @@ void Foam::processorFaPatchField<Type>::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
diff --git a/src/finiteVolume/fields/fvPatchFields/constraint/calculatedProcessor/calculatedProcessorFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/constraint/calculatedProcessor/calculatedProcessorFvPatchField.C
index 875c98105f3..25e6ee5a234 100644
--- a/src/finiteVolume/fields/fvPatchFields/constraint/calculatedProcessor/calculatedProcessorFvPatchField.C
+++ b/src/finiteVolume/fields/fvPatchFields/constraint/calculatedProcessor/calculatedProcessorFvPatchField.C
@@ -76,13 +76,7 @@ Foam::calculatedProcessorFvPatchField<Type>::calculatedProcessorFvPatchField
 template<class Type>
 bool Foam::calculatedProcessorFvPatchField<Type>::ready() const
 {
-    if (!UPstream::finishedRequest(this->sendRequest_)) return false;
-    this->sendRequest_ = -1;
-
-    if (!UPstream::finishedRequest(this->recvRequest_)) return false;
-    this->recvRequest_ = -1;
-
-    return true;
+    return UPstream::finishedRequestPair(recvRequest_, sendRequest_);
 }
 
 
@@ -109,7 +103,7 @@ void Foam::calculatedProcessorFvPatchField<Type>::initEvaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
         if (!is_contiguous<Type>::value)
         {
@@ -164,12 +158,13 @@ void Foam::calculatedProcessorFvPatchField<Type>::evaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
 }
 
@@ -278,14 +273,16 @@ void Foam::calculatedProcessorFvPatchField<Type>::updateInterfaceMatrix
         return;
     }
 
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
 
+
     // Consume straight from receive buffer. Note use of our own
     // helper to avoid using fvPatch addressing
     addToInternalField(result, !add, coeffs, scalarRecvBuf_);
diff --git a/src/finiteVolume/fields/fvPatchFields/constraint/processor/processorFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/constraint/processor/processorFvPatchField.C
index b5350e4f3ff..3fdec71d964 100644
--- a/src/finiteVolume/fields/fvPatchFields/constraint/processor/processorFvPatchField.C
+++ b/src/finiteVolume/fields/fvPatchFields/constraint/processor/processorFvPatchField.C
@@ -111,6 +111,7 @@ Foam::processorFvPatchField<Type>::processorFvPatchField
     if (!isA<processorFvPatch>(this->patch()))
     {
         FatalErrorInFunction
+            << "\n    patch type '" << p.type()
             << "' not constraint type '" << typeName << "'"
             << "\n    for patch " << p.name()
             << " of field " << this->internalField().name()
@@ -177,13 +178,7 @@ Foam::processorFvPatchField<Type>::processorFvPatchField
 template<class Type>
 bool Foam::processorFvPatchField<Type>::ready() const
 {
-    if (!UPstream::finishedRequest(sendRequest_)) return false;
-    sendRequest_ = -1;
-
-    if (!UPstream::finishedRequest(recvRequest_)) return false;
-    recvRequest_ = -1;
-
-    return true;
+    return UPstream::finishedRequestPair(recvRequest_, sendRequest_);
 }
 
 
@@ -208,7 +203,7 @@ void Foam::processorFvPatchField<Type>::initEvaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
         this->patchInternalField(sendBuf_);
 
@@ -264,7 +259,7 @@ void Foam::processorFvPatchField<Type>::evaluate
     const Pstream::commsTypes commsType
 )
 {
-    if (Pstream::parRun())
+    if (UPstream::parRun())
     {
         if
         (
@@ -274,10 +269,11 @@ void Foam::processorFvPatchField<Type>::evaluate
         {
             // Fast path: received into *this
 
-            // Treat send as finished when recv is done
-            UPstream::waitRequest(recvRequest_);
-            recvRequest_ = -1;
-            sendRequest_ = -1;
+            // Require receive data. Update the send request state.
+            // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+            UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+            if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
         }
         else
         {
@@ -402,10 +398,11 @@ void Foam::processorFvPatchField<Type>::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
@@ -523,10 +520,11 @@ void Foam::processorFvPatchField<Type>::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
diff --git a/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.C b/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.C
index cc58546967d..adf9b74a17a 100644
--- a/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.C
+++ b/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.C
@@ -172,10 +172,11 @@ void Foam::calculatedProcessorGAMGInterfaceField::updateInterfaceMatrix
     {
         // Fast path: consume straight from receive buffer
 
-        // Treat send as finished when recv is done
-        UPstream::waitRequest(recvRequest_);
-        recvRequest_ = -1;
-        sendRequest_ = -1;
+        // Require receive data. Update the send request state.
+        // OR: UPstream::waitRequestPair(recvRequest_, sendRequest_);
+
+        UPstream::waitRequest(recvRequest_); recvRequest_ = -1;
+        if (UPstream::finishedRequest(sendRequest_)) sendRequest_ = -1;
     }
     else
     {
-- 
GitLab