Compare commits

..

7 Commits

Author SHA1 Message Date
Brett 2b1f1c92ab working on main is bad 2024-09-06 12:40:08 -04:00
Brett 8b0b0dbefa Merge branch 'dev' 2024-09-06 12:30:14 -04:00
Brett a1b43bee51 revert changes? 2024-09-04 21:21:04 -04:00
Brett be87d29128 get rid of alloc message 2024-09-04 16:16:56 -04:00
Brett a19712a4d7 fetch allocator 2024-09-04 16:05:35 -04:00
Brett d40f741431 fix the stupid segfault using an ugly hack. TODO: make allocator owned by classes (silly) 2024-09-04 13:30:05 -04:00
Brett 20e41f34cc trying huge pages 2024-09-04 02:42:31 -04:00
7 changed files with 182 additions and 9 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.48) project(blt-gp VERSION 0.1.51)
include(CTest) include(CTest)

View File

@ -46,6 +46,16 @@ 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};
@ -80,8 +90,6 @@ 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,6 +25,9 @@
#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
{ {
@ -113,6 +116,159 @@ 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,6 +57,10 @@ 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;
@ -78,7 +82,8 @@ 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.
@ -167,7 +172,7 @@ namespace blt::gp
0.01, // JUMP_FUNC 0.01, // JUMP_FUNC
0.05 // COPY 0.05 // COPY
); );
}; };
} }

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

View File

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

View File

@ -211,9 +211,13 @@ 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) const std::optional<blt::ptrdiff_t> crossover_t::find_place_of_type(gp_program& program, const tree_t& t, type_id type)
{ {
return std::optional<blt::ptrdiff_t>(); auto attempted_point = program.get_random().get_size_t(1ul, t.get_operations().size());
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)