From 95b88aeebfe3adca38614c4309315adac8e1326b Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Sun, 25 Aug 2024 17:01:06 -0400 Subject: [PATCH] shared memory? --- CMakeLists.txt | 2 +- examples/symbolic_regression.cpp | 5 ++++ include/blt/gp/generators.h | 6 ++--- include/blt/gp/program.h | 7 ++++- include/blt/gp/tree.h | 2 ++ src/generators.cpp | 45 ++++++++++++++++++++------------ src/transformers.cpp | 26 +++++++++++++----- src/tree.cpp | 9 +++++++ 8 files changed, 73 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7055ed..a89ee8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.23) +project(blt-gp VERSION 0.1.25) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index 3f2d7c3..7bb1160 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -170,6 +170,11 @@ int main() BLT_PRINT_PROFILE("Symbolic Regression", blt::PRINT_CYCLES | blt::PRINT_THREAD | blt::PRINT_WALL); +#ifdef BLT_TRACK_ALLOCATIONS + BLT_TRACE("Total Allocations: %ld times with a total of %s", blt::gp::tracker.getAllocations(), + blt::byte_convert_t(blt::gp::tracker.getAllocatedBytes()).convert_to_nearest_type().to_pretty_string().c_str()); +#endif + // BLT_TRACE("Allocations:"); // auto h = static_cast(blt::gp::hello.load()); // auto u = static_cast(blt::gp::unhello.load()); diff --git a/include/blt/gp/generators.h b/include/blt/gp/generators.h index fac1ec7..99127d0 100644 --- a/include/blt/gp/generators.h +++ b/include/blt/gp/generators.h @@ -51,7 +51,7 @@ namespace blt::gp class tree_generator_t { public: - virtual tree_t generate(const generator_arguments& args) = 0; + virtual void generate(tree_t& out, const generator_arguments& args) = 0; virtual ~tree_generator_t() = default; }; @@ -59,13 +59,13 @@ namespace blt::gp class grow_generator_t : public tree_generator_t { public: - tree_t generate(const generator_arguments& args) final; + void generate(tree_t& out, const generator_arguments& args) final; }; class full_generator_t : public tree_generator_t { public: - tree_t generate(const generator_arguments& args) final; + void generate(tree_t& out, const generator_arguments& args) final; }; class population_initializer_t diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index f4e8685..5d7bfec 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -136,6 +136,11 @@ namespace blt::gp continue; } call_jmp_table(operation.id, context, results.values, results.values, operators...); +// if (results.values.internal_storage_size() != l) +// { +// BLT_DEBUG("Size %ld is now %ld", l, results.values.internal_storage_size()); +// l = results.values.internal_storage_size(); +// } } return results; @@ -301,7 +306,7 @@ namespace blt::gp template static inline void call_jmp_table_internal(size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, - std::integer_sequence, Operators&... operators) + std::integer_sequence, Operators& ... operators) { if (op >= sizeof...(operator_ids)) { diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 8045428..2fc985e 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -57,6 +57,8 @@ namespace blt::gp public: explicit tree_t(gp_program& program); + void clear(gp_program& program); + struct child_t { blt::ptrdiff_t start; diff --git a/src/generators.cpp b/src/generators.cpp index 1ca796e..0b9185a 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -49,11 +49,10 @@ namespace blt::gp } template - inline tree_t create_tree(Func&& perChild, const generator_arguments& args) + inline void create_tree(tree_t& tree, Func&& perChild, const generator_arguments& args) { std::stack tree_generator = get_initial_stack(args.program, args.root_type); blt::size_t max_depth = 0; - tree_t tree{args.program}; while (!tree_generator.empty()) { @@ -77,13 +76,11 @@ namespace blt::gp for (const auto& child : info.argument_types) std::forward(perChild)(args.program, tree_generator, child, top.depth + 1); } - - return tree; } - tree_t grow_generator_t::generate(const generator_arguments& args) + void grow_generator_t::generate(tree_t& tree, const generator_arguments& args) { - return create_tree([args](gp_program& program, std::stack& tree_generator, type_id type, blt::size_t new_depth) { + return create_tree(tree, [args](gp_program& program, std::stack& tree_generator, type_id type, blt::size_t new_depth) { if (new_depth >= args.max_depth) { if (program.get_type_terminals(type).empty()) @@ -99,9 +96,9 @@ namespace blt::gp }, args); } - tree_t full_generator_t::generate(const generator_arguments& args) + void full_generator_t::generate(tree_t& tree, const generator_arguments& args) { - return create_tree([args](gp_program& program, std::stack& tree_generator, type_id type, blt::size_t new_depth) { + return create_tree(tree, [args](gp_program& program, std::stack& tree_generator, type_id type, blt::size_t new_depth) { if (new_depth >= args.max_depth) { if (program.get_type_terminals(type).empty()) @@ -119,7 +116,11 @@ namespace blt::gp population_t pop; for (auto i = 0ul; i < args.size; i++) - pop.get_individuals().emplace_back(grow.generate(args.to_gen_args())); + { + tree_t tree{args.program}; + grow.generate(tree, args.to_gen_args()); + pop.get_individuals().emplace_back(tree); + } return pop; } @@ -129,7 +130,11 @@ namespace blt::gp population_t pop; for (auto i = 0ul; i < args.size; i++) - pop.get_individuals().emplace_back(full.generate(args.to_gen_args())); + { + tree_t tree{args.program}; + full.generate(tree, args.to_gen_args()); + pop.get_individuals().emplace_back(tree); + } return pop; } @@ -140,10 +145,12 @@ namespace blt::gp for (auto i = 0ul; i < args.size; i++) { + tree_t tree{args.program}; if (args.program.get_random().choice()) - pop.get_individuals().emplace_back(full.generate(args.to_gen_args())); + full.generate(tree, args.to_gen_args()); else - pop.get_individuals().emplace_back(grow.generate(args.to_gen_args())); + grow.generate(tree, args.to_gen_args()); + pop.get_individuals().emplace_back(tree); } return pop; @@ -160,22 +167,26 @@ namespace blt::gp { for (auto i = 0ul; i < per_step; i++) { + tree_t tree{args.program}; if (args.program.get_random().choice()) - pop.get_individuals().emplace_back(full.generate({args.program, args.root_type, args.min_depth, depth})); + full.generate(tree, {args.program, args.root_type, args.min_depth, depth}); else - pop.get_individuals().emplace_back(grow.generate({args.program, args.root_type, args.min_depth, depth})); + grow.generate(tree, {args.program, args.root_type, args.min_depth, depth}); + pop.get_individuals().emplace_back(tree); } } for (auto i = 0ul; i < remainder; i++) { + tree_t tree{args.program}; if (args.program.get_random().choice()) - pop.get_individuals().emplace_back(full.generate(args.to_gen_args())); + full.generate(tree, args.to_gen_args()); else - pop.get_individuals().emplace_back(grow.generate(args.to_gen_args())); + grow.generate(tree, args.to_gen_args()); + pop.get_individuals().emplace_back(tree); } - blt_assert(pop.get_individuals().size() == args.size); + BLT_ASSERT(pop.get_individuals().size() == args.size); return pop; } diff --git a/src/transformers.cpp b/src/transformers.cpp index a64faa5..149a5d4 100644 --- a/src/transformers.cpp +++ b/src/transformers.cpp @@ -27,6 +27,15 @@ namespace blt::gp { + grow_generator_t grow_generator; + + inline tree_t& get_static_tree_tl(gp_program& program) + { + static thread_local tree_t new_tree{program}; + new_tree.clear(program); + return new_tree; + } + inline blt::size_t accumulate_type_sizes(detail::op_iter begin, detail::op_iter end) { blt::size_t total = 0; @@ -47,8 +56,6 @@ namespace blt::gp return buffer.data(); } - grow_generator_t grow_generator; - mutation_t::config_t::config_t(): generator(grow_generator) {} @@ -228,7 +235,8 @@ namespace blt::gp auto begin_itr = ops_r.begin() + begin_point; auto end_itr = ops_r.begin() + end_point; - auto new_tree = config.generator.get().generate({program, type_info.return_type, config.replacement_min_depth, config.replacement_max_depth}); + auto& new_tree = get_static_tree_tl(program); + config.generator.get().generate(new_tree, {program, type_info.return_type, config.replacement_min_depth, config.replacement_max_depth}); auto& new_ops_r = new_tree.get_operations(); auto& new_vals_r = new_tree.get_values(); @@ -364,7 +372,8 @@ namespace blt::gp if (index < current_func_info.argument_types.size() && val.id != current_func_info.argument_types[index].id) { // TODO: new config? - auto tree = config.generator.get().generate( + auto& tree = get_static_tree_tl(program); + config.generator.get().generate(tree, {program, val.id, config.replacement_min_depth, config.replacement_max_depth}); auto& child = children_data[children_data.size() - 1 - index]; @@ -445,7 +454,8 @@ namespace blt::gp for (blt::ptrdiff_t i = static_cast(replacement_func_info.argc.argc) - 1; i >= current_func_info.argc.argc; i--) { - auto tree = config.generator.get().generate( + auto& tree = get_static_tree_tl(program); + config.generator.get().generate(tree, {program, replacement_func_info.argument_types[i].id, config.replacement_min_depth, config.replacement_max_depth}); blt::size_t total_bytes_for = tree.total_value_bytes(); @@ -518,7 +528,8 @@ namespace blt::gp blt::size_t start_index = c_node; for (blt::ptrdiff_t i = new_argc - 1; i > static_cast(arg_position); i--) { - auto tree = config.generator.get().generate( + auto& tree = get_static_tree_tl(program); + config.generator.get().generate(tree, {program, replacement_func_info.argument_types[i].id, config.replacement_min_depth, config.replacement_max_depth}); blt::size_t total_bytes_for = tree.total_value_bytes(); @@ -531,7 +542,8 @@ namespace blt::gp vals.copy_from(combined_ptr, for_bytes); for (blt::ptrdiff_t i = static_cast(arg_position) - 1; i >= 0; i--) { - auto tree = config.generator.get().generate( + auto& tree = get_static_tree_tl(program); + config.generator.get().generate(tree, {program, replacement_func_info.argument_types[i].id, config.replacement_min_depth, config.replacement_max_depth}); blt::size_t total_bytes_for = tree.total_value_bytes(); diff --git a/src/tree.cpp b/src/tree.cpp index bf80000..8ec56d3 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -299,4 +299,13 @@ namespace blt::gp { } + + void tree_t::clear(gp_program& program) + { + auto* f = &program.get_eval_func(); + if (f != func) + func = f; + operations.clear(); + values.reset(); + } } \ No newline at end of file