parent
b28c17e4b1
commit
2848904507
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* opencl stream executor
|
||||
* Copyright (C) 2021 trivernis
|
||||
* See LICENSE for more information
|
||||
*/
|
||||
|
||||
use crate::traits::ToOclBuffer;
|
||||
use crate::utils::shared_buffer::SharedBuffer;
|
||||
use ocl::{OclPrm, ProQue};
|
||||
|
||||
pub trait ToSharedBuffer<T>
|
||||
where
|
||||
T: OclPrm,
|
||||
{
|
||||
fn to_shared_buffer(&self, pro_que: &ProQue) -> ocl::Result<SharedBuffer<T>>;
|
||||
}
|
||||
|
||||
impl<T> ToSharedBuffer<T> for Vec<T>
|
||||
where
|
||||
T: OclPrm,
|
||||
{
|
||||
fn to_shared_buffer(&self, pro_que: &ProQue) -> ocl::Result<SharedBuffer<T>> {
|
||||
let buffer = self.to_ocl_buffer(pro_que)?;
|
||||
|
||||
Ok(SharedBuffer::new(buffer))
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* opencl stream executor
|
||||
* Copyright (C) 2021 trivernis
|
||||
* See LICENSE for more information
|
||||
*/
|
||||
use ocl::{Buffer, OclPrm};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SharedBuffer<T>
|
||||
where
|
||||
T: OclPrm,
|
||||
{
|
||||
inner: Arc<Mutex<Buffer<T>>>,
|
||||
}
|
||||
|
||||
impl<T> SharedBuffer<T>
|
||||
where
|
||||
T: OclPrm,
|
||||
{
|
||||
/// Creates a new shared buffer with an inner ocl buffer
|
||||
pub fn new(buf: Buffer<T>) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(Mutex::new(buf)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes into the buffer
|
||||
pub fn write(&self, src: &[T]) -> ocl::Result<()> {
|
||||
let buffer = self.inner.lock();
|
||||
buffer.write(src).enq()
|
||||
}
|
||||
|
||||
/// Reads from the buffer
|
||||
pub fn read(&self, dst: &mut [T]) -> ocl::Result<()> {
|
||||
let buffer = self.inner.lock();
|
||||
buffer.read(dst).enq()
|
||||
}
|
||||
|
||||
/// Returns the inner buffer
|
||||
pub fn inner(&self) -> Arc<Mutex<Buffer<T>>> {
|
||||
Arc::clone(&self.inner)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue