fips_md/codegen/
context.rs

1//! Context data for the global level, simulation level and thread level
2
3use std::{collections::HashMap, sync::{self, Arc, RwLock, mpsc}};
4use anyhow::Result;
5use rand_xoshiro::Xoshiro256PlusPlus;
6
7use crate::runtime::{InteractionID, ParticleBorrowMut, ParticleID, Runtime};
8
9use super::{CallbackMessage, analysis::BarrierID, analysis::{SimulationGraph, SymbolTable}, neighbors::NeighborList, util};
10
11/// Context for global data (i.e. data shared by all threads)
12/// Every thread gets an Arc reference of this
13pub struct GlobalContext {
14    /// Runtime that is compiled
15    pub(crate) runtime: Runtime,
16    /// Global symbol table
17    pub(crate) global_symbols: SymbolTable<()>,
18    /// Simulation graph of the compiled runtime
19    pub(crate) simgraph: SimulationGraph,
20}
21
22/// Context of the executor (usually shared synchronization primitives)
23/// Every threads gets a clone of this
24#[derive(Clone)]
25pub(crate) struct ExecutorContext {
26    /// Synchronization barriers
27    pub(crate) barriers: HashMap<BarrierID, Arc<sync::Barrier>>,
28    /// The simulation global step barrier
29    pub(crate) step_barrier: Arc<sync::Barrier>,
30    /// The simulation step counter
31    pub(crate) step_counter: Arc<RwLock<usize>>,
32    /// The simulation global barrier for ending a call to Rust
33    /// (this unblocks the worker threads)
34    pub(crate) call_end_barrier: Arc<sync::Barrier>,
35    /// Sender for communication with the callback thread
36    pub(crate) call_sender: mpsc::Sender<CallbackMessage>,
37    /// Neighbor lists for interactions
38    pub(crate) neighbor_lists: HashMap<InteractionID, Arc<RwLock<NeighborList>>>,
39    /// Immutable reference to compiled runtime
40    pub(crate) global_context: Arc<GlobalContext>
41}
42
43/// Representation of all data in a single worker thread
44pub(crate) struct ThreadContext {
45    /// Particle (type) ID this thread is associated with
46    pub(crate) particle_id: ParticleID,
47    /// Range of particle numbers this thread is associated with
48    pub(crate) particle_range: util::IndexRange,
49    /// Thread-local random number generator
50    pub(crate) rng: Xoshiro256PlusPlus,
51    /// Normal distribution (TODO: Move this somewhere more sensible)
52    pub(crate) normal_dist: rand_distr::Normal<f64>,
53    /// Immutable reference to simulation context
54    pub(crate) executor_context: ExecutorContext,
55}
56
57impl GlobalContext {
58    // Borrow data belonging to a particle type
59    pub fn borrow_particle_mut(&self, particle_name: &str) -> Result<ParticleBorrowMut> {
60        self.runtime.borrow_particle_mut(particle_name)
61    }
62}