Skip to content

Commit

Permalink
#7821: replace rt polymorphism in allocator algo free list
Browse files Browse the repository at this point in the history
  • Loading branch information
abhullar-tt committed May 21, 2024
1 parent 8bef5f0 commit ac1922c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
50 changes: 35 additions & 15 deletions tt_metal/impl/allocator/algorithms/allocator_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ namespace tt_metal {

namespace allocator {

template <class AllocatorType>
class Algorithm {
public:
private:
friend AllocatorType;
Algorithm(uint64_t max_size_bytes, uint64_t offset_bytes, uint64_t min_allocation_size, uint64_t alignment)
: max_size_bytes_(max_size_bytes), offset_bytes_(offset_bytes), min_allocation_size_(min_allocation_size), alignment_(alignment), lowest_occupied_address_(std::nullopt) {
TT_ASSERT(offset_bytes % this->alignment_ == 0, "Offset {} should be {} B aligned", offset_bytes, this->alignment_);
}

virtual ~Algorithm() {}
public:

~Algorithm() {}

uint64_t align(uint64_t address) const {
uint64_t factor = (address + alignment_ - 1) / alignment_;
Expand All @@ -40,29 +44,45 @@ class Algorithm {
return this->lowest_occupied_address_.value() + this->offset_bytes_;
}

virtual void init() = 0;
void init() {
static_cast<AllocatorType *>(this)->init();
}

virtual std::vector<std::pair<uint64_t, uint64_t>> available_addresses(uint64_t size_bytes) const = 0;
std::vector<std::pair<uint64_t, uint64_t>> available_addresses(uint64_t size_bytes) const {
return static_cast<AllocatorType *>(this)->available_addresses(size_bytes);
}

// bottom_up=true indicates that allocation grows from address 0
virtual std::optional<uint64_t> allocate(uint64_t size_bytes, bool bottom_up=true, uint64_t address_limit=0) = 0;
std::optional<uint64_t> allocate(uint64_t size_bytes, bool bottom_up=true, uint64_t address_limit=0) {
return static_cast<AllocatorType *>(this)->allocate(size_bytes, bottom_up, address_limit);
}

virtual std::optional<uint64_t> allocate_at_address(uint64_t absolute_start_address, uint64_t size_bytes) = 0;
std::optional<uint64_t> allocate_at_address(uint64_t absolute_start_address, uint64_t size_bytes) {
return static_cast<AllocatorType *>(this)->allocate_at_address(absolute_start_address, size_bytes);
}

virtual void deallocate(uint64_t absolute_address) = 0;
void deallocate(uint64_t absolute_address) {
static_cast<AllocatorType *>(this)->deallocate(absolute_address);
}

virtual void clear() = 0;
void clear() {
static_cast<AllocatorType *>(this)->clear();
}

virtual Statistics get_statistics() const = 0;
Statistics get_statistics() const {
return static_cast<AllocatorType *>(this)->get_statistics();
}

virtual void dump_blocks(std::ofstream &out) const = 0;
void dump_blocks(std::ofstream &out) const {
static_cast<AllocatorType *>(this)->dump_block();
}

protected:
uint64_t max_size_bytes_;
uint64_t offset_bytes_;
uint64_t min_allocation_size_;
uint64_t alignment_;
std::optional<uint64_t> lowest_occupied_address_;
uint64_t max_size_bytes_ = 0;
uint64_t offset_bytes_ = 0;
uint64_t min_allocation_size_ = 0;
uint64_t alignment_ = 0;
std::optional<uint64_t> lowest_occupied_address_ = std::nullopt;
};

} // namespace allocator
Expand Down
2 changes: 1 addition & 1 deletion tt_metal/impl/allocator/algorithms/free_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace tt_metal {

namespace allocator {

class FreeList : public Algorithm {
class FreeList : public Algorithm<FreeList> {
public:
enum class SearchPolicy {
BEST = 0,
Expand Down
4 changes: 2 additions & 2 deletions tt_metal/impl/allocator/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "allocator_types.hpp"
#include "tt_metal/common/assert.hpp"
#include "tt_metal/common/core_coord.h"
#include "tt_metal/impl/allocator/algorithms/allocator_algorithm.hpp"
#include "tt_metal/impl/allocator/algorithms/free_list.hpp"

namespace tt {

Expand Down Expand Up @@ -62,7 +62,7 @@ class BankManager {
// This is to store offsets for any banks that share a core or node (dram in wh/storage core), so we can view all banks using only bank_id
// Set to 0 for cores/nodes with only 1 bank
std::unordered_map<uint32_t, int64_t> bank_id_to_bank_offset_;
std::unique_ptr<Algorithm> allocator_;
std::unique_ptr<FreeList> allocator_;
uint64_t interleaved_address_limit_;
void validate_bank_id(uint32_t bank_id) const;

Expand Down

0 comments on commit ac1922c

Please sign in to comment.