diff --git a/CMakeLists.txt b/CMakeLists.txt index 00f2f30..d3d5f2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.19) +project(blt-gp VERSION 0.1.20) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index d9c0b05..f620edd 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -113,6 +113,7 @@ int main() while (!program.should_terminate()) { BLT_TRACE("------------{Begin Generation %ld}------------", program.get_current_generation()); + BLT_TRACE("Creating next generation"); BLT_START_INTERVAL("Symbolic Regression", "Gen"); program.create_next_generation(); BLT_END_INTERVAL("Symbolic Regression", "Gen"); @@ -148,5 +149,12 @@ int main() BLT_PRINT_PROFILE("Symbolic Regression", blt::PRINT_CYCLES | blt::PRINT_THREAD | blt::PRINT_WALL); +// BLT_TRACE("Allocations:"); +// auto h = static_cast(blt::gp::hello.load()); +// auto u = static_cast(blt::gp::unhello.load()); +// BLT_TRACE("Allocated: %ld", h); +// BLT_TRACE("Deallocated: %ld", u); +// BLT_TRACE("Ratio: %lf Difference: %ld", static_cast(h) / static_cast(u), std::abs(h - u)); + return 0; } \ No newline at end of file diff --git a/include/blt/gp/fwdecl.h b/include/blt/gp/fwdecl.h index c058d5a..2c41f91 100644 --- a/include/blt/gp/fwdecl.h +++ b/include/blt/gp/fwdecl.h @@ -55,7 +55,7 @@ namespace blt::gp class operator_storage_test; // context*, read stack, write stack using operator_func_t = std::function; - using eval_func_t = std::function; + using eval_func_t = std::function; // debug function, using print_func_t = std::function; diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 8a87553..69e52cf 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -115,12 +115,14 @@ namespace blt::gp for (auto v : sizes) largest = std::max(v, largest); - storage.eval_func = [&operators..., largest](const tree_t& tree, void* context) { + storage.eval_func = [&operators..., largest](const tree_t& tree, void* context) -> evaluation_context& { const auto& ops = tree.get_operations(); const auto& vals = tree.get_values(); - evaluation_context results{}; + static thread_local evaluation_context results{}; + results.values.reset(); results.values.reserve(largest); +// BLT_DEBUG("%ld stored %ld", largest, results.values.internal_storage_size()); blt::size_t total_so_far = 0; @@ -261,7 +263,7 @@ namespace blt::gp storage.names.push_back(op.get_name()); if (op.is_ephemeral()) storage.static_types.insert(operator_id); - return total_size_required; + return total_size_required * 2; } template diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index 21668eb..d2e4fe3 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -20,7 +20,7 @@ #define BLT_GP_STACK_H #include -#include +#include #include #include #include @@ -43,16 +43,25 @@ namespace blt::gp BLT_META_MAKE_FUNCTION_CHECK(drop); } +// inline std::atomic_uint64_t hello = 0; +// inline std::atomic_uint64_t unhello = 0; + class aligned_allocator { public: void* allocate(blt::size_t bytes) // NOLINT { +// hello.fetch_add(1, std::memory_order_relaxed); +// BLT_TRACE("Allocating %ld bytes", bytes); return std::aligned_alloc(8, bytes); } void deallocate(void* ptr, blt::size_t) // NOLINT { +// if (ptr == nullptr) +// return; +// unhello.fetch_add(1, std::memory_order_relaxed); +// BLT_TRACE("Deallocating %ld bytes", bytes); std::free(ptr); } }; @@ -122,7 +131,6 @@ namespace blt::gp ~stack_allocator() { - //std::free(data_); get_allocator().deallocate(data_, size_); } @@ -266,13 +274,33 @@ namespace blt::gp void reserve(blt::size_t bytes) { if (bytes > size_) - expand(bytes); + expand_raw(bytes); + } + + [[nodiscard]] blt::size_t stored() const + { + return bytes_stored; + } + + [[nodiscard]] blt::size_t internal_storage_size() const + { + return size_; + } + + void reset() + { + bytes_stored = 0; } private: void expand(blt::size_t bytes) { bytes = to_nearest_page_size(bytes); + expand_raw(bytes); + } + + void expand_raw(blt::size_t bytes) + { auto new_data = static_cast(get_allocator().allocate(bytes)); if (bytes_stored > 0) std::memcpy(new_data, data_, bytes_stored); diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 96dedfa..8045428 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -84,7 +84,7 @@ namespace blt::gp return values; } - evaluation_context evaluate(void* context) const + evaluation_context& evaluate(void* context) const { return (*func)(*this, context); } @@ -115,7 +115,7 @@ namespace blt::gp template T get_evaluation_value(void* context) { - auto results = evaluate(context); + auto& results = evaluate(context); return results.values.pop(); }