use cubecl_common::bytes::AllocationProperty;
const MB: usize = 1024 * 1024;
const STAGE_MAX: usize = 100 * MB;
const FLUSH_MIN: usize = 10 * MB;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Staging {
pub through_pinned: bool,
pub flush_after: bool,
}
impl Staging {
pub fn of(size: usize, property: AllocationProperty) -> Self {
let file_backed = matches!(property, AllocationProperty::File);
Self {
through_pinned: file_backed
|| (size < STAGE_MAX && !matches!(property, AllocationProperty::Pinned)),
flush_after: file_backed || size > FLUSH_MIN,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn file_backed_data_is_always_staged() {
for size in [1, STAGE_MAX, STAGE_MAX * 4] {
assert!(Staging::of(size, AllocationProperty::File).through_pinned);
}
}
#[test]
fn pinned_data_is_never_restaged() {
for size in [1, STAGE_MAX / 2, STAGE_MAX * 4] {
assert!(!Staging::of(size, AllocationProperty::Pinned).through_pinned);
}
}
#[test]
fn plain_data_is_staged_up_to_the_threshold() {
assert!(Staging::of(STAGE_MAX - 1, AllocationProperty::Native).through_pinned);
assert!(!Staging::of(STAGE_MAX, AllocationProperty::Native).through_pinned);
}
#[test]
fn a_large_source_is_released_without_waiting_for_the_batch() {
assert!(!Staging::of(FLUSH_MIN, AllocationProperty::Native).flush_after);
assert!(Staging::of(FLUSH_MIN + 1, AllocationProperty::Native).flush_after);
assert!(Staging::of(1, AllocationProperty::File).flush_after);
}
}