Interface Documentation
Version: invalid
id.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #if defined(_MSC_VER)
19 #include "uint128.h"
20 #endif
21 
22 #include <cassert>
23 #include <climits>
24 #include <cstdint>
25 #include <iostream>
26 
27 namespace flecsi {
28 namespace util {
29 
30 using local_id_t = __uint128_t;
31 
32 template<std::size_t PBITS,
33  std::size_t EBITS,
34  std::size_t FBITS,
35  std::size_t GBITS>
36 class id_
37 {
38 public:
39  static constexpr std::size_t FLAGS_UNMASK =
40  ~(((std::size_t(1) << FBITS) - std::size_t(1)) << 59);
41 
42  static_assert(PBITS + EBITS + FBITS + GBITS + 4 == 128,
43  "invalid id bit configuration");
44 
45  // FLAGS_UNMASK's "<< 59" would seem to require this... - martin
46  static_assert(sizeof(std::size_t) * CHAR_BIT >= 64,
47  "need std::size_t >= 64 bit");
48 
49  id_() = default;
50  id_(id_ &&) = default;
51 
52  id_(const id_ & id)
53  : dimension_(id.dimension_), domain_(id.domain_), partition_(id.partition_),
54  entity_(id.entity_), flags_(id.flags_), global_(id.global_) {}
55 
56  explicit id_(const std::size_t local_id)
57  : dimension_(0), domain_(0), partition_(0), entity_(local_id), flags_(0),
58  global_(0) {}
59 
60  template<std::size_t D, std::size_t M>
61  static id_ make(const std::size_t local_id,
62  const std::size_t partition_id = 0,
63  const std::size_t flags = 0,
64  const std::size_t global = 0) {
65  id_ global_id;
66  global_id.dimension_ = D;
67  global_id.domain_ = M;
68  global_id.partition_ = partition_id;
69  global_id.entity_ = local_id;
70  global_id.global_ = global;
71  global_id.flags_ = flags;
72 
73  return global_id;
74  }
75 
76  template<std::size_t M>
77  static id_ make(const std::size_t dim,
78  const std::size_t local_id,
79  const std::size_t partition_id = 0,
80  const std::size_t flags = 0,
81  const std::size_t global = 0) {
82  id_ global_id;
83  global_id.dimension_ = dim;
84  global_id.domain_ = M;
85  global_id.partition_ = partition_id;
86  global_id.entity_ = local_id;
87  global_id.global_ = global;
88  global_id.flags_ = flags;
89 
90  return global_id;
91  }
92 
93  static id_ make(const std::size_t dim,
94  const std::size_t local_id,
95  const std::size_t partition_id = 0,
96  const std::size_t flags = 0,
97  const std::size_t global = 0,
98  const std::size_t domain = 0) {
99  id_ global_id;
100  global_id.dimension_ = dim;
101  global_id.domain_ = domain;
102  global_id.partition_ = partition_id;
103  global_id.entity_ = local_id;
104  global_id.global_ = global;
105  global_id.flags_ = flags;
106 
107  return global_id;
108  }
109 
110  local_id_t local_id() const {
111  local_id_t r = dimension_;
112  r |= local_id_t(domain_) << 2;
113  r |= local_id_t(partition_) << 4;
114  r |= local_id_t(entity_) << (4 + PBITS);
115  return r;
116  }
117 
118  std::size_t global_id() const {
119  constexpr std::size_t unmask = ~((std::size_t(1) << EBITS) - 1);
120  return static_cast<std::size_t>((local_id() & unmask) | global_);
121  }
122 
123  void set_global(const std::size_t global) {
124  global_ = global;
125  }
126 
127  std::size_t global() const {
128  return global_;
129  }
130 
131  void set_partition(const std::size_t partition) {
132  partition_ = partition;
133  }
134 
135  id_ & operator=(id_ &&) = default;
136 
137  id_ & operator=(const id_ & id) {
138  dimension_ = id.dimension_;
139  domain_ = id.domain_;
140  partition_ = id.partition_;
141  entity_ = id.entity_;
142  global_ = id.global_;
143  flags_ = id.flags_;
144 
145  return *this;
146  }
147 
148  std::size_t dimension() const {
149  return dimension_;
150  }
151 
152  std::size_t domain() const {
153  return domain_;
154  }
155 
156  std::size_t partition() const {
157  return partition_;
158  }
159 
160  std::size_t entity() const {
161  return entity_;
162  }
163 
164  std::size_t index_space_index() const {
165  return entity_;
166  }
167 
168  std::size_t flags() const {
169  return flags_;
170  }
171 
172  void set_flags(const std::size_t flags) {
173  assert(flags < 1 << FBITS && "flag bits exceeded");
174  flags_ = flags;
175  }
176 
177  bool operator<(const id_ & id) const {
178  return local_id() < id.local_id();
179  }
180 
181  bool operator==(const id_ & id) const {
182  return (local_id() & FLAGS_UNMASK) == (id.local_id() & FLAGS_UNMASK);
183  }
184 
185  bool operator!=(const id_ & id) const {
186  return !(local_id() == id.local_id());
187  }
188 
189 private:
190  std::size_t dimension_ : 2;
191  std::size_t domain_ : 2;
192  std::size_t partition_ : PBITS;
193  std::size_t entity_ : EBITS;
194  std::size_t flags_ : FBITS;
195  std::size_t global_ : GBITS;
196 }; // id_
197 
198 } // namespace util
199 } // namespace flecsi
200 
201 // Defining operator<< out-of-namespace prevents an overload ambiguity problem
202 // that the unit-test code uncovered when the definition was in flecsi::util.
203 inline std::ostream &
204 operator<<(std::ostream & ostr, const flecsi::util::local_id_t x) {
205  return ostr << uint64_t(x >> 64) << ":" << uint64_t(x);
206 }
Definition: id.hh:36
Definition: uint128.hh:57
std::ostream & operator<<(std::ostream &ostr, const filling_curve< D, T, DER > &k)
output for filling_curve using output_ function defined in the class
Definition: filling_curve.hh:206
Definition: control.hh:31