Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,12 @@ pub struct RuntimeSchema<A> {

/// Width of [`SchedulerSlots::limit`], [`SchedulerSlots::now`],
/// [`SchedulerSlots::exit_arg`], [`SchedulerSlots::count`],
/// [`SchedulerSlots::current`], [`SchedulerSlots::levels`] and every [`SchedulerSlots::wake`] object.
/// [`SchedulerSlots::current`], [`SchedulerSlots::levels`],
/// [`SchedulerSlots::ready`] and every [`SchedulerSlots::wake`] object.
pub const SCHEDULER_TIME_WIDTH: usize = 64;
/// Width of [`SchedulerSlots::exit`], [`SchedulerSlots::finish`],
/// [`SchedulerSlots::cont`] and every [`SchedulerSlots::waiting`] object.
/// [`SchedulerSlots::cont`] and every [`SchedulerSlots::waiting`] and
/// [`SchedulerSlots::baseline`] object.
pub const SCHEDULER_FLAG_WIDTH: usize = 8;

/// Hidden state objects through which a compiled scheduler and the runtime
Expand Down Expand Up @@ -911,11 +913,16 @@ pub struct SchedulerSlots<A> {
/// The levels of the event signals at the start of the last slot, one
/// bit per event in event order.
pub levels: A,
/// The kernels ready to run in the current slot, one bit per kernel in
/// kernel order.
pub ready: A,
/// The time each process kernel resumes at, in kernel order, valid
/// where [`Self::waiting`] is set.
pub wake: Vec<A>,
/// Whether each process kernel waits for its wake time.
/// Whether each process kernel waits for its wake time or event.
pub waiting: Vec<A>,
/// The level a kernel waiting for an event last saw its signal at.
pub baseline: Vec<A>,
}

impl<A> SchedulerSlots<A> {
Expand All @@ -930,8 +937,10 @@ impl<A> SchedulerSlots<A> {
count: map(self.count),
current: map(self.current),
levels: map(self.levels),
ready: map(self.ready),
wake: self.wake.into_iter().map(&mut map).collect(),
waiting: self.waiting.into_iter().map(map).collect(),
waiting: self.waiting.into_iter().map(&mut map).collect(),
baseline: self.baseline.into_iter().map(map).collect(),
}
}

Expand All @@ -947,10 +956,12 @@ impl<A> SchedulerSlots<A> {
&self.count,
&self.current,
&self.levels,
&self.ready,
]
.into_iter()
.chain(&self.wake)
.chain(&self.waiting)
.chain(&self.baseline)
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2266,12 +2266,16 @@ fn declare_scheduler_slots(
count: declare("count".into(), TIME),
current: declare("current".into(), TIME),
levels: declare("levels".into(), TIME),
ready: declare("ready".into(), TIME),
wake: (0..process_count)
.map(|index| declare(format!("wake[{index}]"), TIME))
.collect(),
waiting: (0..process_count)
.map(|index| declare(format!("waiting[{index}]"), FLAG))
.collect(),
baseline: (0..process_count)
.map(|index| declare(format!("baseline[{index}]"), FLAG))
.collect(),
})
}

