From f75afe01fc662fe367912fbd182f5c638e62a515 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Sat, 21 Sep 2024 16:19:03 -0400 Subject: [PATCH 1/6] add prints --- vpr/test/test_odd_even_routing.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/vpr/test/test_odd_even_routing.cpp b/vpr/test/test_odd_even_routing.cpp index e642518076b..6b9435268df 100644 --- a/vpr/test/test_odd_even_routing.cpp +++ b/vpr/test/test_odd_even_routing.cpp @@ -5,6 +5,7 @@ #include "channel_dependency_graph.h" #include +#include namespace { @@ -232,6 +233,8 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { NocTrafficFlows traffic_flow_storage; + std::cout << "Before for loop" << std::endl; + for (int i = 0; i < 100; i++) { auto src_blk_id = (ClusterBlockId)dist(rand_num_gen); @@ -240,20 +243,28 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { dst_blk_id = (ClusterBlockId)dist(rand_num_gen); } while (src_blk_id == dst_blk_id); + std::cout << "before call i: " << i << std::endl; traffic_flow_storage.create_noc_traffic_flow("dummy_name_1", "dummy_name_2", src_blk_id, dst_blk_id, 1, 1, 1); + std::cout << "after call i: " << i << std::endl; } + + std::cout << "before finished "<< std::endl; traffic_flow_storage.finished_noc_traffic_flows_setup(); + std::cout << "after finished "<< std::endl; vtr::vector> traffic_flow_routes(traffic_flow_storage.get_number_of_traffic_flows()); for (const auto& [id, traffic_flow] : traffic_flow_storage.get_all_traffic_flows().pairs()) { + std::cout << (size_t)id << " " << (size_t)traffic_flow.source_router_cluster_id << " " << (size_t)traffic_flow.sink_router_cluster_id << std::endl; NocRouterId src_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.source_router_cluster_id].loc); NocRouterId dst_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.sink_router_cluster_id].loc); + std::cout <<"before route" << std::endl; REQUIRE_NOTHROW(routing_algorithm.route_flow(src_router_id, dst_router_id, id, traffic_flow_routes[id], noc_model)); + std::cout <<"after route" << std::endl; } ChannelDependencyGraph cdg(noc_model, traffic_flow_storage, traffic_flow_routes, block_locs); From 0a0554509ed5c5fb11b40d20ae99880653a9afe5 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Sat, 21 Sep 2024 17:05:56 -0400 Subject: [PATCH 2/6] move prints --- vpr/test/test_odd_even_routing.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vpr/test/test_odd_even_routing.cpp b/vpr/test/test_odd_even_routing.cpp index 6b9435268df..6396f2dedee 100644 --- a/vpr/test/test_odd_even_routing.cpp +++ b/vpr/test/test_odd_even_routing.cpp @@ -233,8 +233,6 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { NocTrafficFlows traffic_flow_storage; - std::cout << "Before for loop" << std::endl; - for (int i = 0; i < 100; i++) { auto src_blk_id = (ClusterBlockId)dist(rand_num_gen); @@ -243,21 +241,23 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { dst_blk_id = (ClusterBlockId)dist(rand_num_gen); } while (src_blk_id == dst_blk_id); - std::cout << "before call i: " << i << std::endl; traffic_flow_storage.create_noc_traffic_flow("dummy_name_1", "dummy_name_2", src_blk_id, dst_blk_id, 1, 1, 1); - std::cout << "after call i: " << i << std::endl; } - - std::cout << "before finished "<< std::endl; traffic_flow_storage.finished_noc_traffic_flows_setup(); std::cout << "after finished "<< std::endl; vtr::vector> traffic_flow_routes(traffic_flow_storage.get_number_of_traffic_flows()); + std::cout << "Size: " << traffic_flow_routes.size() << std::endl; + + std::cout << "getting pairs" << std::endl; + std::cout << "getting pairs " << traffic_flow_storage.get_all_traffic_flows().size() << std::endl; + const auto& all_pairs = traffic_flow_storage.get_all_traffic_flows().pairs(); + std::cout << "got pairs" << std::endl; - for (const auto& [id, traffic_flow] : traffic_flow_storage.get_all_traffic_flows().pairs()) { + for (const auto& [id, traffic_flow] : all_pairs) { - std::cout << (size_t)id << " " << (size_t)traffic_flow.source_router_cluster_id << " " << (size_t)traffic_flow.sink_router_cluster_id << std::endl; + std::cout << "loop: " << (size_t)id << " " << (size_t)traffic_flow.source_router_cluster_id << " " << (size_t)traffic_flow.sink_router_cluster_id << std::endl; NocRouterId src_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.source_router_cluster_id].loc); NocRouterId dst_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.sink_router_cluster_id].loc); From e6d2777ca8f84e6d223bc1483b7b8e3618039477 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Tue, 29 Oct 2024 16:11:31 -0400 Subject: [PATCH 3/6] chagne the implementation of vtr::vector::pairs() --- libs/libvtrutil/src/vtr_vector.h | 82 ++++++++---------------------- vpr/test/test_odd_even_routing.cpp | 2 +- 2 files changed, 23 insertions(+), 61 deletions(-) diff --git a/libs/libvtrutil/src/vtr_vector.h b/libs/libvtrutil/src/vtr_vector.h index dcf783abe99..b4072a0d6d6 100644 --- a/libs/libvtrutil/src/vtr_vector.h +++ b/libs/libvtrutil/src/vtr_vector.h @@ -56,9 +56,6 @@ class vector : private std::vector { class key_iterator; typedef vtr::Range key_range; - class pair_iterator; - typedef vtr::Range pair_range; - public: //Pass through std::vector's types using typename storage::allocator_type; @@ -157,16 +154,31 @@ class vector : private std::vector { } /** - * @brief Returns a range containing the key-value pairs. + * @brief Provides an iterable object to enumerate the vector. * - * This function returns a range object that represents the sequence of key-value - * pairs within the vector. The range can be used to iterate over the pairs using - * standard range-based loops or algorithms. + * This function allows for easy enumeration, yielding a tuple of (index, element) + * pairs in each iteration. It is similar in functionality to Python's `enumerate()`. * - * @return A `pair_range` object representing the range of key-value pairs. + * @ return An iterable wrapper that can be used in a range-based for loop to obtain + * (index, element) pairs. */ - pair_range pairs() const { - return vtr::make_range(pair_begin(), pair_end()); + auto pairs() const { + struct enumerated_iterator { + key_type i; + vector::const_iterator iter; + + bool operator!=(const enumerated_iterator& other) const { return iter != other.iter; } + void operator++() { i = key_type(size_t(i) + 1); iter++; } + std::tuple operator*() { return std::tie(i, *iter); } + }; + + struct enumerated_wrapper { + const vector& vec; + auto begin() { return enumerated_iterator{ key_type(0), vec.begin() }; } + auto end() { return enumerated_iterator{ key_type(vec.size()), vec.end() }; } + }; + + return enumerated_wrapper{ *this }; } public: @@ -218,59 +230,9 @@ class vector : private std::vector { value_type value_; }; - /** - * @brief A bidirectional iterator for a vtr:vector object. - * - * The `pair_iterator` class provides a way to iterate over key-value pairs - * within a vtr::vector container. It supports bidirectional iteration, - * allowing the user to traverse the container both forwards and backwards. - */ - class pair_iterator { - public: - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = std::ptrdiff_t; - using value_type = std::pair; - using pointer = value_type*; - using reference = value_type&; - - /// @brief constructor - pair_iterator(vector& vec, key_type init) - : vec_(vec), value_(init, vec[init]) {} - - /// @brief ++ operator - pair_iterator& operator++() { - value_ = std::make_pair(key_type(size_t(value_.first) + 1), vec_[key_type(size_t(value_.first) + 1)]); - return *this; - } - /// @brief -- operator - pair_iterator& operator--() { - value_ = std::make_pair(key_type(size_t(value_.first) - 1), vec_[key_type(size_t(value_.first) - 1)]); - return *this; - } - /// @brief dereference operator - reference operator*() { return value_; } - /// @brief -> operator - pointer operator->() { return &value_; } - - /// @brief == operator - friend bool operator==(const pair_iterator& lhs, const pair_iterator& rhs) { return lhs.value_.first == rhs.value_.first; } - /// @brief != operator - friend bool operator!=(const pair_iterator& lhs, const pair_iterator& rhs) { return !(lhs == rhs); } - - private: - /// @brief Reference to the vector of key-value pairs. - vector& vec_; - // @brief The current key-value pair being pointed to by the iterator. - value_type value_; - }; - private: key_iterator key_begin() const { return key_iterator(key_type(0)); } key_iterator key_end() const { return key_iterator(key_type(size())); } - - pair_iterator pair_begin() const { return pair_iterator(*const_cast*>(this), key_type(0)); } - pair_iterator pair_end() const { return pair_iterator(*const_cast*>(this), key_type(size())); } }; - } // namespace vtr #endif diff --git a/vpr/test/test_odd_even_routing.cpp b/vpr/test/test_odd_even_routing.cpp index 6396f2dedee..f19559c0bd1 100644 --- a/vpr/test/test_odd_even_routing.cpp +++ b/vpr/test/test_odd_even_routing.cpp @@ -226,7 +226,7 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { compare_routes(golden_path, found_path, noc_model); } - SECTION("Test case where multiple traffic flows are router, and routes are checked for turn legality and deadlock freedom.") { + SECTION("Test case where multiple traffic flows are routed, and routes are checked for turn legality and deadlock freedom.") { std::random_device device; std::mt19937 rand_num_gen(device()); std::uniform_int_distribution dist(0, 99); From 21f86917a2b409d0b318d90d2a42c82634c6a072 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Tue, 29 Oct 2024 16:58:22 -0400 Subject: [PATCH 4/6] remove print statements --- vpr/test/test_odd_even_routing.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/vpr/test/test_odd_even_routing.cpp b/vpr/test/test_odd_even_routing.cpp index f19559c0bd1..c0cf9278bee 100644 --- a/vpr/test/test_odd_even_routing.cpp +++ b/vpr/test/test_odd_even_routing.cpp @@ -245,26 +245,15 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") { } traffic_flow_storage.finished_noc_traffic_flows_setup(); - std::cout << "after finished "<< std::endl; vtr::vector> traffic_flow_routes(traffic_flow_storage.get_number_of_traffic_flows()); - std::cout << "Size: " << traffic_flow_routes.size() << std::endl; - std::cout << "getting pairs" << std::endl; - std::cout << "getting pairs " << traffic_flow_storage.get_all_traffic_flows().size() << std::endl; - const auto& all_pairs = traffic_flow_storage.get_all_traffic_flows().pairs(); - std::cout << "got pairs" << std::endl; - - for (const auto& [id, traffic_flow] : all_pairs) { - - std::cout << "loop: " << (size_t)id << " " << (size_t)traffic_flow.source_router_cluster_id << " " << (size_t)traffic_flow.sink_router_cluster_id << std::endl; + for (const auto& [id, traffic_flow] : traffic_flow_storage.get_all_traffic_flows().pairs()) { NocRouterId src_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.source_router_cluster_id].loc); NocRouterId dst_router_id = noc_model.get_router_at_grid_location(block_locs[traffic_flow.sink_router_cluster_id].loc); - std::cout <<"before route" << std::endl; REQUIRE_NOTHROW(routing_algorithm.route_flow(src_router_id, dst_router_id, id, traffic_flow_routes[id], noc_model)); - std::cout <<"after route" << std::endl; } ChannelDependencyGraph cdg(noc_model, traffic_flow_storage, traffic_flow_routes, block_locs); From 2266adbcf3df9b4cb723758742c9ccca5bd747b7 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Tue, 29 Oct 2024 19:34:21 -0400 Subject: [PATCH 5/6] fix no member named 'tie' in namespace 'std' --- libs/libvtrutil/src/vtr_vector.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/libvtrutil/src/vtr_vector.h b/libs/libvtrutil/src/vtr_vector.h index b4072a0d6d6..1384e7245df 100644 --- a/libs/libvtrutil/src/vtr_vector.h +++ b/libs/libvtrutil/src/vtr_vector.h @@ -4,6 +4,8 @@ #include #include #include +#include + #include "vtr_range.h" namespace vtr { From ab0a82ba5efdd07f5a3b98bc55ebf09090ec4493 Mon Sep 17 00:00:00 2001 From: soheilshahrouz Date: Wed, 30 Oct 2024 11:35:41 -0400 Subject: [PATCH 6/6] add an example in the comment for pairs() method of vtr::vector --- libs/libvtrutil/src/vtr_vector.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libs/libvtrutil/src/vtr_vector.h b/libs/libvtrutil/src/vtr_vector.h index 1384e7245df..be4e5874e98 100644 --- a/libs/libvtrutil/src/vtr_vector.h +++ b/libs/libvtrutil/src/vtr_vector.h @@ -160,6 +160,16 @@ class vector : private std::vector { * * This function allows for easy enumeration, yielding a tuple of (index, element) * pairs in each iteration. It is similar in functionality to Python's `enumerate()`. + * This function can be used in range-based with structured binding to iterate over + * indices and values at the same time. + * + * vtr::vector vec; + * for (const auto& [idx, value] : vec) { + * ... + * } + * It should be noted that value is returned by reference even if "&" + * does not appear after auto keyword. However, it is recommended to use "&" + * explicitly to avoid any confusion about value's scope. * * @ return An iterable wrapper that can be used in a range-based for loop to obtain * (index, element) pairs.