ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
multiindex.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 10/21/22.
4//
5
6#ifndef TTG_UTIL_MULTIINDEX_H
7#define TTG_UTIL_MULTIINDEX_H
8
10
11namespace ttg {
12
13 template <std::size_t Rank, typename Int = int>
14 struct MultiIndex {
15 static constexpr const std::size_t max_index = 1 << 21;
16 static constexpr const std::size_t max_index_square = max_index * max_index;
17 MultiIndex() = default;
18 template <typename Integer, typename = std::enable_if_t<std::is_integral_v<Int>>>
19 MultiIndex(std::initializer_list<Integer> ilist) {
20 std::copy(ilist.begin(), ilist.end(), data_.begin());
21 assert(valid());
22 }
23 template <typename... Ints, typename = std::enable_if_t<(std::is_integral_v<Ints> && ...)>>
24 MultiIndex(Ints... ilist) : data_{{static_cast<Int>(ilist)...}} {
25 assert(valid());
26 }
27 explicit MultiIndex(std::size_t hash) {
28 static_assert(Rank == 1 || Rank == 2 || Rank == 3,
29 "MultiIndex<Rank>::MultiIndex(hash) only implemented for Rank={1,2,3}");
30 if (Rank == 1) {
31 assert(hash < max_index);
32 (*this)[0] = hash;
33 }
34 if (Rank == 2) {
35 (*this)[0] = hash / max_index;
36 (*this)[1] = hash % max_index;
37 } else if (Rank == 3) {
38 (*this)[0] = hash / max_index_square;
39 (*this)[1] = (hash % max_index_square) / max_index;
40 (*this)[2] = hash % max_index;
41 }
42 }
43 std::size_t hash() const {
44 static_assert(Rank == 1 || Rank == 2 || Rank == 3, "MultiIndex<Rank>::hash only implemented for Rank={1,2,3}");
45 if constexpr (Rank == 1)
46 return (*this)[0];
47 else if constexpr (Rank == 2) {
48 return (*this)[0] * max_index + (*this)[1];
49 } else if constexpr (Rank == 3) {
50 return ((*this)[0] * max_index + (*this)[1]) * max_index + (*this)[2];
51 }
52 }
53
54 const auto &operator[](std::size_t idx) const {
55 if (idx >= Rank) assert(idx < Rank);
56 return data_[idx];
57 }
58
59 template <typename Archive>
60 void serialize(Archive &ar, const unsigned int version = 0) {
61 ar &data_;
62 }
63
64 private:
65 bool valid() {
66 bool result = true;
67 for (const auto &idx : data_) {
68 result = result && (idx < max_index);
69 }
70 return result;
71 }
72
73 std::array<Int, Rank> data_;
74
75 friend bool operator==(const MultiIndex<Rank> &lhs, const MultiIndex<Rank> &rhs) {
76 return lhs.data_ == rhs.data_;
77 }
78 friend bool operator!=(const MultiIndex<Rank> &lhs, const MultiIndex<Rank> &rhs) {
79 return !(lhs == rhs);
80 }
81 };
82
83 template <std::size_t Rank>
84 std::ostream &operator<<(std::ostream &os, const MultiIndex<Rank> &key) {
85 os << "{";
86 for (size_t i = 0; i != Rank; ++i) os << key[i] << (i + 1 != Rank ? "," : "");
87 os << "}";
88 return os;
89 }
90
91} // namespace ttg
92
93#ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
94namespace madness {
95 namespace archive {
96 template <class Archive, std::size_t Rank>
97 struct ArchiveStoreImpl<Archive, ttg::MultiIndex<Rank>> {
98 static inline void store(const Archive& ar, const ttg::MultiIndex<Rank>& mi) {
99 for (size_t i = 0; i != Rank; ++i) ar << mi[i];
100 }
101 };
102
103 template <class Archive, std::size_t Rank>
104 struct ArchiveLoadImpl<Archive, ttg::MultiIndex<Rank>> {
105 static inline void load(const Archive& ar, ttg::MultiIndex<Rank>& mi) {
106 for (size_t i = 0; i != Rank; ++i) ar >> mi[i];
107 }
108 };
109 } // namespace archive
110} // namespace madness
111
112static_assert(madness::is_serializable_v<madness::archive::BufferOutputArchive, ttg::MultiIndex<3>>);
113
114#endif // TTG_SERIALIZATION_SUPPORTS_MADNESS
115
116#endif // TTG_UTIL_MULTIINDEX_H
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
std::array< int, 3 > version()
Definition version.cc:5
MultiIndex(std::size_t hash)
Definition multiindex.h:27
MultiIndex(std::initializer_list< Integer > ilist)
Definition multiindex.h:19
MultiIndex(Ints... ilist)
Definition multiindex.h:24
std::size_t hash() const
Definition multiindex.h:43
void serialize(Archive &ar, const unsigned int version=0)
Definition multiindex.h:60
static constexpr const std::size_t max_index_square
Definition multiindex.h:16
friend bool operator==(const MultiIndex< Rank > &lhs, const MultiIndex< Rank > &rhs)
Definition multiindex.h:75
static constexpr const std::size_t max_index
Definition multiindex.h:15
friend bool operator!=(const MultiIndex< Rank > &lhs, const MultiIndex< Rank > &rhs)
Definition multiindex.h:78
MultiIndex()=default
const auto & operator[](std::size_t idx) const
Definition multiindex.h:54
Computes hash values for objects of type T.
Definition hash.h:82