Expand Down
59 changes: 48 additions & 11 deletions crates/celox-runtime/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ pub(crate) struct SchedulerRefs {
pub(crate) count: SignalRef,
pub(crate) current: SignalRef,
pub(crate) levels: SignalRef,
/// The wake and waiting slots of each kernel the scheduler runs, by
/// process index.
pub(crate) ready: SignalRef,
/// The wake and waiting slots of each kernel the scheduler runs that
/// only delays, by process index.
pub(crate) wake: Vec<Option<(SignalRef, SignalRef)>>,
/// The waiting and baseline slots of each kernel the scheduler runs that
/// waits for an edge, by process index.
pub(crate) waits: Vec<Option<(SignalRef, SignalRef)>>,
/// The number of such kernels.
pub(crate) wait_kernels: usize,
/// The bit of each kernel the scheduler runs in its ready mask: its
/// place among them in kernel order.
pub(crate) ordinals: Vec<Option<u32>>,
/// The event ids of the bits of `levels`.
pub(crate) events: Vec<usize>,
}
Expand Down Expand Up @@ -283,17 +292,45 @@ impl<B: SimBackend> ResolvedSchedule<B> {
count: resolve(&slots.count),
current: resolve(&slots.current),
levels: resolve(&slots.levels),
wake: slots
.wake
.iter()
.zip(&slots.waiting)
.zip(&scheduler.fallback)
.map(|((wake, waiting), fallback)| {
fallback
.is_none()
.then(|| (resolve(wake), resolve(waiting)))
ready: resolve(&slots.ready),
wake: (0..scheduler.fallback.len())
.map(|index| {
(scheduler.fallback[index].is_none()
&& model.processes[index].waits.is_empty())
.then(|| (resolve(&slots.wake[index]), resolve(&slots.waiting[index])))
})
.collect(),
waits: (0..scheduler.fallback.len())
.map(|index| {
(scheduler.fallback[index].is_none()
&& !model.processes[index].waits.is_empty())
.then(|| {
(
resolve(&slots.waiting[index]),
resolve(&slots.baseline[index]),
)
})
})
.collect(),
ordinals: {
let mut next = 0;
scheduler
.fallback
.iter()
.map(|fallback| {
fallback.is_none().then(|| {
next += 1;
next - 1
})
})
.collect()
},
wait_kernels: (0..scheduler.fallback.len())
.filter(|&index| {
scheduler.fallback[index].is_none()
&& !model.processes[index].waits.is_empty()
})
.count(),
events: scheduler
.events
.iter()
Expand Down
96 changes: 84 additions & 12 deletions crates/celox-runtime/src/simulation/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use std::cmp::Reverse;

use celox_design::SchedulerExit;

use super::{CompiledSlots, SchedulerMode, SimulationExecutor, SimulationState};
use crate::{SimulatorErrorCode, backend::SimBackend, timing::Halt};
use super::{Baseline, CompiledSlots, SchedulerMode, SimulationExecutor, SimulationState};
use crate::{
SimulatorErrorCode,
backend::SimBackend,
timing::{Halt, copy_planes},
};

impl<B: SimBackend> SimulationState<B> {
/// Whether a compiled scheduler may run slots of this simulation.
Expand Down Expand Up @@ -37,7 +41,10 @@ impl<B: SimBackend> SimulationState<B> {
|| !pending.ready.is_empty()
|| !pending.inactive.is_empty()
|| !pending.nba_queue.is_empty()
|| !pending.waiters.is_empty()
|| pending
.waiters
.iter()
.any(|(process, _)| scheduler.waits.get(process).is_none_or(Option::is_none))
{
return None;
}
Expand Down Expand Up @@ -111,6 +118,28 @@ impl<B: SimBackend> SimulationState<B> {
for &(_, waiting) in scheduler.wake.iter().flatten() {
backend.set(waiting, 0u8);
}
backend.set(scheduler.ready, 0u64);
// The scheduler's waiting kernels wait in its slots during the run,
// with the level each wait last saw.
for (process, slots) in scheduler.waits.iter().enumerate() {
let Some((waiting, baseline)) = *slots else {
continue;
};
let level = match self.process_state.waiters.get(process) {
Some(waiter) => match waiter.baselines.first() {
Some(Baseline::Raw(raw)) => raw.first().is_some_and(|byte| byte & 1 != 0),
Some(Baseline::Value((value, _))) => value.bit(0),
_ => return Err(SimulatorErrorCode::InternalError.into()),
},
None => {
backend.set(waiting, 0u8);
continue;
}
};
backend.set(waiting, 1u8);
backend.set(baseline, u8::from(level));
self.process_state.waiters.remove(process);
}
// The scheduler's kernels wait in its wake slots during the run.
let mut kept = Vec::with_capacity(self.process_wakeups.len());
for wakeup @ Reverse((time, process)) in self.process_wakeups.drain() {
Expand All @@ -136,26 +165,69 @@ impl<B: SimBackend> SimulationState<B> {
let now: u64 = backend.get_as(scheduler.now);
let exit = SchedulerExit::from_code(backend.get_as(scheduler.exit));
let current: u64 = backend.get_as(scheduler.current);
let ready: u64 = backend.get_as(scheduler.ready);
let complete = result.is_ok() && exit == Some(SchedulerExit::Limit);
// A run that returned in the middle of slot `now` leaves the kernel
// that ended it filed nowhere, as the reference loop leaves it, and
// the kernels still ready in the slot ready.
let ordinals = &scheduler.ordinals;
let is_ready = |process: usize| {
ordinals
.get(process)
.copied()
.flatten()
.is_some_and(|ordinal| ready >> ordinal & 1 != 0)
};
let pending =
|process: usize| !complete && (process as u64 == current || is_ready(process));
for (process, wake) in scheduler.wake.iter().enumerate() {
let Some((wake, waiting)) = *wake else {
continue;
};
if backend.get_as::<u8>(waiting) == 0 {
if pending(process) || backend.get_as::<u8>(waiting) == 0 {
continue;
}
let time: u64 = backend.get_as(wake);
if !complete && time == now {
// The run returned in the middle of slot `now`: the kernel
// that ended it is filed nowhere, as the reference loop
// leaves it, and kernels due then that did not run yet are
// still ready.
if process as u64 != current {
self.process_wakeups.push(Reverse((time, process)));
}
for (process, slots) in scheduler.waits.iter().enumerate() {
let Some((waiting, baseline)) = *slots else {
continue;
};
if pending(process) || backend.get_as::<u8>(waiting) == 0 {
continue;
}
let site = backend.get_as::<u64>(self.model.processes[process].delay) as usize;
let terms = self.model.processes[process]
.wait_sites
.get(site)
.ok_or_else(|| Self::site_error(process, site))?;
let level = [backend.get_as::<u8>(baseline)];
// The slot keeps its storage, as `register_waiter` reuses it.
self.process_state.waiters.register(
process,
terms.iter().any(|term| term.driven),
|waiter| {
waiter.site = site;
waiter.baselines.truncate(1);
match waiter.baselines.first_mut() {
Some(Baseline::Raw(raw)) => copy_planes(raw, &level, 1),
_ => {
let mut raw = Vec::with_capacity(1);
copy_planes(&mut raw, &level, 1);
waiter.baselines.clear();
waiter.baselines.push(Baseline::Raw(raw));
}
}
},
);
}
if !complete {
for process in 0..ordinals.len() {
if is_ready(process) && process as u64 != current {
self.process_state.ready.insert(process);
}
continue;
}
self.process_wakeups.push(Reverse((time, process)));
}
self.stats.compiled_slots += count;
if count > 0 {
Expand Down
12 changes: 9 additions & 3 deletions crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,19 @@ impl<B: SimBackend> SimulationState<B> {
return Ok(false);
}
let compiles = self.compiles_slots();
let waiting_kernels = self
.model
.scheduler
.as_ref()
.map_or(0, |scheduler| scheduler.wait_kernels);
while let Some(next_time) = self.next_event_time() {
if next_time > end {
break;
}
// A waiting process keeps the slot in the reference loop; test
// that first, as fallback designs fail it at every step.
let compiled = if compiles && self.process_state.waiters.is_empty() {
// A process the scheduler does not run waits: the slot stays in
// the reference loop. Test that first, as designs with such
// processes fail it at every step.
let compiled = if compiles && self.process_state.waiters.len() <= waiting_kernels {
self.compiled_limit(executor, end, observer.one_slot())
} else {
None
Expand Down
9 changes: 9 additions & 0 deletions crates/celox-runtime/src/simulation/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ impl Waiters {
self.register(process, driven, |slot| *slot = waiter);
}

/// The wait of process `process`, if it waits.
pub fn get(&self, process: usize) -> Option<&Waiter> {
self.waiting
.get(process)
.copied()
.unwrap_or(false)
.then(|| &self.slots[process])
}

/// Process `process` no longer waits.
pub fn remove(&mut self, process: usize) {
if self.waiting.get(process).copied().unwrap_or(false) {
Expand Down
2 changes: 1 addition & 1 deletion crates/celox/src/backend/native/image_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{fmt, path::Path};
use super::backend::NativeProgramImage;

const TRAILER_MAGIC: &[u8; 8] = b"CELOXNPI";
const CONTAINER_VERSION: u16 = 29;
const CONTAINER_VERSION: u16 = 30;
const TRAILER_SIZE: usize = 32;
const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
Expand Down
Loading
Loading