From 07ca89d71e17d63ac3e67890903aaed3d8667d21 Mon Sep 17 00:00:00 2001 From: Zhenghong Yu Date: Mon, 20 May 2024 17:59:02 +0800 Subject: [PATCH] feat(cache): add cargo number --- README.md | 3 +-- calmapf/include/cache.hpp | 1 + calmapf/include/parser.hpp | 4 +++- calmapf/src/cache.cpp | 25 +++++++++++++++++-------- calmapf/src/graph.cpp | 1 + calmapf/src/parser.cpp | 2 ++ calmapf/src/utils.cpp | 14 +++----------- tests/test_cache.cpp | 16 ++++++++++++++-- 8 files changed, 42 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 6224abe..edbaaa2 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,4 @@ cmake -B build && make -C build ## Assumption 1. Assume cargo in the warehouse is infinite -2. Assume agents can bring infinite cargos back to the cache -3. Assume cargoes that are evicted from the cache can immediately disappear \ No newline at end of file +2. Assume cargoes that are evicted from the cache can immediately disappear \ No newline at end of file diff --git a/calmapf/include/cache.hpp b/calmapf/include/cache.hpp index 2eb7ba4..1fa0a6e 100644 --- a/calmapf/include/cache.hpp +++ b/calmapf/include/cache.hpp @@ -11,6 +11,7 @@ struct Cache { std::vector node_cargo; std::vector node_id; std::vector node_coming_cargo; + std::vector> node_cargo_num; std::vector> bit_cache_get_lock; std::vector> bit_cache_insert_lock; std::vector> is_empty; diff --git a/calmapf/include/parser.hpp b/calmapf/include/parser.hpp index 18c01b1..c8e3ce4 100644 --- a/calmapf/include/parser.hpp +++ b/calmapf/include/parser.hpp @@ -26,9 +26,11 @@ struct Parser { uint goals_max_m; std::string real_dist_file_path; - // Instance settings + // Agent settings uint num_agents; + uint agent_capacity; + // Instance settings int random_seed; std::mt19937 MT; diff --git a/calmapf/src/cache.cpp b/calmapf/src/cache.cpp index d187a7b..0771786 100644 --- a/calmapf/src/cache.cpp +++ b/calmapf/src/cache.cpp @@ -99,11 +99,17 @@ int Cache::_get_cache_block_in_cache_position(Vertex* block) { } int Cache::_get_cargo_in_cache_position(Vertex* cargo) { - int index = -1; + int index = -2; for (uint i = 0; i < node_cargo[cargo->group].size(); i++) { if (node_cargo[cargo->group][i] == cargo) { - index = i; - break; + if (node_cargo_num[cargo->group][i] > 0) { + index = i; + break; + } + else { + index = -1; + break; + } } } return index; @@ -120,7 +126,7 @@ bool Cache::_is_cargo_in_coming_cache(Vertex* cargo) { bool Cache::look_ahead_cache(Vertex* cargo) { int cache_index = _get_cargo_in_cache_position(cargo); - if (cache_index != -1 && bit_cache_insert_lock[cargo->group][cache_index] == 0) return true; + if (cache_index >= 0 && bit_cache_insert_lock[cargo->group][cache_index] == 0) return true; return false; } @@ -128,7 +134,7 @@ Vertex* Cache::try_cache_cargo(Vertex* cargo) { int cache_index = _get_cargo_in_cache_position(cargo); // If we can find cargo cached and is not reserved to be replaced , we go to cache and get it - if (cache_index != -1 && bit_cache_insert_lock[cargo->group][cache_index] == 0) { + if (cache_index >= 0 && bit_cache_insert_lock[cargo->group][cache_index] == 0) { cache_console->debug("Cache hit! Agent will go {} to get cargo {}", *node_id[cargo->group][cache_index], *cargo); // For here, we allow multiple agents lock on cache get position // It is impossible that a coming agent move cargo to this @@ -136,6 +142,8 @@ Vertex* Cache::try_cache_cargo(Vertex* cargo) { bit_cache_get_lock[cargo->group][cache_index] += 1; // We also update cache evicted policy statistics _update_cache_evited_policy_statistics(cargo->group, cache_index, false); + // Update cargo number + node_cargo_num[cargo->group][cache_index] -= 1; return node_id[cargo->group][cache_index]; } @@ -151,7 +159,7 @@ Vertex* Cache::try_insert_cache(Vertex* cargo, std::vector port_list) { // First, if cargo has already cached or is coming on the way, we directly go // to unloading port, for simplify, we just check cache group here - if (_get_cargo_in_cache_position(cargo) != -1 || _is_cargo_in_coming_cache(cargo)) return unloading_port; + if (_get_cargo_in_cache_position(cargo) != -2 || _is_cargo_in_coming_cache(cargo)) return unloading_port; // Second try to find a empty position to insert cargo // TODO: optimization, can set a flag to skip this @@ -192,13 +200,14 @@ bool Cache::update_cargo_into_cache(Vertex* cargo, Vertex* cache_node) { int cache_index = _get_cache_block_in_cache_position(cache_node); // We should only update it while it is not in cache - assert(cargo_index == -1); + assert(cargo_index == -2); assert(_is_cargo_in_coming_cache(cargo)); // Update cache cache_console->debug("Update cargo {} to cache block {}", *cargo, *cache_node); node_cargo[cache_node->group][cache_index] = cargo; bit_cache_insert_lock[cache_node->group][cache_index] -= 1; + node_cargo_num[cache_node->group][cache_index] = parser->agent_capacity - 1; return true; } @@ -207,7 +216,7 @@ bool Cache::update_cargo_from_cache(Vertex* cargo, Vertex* cache_node) { int cache_index = _get_cache_block_in_cache_position(cache_node); // We must make sure the cargo is still in the cache - assert(cargo_index != -1); + assert(cargo_index != -2); // Simply release lock cache_console->debug("Agents gets {} from cache {}", *cargo, *cache_node); diff --git a/calmapf/src/graph.cpp b/calmapf/src/graph.cpp index a4cb1c7..79ed0b0 100644 --- a/calmapf/src/graph.cpp +++ b/calmapf/src/graph.cpp @@ -179,6 +179,7 @@ Graph::Graph(Parser* _parser) : parser(_parser) cache->node_cargo.push_back(tmp_cache_node); cache->node_id.push_back(tmp_cache_node); cache->node_coming_cargo.push_back(tmp_cache_node); + cache->node_cargo_num.emplace_back(tmp_cache_node.size(), 0); cache->bit_cache_get_lock.emplace_back(tmp_cache_node.size(), 0); cache->bit_cache_insert_lock.emplace_back(tmp_cache_node.size(), 0); cache->is_empty.emplace_back(tmp_cache_node.size(), true); diff --git a/calmapf/src/parser.cpp b/calmapf/src/parser.cpp index 26f878f..c9e621d 100644 --- a/calmapf/src/parser.cpp +++ b/calmapf/src/parser.cpp @@ -52,6 +52,8 @@ Parser::Parser(int argc, char* argv[]) { real_dist_file_path = program.get("real-dist-file-path"); num_agents = std::stoi(program.get("num-agents")); + agent_capacity = std::stoi(program.get("agent-capacity")); + random_seed = std::stoi(program.get("random-seed")); time_limit_sec = std::stoi(program.get("time-limit-sec")); diff --git a/calmapf/src/utils.cpp b/calmapf/src/utils.cpp index c2f15ab..67812b2 100644 --- a/calmapf/src/utils.cpp +++ b/calmapf/src/utils.cpp @@ -2,12 +2,7 @@ Vertex::Vertex(int _id, int _index, int _width, int _group) : id(_id), index(_index), width(_width), group(_group), neighbor(Vertices()) {} -void info(const int level, const int verbose) { std::cout << std::endl; } - -Deadline::Deadline(double _time_limit_ms) - : t_s(Time::now()), time_limit_ms(_time_limit_ms) -{ -} +Deadline::Deadline(double _time_limit_ms) : t_s(Time::now()), time_limit_ms(_time_limit_ms) {} bool Deadline::reset() { t_s = Time::now(); @@ -16,15 +11,12 @@ bool Deadline::reset() { double Deadline::elapsed_ms() const { - return std::chrono::duration_cast(Time::now() - - t_s) - .count(); + return std::chrono::duration_cast(Time::now() - t_s).count(); } double Deadline::elapsed_ns() const { - return std::chrono::duration_cast(Time::now() - t_s) - .count(); + return std::chrono::duration_cast(Time::now() - t_s).count(); } double elapsed_ms(const Deadline* deadline) diff --git a/tests/test_cache.cpp b/tests/test_cache.cpp index c604e0b..3a78629 100644 --- a/tests/test_cache.cpp +++ b/tests/test_cache.cpp @@ -67,6 +67,12 @@ TEST(Cache, cache_LRU_single_port_test) cache.LRU_cnt.push_back(3); + std::vector tmp_cache_node_cargo_num; + tmp_cache_node_cargo_num.push_back(10); + tmp_cache_node_cargo_num.push_back(10); + tmp_cache_node_cargo_num.push_back(10); + cache.node_cargo_num.push_back(tmp_cache_node_cargo_num); + std::vector tmp_cache_bit_cache_get_lock; tmp_cache_bit_cache_get_lock.push_back(0); tmp_cache_bit_cache_get_lock.push_back(0); @@ -84,7 +90,7 @@ TEST(Cache, cache_LRU_single_port_test) // Test `_get_cargo_in_cache_index(Vertex* cargo)` ASSERT_EQ(0, cache._get_cargo_in_cache_position(cargo_1)); - ASSERT_EQ(-1, cache._get_cargo_in_cache_position(cargo_5)); + ASSERT_EQ(-2, cache._get_cargo_in_cache_position(cargo_5)); // Test `_is_cargo_in_coming_cache(Vertex* cargo)` ASSERT_EQ(true, cache._is_cargo_in_coming_cache(cargo_4)); @@ -180,6 +186,12 @@ TEST(Cache, cache_FIFO_single_port_test) cache.FIFO_cnt.push_back(3); + std::vector tmp_cache_node_cargo_num; + tmp_cache_node_cargo_num.push_back(10); + tmp_cache_node_cargo_num.push_back(10); + tmp_cache_node_cargo_num.push_back(10); + cache.node_cargo_num.push_back(tmp_cache_node_cargo_num); + std::vector tmp_cache_bit_cache_get_lock; tmp_cache_bit_cache_get_lock.push_back(0); tmp_cache_bit_cache_get_lock.push_back(0); @@ -197,7 +209,7 @@ TEST(Cache, cache_FIFO_single_port_test) // Test `_get_cargo_in_cache_index(Vertex* cargo)` ASSERT_EQ(0, cache._get_cargo_in_cache_position(cargo_1)); - ASSERT_EQ(-1, cache._get_cargo_in_cache_position(cargo_5)); + ASSERT_EQ(-2, cache._get_cargo_in_cache_position(cargo_5)); // Test `_is_cargo_in_coming_cache(Vertex* cargo)` ASSERT_EQ(true, cache._is_cargo_in_coming_cache(cargo_4));