From db69a4e27c732bf957ef1418d5124d2bba283da4 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 31 Mar 2022 15:09:00 -0400 Subject: [PATCH 1/4] einsum: permutations were computed in preimage convention, but Permutation ctor takes image --- src/TiledArray/util/index.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/TiledArray/util/index.h b/src/TiledArray/util/index.h index 53d8ccb1c1..3af669a238 100644 --- a/src/TiledArray/util/index.h +++ b/src/TiledArray/util/index.h @@ -163,15 +163,19 @@ Index sorted(const Index& a) { return Index(r); } -template -Permutation permutation(const Index &s, const Index &p) { - assert(sorted(s) == sorted(p)); - small_vector m; - m.reserve(p.size()); - for (size_t i = 0; i != p.size(); ++i) { - m.push_back(s.indexof(p[i])); +/// @param[in] from original (preimage) indices +/// @param[in] to target (image) indices +/// @return Permutation mapping @p from to @p to +template +Permutation permutation(const Index &from, const Index &to) { + const auto order = from.size(); + TA_ASSERT(order == to.size() && sorted(from) == sorted(to)); + small_vector p; + p.reserve(order); + for (auto &&f : from) { + p.emplace_back(to.indexof(f)); } - return Permutation(m); + return Permutation(std::move(p)); } template From 9e234514da2424e32486b03ac587be011dd4eac9 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 31 Mar 2022 15:12:46 -0400 Subject: [PATCH 2/4] assert->TA_ASSERT --- src/TiledArray/util/index.h | 8 ++++---- src/TiledArray/util/singleton.h | 2 +- tests/diagonal_array.cpp | 2 +- tests/linalg.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/TiledArray/util/index.h b/src/TiledArray/util/index.h index 3af669a238..4d034d3aac 100644 --- a/src/TiledArray/util/index.h +++ b/src/TiledArray/util/index.h @@ -208,11 +208,11 @@ struct IndexMap { auto it = begin; data_.reserve(keys.size()); for (auto &&key : keys) { - assert(it != end); - data_.emplace_back(std::pair{key, *it}); + TA_ASSERT(it != end); + data_.emplace_back(std::pair{key, *it}); ++it; } - assert(it == end); + TA_ASSERT(it == end); } IndexMap(const small_vector > &data) : data_(data) { } @@ -290,7 +290,7 @@ template auto idx(const std::string &s) { if constexpr (detail::is_tensor_of_tensor_v) { auto semi = std::find(s.begin(), s.end(), ';'); - assert(semi != s.end()); + TA_ASSERT(semi != s.end()); auto first = std::string(s.begin(), semi); auto second = std::string(semi+1, s.end()); return std::tuple{ first, second }; diff --git a/src/TiledArray/util/singleton.h b/src/TiledArray/util/singleton.h index 7354460bfe..6a8618c534 100644 --- a/src/TiledArray/util/singleton.h +++ b/src/TiledArray/util/singleton.h @@ -118,7 +118,7 @@ class Singleton { /// @param args a parameter pack template static void set_instance(Args&&... args) { - assert(instance_accessor() == nullptr); + TA_ASSERT(instance_accessor() == nullptr); instance_accessor() = std::move( std::unique_ptr(new Derived(std::forward(args)...))); } diff --git a/tests/diagonal_array.cpp b/tests/diagonal_array.cpp index 806f41f857..ff83813195 100644 --- a/tests/diagonal_array.cpp +++ b/tests/diagonal_array.cpp @@ -5,7 +5,7 @@ #include "unit_test_config.h" auto gen_trange1(size_t N, const std::vector& TA_NBs) { - assert(TA_NBs.size() > 0); + TA_ASSERT(TA_NBs.size() > 0); static int seed = 0; std::default_random_engine gen(seed++); diff --git a/tests/linalg.cpp b/tests/linalg.cpp index c5a706c565..3e031b5e72 100644 --- a/tests/linalg.cpp +++ b/tests/linalg.cpp @@ -117,7 +117,7 @@ struct LinearAlgebraFixture : ReferenceFixture { }; TA::TiledRange gen_trange(size_t N, const std::vector& TA_NBs) { - assert(TA_NBs.size() > 0); + TA_ASSERT(TA_NBs.size() > 0); std::default_random_engine gen(0); std::uniform_int_distribution<> dist(0, TA_NBs.size() - 1); From 432bfec25d5539019d32e346ff92eb94316109e1 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 31 Mar 2022 15:13:04 -0400 Subject: [PATCH 3/4] [format] index.h --- src/TiledArray/util/index.h | 118 +++++++++++++++++------------------- 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/src/TiledArray/util/index.h b/src/TiledArray/util/index.h index 4d034d3aac..de71b88339 100644 --- a/src/TiledArray/util/index.h +++ b/src/TiledArray/util/index.h @@ -2,15 +2,15 @@ #include "TiledArray/expressions/fwd.h" #include -#include #include +#include -#include #include +#include namespace TiledArray::index { -template +template using small_vector = container::svector; small_vector tokenize(const std::string &s); @@ -19,30 +19,32 @@ small_vector validate(const small_vector &v); std::string join(const small_vector &v); -template -using enable_if_string = std::enable_if_t< std::is_same_v, U>; +template +using enable_if_string = std::enable_if_t, U>; /// an `n`-index, with `n` a runtime parameter -template +template class Index { -public: + public: using container_type = small_vector; using value_type = typename container_type::value_type; Index() = default; Index(container_type &&s) : data_(std::move(s)) {} - template + template Index(const S &s) : data_(s.begin(), s.end()) {} - template + template explicit Index(const std::string &s) : Index(index::tokenize(s)) {} - template + template explicit Index(const char *s) : Index(std::string(s)) {} - template - operator std::string() const { return index::join(data_); } + template + operator std::string() const { + return index::join(data_); + } explicit operator bool() const { return !data_.empty(); } @@ -50,25 +52,23 @@ class Index { return (this->data_ == other.data_); } - bool operator!=(const Index &other) const { - return !(*this == other); - } + bool operator!=(const Index &other) const { return !(*this == other); } size_t size() const { return data_.size(); } auto begin() const { return data_.begin(); } auto end() const { return data_.end(); } - auto find(const T& v) const { + auto find(const T &v) const { return std::find(this->begin(), this->end(), v); } - const auto& operator[](size_t idx) const { return data_.at(idx); } + const auto &operator[](size_t idx) const { return data_.at(idx); } /// @param[in] v element to seek /// @pre `this->contains(v)` /// @return the location of @p v in `*this` - size_t indexof(const T& v) const { + size_t indexof(const T &v) const { for (size_t i = 0; i < this->size(); ++i) { if ((*this)[i] == v) return i; } @@ -76,16 +76,14 @@ class Index { } /// Returns true if argument exists in the Index object, else returns false - bool contains(const T& v) const { - return (this->find(v) != this->end()); - } + bool contains(const T &v) const { return (this->find(v) != this->end()); } private: container_type data_; }; -template -std::ostream& operator<<(std::ostream& os, const Index &idx) { +template +std::ostream &operator<<(std::ostream &os, const Index &idx) { os << std::string(idx); return os; } @@ -94,7 +92,7 @@ std::ostream& operator<<(std::ostream& os, const Index &idx) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template Index operator&(const Index &a, const Index &b) { typename Index::container_type r; for (const auto &s : a) { @@ -107,7 +105,7 @@ Index operator&(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template Index operator|(const Index &a, const Index &b) { typename Index::container_type r; r.assign(a.begin(), a.end()); @@ -122,7 +120,7 @@ Index operator|(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @note unline operator| @p a and @p b can have have duplicates -template +template Index operator+(const Index &a, const Index &b) { typename Index::container_type r; r.assign(a.begin(), a.end()); @@ -134,7 +132,7 @@ Index operator+(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @note unline operator& @p a and @p b can have have duplicates -template +template Index operator-(const Index &a, const Index &b) { typename Index::container_type r; for (const auto &s : a) { @@ -148,16 +146,18 @@ Index operator-(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template inline Index operator^(const Index &a, const Index &b) { return (a | b) - (a & b); } -template -size_t rank(const Index &idx) { return idx.size(); } +template +size_t rank(const Index &idx) { + return idx.size(); +} template -Index sorted(const Index& a) { +Index sorted(const Index &a) { typename Index::container_type r(a.begin(), a.end()); std::sort(r.begin(), r.end()); return Index(r); @@ -178,30 +178,30 @@ Permutation permutation(const Index &from, const Index &to) { return Permutation(std::move(p)); } -template -auto permute(const Permutation &p, const Index &s, std::bool_constant) { +template +auto permute(const Permutation &p, const Index &s, + std::bool_constant) { if (!p) return s; using R = typename Index::container_type; R r(p.size()); - detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(), std::bool_constant{}); + detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(), + std::bool_constant{}); return Index{r}; } /// @brief Index-annotated collection of objects /// @tparam Value /// This is a map using Index::element_type as key -template +template struct IndexMap { - using key_type = K; using value_type = V; IndexMap(const Index &keys, std::initializer_list s) - : IndexMap(keys, s.begin(), s.end()) {} + : IndexMap(keys, s.begin(), s.end()) {} template - IndexMap(const Index &keys, S &&s) - : IndexMap(keys, s.begin(), s.end()) {} + IndexMap(const Index &keys, S &&s) : IndexMap(keys, s.begin(), s.end()) {} template IndexMap(const Index &keys, It begin, It end) { @@ -215,19 +215,17 @@ struct IndexMap { TA_ASSERT(it == end); } - IndexMap(const small_vector > &data) : data_(data) { } + IndexMap(const small_vector > &data) : data_(data) {} /// @return const iterator pointing to the element associated with @p key auto find(const key_type &key) const { - return std::find_if( - data_.begin(), data_.end(), - [&key](const auto &v) { return key == v.first; } - ); + return std::find_if(data_.begin(), data_.end(), + [&key](const auto &v) { return key == v.first; }); } /// @return reference to the element associated with @p key /// @throw TA::Exception if @p key is not in this map - const auto& operator[](const key_type &key) const { + const auto &operator[](const key_type &key) const { auto it = find(key); if (it != data_.end()) return it->second; throw TiledArray::Exception("IndexMap::at(key): key not found"); @@ -249,16 +247,15 @@ struct IndexMap { auto end() const { return data_.end(); } private: - small_vector< std::pair > data_; - + small_vector > data_; }; -template -bool operator==(const IndexMap& lhs, const IndexMap& rhs) { - for (const auto& [k,v] : lhs) { +template +bool operator==(const IndexMap &lhs, const IndexMap &rhs) { + for (const auto &[k, v] : lhs) { if (rhs.find(k) == rhs.end() || v != rhs[k]) return false; } - for (const auto& [k,v] : rhs) { + for (const auto &[k, v] : rhs) { if (lhs.find(k) == lhs.end()) return false; } return true; @@ -266,9 +263,9 @@ bool operator==(const IndexMap& lhs, const IndexMap& rhs) { /// TODO to be filled by Sam template -IndexMap operator|(const IndexMap &a, const IndexMap &b) { - small_vector< std::pair > d(a.begin(), a.end()); - for (const auto [k,v] : b) { +IndexMap operator|(const IndexMap &a, const IndexMap &b) { + small_vector > d(a.begin(), a.end()); + for (const auto [k, v] : b) { if (a.find(k) != a.end()) { TA_ASSERT(a[k] == b[k]); continue; @@ -286,22 +283,21 @@ using Index = TiledArray::index::Index; using TiledArray::index::IndexMap; /// converts the annotation of an expression to an Index -template +template auto idx(const std::string &s) { if constexpr (detail::is_tensor_of_tensor_v) { auto semi = std::find(s.begin(), s.end(), ';'); TA_ASSERT(semi != s.end()); auto first = std::string(s.begin(), semi); - auto second = std::string(semi+1, s.end()); - return std::tuple{ first, second }; - } - else { - return std::tuple{ s }; + auto second = std::string(semi + 1, s.end()); + return std::tuple{first, second}; + } else { + return std::tuple{s}; } } /// converts the annotation of an expression to an Index -template +template auto idx(const expressions::TsrExpr &e) { return idx(e.annotation()); } From fca00dc8672254c86175cae333c06d50a01ec577 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 31 Mar 2022 15:13:04 -0400 Subject: [PATCH 4/4] bump VG's cmake kit tag to bring in https://github.com/ajaypanyala/linalg-cmake-modules/pull/3 --- external/versions.cmake | 2 +- src/TiledArray/util/index.h | 118 +++++++++++++++++------------------- 2 files changed, 58 insertions(+), 62 deletions(-) diff --git a/external/versions.cmake b/external/versions.cmake index 2d45655ccd..450e4216b5 100644 --- a/external/versions.cmake +++ b/external/versions.cmake @@ -1,7 +1,7 @@ # for each dependency track both current and previous id (the variable for the latter must contain PREVIOUS) # to be able to auto-update them -set(TA_TRACKED_VGCMAKEKIT_TAG d29dc6177884557d13fb87f364eb76350e8ddc9c) +set(TA_TRACKED_VGCMAKEKIT_TAG f946aa274745da09702a727492f1cabb843dc9e6) # Boost explicitly downgraded to 1.59 from 1.68 set(TA_TRACKED_BOOST_VERSION 1.59) diff --git a/src/TiledArray/util/index.h b/src/TiledArray/util/index.h index 4d034d3aac..de71b88339 100644 --- a/src/TiledArray/util/index.h +++ b/src/TiledArray/util/index.h @@ -2,15 +2,15 @@ #include "TiledArray/expressions/fwd.h" #include -#include #include +#include -#include #include +#include namespace TiledArray::index { -template +template using small_vector = container::svector; small_vector tokenize(const std::string &s); @@ -19,30 +19,32 @@ small_vector validate(const small_vector &v); std::string join(const small_vector &v); -template -using enable_if_string = std::enable_if_t< std::is_same_v, U>; +template +using enable_if_string = std::enable_if_t, U>; /// an `n`-index, with `n` a runtime parameter -template +template class Index { -public: + public: using container_type = small_vector; using value_type = typename container_type::value_type; Index() = default; Index(container_type &&s) : data_(std::move(s)) {} - template + template Index(const S &s) : data_(s.begin(), s.end()) {} - template + template explicit Index(const std::string &s) : Index(index::tokenize(s)) {} - template + template explicit Index(const char *s) : Index(std::string(s)) {} - template - operator std::string() const { return index::join(data_); } + template + operator std::string() const { + return index::join(data_); + } explicit operator bool() const { return !data_.empty(); } @@ -50,25 +52,23 @@ class Index { return (this->data_ == other.data_); } - bool operator!=(const Index &other) const { - return !(*this == other); - } + bool operator!=(const Index &other) const { return !(*this == other); } size_t size() const { return data_.size(); } auto begin() const { return data_.begin(); } auto end() const { return data_.end(); } - auto find(const T& v) const { + auto find(const T &v) const { return std::find(this->begin(), this->end(), v); } - const auto& operator[](size_t idx) const { return data_.at(idx); } + const auto &operator[](size_t idx) const { return data_.at(idx); } /// @param[in] v element to seek /// @pre `this->contains(v)` /// @return the location of @p v in `*this` - size_t indexof(const T& v) const { + size_t indexof(const T &v) const { for (size_t i = 0; i < this->size(); ++i) { if ((*this)[i] == v) return i; } @@ -76,16 +76,14 @@ class Index { } /// Returns true if argument exists in the Index object, else returns false - bool contains(const T& v) const { - return (this->find(v) != this->end()); - } + bool contains(const T &v) const { return (this->find(v) != this->end()); } private: container_type data_; }; -template -std::ostream& operator<<(std::ostream& os, const Index &idx) { +template +std::ostream &operator<<(std::ostream &os, const Index &idx) { os << std::string(idx); return os; } @@ -94,7 +92,7 @@ std::ostream& operator<<(std::ostream& os, const Index &idx) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template Index operator&(const Index &a, const Index &b) { typename Index::container_type r; for (const auto &s : a) { @@ -107,7 +105,7 @@ Index operator&(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template Index operator|(const Index &a, const Index &b) { typename Index::container_type r; r.assign(a.begin(), a.end()); @@ -122,7 +120,7 @@ Index operator|(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @note unline operator| @p a and @p b can have have duplicates -template +template Index operator+(const Index &a, const Index &b) { typename Index::container_type r; r.assign(a.begin(), a.end()); @@ -134,7 +132,7 @@ Index operator+(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @note unline operator& @p a and @p b can have have duplicates -template +template Index operator-(const Index &a, const Index &b) { typename Index::container_type r; for (const auto &s : a) { @@ -148,16 +146,18 @@ Index operator-(const Index &a, const Index &b) { /// @param[in] a an Index object /// @param[in] b an Index object /// @pre a and b do not have duplicates -template +template inline Index operator^(const Index &a, const Index &b) { return (a | b) - (a & b); } -template -size_t rank(const Index &idx) { return idx.size(); } +template +size_t rank(const Index &idx) { + return idx.size(); +} template -Index sorted(const Index& a) { +Index sorted(const Index &a) { typename Index::container_type r(a.begin(), a.end()); std::sort(r.begin(), r.end()); return Index(r); @@ -178,30 +178,30 @@ Permutation permutation(const Index &from, const Index &to) { return Permutation(std::move(p)); } -template -auto permute(const Permutation &p, const Index &s, std::bool_constant) { +template +auto permute(const Permutation &p, const Index &s, + std::bool_constant) { if (!p) return s; using R = typename Index::container_type; R r(p.size()); - detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(), std::bool_constant{}); + detail::permute_n(p.size(), p.begin(), s.begin(), r.begin(), + std::bool_constant{}); return Index{r}; } /// @brief Index-annotated collection of objects /// @tparam Value /// This is a map using Index::element_type as key -template +template struct IndexMap { - using key_type = K; using value_type = V; IndexMap(const Index &keys, std::initializer_list s) - : IndexMap(keys, s.begin(), s.end()) {} + : IndexMap(keys, s.begin(), s.end()) {} template - IndexMap(const Index &keys, S &&s) - : IndexMap(keys, s.begin(), s.end()) {} + IndexMap(const Index &keys, S &&s) : IndexMap(keys, s.begin(), s.end()) {} template IndexMap(const Index &keys, It begin, It end) { @@ -215,19 +215,17 @@ struct IndexMap { TA_ASSERT(it == end); } - IndexMap(const small_vector > &data) : data_(data) { } + IndexMap(const small_vector > &data) : data_(data) {} /// @return const iterator pointing to the element associated with @p key auto find(const key_type &key) const { - return std::find_if( - data_.begin(), data_.end(), - [&key](const auto &v) { return key == v.first; } - ); + return std::find_if(data_.begin(), data_.end(), + [&key](const auto &v) { return key == v.first; }); } /// @return reference to the element associated with @p key /// @throw TA::Exception if @p key is not in this map - const auto& operator[](const key_type &key) const { + const auto &operator[](const key_type &key) const { auto it = find(key); if (it != data_.end()) return it->second; throw TiledArray::Exception("IndexMap::at(key): key not found"); @@ -249,16 +247,15 @@ struct IndexMap { auto end() const { return data_.end(); } private: - small_vector< std::pair > data_; - + small_vector > data_; }; -template -bool operator==(const IndexMap& lhs, const IndexMap& rhs) { - for (const auto& [k,v] : lhs) { +template +bool operator==(const IndexMap &lhs, const IndexMap &rhs) { + for (const auto &[k, v] : lhs) { if (rhs.find(k) == rhs.end() || v != rhs[k]) return false; } - for (const auto& [k,v] : rhs) { + for (const auto &[k, v] : rhs) { if (lhs.find(k) == lhs.end()) return false; } return true; @@ -266,9 +263,9 @@ bool operator==(const IndexMap& lhs, const IndexMap& rhs) { /// TODO to be filled by Sam template -IndexMap operator|(const IndexMap &a, const IndexMap &b) { - small_vector< std::pair > d(a.begin(), a.end()); - for (const auto [k,v] : b) { +IndexMap operator|(const IndexMap &a, const IndexMap &b) { + small_vector > d(a.begin(), a.end()); + for (const auto [k, v] : b) { if (a.find(k) != a.end()) { TA_ASSERT(a[k] == b[k]); continue; @@ -286,22 +283,21 @@ using Index = TiledArray::index::Index; using TiledArray::index::IndexMap; /// converts the annotation of an expression to an Index -template +template auto idx(const std::string &s) { if constexpr (detail::is_tensor_of_tensor_v) { auto semi = std::find(s.begin(), s.end(), ';'); TA_ASSERT(semi != s.end()); auto first = std::string(s.begin(), semi); - auto second = std::string(semi+1, s.end()); - return std::tuple{ first, second }; - } - else { - return std::tuple{ s }; + auto second = std::string(semi + 1, s.end()); + return std::tuple{first, second}; + } else { + return std::tuple{s}; } } /// converts the annotation of an expression to an Index -template +template auto idx(const expressions::TsrExpr &e) { return idx(e.annotation()); }