Skip to content

Commit

Permalink
feat(cache): add cargo number
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed May 20, 2024
1 parent 4bd8081 commit 07ca89d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2. Assume cargoes that are evicted from the cache can immediately disappear
1 change: 1 addition & 0 deletions calmapf/include/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Cache {
std::vector<Vertices> node_cargo;
std::vector<Vertices> node_id;
std::vector<Vertices> node_coming_cargo;
std::vector<std::vector<uint>> node_cargo_num;
std::vector<std::vector<uint>> bit_cache_get_lock;
std::vector<std::vector<uint>> bit_cache_insert_lock;
std::vector<std::vector<bool>> is_empty;
Expand Down
4 changes: 3 additions & 1 deletion calmapf/include/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 17 additions & 8 deletions calmapf/src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -120,22 +126,24 @@ 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;
}

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
// position while the cargo has already here
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];
}
Expand All @@ -151,7 +159,7 @@ Vertex* Cache::try_insert_cache(Vertex* cargo, std::vector<Vertex*> 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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions calmapf/src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions calmapf/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Parser::Parser(int argc, char* argv[]) {
real_dist_file_path = program.get<std::string>("real-dist-file-path");

num_agents = std::stoi(program.get<std::string>("num-agents"));
agent_capacity = std::stoi(program.get<std::string>("agent-capacity"));

random_seed = std::stoi(program.get<std::string>("random-seed"));
time_limit_sec = std::stoi(program.get<std::string>("time-limit-sec"));

Expand Down
14 changes: 3 additions & 11 deletions calmapf/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -16,15 +11,12 @@ bool Deadline::reset() {

double Deadline::elapsed_ms() const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(Time::now() -
t_s)
.count();
return std::chrono::duration_cast<std::chrono::milliseconds>(Time::now() - t_s).count();
}

double Deadline::elapsed_ns() const
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(Time::now() - t_s)
.count();
return std::chrono::duration_cast<std::chrono::nanoseconds>(Time::now() - t_s).count();
}

double elapsed_ms(const Deadline* deadline)
Expand Down
16 changes: 14 additions & 2 deletions tests/test_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ TEST(Cache, cache_LRU_single_port_test)

cache.LRU_cnt.push_back(3);

std::vector<uint> 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<uint> tmp_cache_bit_cache_get_lock;
tmp_cache_bit_cache_get_lock.push_back(0);
tmp_cache_bit_cache_get_lock.push_back(0);
Expand All @@ -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));
Expand Down Expand Up @@ -180,6 +186,12 @@ TEST(Cache, cache_FIFO_single_port_test)

cache.FIFO_cnt.push_back(3);

std::vector<uint> 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<uint> tmp_cache_bit_cache_get_lock;
tmp_cache_bit_cache_get_lock.push_back(0);
tmp_cache_bit_cache_get_lock.push_back(0);
Expand All @@ -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));
Expand Down

0 comments on commit 07ca89d

Please sign in to comment.