From e676f9472f8027931974ee11c2d19c1e15e98b95 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 31 Jan 2013 16:55:32 +0000
Subject: [PATCH] ENH: parallel-communicators: user defined communicators test

---
 .../test/parallel-communicators/Make/files    |   3 +
 .../test/parallel-communicators/Make/options  |   0
 .../Test-parallel-communicators.C             | 200 ++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 applications/test/parallel-communicators/Make/files
 create mode 100644 applications/test/parallel-communicators/Make/options
 create mode 100644 applications/test/parallel-communicators/Test-parallel-communicators.C

diff --git a/applications/test/parallel-communicators/Make/files b/applications/test/parallel-communicators/Make/files
new file mode 100644
index 00000000000..ab261d4da9d
--- /dev/null
+++ b/applications/test/parallel-communicators/Make/files
@@ -0,0 +1,3 @@
+Test-parallel-communicators.C
+
+EXE = $(FOAM_USER_APPBIN)/Test-parallel-communicators
diff --git a/applications/test/parallel-communicators/Make/options b/applications/test/parallel-communicators/Make/options
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/applications/test/parallel-communicators/Test-parallel-communicators.C b/applications/test/parallel-communicators/Test-parallel-communicators.C
new file mode 100644
index 00000000000..e5c4b996482
--- /dev/null
+++ b/applications/test/parallel-communicators/Test-parallel-communicators.C
@@ -0,0 +1,200 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2013 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Application
+    Test-parallel-communicators
+
+Description
+    Checks communication using user-defined communicators
+
+\*---------------------------------------------------------------------------*/
+
+#include "argList.H"
+#include "Time.H"
+#include "IPstream.H"
+#include "OPstream.H"
+#include "vector.H"
+#include "IOstreams.H"
+#include "PstreamReduceOps.H"
+
+using namespace Foam;
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+scalar sumReduce
+(
+    const label comm,
+    const scalar localValue
+)
+{
+    scalar sum;
+    if (Pstream::parRun())
+    {
+        if (UPstream::master(comm))
+        {
+            // Add master value and all slaves
+            sum = localValue;
+
+            for
+            (
+                int slave=Pstream::firstSlave();
+                slave<=Pstream::lastSlave(comm);
+                slave++
+            )
+            {
+                scalar slaveValue;
+                UIPstream::read
+                (
+                    Pstream::blocking,
+                    slave,
+                    reinterpret_cast<char*>(&slaveValue),
+                    sizeof(scalar),
+                    UPstream::msgType(),    // tag
+                    comm                    // communicator
+                );
+
+                sum += slaveValue;
+            }
+
+            // Send back to slaves
+
+            for
+            (
+                int slave=UPstream::firstSlave();
+                slave<=UPstream::lastSlave(comm);
+                slave++
+            )
+            {
+                UOPstream::write
+                (
+                    UPstream::blocking,
+                    slave,
+                    reinterpret_cast<const char*>(&sum),
+                    sizeof(scalar),
+                    UPstream::msgType(),    // tag
+                    comm                    // communicator
+                );
+            }
+        }
+        else
+        {
+            {
+                UOPstream::write
+                (
+                    UPstream::blocking,
+                    UPstream::masterNo(),
+                    reinterpret_cast<const char*>(&localValue),
+                    sizeof(scalar),
+                    UPstream::msgType(),    // tag
+                    comm                    // communicator
+                );
+            }
+
+            {
+                UIPstream::read
+                (
+                    UPstream::blocking,
+                    UPstream::masterNo(),
+                    reinterpret_cast<char*>(&sum),
+                    sizeof(scalar),
+                    UPstream::msgType(),    // tag
+                    comm                    // communicator
+                );
+            }
+        }
+    }
+    return sum;
+}
+
+
+int main(int argc, char *argv[])
+{
+
+#   include "setRootCase.H"
+#   include "createTime.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+    // Allocate a communicator
+    label n = Pstream::nProcs(UPstream::worldComm);
+
+    DynamicList<label> bottom;
+    DynamicList<label> top;
+
+    for (label i = 0; i < n/2; i++)
+    {
+        bottom.append(i);
+    }
+    for (label i = n/2; i < n; i++)
+    {
+        top.append(i);
+    }
+
+    Pout<< "bottom:" << bottom << endl;
+    Pout<< "top:" << top << endl;
+
+
+    scalar localValue = 111*UPstream::myProcNo(UPstream::worldComm);
+    Pout<< "localValue:" << localValue << endl;
+
+    if (Pstream::myProcNo(UPstream::worldComm) < n/2)
+    {
+        label comm = Pstream::allocateCommunicator
+        (
+            UPstream::worldComm,
+            bottom
+        );
+
+        Pout<< "allocated bottom comm:" << comm << endl;
+        Pout<< "comm myproc          :" << Pstream::myProcNo(comm)
+            << endl;
+        scalar sum = sumReduce(comm, localValue);
+        Pout<< "sum                  :" << sum << endl;
+
+        Pstream::freeCommunicator(comm);
+    }
+    else
+    {
+        label comm = Pstream::allocateCommunicator
+        (
+            UPstream::worldComm,
+            top
+        );
+
+        Pout<< "allocated top comm:" << comm << endl;
+        Pout<< "comm myproc       :" << Pstream::myProcNo(comm)
+            << endl;
+        scalar sum = sumReduce(comm, localValue);
+        Pout<< "sum                  :" << sum << endl;
+
+        Pstream::freeCommunicator(comm);
+    }
+
+
+    Pout<< "End\n" << endl;
+
+    return 0;
+}
+
+
+// ************************************************************************* //
-- 
GitLab