use std::any::Any;
use std::panic::Location;
use std::sync::{Arc, Mutex};
use crate::diag::{Access, AccessKind, Diagnostic};
use crate::dim::Dim3;
use crate::launch::LaunchState;
use crate::mem::SharedArray;
use crate::sched::AbortSignal;
use crate::sync::{key, Clock, Ordering};
pub struct ThreadCtx<'l> {
pub(crate) launch: &'l LaunchState,
pub(crate) block: u32,
pub(crate) thread: u32,
pub(crate) gid: usize,
pub(crate) slot: usize,
}
impl<'l> ThreadCtx<'l> {
fn wave_block(&self) -> usize {
self.slot / self.launch.config.block.count() as usize
}
pub fn thread_idx(&self) -> Dim3 {
Dim3::from_linear(self.thread, self.launch.config.block)
}
pub fn block_idx(&self) -> Dim3 {
Dim3::from_linear(self.block, self.launch.config.grid)
}
pub fn block_dim(&self) -> Dim3 {
self.launch.config.block
}
pub fn grid_dim(&self) -> Dim3 {
self.launch.config.grid
}
pub fn thread_linear(&self) -> usize {
self.thread as usize
}
pub fn block_linear(&self) -> usize {
self.block as usize
}
pub fn global_linear(&self) -> usize {
self.gid
}
pub fn warp_size(&self) -> u32 {
self.launch.config.warp_size
}
pub fn warp_id(&self) -> u32 {
self.thread / self.warp_size()
}
pub fn lane_id(&self) -> u32 {
self.thread % self.warp_size()
}
pub fn warp_valid_mask(&self) -> u32 {
self.launch.sched.valid_mask(self.warp_id() as usize)
}
#[track_caller]
pub fn sync_threads(&self) {
self.sync_threads_at(Location::caller());
}
pub(crate) fn sync_threads_at(&self, loc: &'static Location<'static>) {
let block = self.block as usize;
self.launch
.sync
.lock()
.unwrap()
.barrier_arrive(self.gid, block);
if self
.launch
.sched
.barrier(self.slot, loc, &self.launch.reporter)
.is_err()
{
std::panic::resume_unwind(Box::new(AbortSignal));
}
self.launch
.sync
.lock()
.unwrap()
.barrier_depart(self.gid, block);
}
pub fn threadfence(&self) {
self.fence(Ordering::AcqRel);
}
pub fn fence(&self, ordering: Ordering) {
self.launch.sync.lock().unwrap().fence(self.gid, ordering);
}
pub fn shared<T: Copy + Default + Send + 'static>(
&self,
name: &'static str,
len: usize,
) -> SharedArray<T> {
let mut map = self.launch.blocks[self.block as usize]
.shared
.lock()
.unwrap();
let entry = map
.entry(name)
.or_insert_with(|| {
Arc::new(SharedArray::<T>::new_inner(self.block, name, len))
as Arc<dyn Any + Send + Sync>
})
.clone();
drop(map);
let arr = SharedArray::<T>::from_any(entry).unwrap_or_else(|| {
panic!("riri: shared array `{name}` reused with a different element type")
});
assert_eq!(
arr.len(),
len,
"riri: shared array `{name}` reused with a different length"
);
arr
}
pub(crate) fn schedule_point(&self) {
if self
.launch
.sched
.yield_now(self.slot, &self.launch.reporter)
.is_err()
{
std::panic::resume_unwind(Box::new(AbortSignal));
}
}
pub(crate) fn access(
&self,
kind: AccessKind,
location: &'static Location<'static>,
) -> (Access, Clock) {
let warp = self.warp_id();
let (epoch, warp_epoch) = self.launch.sched.epochs(self.wave_block(), warp as usize);
let mut sync = self.launch.sync.lock().unwrap();
let seq = sync.tick(self.gid, key(self.block, self.thread));
let now = sync.clock(self.gid).clone();
let access = Access {
block: self.block,
thread: self.thread,
epoch,
warp,
warp_epoch,
seq,
kind,
location,
};
(access, now)
}
pub(crate) fn release_into(&self, target: &mut Clock, ordering: Ordering) {
self.launch
.sync
.lock()
.unwrap()
.release(self.gid, target, ordering);
}
pub(crate) fn acquire_from(&self, source: &Clock, ordering: Ordering) {
self.launch
.sync
.lock()
.unwrap()
.acquire(self.gid, source, ordering);
}
pub(crate) fn warp_slots(&self) -> &Mutex<Vec<Option<Box<dyn Any + Send>>>> {
let index = self.block as usize * self.launch.warps_per_block + self.warp_id() as usize;
&self.launch.warps[index].slots
}
pub(crate) fn warp_rendezvous(
&self,
phase: u8,
mask: u32,
at: &'static Location<'static>,
op: &'static str,
) {
let r = self.launch.sched.warp_rendezvous(
self.slot,
phase,
mask,
at,
op,
&self.launch.reporter,
);
if r.is_err() {
std::panic::resume_unwind(Box::new(AbortSignal));
}
}
pub(crate) fn report(&self, d: Diagnostic) {
self.launch.reporter.push(d);
}
pub(crate) fn trap(&self, d: Diagnostic) -> ! {
self.launch.reporter.push(d);
self.launch.sched.abort();
std::panic::resume_unwind(Box::new(AbortSignal));
}
pub(crate) fn launch_id(&self) -> u64 {
self.launch.id
}
}