COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
Partitioner.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 // coolfluid
8 #include "common/Builder.hpp"
9 #include "common/OptionList.hpp"
10 #include "common/OptionT.hpp"
11 #include "common/Log.hpp"
12 #include "common/PE/Comm.hpp"
14 #include "mesh/Dictionary.hpp"
15 
16 namespace cf3 {
17 namespace mesh {
18 namespace ptscotch {
19 
20  using namespace common;
21  using namespace common::PE;
22 
24 
26 
28 
29 Partitioner::Partitioner ( const std::string& name ) :
30  MeshPartitioner(name)
31 {
32  // initialize the graph
33  if (SCOTCH_dgraphInit(&graph, Comm::instance().communicator()))
34  throw BadValue(FromHere(),"ptscotch error");
35 }
36 
38 
40 {
41  // delete the graph
42  SCOTCH_dgraphExit(&graph);
43 }
44 
46 
48 {
50 
51  // resize vertloctab to the number of owned objects
52  // +1 because of compact form without holes in global numbering
54 
55  // copy number of outgoing edges per object in vertloctab
57 
58  // baseval is C style
59  baseval = 0;
60 
61  // set local number of objects
63 
64  // set maximum local number of objects
66 
67  // Convert vertloctab to pt-scotch format
68  Uint nb_edges;
69  Uint total_nb_edges = 0;
70  for (int i=0; i<vertlocnbr; ++i)
71  {
72  nb_edges=vertloctab[i];
73  vertloctab[i]=total_nb_edges;
74  total_nb_edges+=nb_edges;
75  }
76  vertloctab[vertlocnbr] = total_nb_edges;
77 
78  // total number of outgoing edges
79  edgelocnbr = total_nb_edges;
80  edgelocsiz = total_nb_edges;
81  edgegsttab.resize(total_nb_edges);
82  edgeloctab.resize(total_nb_edges);
83 
84  cf3_assert(edgelocsiz >= vertloctab[vertlocnbr]);
85 
87 
88  if (SCOTCH_dgraphBuild(&graph,
89  baseval,
90  vertlocnbr, // number of local vertices (for creation of proccnttab)
91  vertlocmax, // max number of local vertices to be created (for creation of procvrttab)
92  &vertloctab[0], // local adjacency index array (size = vertlocnbr+1 if vendloctab matches or is null)
93  &vertloctab[1], // (optional) local adjacency end index array
94  NULL, //veloloctab, // (optional) local vertex load array
95  NULL, //vlblocltab, // (optional) local vertex label array (size = vertlocnbr+1)
96  edgelocnbr, // total number of arcs (twice number of edges)
97  edgelocsiz, // minimum size of the edge array required to encompass all used adjacency values (at least equal to the max of vendloctab entries)
98  &edgeloctab[0], // edgeloctab, local adjacency array which stores global indices
99  &edgegsttab[0], // edgegsttab, // (optional) if passed it is assumed an empty array that will be filled by SCOTHC_dgraphGhst if required
100  NULL)) //edloloctab)) // (optional) arc load array of size edgelocsiz
101  throw BadValue(FromHere(),"Could not build PT-scotch graph");
102 
103 
104  SCOTCH_dgraphSize(&graph,
105  &vertglbnbr,
106  &vertlocnbr,
107  &edgeglbnbr,
108  &edgelocnbr);
109 
110  proccnttab.resize(Comm::instance().size());
111  procvrttab.resize(Comm::instance().size()+1);
112 
114  //boost::mpi::communicator world;
115  //boost::mpi::all_gather(world, vertlocnbr, proccnttab);
116  Comm::instance().all_gather(vertlocnbr, proccnttab);
117 
119 
120  Uint cnt=0;
121  for (Uint p=0; p<proccnttab.size(); ++p)
122  {
123  procvrttab[p] = cnt;
124  cnt += proccnttab[p];
125  }
126  procvrttab[Comm::instance().size()] = cnt;
127 
128  // CFinfo << "\n" << CFendl;
129  // CFinfo << "global graph info" << CFendl;
130  // CFinfo << "-----------------" << CFendl;
131  // CFLogVar(vertglbnbr);
132  // CFLogVar(edgeglbnbr);
133  // CFinfo << "proccnttab = [ ";
134  // for (Uint i=0; i<Comm::instance().size(); ++i)
135  // CFinfo << proccnttab[i] << " ";
136  // CFinfo << "]" << CFendl;
137  // CFinfo << "procvrttab = [ ";
138  // for (Uint i=0; i<Comm::instance().size()+1; ++i)
139  // CFinfo << procvrttab[i] << " ";
140  // CFinfo << "]" << CFendl;
141  //
142  // CFinfo << CFendl << CFendl;
143  //
144  if (SCOTCH_dgraphCheck(&graph))
145  throw BadValue(FromHere(),"There is an error in the PT-scotch graph");
146 }
147 
149 
151 {
152  //PECheckPoint(1,"begin partition_graph()");
154 
155  SCOTCH_Strat stradat;
156  if(SCOTCH_stratInit(&stradat))
157  throw BadValue (FromHere(), "Could not initialze a PT-scotch strategy");
158 
159  partloctab.resize(vertlocmax);
161 
162  //PECheckPoint(1," begin SCOTCH_dgraphPart()");
163  if (SCOTCH_dgraphPart(&graph,
164  options().value<Uint>("nb_parts"),
165  &stradat,
166  &partloctab[0]))
167  throw BadValue (FromHere(), "Could not partition PT-scotch graph");
168  //PECheckPoint(1," end SCOTCH_dgraphPart()");
169  SCOTCH_stratExit(&stradat);
171 
172  std::vector<Uint> owned_objects(vertlocnbr);
173  list_of_objects_owned_by_part(Comm::instance().rank(),owned_objects);
174 
175 // Uint nb_changes = 0;
176 // for (int i=0; i<vertlocnbr; ++i)
177 // {
178 // if ((Uint) partloctab[i] != Comm::instance().rank())
179 // {
180 // ++nb_changes;
181 // }
182 // }
183 
184 // m_changes->reserve(nb_changes);
185 // for (int i=0; i<vertlocnbr; ++i)
186 // {
187 // if ((Uint)partloctab[i] != Comm::instance().rank())
188 // {
189 // m_changes->insert_blindly(owned_objects[i],partloctab[i]);
190 // }
191 // }
192 
193 // m_changes->sort_keys();
195 
196 
197  Uint comp; Uint loc_idx; bool found;
198  for (int i=0; i<vertlocnbr; ++i)
199  {
200  if ((Uint)partloctab[i] != Comm::instance().rank())
201  {
202  boost::tie(comp,loc_idx) = location_idx(owned_objects[i]);
203  if (comp == 0) // if is node
204  m_nodes_to_export[partloctab[i]].push_back(loc_idx);
205  else
206  m_elements_to_export[comp-1][partloctab[i]].push_back(loc_idx);
207  }
208  }
209 
210  // PECheckPoint(1,"end partition_graph()");
211 }
212 
214 
215 
217 
218 } // ptscotch
219 } // mesh
220 } // CF
std::vector< SCOTCH_Num > vertloctab
Definition: Partitioner.hpp:68
std::string name(ComponentWrapper &self)
void list_of_objects_owned_by_part(const Uint part, VectorT &obj_list) const
Helper class to create the Builder and place it in the factory.
Definition: Builder.hpp:212
cf3::common::ComponentBuilder< Partitioner, MeshTransformer, LibPTScotch > ptscotch_partitioner_builder
Definition: Partitioner.cpp:25
#define cf3_assert(a)
Definition: Assertions.hpp:93
virtual void build_graph()
Partitioning functions.
Definition: Partitioner.cpp:47
void list_of_connected_objects_in_part(const Uint part, VectorT &connections_per_obj, WeightsT &edge_weights) const
boost::tuple< Uint, Uint > location_idx(const Uint glb_obj) const
Uint nb_objects_owned_by_part(const Uint part) const
Graph building functions.
SCOTCH_Dgraph graph
common values accessed by all tests goes here
Definition: Partitioner.hpp:55
#define CF3_DEBUG_POINT
Definition of a macro for placing a debug point in the code.
Definition: Log.hpp:125
Uint size() const
Return the number of processes, or 1 if is_init==0.
Definition: Comm.cpp:145
std::vector< SCOTCH_Num > edgeloctab
Definition: Partitioner.hpp:69
std::vector< SCOTCH_Num > proccnttab
Definition: Partitioner.hpp:72
virtual ~Partitioner()
Virtual destructor.
Definition: Partitioner.cpp:39
Partitioner(const std::string &name)
type of pointer to Component
Definition: Partitioner.cpp:29
Top-level namespace for coolfluid.
Definition: Action.cpp:18
std::vector< std::vector< Uint > > m_nodes_to_export
nodes_to_export[part][loc_node_idx]
std::vector< SCOTCH_Num > procvrttab
Definition: Partitioner.hpp:73
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
std::vector< SCOTCH_Num > partloctab
Definition: Partitioner.hpp:71
OptionList & options()
Definition: Component.cpp:856
std::vector< std::vector< std::vector< Uint > > > m_elements_to_export
elements_to_export[part][elements_comp_idx][loc_elem_idx]
static Comm & instance()
Return a reference to the current PE.
Definition: Comm.cpp:44
Uint nb_connected_objects_in_part(const Uint part, VectorT &nb_connections_per_obj) const
T * all_gather(const T *in_values, const int in_n, T *out_values, const int stride=1)
Definition: Comm.hpp:192
std::vector< SCOTCH_Num > edgegsttab
Definition: Partitioner.hpp:70
#define FromHere()
Send comments to:
COOLFluiD Web Admin