shared memory?

shared
Brett 2024-08-25 17:01:06 -04:00
parent 6ded725802
commit 95b88aeebf
8 changed files with 73 additions and 29 deletions

View File

@ -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)

View File

@ -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::ptrdiff_t>(blt::gp::hello.load());
// auto u = static_cast<blt::ptrdiff_t>(blt::gp::unhello.load());

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -49,11 +49,10 @@ namespace blt::gp
}
template<typename Func>
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<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<Func>(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<stack>& tree_generator, type_id type, blt::size_t new_depth) {
return create_tree(tree, [args](gp_program& program, std::stack<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<stack>& tree_generator, type_id type, blt::size_t new_depth) {
return create_tree(tree, [args](gp_program& program, std::stack<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;
}

View File

@ -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<blt::ptrdiff_t>(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<blt::ptrdiff_t>(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<blt::ptrdiff_t>(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();

View File

@ -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();
}
}