Parcourir la source

pdevs/mpi: add example

Eric Ramat il y a 7 ans
Parent
commit
c5a382516b

+ 1 - 0
data/cluster/graphe/graphe_0.txt

@@ -0,0 +1 @@
+9 10

+ 3 - 0
data/cluster/graphe/graphe_1.txt

@@ -0,0 +1,3 @@
+5 1
+5 6
+6 1

+ 3 - 0
data/cluster/graphe/graphe_2.txt

@@ -0,0 +1,3 @@
+2 0
+2 3
+3 0

+ 3 - 0
data/cluster/graphe/graphe_3.txt

@@ -0,0 +1,3 @@
+7 4
+7 8
+8 4

+ 0 - 0
data/cluster/input_edges/input_edges_0.txt


+ 2 - 0
data/cluster/input_edges/input_edges_1.txt

@@ -0,0 +1,2 @@
+4 1
+4 5

+ 5 - 0
data/cluster/input_edges/input_edges_2.txt

@@ -0,0 +1,5 @@
+1 0
+1 2
+6 2
+9 3
+10 3

+ 0 - 0
data/cluster/input_edges/input_edges_3.txt


+ 2 - 0
data/cluster/output_edges/output_edges_0.txt

@@ -0,0 +1,2 @@
+9 3
+10 3

+ 3 - 0
data/cluster/output_edges/output_edges_1.txt

@@ -0,0 +1,3 @@
+1 0
+1 2
+6 2

+ 0 - 0
data/cluster/output_edges/output_edges_2.txt


+ 2 - 0
data/cluster/output_edges/output_edges_3.txt

@@ -0,0 +1,2 @@
+4 1
+4 5

+ 2 - 0
data/cluster/parent_connection/parent_connection_0.txt

@@ -0,0 +1,2 @@
+0 9 2 3
+0 10 2 3

+ 3 - 0
data/cluster/parent_connection/parent_connection_1.txt

@@ -0,0 +1,3 @@
+1 1 2 0
+1 1 2 2
+1 6 2 2

+ 0 - 0
data/cluster/parent_connection/parent_connection_2.txt


+ 2 - 0
data/cluster/parent_connection/parent_connection_3.txt

@@ -0,0 +1,2 @@
+3 4 1 1
+3 4 1 5

+ 4 - 0
data/cluster/partition.txt

@@ -0,0 +1,4 @@
+9 10 
+1 5 6 
+0 2 3 
+4 7 8 

+ 100 - 38
src/tests/mpi/cluster/graph_manager.hpp

