Skip to content

Commit

Permalink
#11208: Slotmap datastructure for creating resource pools (#13427)
Browse files Browse the repository at this point in the history
This reverts commit ae2c1d6.

Force-merged by @tt-rkim because it fixes a previous build problem.
  • Loading branch information
yan-zaretskiy authored Oct 3, 2024
1 parent 9a49fd6 commit 65ffbea
Show file tree
Hide file tree
Showing 3 changed files with 513 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/tt_metal/tt_metal/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(UNIT_TESTS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/multichip/device_cluster_api.cpp
${CMAKE_CURRENT_SOURCE_DIR}/multichip/erisc_app_direct_send.cpp
${CMAKE_CURRENT_SOURCE_DIR}/multichip/ring_gather_kernels.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tt_stl/slotmap.cpp
)

add_executable(unit_tests ${UNIT_TESTS_SRC} $<TARGET_OBJECTS:unit_tests_common_o>)
Expand Down
133 changes: 133 additions & 0 deletions tests/tt_metal/tt_metal/unit_tests/tt_stl/slotmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.
//
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>

#include "tt_metal/tt_stl/slotmap.hpp"


MAKE_SLOTMAP_KEY(IntKey, uint16_t, 10);
using IntSlotMap = tt::stl::SlotMap<IntKey, int>;


MAKE_SLOTMAP_KEY(StringKey, uint16_t, 10);
using StringSlotMap = tt::stl::SlotMap<StringKey, std::string>;

TEST(SlotMapTest, CanCreateSlotMap) {
IntSlotMap slotmap;
EXPECT_TRUE(slotmap.empty());
}

TEST(SlotMapTest, CanInsertIntoSlotMap) {
IntSlotMap slotmap;
auto key = slotmap.insert(42);

EXPECT_TRUE(slotmap.contains(key));
EXPECT_EQ(slotmap.size(), 1);
EXPECT_EQ(*slotmap.get(key), 42);
}

TEST(SlotMapTest, CanInsertIntoStringSlotMap) {
StringSlotMap slotmap;
auto key = slotmap.insert("hello");

EXPECT_TRUE(slotmap.contains(key));
EXPECT_EQ(slotmap.size(), 1);
EXPECT_EQ(*slotmap.get(key), "hello");
}

TEST(SlotMapTest, CanInsertMultipleValuesIntoSlotMap) {
IntSlotMap slotmap;

auto key1 = slotmap.insert(42);
auto key2 = slotmap.insert(43);
auto key3 = slotmap.insert(44);

EXPECT_TRUE(slotmap.contains(key1));
EXPECT_TRUE(slotmap.contains(key2));
EXPECT_TRUE(slotmap.contains(key3));
EXPECT_EQ(slotmap.size(), 3);
EXPECT_EQ(*slotmap.get(key1), 42);
EXPECT_EQ(*slotmap.get(key2), 43);
EXPECT_EQ(*slotmap.get(key3), 44);
}

TEST(SlotMapTest, CanRemoveValueFromSlotMap) {
IntSlotMap slotmap;

auto key1 = slotmap.insert(42);
auto key2 = slotmap.insert(43);

EXPECT_TRUE(slotmap.contains(key1));
EXPECT_TRUE(slotmap.contains(key2));
EXPECT_EQ(slotmap.size(), 2);

slotmap.remove(key2);

EXPECT_TRUE(slotmap.contains(key1));
EXPECT_FALSE(slotmap.contains(key2));
EXPECT_EQ(slotmap.size(), 1);
EXPECT_EQ(*slotmap.get(key1), 42);
}

TEST(SlotMapTest, CanRemoveValueFromStringSlotMap) {
StringSlotMap slotmap(2);

auto key1 = slotmap.insert("hello");
auto key2 = slotmap.insert("world");

EXPECT_TRUE(slotmap.contains(key1));
EXPECT_TRUE(slotmap.contains(key2));
EXPECT_EQ(slotmap.size(), 2);

slotmap.remove(key1);

EXPECT_FALSE(slotmap.contains(key1));
EXPECT_TRUE(slotmap.contains(key2));
EXPECT_EQ(slotmap.size(), 1);
EXPECT_EQ(*slotmap.get(key2), "world");
}

TEST(SlotMapTest, CanIterateOverSlotMap) {
IntSlotMap slotmap;

slotmap.insert(42);
slotmap.insert(43);
slotmap.insert(44);

std::vector<int> expected_values = {42, 43, 44};
std::vector<int> actual_values;
std::copy(slotmap.cbegin(), slotmap.cend(), std::back_inserter(actual_values));

EXPECT_EQ(actual_values, expected_values);
}

TEST(KeyTest, CanCreateKeyFromRaw) {
uint16_t raw = 0b0000000101000011;
IntKey key(raw);

EXPECT_EQ(key.index(), 0b101);
EXPECT_EQ(key.version(), 0b11);
}

TEST(SlotMapTest, ThrowsOnInsertIfMaxIndex) {
IntSlotMap slotmap;

IntKey key;
for (int i = 0; i < IntKey::max_index + 1; i++) {
key = slotmap.insert(i);
}

EXPECT_EQ(key.index(), IntKey::max_index);
EXPECT_THROW(slotmap.insert(0), std::runtime_error);
}

TEST(SlotMapTest, CanReserveSlots) {
StringSlotMap slotmap;

slotmap.insert("hello");
slotmap.reserve(32);

EXPECT_GE(slotmap.capacity(), 32);
}
Loading

0 comments on commit 65ffbea

Please sign in to comment.