From f6ed5ddc163b1554f3a67358ec302bf51ec79376 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Tue, 19 Mar 2013 13:25:11 +0000
Subject: [PATCH] BUG: #704 processorCyclic: non-blocking comms, multiple tags

---
 .../processor/processorPointPatchField.C      | 70 +--------------
 .../processor/processorPointPatchField.H      | 15 +---
 .../processorCyclicPointPatchField.C          | 53 ++++++++----
 .../processorCyclicPointPatchField.H          |  5 +-
 .../processorCyclicPolyPatch.C                | 85 ++++++++++++-------
 .../processorCyclicPolyPatch.H                | 13 ++-
 6 files changed, 103 insertions(+), 138 deletions(-)

diff --git a/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.C b/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.C
index c3d68e28374..8a647b6db03 100644
--- a/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.C
+++ b/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -24,7 +24,6 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "processorPointPatchField.H"
-//#include "transformField.H"
 #include "processorPolyPatch.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@@ -92,73 +91,6 @@ processorPointPatchField<Type>::~processorPointPatchField()
 {}
 
 
-// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
-
-template<class Type>
-void processorPointPatchField<Type>::initSwapAddSeparated
-(
-    const Pstream::commsTypes commsType,
-    Field<Type>& pField
-)
-const
-{
-//    if (Pstream::parRun())
-//    {
-//        // Get internal field into correct order for opposite side
-//        Field<Type> pf
-//        (
-//            this->patchInternalField
-//            (
-//                pField,
-//                procPatch_.reverseMeshPoints()
-//            )
-//        );
-//
-//        OPstream::write
-//        (
-//            commsType,
-//            procPatch_.neighbProcNo(),
-//            reinterpret_cast<const char*>(pf.begin()),
-//            pf.byteSize(),
-//            procPatch_.tag()
-//        );
-//    }
-}
-
-
-template<class Type>
-void processorPointPatchField<Type>::swapAddSeparated
-(
-    const Pstream::commsTypes commsType,
-    Field<Type>& pField
-) const
-{
-//    if (Pstream::parRun())
-//    {
-//        Field<Type> pnf(this->size());
-//
-//        IPstream::read
-//        (
-//            commsType,
-//            procPatch_.neighbProcNo(),
-//            reinterpret_cast<char*>(pnf.begin()),
-//            pnf.byteSize(),
-//            procPatch_.tag()
-//        );
-//
-//        if (doTransform())
-//        {
-//            const processorPolyPatch& ppp = procPatch_.procPolyPatch();
-//            const tensor& forwardT = ppp.forwardT();
-//
-//            transform(pnf, forwardT, pnf);
-//        }
-//
-//        addToInternalField(pField, pnf, procPatch_.separatedPoints());
-//    }
-}
-
-
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 } // End namespace Foam
diff --git a/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.H b/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.H
index 6582b62861a..5dfceda2d11 100644
--- a/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.H
+++ b/src/OpenFOAM/fields/pointPatchFields/constraint/processor/processorPointPatchField.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -57,7 +57,6 @@ class processorPointPatchField
         //- Local reference to processor patch
         const processorPointPatch& procPatch_;
 
-
 public:
 
     //- Runtime type information
@@ -176,19 +175,13 @@ public:
             )
             {}
 
-            //- Initialise swap of non-collocated patch point values
-            virtual void initSwapAddSeparated
-            (
-                const Pstream::commsTypes commsType,
-                Field<Type>&
-            ) const;
-
-            //- Complete swap of patch point values and add to local values
+            //- Assume processor patch always collocated
             virtual void swapAddSeparated
             (
                 const Pstream::commsTypes commsType,
                 Field<Type>&
-            ) const;
+            ) const
+            {}
 };
 
 
diff --git a/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.C b/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.C
index 4e28c3466df..6bea99a1bc9 100644
--- a/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.C
+++ b/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -38,7 +38,8 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
 )
 :
     coupledPointPatchField<Type>(p, iF),
-    procPatch_(refCast<const processorCyclicPointPatch>(p))
+    procPatch_(refCast<const processorCyclicPointPatch>(p)),
+    receiveBuf_(0)
 {}
 
 
