VTK
vtkPBGLGraphAdapter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPBGLGraphAdapter.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*
16  * Copyright (C) 2008 The Trustees of Indiana University.
17  * Use, modification and distribution is subject to the Boost Software
18  * License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
19  */
28 #ifndef vtkPBGLGraphAdapter_h
29 #define vtkPBGLGraphAdapter_h
30 
31 #include "vtkBoostGraphAdapter.h" // for the sequential BGL adapters
32 
33 //BTX
34 #include <vtksys/stl/map> // required for Boost 1.54.0
35 #include <boost/graph/use_mpi.hpp>
36 #include <boost/graph/distributed/mpi_process_group.hpp>
37 #include <boost/graph/properties.hpp>
38 #include <boost/graph/parallel/container_traits.hpp>
39 #include <boost/property_map/parallel/local_property_map.hpp>
40 #include <boost/property_map/parallel/distributed_property_map.hpp>
41 #include <boost/serialization/base_object.hpp>
42 #include <boost/functional/hash.hpp>
43 //ETX
44 
47 
48 namespace boost {
49 
50 // Define specializations of class template property_map for
51 // vtkDirectedGraph and vtkUndirectedGraph, based on the
52 // specialization for vtkGraph.
53 #define SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS(Property) \
54  template<> \
55  struct property_map<vtkDirectedGraph *, Property> \
56  : property_map<vtkGraph *, Property> { }; \
57  \
58  template<> \
59  struct property_map<vtkUndirectedGraph *, Property> \
60  : property_map<vtkGraph *, Property> { }; \
61  \
62  template<> \
63  struct property_map<vtkDirectedGraph * const, Property> \
64  : property_map<vtkGraph *, Property> { }; \
65  \
66  template<> \
67  struct property_map<vtkUndirectedGraph * const, Property> \
68  : property_map<vtkGraph *, Property> { }
69 
70  // Property map from a vertex descriptor to the owner of the vertex
72  {
73  // Default-construct an empty (useless!) vertex-owner map
75 
76  // Construct a vertex-owner map for a specific vtkGraph
77  explicit vtkVertexOwnerMap(vtkGraph* graph)
78  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
79 
80  // The distributed graph helper that will aid in mapping vertices
81  // to their owners.
83  };
84 
85  // Property map traits for the vertex-owner map
86  template<>
88  {
92  typedef readable_property_map_tag category;
93  };
94 
95  // Retrieve the owner of the given vertex (the key)
97  get(
98  vtkVertexOwnerMap owner_map,
100  {
101  return owner_map.helper->GetVertexOwner(key);
102  }
103 
104  // State that the vertex owner property map of a vtkGraph is the
105  // vtkVertexOwnerMap
106  template<>
107  struct property_map<vtkGraph*, vertex_owner_t>
108  {
111  };
112 
114 
115  // Retrieve the vertex-owner property map from a vtkGraph
116  inline vtkVertexOwnerMap
117  get(vertex_owner_t, vtkGraph* graph)
118  {
119  return vtkVertexOwnerMap(graph);
120  }
121 
122  // Property map from a vertex descriptor to the local descriptor of
123  // the vertex
125  {
126  // Default-construct an empty (useless!) vertex-local map
128 
129  // Construct a vertex-local map for a specific vtkGraph
130  explicit vtkVertexLocalMap(vtkGraph* graph)
131  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
132 
133  // The distributed graph helper that will aid in mapping vertices
134  // to their locals.
136  };
137 
138  // Property map traits for the vertex-local map
139  template<>
141  {
142  typedef int value_type;
143  typedef int reference;
145  typedef readable_property_map_tag category;
146  };
147 
148  // Retrieve the local descriptor of the given vertex (the key)
150  get(
151  vtkVertexLocalMap local_map,
153  {
154  return local_map.helper->GetVertexIndex(key);
155  }
156 
157  // State that the vertex local property map of a vtkGraph is the
158  // vtkVertexLocalMap
159  template<>
160  struct property_map<vtkGraph*, vertex_local_t>
161  {
164  };
165 
167 
168  // Retrieve the vertex-local property map from a vtkGraph
169  inline vtkVertexLocalMap
170  get(vertex_local_t, vtkGraph* graph)
171  {
172  return vtkVertexLocalMap(graph);
173  }
174 
175  // Map from vertex descriptor to (owner, local descriptor)
177  {
179 
180  explicit vtkVertexGlobalMap(vtkGraph* graph)
181  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
182 
184  };
185 
186  template<>
188  {
189  typedef std::pair<int, vtkIdType> value_type;
190  typedef value_type reference;
192  typedef readable_property_map_tag category;
193  };
194 
196  get(
197  vtkVertexGlobalMap global_map,
199  {
200  return std::pair<int,vtkIdType>(global_map.helper->GetVertexOwner(key),
201  global_map.helper->GetVertexIndex(key));
202  }
203 
204  //
205  template<>
206  struct property_map<vtkGraph*, vertex_global_t>
207  {
210  };
211 
212  SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS(vertex_global_t);
213 
214  inline vtkVertexGlobalMap
215  get(vertex_global_t, vtkGraph* graph)
216  {
217  return vtkVertexGlobalMap(graph);
218  }
219 
220  // Map from edge descriptor to (owner, local descriptor)
222  {
224 
225  explicit vtkEdgeGlobalMap(vtkGraph* graph)
226  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
227 
229  };
230 
231  template<>
233  {
234  typedef std::pair<int, vtkIdType> value_type;
235  typedef value_type reference;
237  typedef readable_property_map_tag category;
238  };
239 
241  get(
242  vtkEdgeGlobalMap global_map,
244  {
245  return std::pair<int, vtkIdType>
246  (global_map.helper->GetEdgeOwner(key.Id), key.Id);
247  }
248 
249  //
250  template<>
251  struct property_map<vtkGraph*, edge_global_t>
252  {
255  };
256 
258 
259  inline vtkEdgeGlobalMap
260  get(edge_global_t, vtkGraph* graph)
261  {
262  return vtkEdgeGlobalMap(graph);
263  }
264 
265 #undef SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS
266 
267  //===========================================================================
268  // Hash functions
269  template<>
270  struct hash<vtkEdgeType>
271  {
272  std::size_t operator()(const vtkEdgeType& edge) const
273  {
274  return hash_value(edge.Id);
275  }
276  };
277 
278 } // namespace boost
279 
280 //----------------------------------------------------------------------------
281 // Extract the process group from a vtkGraph
282 //----------------------------------------------------------------------------
283 
284 namespace boost { namespace graph { namespace parallel {
285  template<>
286  struct process_group_type<vtkGraph *>
287  {
288  typedef boost::graph::distributed::mpi_process_group type;
289  };
290 
291  template<>
292  struct process_group_type<vtkDirectedGraph *>
294 
295  template<>
296  struct process_group_type<vtkUndirectedGraph *>
298 
299  template<>
300  struct process_group_type<vtkDirectedGraph * const>
302 
303  template<>
304  struct process_group_type<vtkUndirectedGraph * const>
306 } } } // end namespace boost::graph::parallel
307 
308 boost::graph::distributed::mpi_process_group process_group(vtkGraph *graph);
309 
310 inline boost::graph::distributed::mpi_process_group
312 {
313  return process_group(static_cast<vtkGraph *>(graph));
314 }
315 
316 inline boost::graph::distributed::mpi_process_group
318 {
319  return process_group(static_cast<vtkGraph *>(graph));
320 }
321 
322 //----------------------------------------------------------------------------
323 // Serialization support for simple VTK structures
324 //----------------------------------------------------------------------------
325 
326 //----------------------------------------------------------------------------
327 template<typename Archiver>
328 void serialize(Archiver& ar, vtkEdgeBase& edge, const unsigned int)
329 {
330  ar & edge.Id;
331 }
332 
333 template<typename Archiver>
334 void serialize(Archiver& ar, vtkOutEdgeType& edge, const unsigned int)
335 {
336  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
337  & edge.Target;
338 }
339 
340 template<typename Archiver>
341 void serialize(Archiver& ar, vtkInEdgeType& edge, const unsigned int)
342 {
343  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
344  & edge.Source;
345 }
346 
347 template<typename Archiver>
348 void serialize(Archiver& ar, vtkEdgeType& edge, const unsigned int)
349 {
350  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
351  & edge.Source
352  & edge.Target;
353 }
354 
355 //----------------------------------------------------------------------------
356 // Simplified tools to build distributed property maps
357 //----------------------------------------------------------------------------
358 
360 
365 typedef boost::local_property_map<boost::graph::distributed::mpi_process_group,
370 
372 
375 {
377  if (!helper)
378  {
379  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
381  }
383 
386  if (!pbglHelper)
387  {
388  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
390  }
391 
395 }
396 
398 
400 template<typename DataArray>
402 {
403  typedef boost::parallel::distributed_property_map<
404  boost::graph::distributed::mpi_process_group,
406  DataArray*> type;
407 };
409 
411 
413 template<typename DataArray>
416 {
419 
421  if (!helper)
422  {
423  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
424  return MapType();
425  }
426 
429  if (!pbglHelper)
430  {
431  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
432  return MapType();
433  }
434 
435  return MapType(pbglHelper->GetProcessGroup(),
437  array);
438 }
439 
441 
443 template<typename DataArray>
445 {
446  typedef boost::parallel::distributed_property_map<
447  boost::graph::distributed::mpi_process_group,
449  DataArray*> type;
450 };
452 
454 
456 template<typename DataArray>
458 MakeDistributedEdgePropertyMap(vtkGraph* graph, DataArray* array)
459 {
462 
464  if (!helper)
465  {
466  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
467  return MapType();
468  }
469 
472  if (!pbglHelper)
473  {
474  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
475  return MapType();
476  }
477 
478  return MapType(pbglHelper->GetProcessGroup(),
480  array);
481 }
482 
483 #endif // vtkPBGLGraphAdapter_h
484 // VTK-HeaderTest-Exclude: vtkPBGLGraphAdapter.h
helper for the vtkGraph class that allows the graph to be distributed across multiple memory spaces...
SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS(vertex_owner_t)
Forward declaration required for Boost serialization.
boost::parallel::distributed_property_map< boost::graph::distributed::mpi_process_group, boost::vtkVertexGlobalMap, DataArray * > type
vtkIdType GetEdgeOwner(vtkIdType e_id) const
vtkIdType GetVertexOwner(vtkIdType v) const
An undirected graph.
boost::graph::distributed::mpi_process_group GetProcessGroup()
vtkVertexOwnerMap(vtkGraph *graph)
int vtkIdType
Definition: vtkType.h:275
vtkGraphDistributedVertexIndexMap MakeDistributedVertexIndexMap(vtkGraph *graph)
vtkDistributedGraphHelper * helper
vtkDistributedGraphHelper * helper
A directed graph.
Base class for graph data types.
Definition: vtkGraph.h:288
vtkIdType GetVertexIndex(vtkIdType v) const
static vtkPBGLDistributedGraphHelper * SafeDownCast(vtkObjectBase *o)
vtkIdType Source
Definition: vtkGraph.h:283
boost::parallel::distributed_property_map< boost::graph::distributed::mpi_process_group, boost::vtkEdgeGlobalMap, DataArray * > type
boost::local_property_map< boost::graph::distributed::mpi_process_group, boost::vtkVertexGlobalMap, boost::vtkGraphIndexMap > vtkGraphDistributedVertexIndexMap
vtkEdgeGlobalMap(vtkGraph *graph)
vtkIdType Id
Definition: vtkGraph.h:255
boost::graph::distributed::mpi_process_group process_group(vtkGraph *graph)
vtkIdType Target
Definition: vtkGraph.h:264
vtkDistributedEdgePropertyMapType< DataArray >::type MakeDistributedEdgePropertyMap(vtkGraph *graph, DataArray *array)
vtkDistributedVertexPropertyMapType< DataArray >::type MakeDistributedVertexPropertyMap(vtkGraph *graph, DataArray *array)
vtkDistributedGraphHelper * GetDistributedGraphHelper()
vtkDistributedGraphHelper * helper
vtkIdType Source
Definition: vtkGraph.h:273
vtkDistributedGraphHelper * helper
vtkVertexGlobalMap(vtkGraph *graph)
std::size_t operator()(const vtkEdgeType &edge) const
boost::graph::distributed::mpi_process_group type
void serialize(Archiver &ar, vtkEdgeBase &edge, const unsigned int)
vtkVertexLocalMap(vtkGraph *graph)
vtkIdType Target
Definition: vtkGraph.h:284
end namespace boost::graph::distributed