From 602fef0c7c92601f33348ca9a7c0e18f1356ff04 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 10 Jul 2024 12:39:01 -0400 Subject: [PATCH] something is wrong with the stack --- CMakeLists.txt | 2 +- examples/gp_test_7.cpp | 11 ++++++++- include/blt/gp/stack.h | 54 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40a8972..1c65dbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.55) +project(blt-gp VERSION 0.0.56) include(CTest) diff --git a/examples/gp_test_7.cpp b/examples/gp_test_7.cpp index f4214ae..34aeeeb 100644 --- a/examples/gp_test_7.cpp +++ b/examples/gp_test_7.cpp @@ -56,7 +56,12 @@ void print_best() auto best = program.get_best<10>(); for (auto& v : best) - BLT_TRACE("%lf (depth: %ld)", v.get().get_evaluation_value(nullptr), v.get().get_depth(program)); + { + auto size = v.get().get_values().size(); + BLT_TRACE("%lf (depth: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%", v.get().get_evaluation_value(nullptr), + v.get().get_depth(program), size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, size.total_remaining_bytes, + static_cast(size.total_used_bytes) / static_cast(size.total_no_meta_bytes)); + } //std::string small("--------------------------"); //for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++) // small += "-"; @@ -96,6 +101,10 @@ int main() while (!program.should_terminate()) { program.evaluate_fitness([](blt::gp::tree_t& current_tree, decltype(result_container)& container, blt::size_t index) { + auto size = current_tree.get_values().size(); + BLT_DEBUG("(depth: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%", + current_tree.get_depth(program), size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, + size.total_remaining_bytes, static_cast(size.total_used_bytes) / static_cast(size.total_no_meta_bytes)); container[index] = current_tree.get_evaluation_value(nullptr); return container[index]; }, result_container); diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index f600846..fba8d7d 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -36,6 +36,15 @@ namespace blt::gp constexpr static blt::size_t PAGE_SIZE = 0x1000; constexpr static blt::size_t MAX_ALIGNMENT = 8; public: + struct size_data_t + { + blt::size_t total_size_bytes = 0; + blt::size_t total_used_bytes = 0; + blt::size_t total_remaining_bytes = 0; + blt::size_t total_no_meta_bytes = 0; + }; + + /** * Pushes an instance of an object on to the stack * @tparam T type to push @@ -121,9 +130,9 @@ namespace blt::gp #endif return; } - #if BLT_DEBUG_LEVEL >= 3 +#if BLT_DEBUG_LEVEL >= 3 counter++; - #endif +#endif #endif auto diff = head->used_bytes_in_block() - bytes; // if there is not enough room left to pop completely off the block, then move to the next previous block @@ -168,8 +177,32 @@ namespace blt::gp return head->used_bytes_in_block(); } + /** + * Warning this function is slow! + * @return the size of the stack allocator in bytes + */ + [[nodiscard]] size_data_t size() + { + size_data_t size_data; + auto next = head; + while (next != nullptr) + { + size_data.total_size_bytes += next->metadata.size; + size_data.total_no_meta_bytes += next->storage_size(); + size_data.total_remaining_bytes += next->remaining_bytes_in_block(); + size_data.total_used_bytes += next->used_bytes_in_block(); + next = next->metadata.next; + } + return size_data; + } + stack_allocator() = default; + // it should be possible to remove the complex copy contrusctor along with trasnfer functions + // simply keep track of the start of the stack, aloing with the current pointer and never dealloacted + // it adds another 8 bytes to each block but should prevent the need for copying when you can just reset the stack. + // (read copy) + // if you keep track of type size information you can memcpy between stack allocators as you already only allow trivially copyable types stack_allocator(const stack_allocator& copy) { if (copy.empty()) @@ -241,6 +274,13 @@ namespace blt::gp explicit block(blt::size_t size) { +#if BLT_DEBUG_LEVEL > 0 + if (size < PAGE_SIZE) + { + BLT_WARN("Hey this block is too small, who allocated it?"); + std::abort(); + } +#endif metadata.size = size; metadata.offset = buffer; } @@ -309,6 +349,16 @@ namespace blt::gp static block* allocate_block(blt::size_t bytes) { +#if BLT_DEBUG_LEVEL > 0 + if (bytes > 32000) + { + BLT_WARN("Size too big!"); + std::abort(); + } +#endif +#if BLT_DEBUG_LEVEL > 2 + BLT_DEBUG("Allocating bytes %ld", bytes); +#endif auto size = to_nearest_page_size(bytes); auto* data = std::aligned_alloc(PAGE_SIZE, size); new(data) block{size};