@@ -51,7 +52,8 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
 )
 :
     coupledPointPatchField<Type>(p, iF, dict),
-    procPatch_(refCast<const processorCyclicPointPatch>(p))
+    procPatch_(refCast<const processorCyclicPointPatch>(p)),
+    receiveBuf_(0)
 {}
 
 
@@ -65,7 +67,8 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
 )
 :
     coupledPointPatchField<Type>(ptf, p, iF, mapper),
-    procPatch_(refCast<const processorCyclicPointPatch>(ptf.patch()))
+    procPatch_(refCast<const processorCyclicPointPatch>(ptf.patch())),
+    receiveBuf_(0)
 {}
 
 
@@ -77,7 +80,8 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
 )
 :
     coupledPointPatchField<Type>(ptf, iF),
-    procPatch_(refCast<const processorCyclicPointPatch>(ptf.patch()))
+    procPatch_(refCast<const processorCyclicPointPatch>(ptf.patch())),
+    receiveBuf_(0)
 {}
 
 
@@ -109,6 +113,18 @@ void Foam::processorCyclicPointPatchField<Type>::initSwapAddSeparated
             )
         );
 
+        if (commsType == Pstream::nonBlocking)
+        {
+            receiveBuf_.setSize(pf.size());
+            IPstream::read
+            (
+                commsType,
+                procPatch_.neighbProcNo(),
+                reinterpret_cast<char*>(receiveBuf_.begin()),
+                receiveBuf_.byteSize(),
+                procPatch_.tag()
+            );
+        }
         OPstream::write
         (
             commsType,
@@ -130,16 +146,19 @@ void Foam::processorCyclicPointPatchField<Type>::swapAddSeparated
 {
     if (Pstream::parRun())
     {
-        Field<Type> pnf(this->size());
-
-        IPstream::read
-        (
-            commsType,
-            procPatch_.neighbProcNo(),
-            reinterpret_cast<char*>(pnf.begin()),
-            pnf.byteSize(),
-            procPatch_.tag()
-        );
+        // If nonblocking data has already been received into receiveBuf_
+        if (commsType != Pstream::nonBlocking)
+        {
+            receiveBuf_.setSize(this->size());
+            IPstream::read
+            (
+                commsType,
+                procPatch_.neighbProcNo(),
+                reinterpret_cast<char*>(receiveBuf_.begin()),
+                receiveBuf_.byteSize(),
+                procPatch_.tag()
+            );
+        }
 
         if (doTransform())
         {
@@ -147,11 +166,11 @@ void Foam::processorCyclicPointPatchField<Type>::swapAddSeparated
                 procPatch_.procCyclicPolyPatch();
             const tensor& forwardT = ppp.forwardT()[0];
 
-            transform(pnf, forwardT, pnf);
+            transform(receiveBuf_, forwardT, receiveBuf_);
         }
 
         // All points are separated
-        this->addToInternalField(pField, pnf);
+        this->addToInternalField(pField, receiveBuf_);
     }
 }
 
diff --git a/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.H b/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.H
index c8575240603..6aa358b868b 100644
--- a/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.H
+++ b/src/OpenFOAM/fields/pointPatchFields/constraint/processorCyclic/processorCyclicPointPatchField.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -57,6 +57,9 @@ class processorCyclicPointPatchField
         //- Local reference to processor patch
         const processorCyclicPointPatch& procPatch_;
 
+        //- Receive buffer for non-blocking communication
+        mutable Field<Type> receiveBuf_;
+
 
 public:
 
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C
index 8988a06eb21..7e45266d241 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2012 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -37,10 +37,7 @@ namespace Foam
 }
 
 
-// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
-
-
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
 (
@@ -66,20 +63,10 @@ Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
         neighbProcNo,
         transform
     ),
-    tag_
-    (
-        Pstream::nProcs()*max(myProcNo, neighbProcNo)
-      + min(myProcNo, neighbProcNo)
-    ),
     referPatchName_(referPatchName),
+    tag_(-1),
     referPatchID_(-1)
-{
-    if (debug)
-    {
-        Pout<< "processorCyclicPolyPatch " << name << " uses tag " << tag_
-            << endl;
-    }
-}
+{}
 
 
 Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
