Compare commits

..

No commits in common. "2b1f1c92abba50851b18347f7003ee70728f8b0a" and "699806aeff9216b9593b8eda8a2db2638531ccdc" have entirely different histories.

7 changed files with 9 additions and 182 deletions

View File

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

View File

@ -46,16 +46,6 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t()
.set_max_generations(50) .set_max_generations(50)
.set_pop_size(500) .set_pop_size(500)
.set_thread_count(0); .set_thread_count(0);
//blt::gp::prog_config_t config = blt::gp::prog_config_t()
// .set_initial_min_tree_size(2)
// .set_initial_max_tree_size(6)
// .set_elite_count(2)
// .set_crossover_chance(0.9)
// .set_mutation_chance(0.1)
// .set_reproduction_chance(0)
// .set_max_generations(50)
// .set_pop_size(500)
// .set_thread_count(0);
blt::gp::gp_program program{SEED, config}; blt::gp::gp_program program{SEED, config};
@ -90,6 +80,8 @@ float example_function(float x)
return x * x * x * x + x * x * x + x * x + x; return x * x * x * x + x * x * x + x * x + x;
} }
BLT_MAKE_CONFIG_TYPE(test, BLT_MAKE_GETTER_AND_SETTER(int, silly) BLT_MAKE_GETTER(int, billy));
int main() int main()
{ {
test t; test t;

View File

@ -25,9 +25,6 @@
#include <blt/gp/stats.h> #include <blt/gp/stats.h>
#include <ostream> #include <ostream>
#include <cstdlib> #include <cstdlib>
#include <mutex>
#include <atomic>
#include <blt/std/mmap.h>
namespace blt::gp namespace blt::gp
{ {
@ -116,159 +113,6 @@ namespace blt::gp
} }
}; };
template<typename Alloc = blt::aligned_huge_allocator>
class variable_bump_allocator
{
public:
explicit variable_bump_allocator(blt::size_t default_block_size = BLT_2MB_SIZE): default_block_size(default_block_size)
{}
void* allocate(blt::size_t bytes)
{
#ifdef BLT_TRACK_ALLOCATIONS
tracker.allocate(bytes);
#endif
std::scoped_lock lock(mutex);
if (head == nullptr || head->remaining_bytes_in_block() < static_cast<blt::ptrdiff_t>(bytes))
{
push_block(bytes);
}
auto ptr = head->metadata.offset;
head->metadata.offset += bytes;
++head->metadata.allocated_objects;
return ptr;
}
void deallocate(void* ptr, blt::size_t bytes)
{
if (ptr == nullptr)
return;
#ifdef BLT_TRACK_ALLOCATIONS
tracker.deallocate(bytes);
#else
(void) bytes;
#endif
std::scoped_lock lock(mutex);
block_t* blk = to_block(ptr);
--blk->metadata.allocated_objects;
if (blk->metadata.allocated_objects == 0)
{
if (blk->metadata.has_deallocated)
alloc.deallocate(blk, blk->metadata.size);
else
{
if (head == blk)
head = head->metadata.next;
else
{
auto prev = head;
auto next = head->metadata.next;
while (next != blk)
{
prev = next;
next = next->metadata.next;
}
prev->metadata.next = next->metadata.next;
}
deallocated_blocks.push_back(blk);
}
}
}
~variable_bump_allocator()
{
std::scoped_lock lock(mutex);
for (auto* blk : deallocated_blocks)
{
alloc.deallocate(blk, blk->metadata.size);
}
auto cur = head;
while (cur != nullptr)
{
auto* ptr = cur;
ptr->metadata.has_deallocated = true;
cur = cur->metadata.next;
}
head = nullptr;
}
private:
struct block_t
{
struct block_metadata_t
{
blt::size_t size;
blt::size_t allocated_objects : 63;
bool has_deallocated : 1;
block_t* next;
blt::u8* offset;
} metadata;
blt::u8 buffer[8]{};
explicit block_t(blt::size_t size): metadata{size, 0, false, nullptr, nullptr}
{
reset();
}
void reset()
{
metadata.offset = buffer;
metadata.allocated_objects = 0;
metadata.next = nullptr;
}
[[nodiscard]] blt::ptrdiff_t storage_size() const noexcept
{
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block_t::block_metadata_t));
}
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const noexcept
{
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
}
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const noexcept
{
return storage_size() - used_bytes_in_block();
}
};
static inline block_t* to_block(void* p)
{
return reinterpret_cast<block_t*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(BLT_2MB_SIZE - 1)));
}
void push_block(blt::size_t bytes)
{
auto blk = allocate_block(bytes);
// BLT_TRACE("Allocated block %p", blk);
blk->metadata.next = head;
head = blk;
}
inline block_t* allocate_block(blt::size_t bytes)
{
if (!deallocated_blocks.empty())
{
block_t* blk = deallocated_blocks.back();
deallocated_blocks.pop_back();
blk->reset();
return blk;
}
auto size = align_size_to(bytes + sizeof(typename block_t::block_metadata_t), default_block_size);
auto* ptr = static_cast<block_t*>(alloc.allocate(size));
new(ptr) block_t{size};
return ptr;
}
private:
block_t* head = nullptr;
std::mutex mutex;
std::vector<block_t*> deallocated_blocks;
blt::size_t default_block_size;
Alloc alloc;
};
template<typename T> template<typename T>
class tracked_allocator_t class tracked_allocator_t
{ {

View File

@ -57,10 +57,6 @@ namespace blt::gp
class crossover_t class crossover_t
{ {
public: public:
struct point_info_t
{
type_id return_type;
};
struct crossover_point_t struct crossover_point_t
{ {
blt::ptrdiff_t p1_crossover_point; blt::ptrdiff_t p1_crossover_point;
@ -82,8 +78,7 @@ namespace blt::gp
{} {}
std::optional<crossover_t::crossover_point_t> get_crossover_point(gp_program& program, const tree_t& c1, const tree_t& c2) const; std::optional<crossover_t::crossover_point_t> get_crossover_point(gp_program& program, const tree_t& c1, const tree_t& c2) const;
std::optional<blt::ptrdiff_t> find_place_of_type(gp_program& program, const tree_t& t, type_id type) const;
static std::optional<blt::ptrdiff_t> find_place_of_type(gp_program& program, const tree_t& t, type_id type);
/** /**
* child1 and child2 are copies of the parents, the result of selecting a crossover point and performing standard subtree crossover. * child1 and child2 are copies of the parents, the result of selecting a crossover point and performing standard subtree crossover.

@ -1 +1 @@
Subproject commit 56a3c2f836a37258290c2cc43b52df18249b1c0c Subproject commit 7410dfe0ff6196e77856a79b4c92b05a90e35880

View File

@ -52,7 +52,7 @@ namespace blt::gp
stack_allocator::Allocator& stack_allocator::get_allocator() stack_allocator::Allocator& stack_allocator::get_allocator()
{ {
static Allocator allocator; thread_local static Allocator allocator;
return allocator; return allocator;
} }

View File

@ -211,13 +211,9 @@ namespace blt::gp
return crossover_point_t{static_cast<blt::ptrdiff_t>(crossover_point), static_cast<blt::ptrdiff_t>(attempted_point)}; return crossover_point_t{static_cast<blt::ptrdiff_t>(crossover_point), static_cast<blt::ptrdiff_t>(attempted_point)};
} }
std::optional<blt::ptrdiff_t> crossover_t::find_place_of_type(gp_program& program, const tree_t& t, type_id type) std::optional<blt::ptrdiff_t> crossover_t::find_place_of_type(gp_program& program, const tree_t& t, type_id type) const
{ {
auto attempted_point = program.get_random().get_size_t(1ul, t.get_operations().size()); return std::optional<blt::ptrdiff_t>();
auto& attempted_point_type = program.get_operator_info(t.get_operations()[attempted_point].id);
if (type == attempted_point_type.return_type)
return {attempted_point};
return {};
} }
bool mutation_t::apply(gp_program& program, const tree_t&, tree_t& c) bool mutation_t::apply(gp_program& program, const tree_t&, tree_t& c)