fpmas 1.5
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
191 bool operator<(const DistributedId& other) const {
192 if(this->_rank==other._rank)
193 return this->_id < other._id;
194 if(this->_rank < other._rank)
195 return true;
196 return false;
197 }
198
205 bool operator==(const DistributedId& other) const {
206 return (other._rank == this->_rank) && (other._id == this->_id);
207 }
208
215 bool operator!=(const DistributedId& other) const {
216 return (other._rank != this->_rank) || (other._id != this->_id);
217 }
218
224 HEDLEY_DEPRECATED(1.1)
225 //TODO: Removes this in 2.0 (conflicts with nlohmann::json, when
226 //declaring std::map<DistributedId, _>)
227 operator std::string() const {
228 return "[" + std::to_string(_rank) + ":" + std::to_string(_id) + "]";
229 }
230
236 std::size_t hash() const {
237 return std::hash<FPMAS_ID_TYPE>()(_id);
238 }
239 };
240
248 std::ostream& operator<<(std::ostream& os, const DistributedId& id);
249}}}
250
252
253namespace fpmas {
261 std::string to_string(const api::graph::DistributedId& id);
262}
263
264namespace fpmas { namespace api { namespace communication {
269 inline static void createMpiTypes() {
270 MPI_Datatype types[2] = {MPI_INT, MPI_UNSIGNED_LONG_LONG};
271 int block[2] = {1, 1};
272 MPI_Aint disp[2] = {
273 offsetof(MpiDistributedId, rank),
274 offsetof(MpiDistributedId, id)
275 };
276 MPI_Type_create_struct(2, block, disp, types, &DistributedId::mpiDistributedIdType);
277 MPI_Type_commit(&DistributedId::mpiDistributedIdType);
278 }
279
283 inline static void freeMpiTypes() {
284 MPI_Type_free(&DistributedId::mpiDistributedIdType);
285 }
286}}}
287
288namespace std {
292 template<> struct hash<DistributedId> {
298 std::size_t operator()(DistributedId const& id) const noexcept
299 {
300 return id.hash();
301 }
302
303 };
304}
305
306
307namespace nlohmann {
309
313 template<>
314 struct adl_serializer<DistributedId> {
321 template<typename JsonType>
322 static void to_json(JsonType& j, const DistributedId& value) {
323 j[0] = value.rank();
324 j[1] = value.id();
325 }
326
334 template<typename JsonType>
335 static void from_json(const JsonType& j, DistributedId& id) {
336 id._rank = j[0].template get<int>();
337 id._id = j[1].template get<FPMAS_ID_TYPE>();
338 }
339 };
340
341 /*
342 * Temporary hack to solve the DistributedId std::string conversion issue
343 *
344 * Will be removed in a next release, when DistributedId automatic
345 * std::string conversion will be disabled.
346 */
347 template<typename Value, typename Hash, typename Equal, typename Alloc>
348 struct adl_serializer<std::unordered_map<DistributedId, Value, Hash, Equal, Alloc>> {
349 typedef std::unordered_map<DistributedId, Value, Hash, Equal, Alloc> Map;
350 static void to_json(json& j, const Map& map) {
351 for(auto item : map)
352 j[json(item.first).dump()] = json(item.second);
353 }
354 static void from_json(const json& j, Map& map) {
355 for(auto item : j.items()) {
356 map.insert({
357 json::parse(item.key()).get<DistributedId>(),
358 item.value().get<Value>()});
359 }
360 }
361 };
362}
363#endif
Definition: distributed_id.h:89
bool operator<(const DistributedId &other) const
Definition: distributed_id.h:191
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:236
int rank() const
Definition: distributed_id.h:157
static MPI_Datatype mpiDistributedIdType
Definition: distributed_id.h:119
bool operator!=(const DistributedId &other) const
Definition: distributed_id.h:215
DistributedId(const MpiDistributedId &mpi_id)
Definition: distributed_id.h:126
bool operator==(const DistributedId &other) const
Definition: distributed_id.h:205
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:314
static void from_json(const JsonType &j, DistributedId &id)
Definition: distributed_id.h:335
static void to_json(JsonType &j, const DistributedId &value)
Definition: distributed_id.h:322
std::size_t operator()(DistributedId const &id) const noexcept
Definition: distributed_id.h:298