fpmas 1.6
distributed_id.h
Go to the documentation of this file.
1#ifndef FPMAS_DISTRIBUTED_ID_H
2#define FPMAS_DISTRIBUTED_ID_H
3
4#include "hedley/hedley.h"
5
10#ifndef FPMAS_ID_TYPE
32 #define FPMAS_ID_TYPE std::uint_fast32_t
33#endif
34
35#include <functional>
36#include <nlohmann/json.hpp>
37#include <mpi.h>
38#include <climits>
39#include <type_traits>
40
41static_assert(
42 std::is_integral<FPMAS_ID_TYPE>::value,
43 "FPMAS_ID_TYPE must be an unsigned integer."
44 );
45static_assert(
46 std::is_unsigned<FPMAS_ID_TYPE>::value,
47 "FPMAS_ID_TYPE must be unsigned."
48 );
49
50
51namespace fpmas { namespace api { namespace graph {
52 class DistributedId;
53}}}
54
55namespace fpmas { namespace api { namespace communication {
66 int rank;
71 };
72}}}
73
74namespace fpmas { namespace api { namespace graph {
75 class DistributedId;
76}}}
77
79
80namespace fpmas { namespace api { namespace graph {
81
91
92 private:
93 int _rank;
94 FPMAS_ID_TYPE _id;
95
96 public:
100 static int max_rank;
112
119 static MPI_Datatype mpiDistributedIdType;
120
126 explicit DistributedId(const MpiDistributedId& mpi_id)
127 : _rank(mpi_id.rank), _id(mpi_id.id) {}
128
133
141 explicit DistributedId(int rank) : DistributedId(rank, 0) {}
142
150 DistributedId(int rank, FPMAS_ID_TYPE id) : _rank(rank), _id(id) {}
151
157 int rank() const {
158 return _rank;
159 }
160
167 return _id;
168 }
169
175 DistributedId old(*this);
176 _id++;
177 return old;
178 };
179
189 bool operator<(const DistributedId& other) const {
190 if(this->_rank==other._rank)
191 return this->_id < other._id;
192 if(this->_rank < other._rank)
193 return true;
194 return false;
195 }
196
201 bool operator<=(const DistributedId& other) const {
202 if(*this == other)
203 return true;
204 return *this < other;
205 }
206
213 bool operator==(const DistributedId& other) const {
214 return (other._rank == this->_rank) && (other._id == this->_id);
215 }
216
223 bool operator!=(const DistributedId& other) const {
224 return (other._rank != this->_rank) || (other._id != this->_id);
225 }
226
232 HEDLEY_DEPRECATED(1.1)
233 //TODO: Removes this in 2.0 (conflicts with nlohmann::json, when
234 //declaring std::map<DistributedId, _>)
235 operator std::string() const {
236 return "[" + std::to_string(_rank) + ":" + std::to_string(_id) + "]";
237 }
238
244 std::size_t hash() const {
245 return std::hash<FPMAS_ID_TYPE>()(_id);
246 }
247 };
248
256 std::ostream& operator<<(std::ostream& os, const DistributedId& id);
257}}}
258
260
261namespace fpmas {
269 std::string to_string(const api::graph::DistributedId& id);
270}
271
272namespace fpmas { namespace api { namespace communication {
277 inline static void createMpiTypes() {
278 MPI_Datatype types[2] = {MPI_INT, MPI_UNSIGNED_LONG_LONG};
279 int block[2] = {1, 1};
280 MPI_Aint disp[2] = {
281 offsetof(MpiDistributedId, rank),
282 offsetof(MpiDistributedId, id)
283 };
284 MPI_Type_create_struct(2, block, disp, types, &DistributedId::mpiDistributedIdType);
285 MPI_Type_commit(&DistributedId::mpiDistributedIdType);
286 }
287
291 inline static void freeMpiTypes() {
292 MPI_Type_free(&DistributedId::mpiDistributedIdType);
293 }
294}}}
295
296namespace std {
300 template<> struct hash<DistributedId> {
306 std::size_t operator()(DistributedId const& id) const noexcept
307 {
308 return id.hash();
309 }
310
311 };
312}
313
314
315namespace nlohmann {
317
321 template<>
322 struct adl_serializer<DistributedId> {
329 template<typename JsonType>
330 static void to_json(JsonType& j, const DistributedId& value) {
331 j[0] = value.rank();
332 j[1] = value.id();
333 }
334
342 template<typename JsonType>
343 static void from_json(const JsonType& j, DistributedId& id) {
344 id._rank = j[0].template get<int>();
345 id._id = j[1].template get<FPMAS_ID_TYPE>();
346 }
347 };
348
349 /*
350 * Temporary hack to solve the DistributedId std::string conversion issue
351 *
352 * Will be removed in a next release, when DistributedId automatic
353 * std::string conversion will be disabled.
354 */
355 template<typename Value, typename Hash, typename Equal, typename Alloc>
356 struct adl_serializer<std::unordered_map<DistributedId, Value, Hash, Equal, Alloc>> {
357 typedef std::unordered_map<DistributedId, Value, Hash, Equal, Alloc> Map;
358 static void to_json(json& j, const Map& map) {
359 for(auto item : map)
360 j[json(item.first).dump()] = json(item.second);
361 }
362 static void from_json(const json& j, Map& map) {
363 for(auto item : j.items()) {
364 map.insert({
365 json::parse(item.key()).get<DistributedId>(),
366 item.value().get<Value>()});
367 }
368 }
369 };
370}
371#endif
Definition: distributed_id.h:89
bool operator<(const DistributedId &other) const
Definition: distributed_id.h:189
DistributedId(int rank, FPMAS_ID_TYPE id)
Definition: distributed_id.h:150
DistributedId operator++(int)
Definition: distributed_id.h:174
DistributedId(int rank)
Definition: distributed_id.h:141
FPMAS_ID_TYPE id() const
Definition: distributed_id.h:166
DistributedId()
Definition: distributed_id.h:132
std::size_t hash() const
Definition: distributed_id.h:244
int rank() const
Definition: distributed_id.h:157
bool operator<=(const DistributedId &other) const
Definition: distributed_id.h:201
static MPI_Datatype mpiDistributedIdType
Definition: distributed_id.h:119
bool operator!=(const DistributedId &other) const
Definition: distributed_id.h:223
DistributedId(const MpiDistributedId &mpi_id)
Definition: distributed_id.h:126
bool operator==(const DistributedId &other) const
Definition: distributed_id.h:213
static int max_rank
Definition: distributed_id.h:100
static FPMAS_ID_TYPE max_id
Definition: distributed_id.h:111
#define FPMAS_ID_TYPE
Definition: distributed_id.h:32
std::ostream & operator<<(std::ostream &os, const DistributedId &id)
Definition: distributed_id.cpp:13
Definition: fpmas.cpp:3
std::string to_string(const api::graph::DistributedId &id)
Definition: distributed_id.cpp:4
Definition: distributed_id.h:62
FPMAS_ID_TYPE id
Definition: distributed_id.h:70
int rank
Definition: distributed_id.h:66
Definition: distributed_id.h:322
static void from_json(const JsonType &j, DistributedId &id)
Definition: distributed_id.h:343
static void to_json(JsonType &j, const DistributedId &value)
Definition: distributed_id.h:330
std::size_t operator()(DistributedId const &id) const noexcept
Definition: distributed_id.h:306