@@ -39,31 +39,64 @@
 
 namespace paradevs { namespace tests { namespace mpi { namespace cluster {
 
+struct SubGraphManagerParameters
+{
+  std::vector < int > indexes;
+  std::vector < std::pair < int, int > > internals;
+  std::vector < std::pair < int, int > > inputs;
+  std::vector < std::pair < int, int > > outputs;  
+};
+
 class SubGraphManager :
-    public paradevs::pdevs::mpi::GraphManager < common::DoubleTime >
+    public paradevs::pdevs::mpi::GraphManager < common::DoubleTime,
+						SubGraphManagerParameters >
 {
 public:
   SubGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
-		  const paradevs::common::NoParameters& parameters) :
-    paradevs::pdevs::mpi::GraphManager < common::DoubleTime >(coordinator,
-							      parameters)
+		  const SubGraphManagerParameters& parameters) :
+    paradevs::pdevs::mpi::GraphManager <
+       common::DoubleTime,
+       SubGraphManagerParameters >(coordinator, parameters)
   {
-    coordinator->add_in_port("in");
-    coordinator->add_out_port("out");
-    
-    for (unsigned int i = 0; i < 1250; ++i) {
-      Simulator* m = new Simulator("m", paradevs::common::NoParameters());
-    
-      models.push_back(m);
+    for (std::vector < int >::const_iterator it = parameters.indexes.begin();
+	 it != parameters.indexes.end(); ++it) {
+      std::stringstream ss;
+      
+      ss << "m_" << *it;
+      Simulator* m = new Simulator(ss.str(), paradevs::common::NoParameters());
+      
+      models[*it] = m;
       add_child(m);
       m->add_in_port("in");
       m->add_out_port("out");
-
-      add_link(coordinator, "in", m, "in");
-      add_link(m, "out", coordinator, "out");
+    }
+    for (std::vector < std::pair < int, int > >::const_iterator it =
+	   parameters.inputs.begin(); it != parameters.inputs.end(); ++it) {
+      std::stringstream ss;
+      
+      ss << "in_" << it->first;
+      if (not coordinator->exist_in_port(ss.str())) {
+	coordinator->add_in_port(ss.str());
+      }
+      add_link(coordinator, ss.str(), models[it->second], "in");
+    }
+    for (std::vector < std::pair < int, int > >::const_iterator it =
+	   parameters.outputs.begin(); it != parameters.outputs.end(); ++it) {
+      std::stringstream ss;
+      
+      ss << "out_" << it->first;
+      if (not coordinator->exist_out_port(ss.str())) {
+	coordinator->add_out_port(ss.str());
+      }
+      add_link(models[it->first], "out", coordinator, ss.str());
+    }
+    for (std::vector < std::pair < int, int > >::const_iterator it =
+	   parameters.internals.begin(); it != parameters.internals.end();
+	 ++it) {
+      add_link(models[it->first], "out", models[it->second], "in");
     }
   }
-
+  
   void init()
   { }
   
@@ -76,21 +109,26 @@ public:
 
   virtual ~SubGraphManager()
   {
-    std::for_each(models.begin(), models.end(),
-		  std::default_delete < Simulator >());
+    for (Simulators::iterator it = models.begin(); it != models.end(); ++it) {
+      delete it->second;
+    }
   }
 
 private:
   typedef paradevs::pdevs::Simulator < common::DoubleTime,
 				       ThreeStateModel > Simulator;
-  typedef std::vector < Simulator* > Simulators;
+  typedef std::map < int, Simulator* > Simulators;
   
   Simulators models;
 };
 
 struct RootGraphManagerParameters
 {
-    std::vector < int > ranks;
+  std::vector <
+    std::vector <
+      std::pair <
+	std::pair < int, int >,
+	std::pair < int, int > > > > parents;
 };
 
 class RootGraphManager :
@@ -99,31 +137,55 @@ class RootGraphManager :
 {
 public:
     RootGraphManager(
-        common::Coordinator < common::DoubleTime >* coordinator,
+	common::Coordinator < common::DoubleTime >* coordinator,
         const RootGraphManagerParameters& parameters) :
         paradevs::pdevs::GraphManager < common::DoubleTime,
                                         RootGraphManagerParameters >(
                                             coordinator, parameters)
     {
-        for (std::vector < int >::const_iterator it = parameters.ranks.begin();
-             it != parameters.ranks.end(); ++it) {
-            std::stringstream ss;
-            ModelProxy* model = 0;
-
-            ss << "S" << *it;
-            model = new ModelProxy(ss.str(), *it, false);
-            models.push_back(model);
-            add_child(model);
-	    
-	    // connections (part)
-            model->add_out_port("out");
-            model->add_in_port("in");
-            // if (it != parameters.ranks.begin()) {
-            //     add_link(previous, "out", model, "in");
-            // }
-        }
+      int index = 0;
+      
+      for (std::vector < std::vector <
+	     std::pair < std::pair < int, int >,
+	     std::pair < int, int > > > >::const_iterator it =
+	     parameters.parents.begin(); it != parameters.parents.end();
+	   ++it) {
+	std::stringstream ss;
+	ModelProxy* model = 0;
+	
+	ss << "S_" << index;
+	model = new ModelProxy(ss.str(), index + 1, false);
+	models.push_back(model);
+	add_child(model);
+	++index;
+      }
+
+      for (std::vector < std::vector <
+	     std::pair < std::pair < int, int >,
+	     std::pair < int, int > > > >::const_iterator it =
+	     parameters.parents.begin(); it != parameters.parents.end();
+	   ++it) {
+	for (std::vector <
+	       std::pair < std::pair < int, int >,
+	       std::pair < int, int > > >::const_iterator it2 =
+	       it->begin(); it2 != it->end(); ++it2) {
+	  std::stringstream out_ss;
+	  std::stringstream in_ss;
+
+	  out_ss << "out_" << it2->first.second;
+	  if (not models[it2->first.first]->exist_out_port(out_ss.str())) {
+	    models[it2->first.first]->add_out_port(out_ss.str());
+	  }
+	  in_ss << "in_" << it2->second.second;
+	  if (not models[it2->second.first]->exist_in_port(in_ss.str())) {
+	    models[it2->second.first]->add_in_port(in_ss.str());
+	  }
+	  add_link(models[it2->first.first], out_ss.str(),
+		   models[it2->second.first], in_ss.str());
+	}
+      }
     }
-
+  
     virtual ~RootGraphManager()
     {
         std::for_each(models.begin(), models.end(),

+ 167 - 12
src/tests/mpi/cluster/main.cpp

@@ -28,18 +28,174 @@
 
 #include <boost/mpi/environment.hpp>
 #include <boost/mpi/communicator.hpp>
+#include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
 
 #include <paradevs/common/time/DoubleTime.hpp>
 #include <paradevs/kernel/pdevs/mpi/LogicalProcessor.hpp>
 #include <tests/mpi/cluster/graph_manager.hpp>
 
 #include <chrono>
+#include <fstream>
 
 using namespace paradevs::tests::mpi::cluster;
 using namespace paradevs::common;
 using namespace boost::mpi;
 using namespace std::chrono;
 
+std::vector < std::vector < int > > clusters;
+std::vector < std::vector < std::pair < int, int > > > graphs;
+std::vector < std::vector < std::pair < int, int > > > inputs;
+std::vector < std::vector < std::pair < int, int > > > outputs;
+std::vector < std::vector < std::pair <
+			      std::pair < int, int >,
+			      std::pair < int, int > > > > parents;
+
+void read_graphs()
+{
+
+  std::cout << "READ GRAPHS" << std::endl;
+  
+  for (int index = 0; index < clusters.size(); ++index) {
+    std::stringstream ss;
+    ss << "../data/cluster/graphe/graphe_" << index << ".txt";
+    std::ifstream graphFile(ss.str());
+
+    graphs.push_back(std::vector < std::pair < int, int > >());
+    while (not graphFile.eof()) {
+      std::string str;
+      std::vector < std::string > strs;
+      
+      std::getline(graphFile, str);
+      if (str.size() > 0) { 
+	boost::split(strs, str, boost::is_any_of(" "));
+	graphs[graphs.size() - 1].
+	  push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
+				   boost::lexical_cast < int >(strs[1])));
+      }
+    }
+    graphFile.close();
+  }
+}
+
+void read_inputs()
+{
+
+  std::cout << "READ INPUTS" << std::endl;
+  
+  for (int index = 0; index < clusters.size(); ++index) {
+    std::stringstream ss;
+    ss << "../data/cluster/input_edges/input_edges_" << index << ".txt";
+    std::ifstream inputsFile(ss.str());
+
+    inputs.push_back(std::vector < std::pair < int, int > >());
+    while (not inputsFile.eof()) {
+      std::string str;
+      std::vector < std::string > strs;
+      
+      std::getline(inputsFile, str);
+      if (str.size() > 0) { 
+	boost::split(strs, str, boost::is_any_of(" "));
+	inputs[inputs.size() - 1].
+	  push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
+				   boost::lexical_cast < int >(strs[1])));
+      }
+    }
+    inputsFile.close();
+  }
+}
+
+void read_outputs()
+{
+
+  std::cout << "READ OUTPUTS" << std::endl;
+  
+  for (int index = 0; index < clusters.size(); ++index) {
+    std::stringstream ss;
+    ss << "../data/cluster/output_edges/output_edges_" << index << ".txt";
+    std::ifstream outputsFile(ss.str());
+
+    outputs.push_back(std::vector < std::pair < int, int > >());
+    while (not outputsFile.eof()) {
+      std::string str;
+      std::vector < std::string > strs;
+      
+      std::getline(outputsFile, str);
+      if (str.size() > 0) { 
+	boost::split(strs, str, boost::is_any_of(" "));
+	outputs[outputs.size() - 1].
+	  push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
+				   boost::lexical_cast < int >(strs[1])));
+      }
+    }
+    outputsFile.close();
+  }
+}
+
+void read_parents()
+{
+
+  std::cout << "READ PARENTS" << std::endl;
+  
+  for (int index = 0; index < clusters.size(); ++index) {
+    std::stringstream ss;
+    ss << "../data/cluster/parent_connection/parent_connection_" << index
+       << ".txt";
+    std::ifstream parentsFile(ss.str());
+
+    parents.push_back(std::vector < std::pair < std::pair < int, int >,
+		      std::pair < int, int > > >());
+    while (not parentsFile.eof()) {
+      std::string str;
+      std::vector < std::string > strs;
+      
+      std::getline(parentsFile, str);
+      if (str.size() > 0) { 
+	boost::split(strs, str, boost::is_any_of(" "));
+	parents[parents.size() - 1].
+	  push_back(std::make_pair(
+	     std::make_pair(boost::lexical_cast < int >(strs[0]),
+			    boost::lexical_cast < int >(strs[1])),
+	     std::make_pair(boost::lexical_cast < int >(strs[2]),
+			    boost::lexical_cast < int >(strs[3]))));
+      }
+    }
+    parentsFile.close();
+  }
+}
+
+void read()
+{
+
+  std::cout << "READ CLUSTERS" << std::endl;
+  
+  std::ifstream clusterFile("../data/cluster/partition.txt");
+
+  while (not clusterFile.eof()) {
+    std::string str;
+    std::vector < std::string > strs;
+
+    std::getline(clusterFile, str);
+    boost::split(strs, str, boost::is_any_of(" "));
+
+    if (str.size() > 0) {
+      clusters.push_back(std::vector < int >());
+      for (std::vector < std::string >::const_iterator it = strs.begin();
+	   it != strs.end(); ++it) {
+	if (it->size() > 0) {
+	  clusters[clusters.size() - 1].
+	    push_back(boost::lexical_cast < int >(*it));
+	}
+      }
+    }
+  }
+  clusterFile.close();
+  read_graphs();
+  read_inputs();
+  read_outputs();
+  read_parents();
+}
+
 void example_simple(int argc, char *argv[])
 {
     environment env(argc, argv);
@@ -50,15 +206,7 @@ void example_simple(int argc, char *argv[])
     if (world.rank() == 0) {
         paradevs::tests::mpi::cluster::RootGraphManagerParameters parameters;
 
-        parameters.ranks.push_back(1);
-        parameters.ranks.push_back(2);
-        parameters.ranks.push_back(3);
-        parameters.ranks.push_back(4);
-        parameters.ranks.push_back(5);
-        parameters.ranks.push_back(6);
-        parameters.ranks.push_back(7);
-        parameters.ranks.push_back(8);
-
+	parameters.parents = parents;
         paradevs::common::RootCoordinator <
             DoubleTime,
             paradevs::pdevs::Coordinator <
@@ -80,14 +228,20 @@ void example_simple(int argc, char *argv[])
         std::cout << "time = " << time_span.count() << std::endl;
 
     } else {
+        paradevs::tests::mpi::cluster::SubGraphManagerParameters parameters;
         std::stringstream ss;
 
+	parameters.indexes = clusters[world.rank() - 1];
+	parameters.inputs = inputs[world.rank() - 1];
+	parameters.outputs = outputs[world.rank() - 1];
+	parameters.internals = graphs[world.rank() - 1];
         ss << "S" << world.rank();
 	paradevs::pdevs::mpi::Coordinator <
 	  DoubleTime,
-	  paradevs::tests::mpi::cluster::SubGraphManager >
-	model(ss.str(), paradevs::common::NoParameters(),
-	      paradevs::common::NoParameters());
+	  paradevs::tests::mpi::cluster::SubGraphManager,
+                paradevs::common::NoParameters,
+                paradevs::tests::mpi::cluster::SubGraphManagerParameters >
+	model(ss.str(), paradevs::common::NoParameters(), parameters);
 	paradevs::pdevs::mpi::LogicalProcessor <
 	  DoubleTime > LP(&model, world.rank(), 0);
 	
@@ -99,5 +253,6 @@ void example_simple(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
   example_simple(argc, argv);
+  read();
   return 0;
 }