commita763bff532Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:33:13 2025 +0000 Added release mode building Now the build script enables you to: - Build in debug mode (default no arguments) - Build in debug mode then run (`run` argument) - Build in release mode (`release` argument) commit7112937b0bAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:30:50 2025 +0000 Better thread pool constructor statically define a number of threads, then setup the necessary machinery to make it work. commita5666328b7Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:29:32 2025 +0000 Fixed the heap-use-after-free issue. Just standard multithreading stuff; access to the allocator while hot threads are running means stuff can change underneath us even /during/ a read. I've mutex locked state for stuff in the drawing domain which stops this issue. commit5d78cb20dfAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:28:21 2025 +0000 Adjust TODOs commit9d3a202c27Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:09:50 2025 +0000 Implement stepping via KEY_SPACE commit424fab2e40Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:08:01 2025 +0000 Weird bug in draw_tree Seems near the 100,000 node mark, the vector craps itself? I keep getting "heap use after free" errors here when trying to access the allocator vector. Disabling fsan just leads to a segment fault near the same point. I think we might need to introduce our own vector :) commit6ffa63f7acAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 04:07:43 2025 +0000 Make general delay 1ms Just for my puny eyes to see. commit2dbe3d0a58Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 03:54:30 2025 +0000 merge generate_children into do_iteration Also fix this really weird slow down we get from doing direct mutation to a state.allocator.get_ref (i.e. a Node by reference). Maybe the compiler just can't inline that instruction? Regardless, this patch fixes the slow downs! commit7e801df280Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 03:53:51 2025 +0000 no more numerics commitf70517cf41Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Dec 12 03:08:56 2025 +0000 Simplify mutex locking scheme (lock at start, unlock at end) commit00858cf74fAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Nov 28 17:24:33 2025 +0000 Rewrite main to draw based on our new state/draw_state Arranges a thread set, draws based on draw_state. No need to lock the mutex since drawing should only require reading. Still has a timer in case we need to do timed checks. commit3e065e50a9Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Nov 28 17:24:12 2025 +0000 Disable previous work We're going to rewrite this commitab0f152742Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Nov 28 17:23:05 2025 +0000 Define `worker` function which a thread should run Pauses until state.pause_work is false (checking every THREAD_PAUSE_DELAY), then performs an iteration. Quits when state.quit_work is true (based on loop). Generally delays itself by THREAD_GENERAL_DELAY. commit014821ceb5Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Nov 28 17:22:39 2025 +0000 Added booleans for thread workers to look at when running pause_work temporarily stops work, but should keep the thread alive. stop_work should kill the thread. commit1bd8a9f7b2Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Fri Nov 28 17:21:21 2025 +0000 Add run component to build script, activated by passing argument `run` commit6f620644bfAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:57:10 2025 +0000 Define constructors for state.hpp explicitly commit0008c31f53Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:53:10 2025 +0000 Add src/worker.cpp to build pipeline commitf8068c4a15Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:52:52 2025 +0000 Implement the two worker functions commit66c56f2c15Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:51:41 2025 +0000 Define thread safe worker functions for generating new children on the tree commit6247f2af49Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:44:23 2025 +0000 Add src/state.cpp to the build pipeline commit40e07e03f2Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:42:24 2025 +0000 Add mutexes to state for the queue and the allocator Our threads need to negotiate access and mutation to the two resources - we don't want them treading on each others toes when generating new child nodes /or/ when trying to remove/add work to the queue. The former situation is particularly worrisome due to how weak the indexing system is with the allocator. commit0abd353368Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:30:35 2025 +0000 Implement cw::state::DrawState::compute_bounds commit0ac316ada4Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:30:21 2025 +0000 Define the general state of the sim (extract from main.cpp) commita03fa13a07Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:19:52 2025 +0000 Implementation (copied from numerics) commitff9a2851d4Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:19:41 2025 +0000 Switch to using i64's instead of optional u64 in Node commit6c2bc93874Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:12:35 2025 +0000 remove index_t definition conflicts with prev code base. Also std::optional doubles the size of the underlying type. horrifying! I don't want to have to give 16 bytes over for something that could be embedded in the 8 bytes of the u64 directly. I'm thinking we could just use i64's instead, sacrificing that top bit to indicate if the value is present or not. commit7a4d158d2fAuthor: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 01:02:31 2025 +0000 Add gcd to base.hpp commite032303773Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 00:56:45 2025 +0000 Define node.hpp (move definitions away from numerics.hpp) commit9821a2ab15Author: Aryadev Chavali <aryadev@aryadevchavali.com> Date: Thu Nov 27 00:55:44 2025 +0000 Define basic type aliases and useful functions/macros for use throughout the system
134 lines
3.3 KiB
C++
134 lines
3.3 KiB
C++
/* node.cpp: Implementations of various functions for fractions and nodes
|
|
* Created: 2025-11-27
|
|
* Author: Aryadev Chavali
|
|
* License: See end of file
|
|
* Commentary:
|
|
*/
|
|
|
|
#include <sstream>
|
|
|
|
#include "node.hpp"
|
|
|
|
namespace cw::node
|
|
{
|
|
/**************************************/
|
|
/* ___ _ _ */
|
|
/* | __| _ __ _ __| |_(_)___ _ _ ___ */
|
|
/* | _| '_/ _` / _| _| / _ \ ' \(_-< */
|
|
/* |_||_| \__,_\__|\__|_\___/_||_/__/ */
|
|
/* */
|
|
/**************************************/
|
|
Fraction::Fraction(u64 numerator, u64 denominator)
|
|
: numerator{numerator}, denominator{denominator},
|
|
norm{numerator / ((f64)denominator)}
|
|
{
|
|
u64 hcf = gcd(MIN(numerator, denominator), MAX(numerator, denominator));
|
|
numerator /= hcf;
|
|
denominator /= hcf;
|
|
}
|
|
|
|
bool Fraction::operator<(const Fraction other)
|
|
{
|
|
if (other.denominator == denominator)
|
|
return numerator < other.numerator;
|
|
return (numerator * other.denominator) < (other.numerator * denominator);
|
|
}
|
|
|
|
bool Fraction::operator==(const Fraction &other)
|
|
{
|
|
return numerator == other.numerator && denominator == other.denominator;
|
|
}
|
|
|
|
std::string to_string(const Fraction &f)
|
|
{
|
|
std::stringstream ss;
|
|
ss << f.numerator << "/" << f.denominator;
|
|
return ss.str();
|
|
}
|
|
|
|
/***************************/
|
|
/* _ _ _ */
|
|
/* | \| |___ __| |___ ___ */
|
|
/* | .` / _ \/ _` / -_|_-< */
|
|
/* |_|\_\___/\__,_\___/__/ */
|
|
/* */
|
|
/***************************/
|
|
Node::Node(const Fraction &&val, i64 left, i64 right)
|
|
: value{val}, left{left}, right{right}
|
|
{
|
|
}
|
|
|
|
NodeAllocator::NodeAllocator(u64 capacity)
|
|
{
|
|
vec.reserve(capacity);
|
|
}
|
|
|
|
u64 NodeAllocator::alloc(Node n)
|
|
{
|
|
u64 ind = vec.size();
|
|
vec.push_back(n);
|
|
return ind;
|
|
}
|
|
|
|
// FIXME: This is annoying. DRY?
|
|
Node &NodeAllocator::get_ref(u64 n)
|
|
{
|
|
if (n >= vec.size())
|
|
return vec[0];
|
|
return vec[n];
|
|
}
|
|
|
|
Node NodeAllocator::get_val(u64 n) const
|
|
{
|
|
if (n >= vec.size())
|
|
return vec[0];
|
|
return vec[n];
|
|
}
|
|
|
|
void indent_depth(int depth, std::stringstream &ss)
|
|
{
|
|
for (int i = 0; i < depth; ++i)
|
|
ss << " ";
|
|
}
|
|
|
|
std::string to_string(const NodeAllocator &allocator, const i64 n, int depth)
|
|
{
|
|
if (n < 0)
|
|
return "NIL";
|
|
|
|
std::stringstream ss;
|
|
Node x = allocator.get_val(n);
|
|
ss << "(" << to_string(x.value) << "\n";
|
|
|
|
indent_depth(depth, ss);
|
|
if (x.left == -1)
|
|
ss << "NIL";
|
|
else
|
|
ss << to_string(allocator, x.left, depth + 1);
|
|
ss << "\n";
|
|
|
|
indent_depth(depth, ss);
|
|
if (x.right == -1)
|
|
ss << "NIL";
|
|
else
|
|
ss << to_string(allocator, x.right, depth + 1);
|
|
|
|
ss << ")";
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace cw::node
|
|
|
|
/* Copyright (C) 2025 Aryadev Chavali
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
|
|
* details.
|
|
|
|
* You may distribute and modify this code under the terms of the GNU General
|
|
* Public License Version 2, which you should have received a copy of along with
|
|
* this program. If not, please go to <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|