main
Brett 2024-08-19 12:13:07 -04:00
parent 701995f120
commit a2944bd745
4 changed files with 31 additions and 8 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.10) project(blt-gp VERSION 0.1.11)
include(CTest) include(CTest)

View File

@ -86,10 +86,6 @@ float example_function(float x)
int main() int main()
{ {
BLT_TRACE("Hello %ld", blt::align_size_to(532, 8));
BLT_TRACE("Align %ld", blt::gp::stack_allocator::aligned_size(532));
return 0;
BLT_INFO("Starting BLT-GP Symbolic Regression Example"); BLT_INFO("Starting BLT-GP Symbolic Regression Example");
BLT_START_INTERVAL("Symbolic Regression", "Main"); BLT_START_INTERVAL("Symbolic Regression", "Main");
BLT_DEBUG("Setup Fitness cases"); BLT_DEBUG("Setup Fitness cases");

View File

@ -43,13 +43,29 @@ namespace blt::gp
BLT_META_MAKE_FUNCTION_CHECK(drop); BLT_META_MAKE_FUNCTION_CHECK(drop);
} }
class aligned_allocator
{
public:
void* allocate(blt::size_t bytes) // NOLINT
{
return std::aligned_alloc(8, bytes);
}
void deallocate(void* ptr, blt::size_t) // NOLINT
{
std::free(ptr);
}
};
class stack_allocator class stack_allocator
{ {
constexpr static blt::size_t PAGE_SIZE = 0x1000; constexpr static blt::size_t PAGE_SIZE = 0x1000;
constexpr static blt::size_t MAX_ALIGNMENT = 8; constexpr static blt::size_t MAX_ALIGNMENT = 8;
template<typename T> template<typename T>
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>; using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
using Allocator = aligned_allocator;
public: public:
static Allocator& get_allocator();
struct size_data_t struct size_data_t
{ {
blt::size_t total_size_bytes = 0; blt::size_t total_size_bytes = 0;
@ -105,7 +121,8 @@ namespace blt::gp
~stack_allocator() ~stack_allocator()
{ {
std::free(data_); //std::free(data_);
get_allocator().deallocate(data_, size_);
} }
void insert(const stack_allocator& stack) void insert(const stack_allocator& stack)
@ -253,10 +270,12 @@ namespace blt::gp
void expand(blt::size_t bytes) void expand(blt::size_t bytes)
{ {
bytes = to_nearest_page_size(bytes); bytes = to_nearest_page_size(bytes);
auto new_data = static_cast<blt::u8*>(std::malloc(bytes)); // auto new_data = static_cast<blt::u8*>(std::malloc(bytes));
auto new_data = static_cast<blt::u8*>(get_allocator().allocate(bytes));
if (bytes_stored > 0) if (bytes_stored > 0)
std::memcpy(new_data, data_, bytes_stored); std::memcpy(new_data, data_, bytes_stored);
std::free(data_); // std::free(data_);
get_allocator().deallocate(data_, size_);
data_ = new_data; data_ = new_data;
size_ = bytes; size_ = bytes;
} }
@ -311,6 +330,8 @@ namespace blt::gp
blt::size_t bytes_stored = 0; blt::size_t bytes_stored = 0;
blt::size_t size_ = 0; blt::size_t size_ = 0;
}; };
} }
#endif //BLT_GP_STACK_H #endif //BLT_GP_STACK_H

View File

@ -50,6 +50,12 @@ namespace blt::gp
return random_engine; return random_engine;
} }
stack_allocator::Allocator& stack_allocator::get_allocator()
{
thread_local static Allocator allocator;
return allocator;
}
void gp_program::create_threads() void gp_program::create_threads()
{ {
if (config.threads == 0) if (config.threads == 0)