VTK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  */
27 #ifndef __vtkPBGLGraphAdapter_h
28 #define __vtkPBGLGraphAdapter_h
29 
30 #include "vtkBoostGraphAdapter.h" // for the sequential BGL adapters
31 
32 //BTX
33 #include <boost/graph/use_mpi.hpp>
34 #include <boost/graph/distributed/mpi_process_group.hpp>
35 #include <boost/graph/properties.hpp>
36 #include <boost/graph/parallel/container_traits.hpp>
37 #include <boost/property_map/parallel/local_property_map.hpp>
38 #include <boost/property_map/parallel/distributed_property_map.hpp>
39 #include <boost/serialization/base_object.hpp>
40 #include <boost/functional/hash.hpp>
41 //ETX
42 
45 
46 namespace boost {
47 
48 // Define specializations of class template property_map for
49 // vtkDirectedGraph and vtkUndirectedGraph, based on the
50 // specialization for vtkGraph.
51 #define SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS(Property) \
52  template<> \
53  struct property_map<vtkDirectedGraph *, Property> \
54  : property_map<vtkGraph *, Property> { }; \
55  \
56  template<> \
57  struct property_map<vtkUndirectedGraph *, Property> \
58  : property_map<vtkGraph *, Property> { }; \
59  \
60  template<> \
61  struct property_map<vtkDirectedGraph * const, Property> \
62  : property_map<vtkGraph *, Property> { }; \
63  \
64  template<> \
65  struct property_map<vtkUndirectedGraph * const, Property> \
66  : property_map<vtkGraph *, Property> { }
67 
68  // Property map from a vertex descriptor to the owner of the vertex
70  {
71  // Default-construct an empty (useless!) vertex-owner map
73 
74  // Construct a vertex-owner map for a specific vtkGraph
75  explicit vtkVertexOwnerMap(vtkGraph* graph)
76  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
77 
78  // The distributed graph helper that will aid in mapping vertices
79  // to their owners.
81  };
82 
83  // Property map traits for the vertex-owner map
84  template<>
86  {
90  typedef readable_property_map_tag category;
91  };
92 
93  // Retrieve the owner of the given vertex (the key)
95  get(
96  vtkVertexOwnerMap owner_map,
98  {
99  return owner_map.helper->GetVertexOwner(key);
100  }
101 
102  // State that the vertex owner property map of a vtkGraph is the
103  // vtkVertexOwnerMap
104  template<>
105  struct property_map<vtkGraph*, vertex_owner_t>
106  {
109  };
110 
112 
113  // Retrieve the vertex-owner property map from a vtkGraph
114  inline vtkVertexOwnerMap
115  get(vertex_owner_t, vtkGraph* graph)
116  {
117  return vtkVertexOwnerMap(graph);
118  }
119 
120  // Property map from a vertex descriptor to the local descriptor of
121  // the vertex
123  {
124  // Default-construct an empty (useless!) vertex-local map
126 
127  // Construct a vertex-local map for a specific vtkGraph
128  explicit vtkVertexLocalMap(vtkGraph* graph)
129  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
130 
131  // The distributed graph helper that will aid in mapping vertices
132  // to their locals.
134  };
135 
136  // Property map traits for the vertex-local map
137  template<>
139  {
140  typedef int value_type;
141  typedef int reference;
143  typedef readable_property_map_tag category;
144  };
145 
146  // Retrieve the local descriptor of the given vertex (the key)
148  get(
149  vtkVertexLocalMap local_map,
151  {
152  return local_map.helper->GetVertexIndex(key);
153  }
154 
155  // State that the vertex local property map of a vtkGraph is the
156  // vtkVertexLocalMap
157  template<>
158  struct property_map<vtkGraph*, vertex_local_t>
159  {
162  };
163 
165 
166  // Retrieve the vertex-local property map from a vtkGraph
167  inline vtkVertexLocalMap
168  get(vertex_local_t, vtkGraph* graph)
169  {
170  return vtkVertexLocalMap(graph);
171  }
172 
173  // Map from vertex descriptor to (owner, local descriptor)
175  {
177 
178  explicit vtkVertexGlobalMap(vtkGraph* graph)
179  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
180 
182  };
183 
184  template<>
186  {
187  typedef std::pair<int, vtkIdType> value_type;
188  typedef value_type reference;
190  typedef readable_property_map_tag category;
191  };
192 
194  get(
195  vtkVertexGlobalMap global_map,
197  {
198  return std::pair<int,vtkIdType>(global_map.helper->GetVertexOwner(key),
199  global_map.helper->GetVertexIndex(key));
200  }
201 
202  //
203  template<>
204  struct property_map<vtkGraph*, vertex_global_t>
205  {
208  };
209 
210  SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS(vertex_global_t);
211 
212  inline vtkVertexGlobalMap
213  get(vertex_global_t, vtkGraph* graph)
214  {
215  return vtkVertexGlobalMap(graph);
216  }
217 
218  // Map from edge descriptor to (owner, local descriptor)
220  {
222 
223  explicit vtkEdgeGlobalMap(vtkGraph* graph)
224  : helper(graph? graph->GetDistributedGraphHelper() : 0) { }
225 
227  };
228 
229  template<>
231  {
232  typedef std::pair<int, vtkIdType> value_type;
233  typedef value_type reference;
235  typedef readable_property_map_tag category;
236  };
237 
239  get(
240  vtkEdgeGlobalMap global_map,
242  {
243  return std::pair<int, vtkIdType>
244  (global_map.helper->GetEdgeOwner(key.Id), key.Id);
245  }
246 
247  //
248  template<>
249  struct property_map<vtkGraph*, edge_global_t>
250  {
253  };
254 
256 
257  inline vtkEdgeGlobalMap
258  get(edge_global_t, vtkGraph* graph)
259  {
260  return vtkEdgeGlobalMap(graph);
261  }
262 
263 #undef SUBCLASS_PROPERTY_MAP_SPECIALIZATIONS
264 
265  //===========================================================================
266  // Hash functions
267  template<>
268  struct hash<vtkEdgeType>
269  {
270  std::size_t operator()(const vtkEdgeType& edge) const
271  {
272  return hash_value(edge.Id);
273  }
274  };
275 
276 } // namespace boost
277 
278 //----------------------------------------------------------------------------
279 // Extract the process group from a vtkGraph
280 //----------------------------------------------------------------------------
281 
282 namespace boost { namespace graph { namespace parallel {
283  template<>
284  struct process_group_type<vtkGraph *>
285  {
286  typedef boost::graph::distributed::mpi_process_group type;
287  };
288 
289  template<>
290  struct process_group_type<vtkDirectedGraph *>
292 
293  template<>
294  struct process_group_type<vtkUndirectedGraph *>
296 
297  template<>
298  struct process_group_type<vtkDirectedGraph * const>
300 
301  template<>
302  struct process_group_type<vtkUndirectedGraph * const>
304 } } } // end namespace boost::graph::parallel
305 
306 boost::graph::distributed::mpi_process_group process_group(vtkGraph *graph);
307 
308 inline boost::graph::distributed::mpi_process_group
310 {
311  return process_group(static_cast<vtkGraph *>(graph));
312 }
313 
314 inline boost::graph::distributed::mpi_process_group
316 {
317  return process_group(static_cast<vtkGraph *>(graph));
318 }
319 
320 //----------------------------------------------------------------------------
321 // Serialization support for simple VTK structures
322 //----------------------------------------------------------------------------
323 
324 //----------------------------------------------------------------------------
325 template<typename Archiver>
326 void serialize(Archiver& ar, vtkEdgeBase& edge, const unsigned int)
327 {
328  ar & edge.Id;
329 }
330 
331 template<typename Archiver>
332 void serialize(Archiver& ar, vtkOutEdgeType& edge, const unsigned int)
333 {
334  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
335  & edge.Target;
336 }
337 
338 template<typename Archiver>
339 void serialize(Archiver& ar, vtkInEdgeType& edge, const unsigned int)
340 {
341  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
342  & edge.Source;
343 }
344 
345 template<typename Archiver>
346 void serialize(Archiver& ar, vtkEdgeType& edge, const unsigned int)
347 {
348  ar & boost::serialization::base_object<vtkEdgeBase>(edge)
349  & edge.Source
350  & edge.Target;
351 }
352 
353 //----------------------------------------------------------------------------
354 // Simplified tools to build distributed property maps
355 //----------------------------------------------------------------------------
356 
358 
363 typedef boost::local_property_map<boost::graph::distributed::mpi_process_group,
368 
370 
373 {
375  if (!helper)
376  {
377  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
379  }
381 
384  if (!pbglHelper)
385  {
386  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
388  }
389 
393 }
394 
396 
398 template<typename DataArray>
400 {
401  typedef boost::parallel::distributed_property_map<
402  boost::graph::distributed::mpi_process_group,
404  DataArray*> type;
405 };
407 
409 
411 template<typename DataArray>
414 {
417 
419  if (!helper)
420  {
421  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
422  return MapType();
423  }
424 
427  if (!pbglHelper)
428  {
429  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
430  return MapType();
431  }
432 
433  return MapType(pbglHelper->GetProcessGroup(),
435  array);
436 }
437 
439 
441 template<typename DataArray>
443 {
444  typedef boost::parallel::distributed_property_map<
445  boost::graph::distributed::mpi_process_group,
447  DataArray*> type;
448 };
450 
452 
454 template<typename DataArray>
456 MakeDistributedEdgePropertyMap(vtkGraph* graph, DataArray* array)
457 {
460 
462  if (!helper)
463  {
464  vtkErrorWithObjectMacro(graph, "A vtkGraph without a distributed graph helper is not a distributed graph");
465  return MapType();
466  }
467 
470  if (!pbglHelper)
471  {
472  vtkErrorWithObjectMacro(graph, "A vtkGraph with a non-Parallel BGL distributed graph helper cannot be used with the Parallel BGL");
473  return MapType();
474  }
475 
476  return MapType(pbglHelper->GetProcessGroup(),
478  array);
479 }
480 
481 #endif // __vtkPBGLGraphAdapter_h
482 // 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:268
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
#define vtkErrorWithObjectMacro(self, x)
Definition: vtkSetGet.h:475