#ifndef MCMC_ARCHITECTURE_SPEC_HPP #define MCMC_ARCHITECTURE_SPEC_HPP #include #include #include #include #include #include namespace MCMC_Architecture { namespace detail { inline constexpr bool is_power_of_two(std::size_t x) noexcept { return x != 0 && (x & (x - 1)) == 0; } } // namespace detail /** * 1. Cache-Aligned Allocator */ template struct CacheAlignedAllocator { using value_type = T; static constexpr std::size_t alignment = 64; // typical x86/ARM cache line CacheAlignedAllocator() noexcept = default; template CacheAlignedAllocator(const CacheAlignedAllocator&) noexcept {} T* allocate(std::size_t n) { if (n > std::numeric_limits::max() / sizeof(T)) { throw std::bad_array_new_length(); } return static_cast(::operator new(n * sizeof(T), std::align_val_t{alignment})); } void deallocate(T* p, std::size_t) noexcept { ::operator delete(p, std::align_val_t{alignment}); } template bool operator==(const CacheAlignedAllocator&) const noexcept { return true; } template bool operator!=(const CacheAlignedAllocator&) const noexcept { return false; } }; /** * 2. Arena Allocator * * O(1) pointer-bump allocation over a single fixed-size, over-aligned backing buffer. */ class ArenaAllocator { public: explicit ArenaAllocator(std::size_t total_size_bytes, std::size_t base_alignment = 64) : total_size(total_size_bytes), arena_base_alignment(base_alignment) { if (total_size_bytes == 0) { throw std::invalid_argument("ArenaAllocator: total_size_bytes must be > 0"); } if (!detail::is_power_of_two(base_alignment)) { throw std::invalid_argument("ArenaAllocator: base_alignment must be a power of two"); } memory_block = static_cast( ::operator new(total_size_bytes, std::align_val_t{base_alignment})); } ~ArenaAllocator() { if (memory_block != nullptr) { ::operator delete(memory_block, std::align_val_t{arena_base_alignment}); } } ArenaAllocator(const ArenaAllocator&) = delete; ArenaAllocator& operator=(const ArenaAllocator&) = delete; ArenaAllocator(ArenaAllocator&& other) noexcept : memory_block(other.memory_block), total_size(other.total_size), offset(other.offset), arena_base_alignment(other.arena_base_alignment) { other.memory_block = nullptr; other.total_size = 0; other.offset = 0; } ArenaAllocator& operator=(ArenaAllocator&& other) noexcept { if (this != &other) { if (memory_block != nullptr) { ::operator delete(memory_block, std::align_val_t{arena_base_alignment}); } memory_block = other.memory_block; total_size = other.total_size; offset = other.offset; arena_base_alignment = other.arena_base_alignment; other.memory_block = nullptr; other.total_size = 0; other.offset = 0; } return *this; } // O(1) pointer bump allocation. void* allocate(std::size_t bytes, std::size_t alignment = 64) { if (!detail::is_power_of_two(alignment)) { throw std::invalid_argument("ArenaAllocator::allocate: alignment must be a power of two"); } if (alignment > arena_base_alignment) { throw std::invalid_argument( "ArenaAllocator::allocate: requested alignment exceeds the arena's base alignment"); } const std::size_t padding = (alignment - (offset % alignment)) % alignment; if (padding > total_size - offset) { throw std::bad_alloc(); } const std::size_t offset_after_padding = offset + padding; if (bytes > total_size - offset_after_padding) { throw std::bad_alloc(); } std::byte* ptr = memory_block + offset_after_padding; offset = offset_after_padding + bytes; return ptr; } void reset() noexcept { offset = 0; } std::size_t capacity() const noexcept { return total_size; } std::size_t used() const noexcept { return offset; } private: std::byte* memory_block = nullptr; std::size_t total_size = 0; std::size_t offset = 0; std::size_t arena_base_alignment = 64; }; inline std::vector make_thread_arenas( std::size_t n_threads, std::size_t bytes_per_thread, std::size_t base_alignment = 64) { std::vector arenas; arenas.reserve(n_threads); for (std::size_t i = 0; i < n_threads; ++i) { arenas.emplace_back(bytes_per_thread, base_alignment); } return arenas; } /** * 3. OpenMP Workspace for Polya-Gamma Data Augmentation */ template using AlignedVector = std::vector>; struct ThreadWorkspace { AlignedVector design_matrix; AlignedVector response_vector; AlignedVector latent_polya_gamma; ThreadWorkspace(std::size_t n_observations, std::size_t n_predictors) : design_matrix(checked_mul(n_observations, n_predictors)), response_vector(n_observations), latent_polya_gamma(n_observations) {} private: static std::size_t checked_mul(std::size_t a, std::size_t b) { if (a != 0 && b > std::numeric_limits::max() / a) { throw std::overflow_error("ThreadWorkspace: n_observations * n_predictors overflows size_t"); } return a * b; } }; /** * 4. MCMC Transition Kernel Interface */ class MCMCTransitionKernel { public: virtual void step(ThreadWorkspace& workspace, ArenaAllocator& arena) = 0; virtual double calculate_marginal_likelihood() const = 0; virtual ~MCMCTransitionKernel() = default; }; } // namespace MCMC_Architecture #endif // MCMC_ARCHITECTURE_SPEC_HPP