silly debugging

pages
Brett 2024-09-03 17:51:54 -04:00
parent 921fec9e6b
commit 06a34d21f1
6 changed files with 48 additions and 14 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.1.43)
project(blt-gp VERSION 0.1.44)
include(CTest)

View File

@ -94,6 +94,7 @@ namespace blt::gp
{
#ifdef BLT_TRACK_ALLOCATIONS
tracker.allocate(bytes);
std::cout << "Hey our aligned allocator allocated " << bytes << " bytes!\n";
#endif
return std::aligned_alloc(8, bytes);
}
@ -104,6 +105,7 @@ namespace blt::gp
return;
#ifdef BLT_TRACK_ALLOCATIONS
tracker.deallocate(bytes);
std::cout << "[Hey our aligned allocator deallocated " << bytes << " bytes!]\n";
#else
(void) bytes;
#endif
@ -134,6 +136,7 @@ namespace blt::gp
{
#ifdef BLT_TRACK_ALLOCATIONS
tracker.allocate(n * sizeof(T));
// std::cout << "Hey our tracked allocator allocated " << (n * sizeof(T)) << " bytes!\n";
#endif
return static_cast<pointer>(std::malloc(n * sizeof(T)));
}
@ -147,6 +150,7 @@ namespace blt::gp
{
#ifdef BLT_TRACK_ALLOCATIONS
tracker.deallocate(n * sizeof(T));
// std::cout << "[Hey our tracked allocator deallocated " << (n * sizeof(T)) << " bytes!]\n";
#else
(void) n;
#endif

View File

@ -83,6 +83,7 @@ namespace blt::gp
{
blt::size_t arg_size_bytes = 0;
blt::size_t return_size_bytes = 0;
argc_t argc{};
};
struct program_operator_storage_t
@ -118,8 +119,13 @@ namespace blt::gp
program_operator_storage_t& build(Operators& ... operators)
{
blt::size_t largest = 0;
blt::u32 largest_argc = 0;
operator_metadata_t meta;
((meta = add_operator(operators), largest = std::max(std::max(meta.arg_size_bytes, meta.return_size_bytes), largest)), ...);
((meta = add_operator(operators), largest_argc = std::max(meta.argc.argc, largest_argc),
largest = std::max(std::max(meta.arg_size_bytes, meta.return_size_bytes), largest)), ...);
// largest = largest * largest_argc;
BLT_TRACE(largest);
storage.eval_func = [&operators..., largest](const tree_t& tree, void* context) -> evaluation_context& {
const auto& ops = tree.get_operations();
@ -130,16 +136,30 @@ namespace blt::gp
results.values.reserve(largest);
blt::size_t total_so_far = 0;
blt::size_t op_pos = 0;
for (const auto& operation : blt::reverse_iterate(ops.begin(), ops.end()))
{
op_pos++;
if (operation.is_value)
{
auto cur = tracker.start_measurement();
total_so_far += stack_allocator::aligned_size(operation.type_size);
results.values.copy_from(vals.from(total_so_far), stack_allocator::aligned_size(operation.type_size));
tracker.stop_measurement(cur);
if (cur.getAllocatedByteDifference() > 0)
{
BLT_TRACE("Operator %ld allocated! pos: %ld", operation.id, op_pos);
}
continue;
}
auto cur = tracker.start_measurement();
call_jmp_table(operation.id, context, results.values, results.values, operators...);
tracker.stop_measurement(cur);
if (cur.getAllocatedByteDifference() > 0)
{
BLT_TRACE("Operator %ld allocated! pos: %ld", operation.id, op_pos);
}
}
return results;
@ -219,13 +239,6 @@ namespace blt::gp
(storage.system.register_type<Args>(), ...);
storage.system.register_type<Return>();
operator_metadata_t meta;
if constexpr (sizeof...(Args) != 0)
{
meta.arg_size_bytes = (stack_allocator::aligned_size(sizeof(Args)) + ...);
}
meta.return_size_bytes = sizeof(Return);
auto return_type_id = storage.system.get_type<Return>().id();
auto operator_id = blt::gp::operator_id(storage.operators.size());
op.id = operator_id;
@ -249,6 +262,15 @@ namespace blt::gp
BLT_ASSERT(info.argc.argc_context - info.argc.argc <= 1 && "Cannot pass multiple context as arguments!");
storage.operators.push_back(info);
operator_metadata_t meta;
if constexpr (sizeof...(Args) != 0)
{
meta.arg_size_bytes = (stack_allocator::aligned_size(sizeof(Args)) + ...);
}
meta.return_size_bytes = sizeof(Return);
meta.argc = info.argc;
storage.operator_metadata.push_back(meta);
storage.print_funcs.push_back([&op](std::ostream& out, stack_allocator& stack) {
if constexpr (blt::meta::is_streamable_v<Return>)

View File

@ -115,7 +115,7 @@ namespace blt::gp
{
if (stack.empty())
return;
if (size_ < stack.bytes_stored + bytes_stored)
if (stack.bytes_stored + bytes_stored > size_)
expand(stack.bytes_stored + size_);
std::memcpy(data_ + bytes_stored, stack.data_, stack.bytes_stored);
bytes_stored += stack.bytes_stored;
@ -125,7 +125,7 @@ namespace blt::gp
{
if (bytes == 0)
return;
if (bytes + bytes_stored >= size_)
if (bytes + bytes_stored > size_)
expand(bytes + size_);
std::memcpy(data_ + bytes_stored, stack.data_ + (stack.bytes_stored - bytes), bytes);
bytes_stored += bytes;
@ -135,7 +135,7 @@ namespace blt::gp
{
if (bytes == 0 || data == nullptr)
return;
if (bytes + bytes_stored >= size_)
if (bytes + bytes_stored > size_)
expand(bytes + size_);
std::memcpy(data_ + bytes_stored, data, bytes);
bytes_stored += bytes;

View File

@ -118,7 +118,14 @@ namespace blt::gp
evaluation_context& evaluate(void* context) const
{
return (*func)(*this, context);
auto cur = tracker.start_measurement();
auto& v = (*func)(*this, context);
tracker.stop_measurement(cur);
if (cur.getAllocatedByteDifference() > 0)
{
print(*program, std::cout, false, true, false);
}
return v;
}
blt::size_t get_depth(gp_program& program);
@ -194,6 +201,7 @@ namespace blt::gp
tracked_vector<op_container_t> operations;
blt::gp::stack_allocator values;
detail::eval_func_t* func;
gp_program* program;
};
struct fitness_t

View File

@ -295,7 +295,7 @@ namespace blt::gp
}
}
tree_t::tree_t(gp_program& program): func(&program.get_eval_func())
tree_t::tree_t(gp_program& program): func(&program.get_eval_func()), program(&program)
{
}