Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
SharedSetData.hpp
Go to the documentation of this file.
1 /** \file SharedSetData.hpp
2  * \author Jason Kraftcheck
3  * \date 2011-06-23
4  */
5 
6 #ifndef moab_SHARED_SET_DATA_HPP
7 #define moab_SHARED_SET_DATA_HPP
8 
9 #include "moab/Types.hpp"
10 #include "moab/RangeMap.hpp"
11 
12 #define STRINGIFY_( X ) #X
13 #define STRINGIFY( X ) STRINGIFY_( X )
14 #ifdef MOAB_HAVE_UNORDERED_MAP
15 #include STRINGIFY( MOAB_HAVE_UNORDERED_MAP )
16 #include STRINGIFY( MOAB_HAVE_UNORDERED_SET )
17 #else
18 #include <map>
19 #include <set>
20 #endif
21 
22 #include <vector>
23 
24 namespace moab
25 {
26 
27 class Interface;
28 
29 /**\brief ParallelComm data about shared entity sets */
31 {
32  public:
33  SharedSetData( Interface& moab, int pcID, unsigned rank );
34 
36 
37  /**\brief Get ranks of sharing procs
38  *
39  * Get list of all process ranks that own at least one set that
40  * is shared with this process.
41  */
42  ErrorCode get_owning_procs( std::vector< unsigned >& ranks_out ) const;
43 
44  /**\brief Get ranks of sharing procs
45  *
46  * Get list of all process ranks with which this process the passed set.
47  * Returns an empty list for non-shared sets.
48  */
49  ErrorCode get_sharing_procs( EntityHandle entity_set, std::vector< unsigned >& ranks_out ) const;
50 
51  /**\brief Get handles for all shared sets */
52  ErrorCode get_shared_sets( Range& sets_out ) const;
53 
54  /**\brief Get handles for all sets shared with specified process */
55  ErrorCode get_shared_sets( unsigned rank, Range& sets_out ) const;
56 
57  /**\brief Get owner and owner's handle for shared set */
58  ErrorCode get_owner( EntityHandle set, unsigned& rank_out, EntityHandle& remote_handle_out ) const;
59 
60  /**\brief Get owner of shared set */
61  ErrorCode get_owner( EntityHandle set, unsigned& rank_out ) const
62  {
63  EntityHandle h;
64  return get_owner( set, rank_out, h );
65  }
66 
67  /**\brief Get owner's handle for shared set */
69  {
70  unsigned rank;
71  return get_owner( set, rank, handle_out );
72  }
73 
74  /**\brief Get local handle for shared set */
75  ErrorCode get_local_handle( unsigned owner_rank, EntityHandle remote_handle, EntityHandle& local_handle_out ) const;
76 
77  ErrorCode set_owner( EntityHandle set, unsigned owner_rank, EntityHandle owner_handle );
78 
79  /**\brief set/update sharing list for a set
80  *
81  *\NOTE sorts \c ranks vector
82  */
83  ErrorCode set_sharing_procs( EntityHandle set_handle, std::vector< unsigned >& ranks );
84 
85  private:
87 
88  /**\brief per-set tag data */
90  {
91  unsigned ownerRank;
93  const std::vector< unsigned >* sharingProcs;
94  };
95 
96  /** Shared set data: opaque tag containing struct SharedSetTagData */
98 
99  /** Map type for lookup of local handle given remote handle */
101 
102  static void append_local_handles( const ProcHandleMapType& map, Range& append_to_this );
103 
104  /** Map type for lookup of ProcHandleMapType instance by rank */
105 #ifdef MOAB_HAVE_UNORDERED_MAP
106  struct hash_vect
107  {
108  // Copied (more or less) from Boost
109  template < typename T >
110  static void hash_combine( size_t& seed, T val )
111  {
112  seed ^= MOAB_UNORDERED_MAP_NS::hash< T >().operator()( val ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
113  }
114  template < typename IT >
115  static size_t hash_range( IT it, IT last )
116  {
117  size_t seed = 0;
118  for( ; it != last; ++it )
119  hash_combine( seed, *it );
120  return seed;
121  }
122  size_t operator()( const std::vector< unsigned >& v ) const
123  {
124  return hash_range( v.begin(), v.end() );
125  }
126  };
127 
128  typedef MOAB_UNORDERED_MAP_NS::unordered_map< unsigned, ProcHandleMapType > RHMap;
129  typedef MOAB_UNORDERED_MAP_NS::unordered_set< std::vector< unsigned >, hash_vect > RProcMap;
130 #else
131  struct less_vect
132  {
133  bool operator()( const std::vector< unsigned >& a, const std::vector< unsigned >& b ) const
134  {
135  // sort by size first
136  if( a.size() != b.size() ) return a.size() < b.size();
137  // if same size, sort by first non-equal value
138  size_t i = 0;
139  while( i != a.size() && a[i] == b[i] )
140  ++i;
141  return i != a.size() && a[i] < b[i];
142  }
143  };
144 
145  typedef std::map< unsigned, ProcHandleMapType > RHMap;
146  typedef std::set< std::vector< unsigned >, less_vect > RProcMap;
147 #endif
148 
149  /** Map for lookup of ProcHandleMapType instance by rank */
151 
152  /** Storage of sharing lists */
154 };
155 
156 } // namespace moab
157 
158 #endif // moab_SHARED_SET_DATA_HPP