use std::fmt;
use oxidelake_core::EngineError;
use crate::{AlignedBuf, MemoryClass};
pub trait PinnedProvider: Send + Sync + fmt::Debug {
fn alloc_pinned(&self, len: usize) -> Result<HostAlloc, EngineError>;
}
enum Inner {
Pageable(AlignedBuf),
#[cfg(feature = "cuda")]
Pinned(crate::pinned::PinnedBuf),
}
pub struct HostAlloc {
class: MemoryClass,
inner: Inner,
}
impl HostAlloc {
pub fn pageable(len: usize) -> Result<Self, EngineError> {
Ok(Self {
class: MemoryClass::Pageable,
inner: Inner::Pageable(AlignedBuf::host(len)?),
})
}
#[cfg(feature = "cuda")]
pub fn from_pinned(buf: crate::pinned::PinnedBuf) -> Self {
Self {
class: MemoryClass::Pinned,
inner: Inner::Pinned(buf),
}
}
pub fn pinned_or_pageable(
len: usize,
provider: Option<&dyn PinnedProvider>,
) -> Result<Self, EngineError> {
if let Some(provider) = provider {
match provider.alloc_pinned(len) {
Ok(buf) => return Ok(buf),
Err(err) => {
tracing::debug!(error = %err, len, "pinned allocation unavailable; using pageable memory")
}
}
}
Self::pageable(len)
}
pub const fn class(&self) -> MemoryClass {
self.class
}
pub fn len(&self) -> usize {
match &self.inner {
Inner::Pageable(b) => b.len(),
#[cfg(feature = "cuda")]
Inner::Pinned(b) => b.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_slice(&self) -> Result<&[u8], EngineError> {
match &self.inner {
Inner::Pageable(b) => Ok(b.as_slice()),
#[cfg(feature = "cuda")]
Inner::Pinned(b) => b.as_slice(),
}
}
pub fn as_mut_slice(&mut self) -> Result<&mut [u8], EngineError> {
match &mut self.inner {
Inner::Pageable(b) => Ok(b.as_mut_slice()),
#[cfg(feature = "cuda")]
Inner::Pinned(b) => b.as_mut_slice(),
}
}
#[cfg(feature = "cuda")]
pub fn as_pinned(&self) -> Option<&crate::pinned::PinnedBuf> {
match &self.inner {
Inner::Pinned(b) => Some(b),
Inner::Pageable(_) => None,
}
}
#[cfg(feature = "cuda")]
pub fn as_pinned_mut(&mut self) -> Option<&mut crate::pinned::PinnedBuf> {
match &mut self.inner {
Inner::Pinned(b) => Some(b),
Inner::Pageable(_) => None,
}
}
}
impl fmt::Debug for HostAlloc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HostAlloc")
.field("class", &self.class)
.field("len", &self.len())
.finish()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[derive(Debug)]
struct FailingProvider;
impl PinnedProvider for FailingProvider {
fn alloc_pinned(&self, len: usize) -> Result<HostAlloc, EngineError> {
Err(EngineError::allocation(len, 64, "no device"))
}
}
#[test]
fn falls_back_to_pageable_when_provider_fails() {
let buf = HostAlloc::pinned_or_pageable(128, Some(&FailingProvider)).unwrap();
assert_eq!(buf.class(), MemoryClass::Pageable);
assert_eq!(buf.len(), 128);
assert!(buf.as_slice().unwrap().iter().all(|&b| b == 0));
}
#[test]
fn no_provider_means_pageable() {
let mut buf = HostAlloc::pinned_or_pageable(8, None).unwrap();
buf.as_mut_slice().unwrap()[0] = 9;
assert_eq!(buf.as_slice().unwrap()[0], 9);
assert_eq!(buf.class().to_string(), "pageable");
}
}