use std::fmt;
use arrow::array::RecordBatch;
use oxidelake_core::params::Predicate;
use oxidelake_core::{BackendKind, DeviceId, EngineError};
use crate::args::{
AggregateArgs, FilterProjectArgs, HashJoinArgs, JoinBuildArgs, JoinProbeArgs,
VectorDistanceArgs,
};
use crate::handles::{
DeviceBatch, DeviceBuffer, DeviceJoinMatches, DeviceJoinTable, DeviceStream, HostBuffer,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryInfo {
pub total_bytes: u64,
pub free_bytes: u64,
pub pinned_supported: bool,
}
impl MemoryInfo {
pub const fn used_bytes(&self) -> u64 {
self.total_bytes.saturating_sub(self.free_bytes)
}
}
pub trait GpuBackend: Send + Sync + fmt::Debug {
fn kind(&self) -> BackendKind;
fn device_id(&self) -> DeviceId;
fn memory_info(&self) -> Result<MemoryInfo, EngineError>;
fn alloc_device(&self, bytes: usize) -> Result<DeviceBuffer, EngineError>;
fn alloc_pinned_host(&self, bytes: usize) -> Result<HostBuffer, EngineError>;
fn create_stream(&self) -> Result<DeviceStream, EngineError>;
fn copy_h2d(
&self,
stream: &DeviceStream,
src: &HostBuffer,
dst: &mut DeviceBuffer,
) -> Result<(), EngineError>;
fn copy_d2h(
&self,
stream: &DeviceStream,
src: &DeviceBuffer,
dst: &mut HostBuffer,
) -> Result<(), EngineError>;
fn synchronize(&self, stream: &DeviceStream) -> Result<(), EngineError>;
fn upload(
&self,
stream: &DeviceStream,
batch: &RecordBatch,
) -> Result<DeviceBatch, EngineError>;
fn download(
&self,
stream: &DeviceStream,
batch: &DeviceBatch,
) -> Result<RecordBatch, EngineError>;
fn filter_project(
&self,
stream: &DeviceStream,
args: FilterProjectArgs<'_>,
) -> Result<DeviceBatch, EngineError>;
fn hash_join(
&self,
stream: &DeviceStream,
args: HashJoinArgs<'_>,
) -> Result<DeviceBatch, EngineError>;
fn prepare_join_build(
&self,
stream: &DeviceStream,
args: JoinBuildArgs<'_>,
) -> Result<DeviceJoinTable, EngineError> {
let _ = (stream, args);
Err(EngineError::unsupported(
"backend.prepare_join_build",
format!(
"backend `{}` does not prepare hash-join build sides",
self.kind()
),
))
}
fn probe_join(
&self,
stream: &DeviceStream,
args: JoinProbeArgs<'_>,
) -> Result<DeviceJoinMatches, EngineError> {
let _ = (stream, args);
Err(EngineError::unsupported(
"backend.probe_join",
format!("backend `{}` does not probe hash joins", self.kind()),
))
}
fn aggregate(
&self,
stream: &DeviceStream,
args: AggregateArgs<'_>,
) -> Result<DeviceBatch, EngineError>;
fn vector_distance(
&self,
stream: &DeviceStream,
args: VectorDistanceArgs<'_>,
) -> Result<DeviceBatch, EngineError>;
fn supports_predicate(&self, predicate: &Predicate) -> bool {
let _ = predicate;
true
}
fn supports_hash_join(&self) -> bool {
true
}
fn supports_aggregate(&self) -> bool {
true
}
}