@@ -92,20 +79,10 @@ Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
 )
 :
     processorPolyPatch(name, dict, index, bm, patchType),
-    tag_
-    (
-        Pstream::nProcs()*max(myProcNo(), neighbProcNo())
-      + min(myProcNo(), neighbProcNo())
-    ),
     referPatchName_(dict.lookup("referPatch")),
+    tag_(dict.lookupOrDefault<int>("tag", -1)),
     referPatchID_(-1)
-{
-    if (debug)
-    {
-        Pout<< "processorCyclicPolyPatch " << name << " uses tag " << tag_
-            << endl;
-    }
-}
+{}
 
 
 Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
@@ -115,8 +92,8 @@ Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
 )
 :
     processorPolyPatch(pp, bm),
-    tag_(pp.tag_),
     referPatchName_(pp.referPatchName()),
+    tag_(pp.tag()),
     referPatchID_(-1)
 {}
 
@@ -132,8 +109,8 @@ Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
 )
 :
     processorPolyPatch(pp, bm, index, newSize, newStart),
-    tag_(pp.tag_),
     referPatchName_(referPatchName),
+    tag_(-1),
     referPatchID_(-1)
 {}
 
@@ -148,8 +125,8 @@ Foam::processorCyclicPolyPatch::processorCyclicPolyPatch
 )
 :
     processorPolyPatch(pp, bm, index, mapAddressing, newStart),
-    tag_(pp.tag_),
     referPatchName_(pp.referPatchName()),
+    tag_(-1),
     referPatchID_(-1)
 {}
 
@@ -162,6 +139,45 @@ Foam::processorCyclicPolyPatch::~processorCyclicPolyPatch()
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
+int Foam::processorCyclicPolyPatch::tag() const
+{
+    if (tag_ == -1)
+    {
+        // Get unique tag to use for all comms. Make sure that both sides
+        // use the same tag
+        const cyclicPolyPatch& cycPatch = refCast<const cyclicPolyPatch>
+        (
+            referPatch()
+        );
+
+        if (cycPatch.owner())
+        {
+            tag_ = Hash<word>()(cycPatch.name());
+        }
+        else
+        {
+            tag_ = Hash<word>()(cycPatch.neighbPatch().name());
+        }
+
+        if (tag_ == Pstream::msgType() || tag_ == -1)
+        {
+            FatalErrorIn("processorCyclicPolyPatch::tag() const")
+                << "Tag calculated from cyclic patch name " << tag_
+                << " is the same as the current message type "
+                << Pstream::msgType() << " or -1" << nl
+                << "Please set a non-conflicting, unique, tag by hand"
+                << " using the 'tag' entry"
+                << exit(FatalError);
+        }
+        if (debug)
+        {
+            Pout<< "processorCyclicPolyPatch " << name() << " uses tag " << tag_
+                << endl;
+        }
+    }
+    return tag_;
+}
+
 
 void Foam::processorCyclicPolyPatch::initGeometry(PstreamBuffers& pBufs)
 {
@@ -283,6 +299,11 @@ void Foam::processorCyclicPolyPatch::write(Ostream& os) const
     processorPolyPatch::write(os);
     os.writeKeyword("referPatch") << referPatchName_
         << token::END_STATEMENT << nl;
+    if (tag_ != -1)
+    {
+        os.writeKeyword("tag") << tag_
+            << token::END_STATEMENT << nl;
+    }
 }
 
 
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.H b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.H
index fa59414586b..0b9d4f70d94 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.H
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2012 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -54,12 +54,12 @@ class processorCyclicPolyPatch
 {
     // Private data
 
-        //- Message tag to use for communication
-        const int tag_;
-
         //- Name of originating patch
         const word referPatchName_;
 
+        //- Message tag to use for communication
+        mutable int tag_;
+
         //- Index of originating patch
         mutable label referPatchID_;
 
@@ -264,10 +264,7 @@ public:
         }
 
         //- Return message tag to use for communication
-        virtual int tag() const
-        {
-            return tag_;
-        }
+        virtual int tag() const;
 
         //- Does this side own the patch ?
         virtual bool owner() const
-- 
GitLab