From 49c80c8f53c499be2e24a966ee6bb08ab7abcb5c Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Sat, 12 Apr 2025 18:28:08 -0400 Subject: [PATCH] hi --- .idea/editor.xml | 245 ++++++++++++++++++++++++++++++++++++- CMakeLists.txt | 2 +- include/blt/gp/program.h | 2 +- include/blt/gp/sync.h | 37 +++++- include/blt/gp/threading.h | 14 +-- lib/blt | 2 +- src/sync.cpp | 15 ++- 7 files changed, 296 insertions(+), 21 deletions(-) diff --git a/.idea/editor.xml b/.idea/editor.xml index 8abdf90..1c959fd 100644 --- a/.idea/editor.xml +++ b/.idea/editor.xml @@ -1,6 +1,8 @@ + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index dea35c2..2c72509 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ macro(compile_options target_name) sanitizers(${target_name}) endmacro() -project(blt-gp VERSION 0.5.2) +project(blt-gp VERSION 0.5.3) include(CTest) diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index bf63684..fc6bc3c 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -1058,7 +1058,7 @@ namespace blt::gp std::atomic_uint64_t next_gen_left = 0; std::atomic_bool lifetime_over = false; - blt::barrier barrier; + blt::barrier_t barrier; explicit concurrency_storage(blt::size_t threads): barrier(threads, lifetime_over) {} diff --git a/include/blt/gp/sync.h b/include/blt/gp/sync.h index 8988b6e..cbff1c3 100644 --- a/include/blt/gp/sync.h +++ b/include/blt/gp/sync.h @@ -27,9 +27,9 @@ namespace blt::gp class sync_t { public: - explicit sync_t(gp_program& program); + explicit sync_t(gp_program& program, fs::writer_t& writer); - void trigger(u64 current_time); + void trigger(u64 current_time) const; sync_t& with_timer(u64 seconds) { @@ -43,12 +43,45 @@ namespace blt::gp return *this; } + sync_t& overwrite_file_on_write() + { + m_reset_to_start_of_file = true; + return *this; + } + + sync_t& append_to_file_on_write() + { + m_reset_to_start_of_file = false; + return *this; + } + + /** + * Save the state of the whole program instead of just the generation information. + */ + sync_t& whole_program() + { + m_whole_program = true; + return *this; + } + + /** + * Only save the current generation to disk. + */ + sync_t& generation_only() + { + m_whole_program = false; + return *this; + } + ~sync_t(); private: gp_program* m_program; + fs::writer_t* m_writer; std::optional m_timer_seconds; std::optional m_generations; + bool m_reset_to_start_of_file = false; + bool m_whole_program = false; }; } diff --git a/include/blt/gp/threading.h b/include/blt/gp/threading.h index 263a896..00c91b1 100644 --- a/include/blt/gp/threading.h +++ b/include/blt/gp/threading.h @@ -97,9 +97,9 @@ namespace blt::gp task_builder_t() = default; template - static std::function make_callable(Tasks&&... tasks) + static std::function make_callable(Tasks&&... tasks) { - return [&tasks...](barrier& sync_barrier, EnumId task, size_t thread_index) + return [&tasks...](barrier_t& sync_barrier, EnumId task, size_t thread_index) { call_jmp_table(sync_barrier, task, thread_index, tasks...); }; @@ -107,7 +107,7 @@ namespace blt::gp private: template - static void execute(barrier& sync_barrier, const size_t thread_index, Task&& task) + static void execute(barrier_t& sync_barrier, const size_t thread_index, Task&& task) { // sync_barrier.wait(); if (task.requires_single_sync) @@ -121,7 +121,7 @@ namespace blt::gp } template - static bool call(barrier& sync_barrier, const EnumId current_task, const size_t thread_index, Task&& task) + static bool call(barrier_t& sync_barrier, const EnumId current_task, const size_t thread_index, Task&& task) { if (static_cast(current_task) == static_cast(task.get_task_id())) { @@ -132,7 +132,7 @@ namespace blt::gp } template - static void call_jmp_table(barrier& sync_barrier, const EnumId current_task, const size_t thread_index, Tasks&&... tasks) + static void call_jmp_table(barrier_t& sync_barrier, const EnumId current_task, const size_t thread_index, Tasks&&... tasks) { if (static_cast(current_task) >= sizeof...(tasks)) BLT_UNREACHABLE; @@ -146,7 +146,7 @@ namespace blt::gp static_assert(std::is_enum_v, "Enum ID must be of enum type!"); public: - explicit thread_manager_t(const size_t thread_count, std::function task_func, + explicit thread_manager_t(const size_t thread_count, std::function task_func, const bool will_main_block = true): barrier(thread_count), will_main_block(will_main_block) { thread_callable = [this, task_func = std::move(task_func)](const size_t thread_index) @@ -226,7 +226,7 @@ namespace blt::gp return will_main_block ? threads.size() + 1 : threads.size(); } - blt::barrier barrier; + blt::barrier_t barrier; std::atomic_bool should_run = true; bool will_main_block; std::vector tasks; diff --git a/lib/blt b/lib/blt index b6fc170..6161d9b 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit b6fc1703995195af611532cdffda52c86993e4ce +Subproject commit 6161d9b79419879d9cd3374c889d124cd1e3617e diff --git a/src/sync.cpp b/src/sync.cpp index 40f8839..7519907 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace blt::gp @@ -79,16 +80,22 @@ namespace blt::gp return state; } - sync_t::sync_t(gp_program& program): m_program(&program) + sync_t::sync_t(gp_program& program, fs::writer_t& writer): m_program(&program), m_writer(&writer) { get_state().add(this); } - void sync_t::trigger(const u64 current_time) + void sync_t::trigger(const u64 current_time) const { - if ((m_timer_seconds && (current_time % *m_timer_seconds == 0)) || (m_generations && (current_time % *m_generations == 0))) + if ((m_timer_seconds && (current_time % *m_timer_seconds == 0)) || (m_generations && (m_program->get_current_generation() % *m_generations == + 0))) { - + if (m_reset_to_start_of_file) + m_writer->seek(0, fs::writer_t::seek_origin::seek_set); + if (m_whole_program) + m_program->save_state(*m_writer); + else + m_program->save_generation(*m_writer); } }