From 1a4190e766634274b18c83f7ddbfa87ad504f52f Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Fri, 3 Dec 2010 11:19:52 +0000
Subject: [PATCH] ENH: IOdictionary : tree distribution of master dictionary

---
 .../db/IOobjects/IOdictionary/IOdictionary.C  | 65 +++++++++-------
 src/OpenFOAM/db/regIOobject/regIOobjectRead.C | 74 +++++++++++--------
 2 files changed, 83 insertions(+), 56 deletions(-)

diff --git a/src/OpenFOAM/db/IOobjects/IOdictionary/IOdictionary.C b/src/OpenFOAM/db/IOobjects/IOdictionary/IOdictionary.C
index 3952d1b6eb3..65276015fed 100644
--- a/src/OpenFOAM/db/IOobjects/IOdictionary/IOdictionary.C
+++ b/src/OpenFOAM/db/IOobjects/IOdictionary/IOdictionary.C
@@ -55,46 +55,59 @@ void Foam::IOdictionary::readFile(const bool masterOnly)
         close();
     }
 
-    if (masterOnly)
+    if (masterOnly && Pstream::parRun())
     {
-        // Scatter master data
-        if (Pstream::master())
+        // Scatter master data using communication scheme
+
+        const List<Pstream::commsStruct>& comms =
+        (
+            (Pstream::nProcs() < Pstream::nProcsSimpleSum)
+          ? Pstream::linearCommunication()
+          : Pstream::treeCommunication()
+        );
+
+
+        // Get my communication order
+        const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
+
+        // Reveive from up
+        if (myComm.above() != -1)
         {
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                slave++
-            )
+            if (debug)
             {
-                // Note: use ASCII for now - binary IO of dictionaries is
-                // not currently supported
-                OPstream toSlave
-                (
-                    Pstream::scheduled,
-                    slave,
-                    0,
-                    UPstream::msgType(),
-                    IOstream::ASCII
-                );
-                IOdictionary::writeData(toSlave);
+                Pout<< "IOdictionary : Reading " << objectPath()
+                    << " from processor " << myComm.above() << endl;
             }
+
+            // Note: use ASCII for now - binary IO of dictionaries is
+            // not currently supported
+            IPstream fromAbove
+            (
+                Pstream::scheduled,
+                myComm.above(),
+                0,
+                IOstream::ASCII
+            );
+            IOdictionary::readData(fromAbove);
         }
-        else
+
+        // Send to my downstairs neighbours
+        forAll(myComm.below(), belowI)
         {
             if (debug)
             {
-                Pout<< "IOdictionary : Reading " << objectPath()
-                    << " from master processor " << Pstream::masterNo() << endl;
+                Pout<< "IOdictionary : Sending " << objectPath()
+                    << " to processor " << myComm.below()[belowI] << endl;
             }
-            IPstream fromMaster
+            OPstream toBelow
             (
                 Pstream::scheduled,
-                Pstream::masterNo(),
+                myComm.below()[belowI],
                 0,
+                Pstream::msgType(),
                 IOstream::ASCII
             );
-            IOdictionary::readData(fromMaster);
+            IOdictionary::writeData(toBelow);
         }
     }
 }
diff --git a/src/OpenFOAM/db/regIOobject/regIOobjectRead.C b/src/OpenFOAM/db/regIOobject/regIOobjectRead.C
index 3ec70c25166..fcf53b985f2 100644
--- a/src/OpenFOAM/db/regIOobject/regIOobjectRead.C
+++ b/src/OpenFOAM/db/regIOobject/regIOobjectRead.C
@@ -177,55 +177,69 @@ bool Foam::regIOobject::read()
         regIOobject::fileModificationChecking == timeStampMaster
      || regIOobject::fileModificationChecking == inotifyMaster;
 
-    bool ok;
+    bool ok = true;
     if (Pstream::master() || !masterOnly)
     {
+        if (IFstream::debug)
+        {
+            Pout<< "regIOobject::read() : "
+                << "reading object " << name()
+                << " from file " << endl;
+        }
         ok = readData(readStream(type()));
         close();
     }
 
-    if (masterOnly)
+    if (masterOnly && Pstream::parRun())
     {
-        // Scatter master data
-        if (Pstream::master())
-        {
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                slave++
-            )
-            {
-                // Note: use ASCII for now - binary IO of dictionaries is
-                // not currently supported
-                OPstream toSlave
-                (
-                    Pstream::scheduled,
-                    slave,
-                    0,
-                    UPstream::msgType(),
-                    IOstream::ASCII
-                );
-                writeData(toSlave);
-            }
-        }
-        else
+        // Scatter master data using communication scheme
+
+        const List<Pstream::commsStruct>& comms =
+        (
+            (Pstream::nProcs() < Pstream::nProcsSimpleSum)
+          ? Pstream::linearCommunication()
+          : Pstream::treeCommunication()
+        );
+
+
+        // Get my communication order
+        const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
+
+        // Reveive from up
+        if (myComm.above() != -1)
         {
             if (IFstream::debug)
             {
                 Pout<< "regIOobject::read() : "
                     << "reading object " << name()
-                    << " from master processor " << Pstream::masterNo()
+                    << " from processor " << myComm.above()
                     << endl;
             }
-            IPstream fromMaster
+
+            // Note: use ASCII for now - binary IO of dictionaries is
+            // not currently supported
+            IPstream fromAbove
+            (
+                Pstream::scheduled,
+                myComm.above(),
+                0,
+                IOstream::ASCII
+            );
+            ok = readData(fromAbove);
+        }
+
+        // Send to my downstairs neighbours
+        forAll(myComm.below(), belowI)
+        {
+            OPstream toBelow
             (
                 Pstream::scheduled,
-                Pstream::masterNo(),
+                myComm.below()[belowI],
                 0,
+                Pstream::msgType(),
                 IOstream::ASCII
             );
-            ok = readData(fromMaster);
+            writeData(toBelow);
         }
     }
     return ok;
-- 
GitLab