Skip to content
Snippets Groups Projects
Commit f2d464fe authored by Aaron Councilman's avatar Aaron Councilman
Browse files

Format hercules_rt

parent 3146ac3c
No related branches found
No related tags found
1 merge request!186Rodinia
......@@ -397,7 +397,7 @@ unsafe impl Sync for __RawPtrSendSync {}
*
* The data held at all of its non-None allocations and references is maintained so that it is the
* same, and so methods will attempt to use the reference or allocation that is most convenient.
*
*
* HerculesImmBox hold references to immutable memory only. All operations on these is through
* immutable references, though internally it uses OnceLocks to protect its resources since the Box
* may be used in multiple parallel threads if it is used in parallel Hercules code invocation.
......@@ -502,9 +502,9 @@ impl<'a, T> From<HerculesCUDARefMut<'a>> for HerculesImmBox<'a, T> {
}
}
impl<'a, T> HerculesImmBox<'a, T>
where
T: Default + Clone
impl<'a, T> HerculesImmBox<'a, T>
where
T: Default + Clone,
{
pub fn as_slice(&'a self) -> &'a [T] {
self.as_cpu_ref().as_slice()
......@@ -520,18 +520,23 @@ where
} else {
#[cfg(feature = "cuda")]
if let Some(cuda_ref) = self.cuda_ref.get() {
return
self.cpu_ref.get_or_init(|| {
return self
.cpu_ref
.get_or_init(|| {
let elements = unsafe { cuda_ref.__size() / size_of::<T>() };
let mut alloc = Vec::new();
alloc.resize_with(elements, Default::default);
let _ = cuda_ref.clone().to_cpu_ref(&mut alloc);
self.cpu_alloc.set(alloc).map_err(|_| ()).expect("HerculesImmBox cpu_alloc was set unexpectedly");
self.cpu_alloc
.set(alloc)
.map_err(|_| ())
.expect("HerculesImmBox cpu_alloc was set unexpectedly");
let alloc = self.cpu_alloc.get().unwrap();
HerculesCPURef::from_slice(alloc)
}).clone();
})
.clone();
}
panic!("HerculesImmBox has no reference to data")
......@@ -544,13 +549,19 @@ where
cuda_ref.clone()
} else {
if let Some(cpu_ref) = self.cpu_ref.get() {
return self.cuda_ref.get_or_init(|| {
// Copy data to CUDA device
let alloc = CUDABox::from_cpu_ref(cpu_ref.clone());
self.cuda_alloc.set(alloc).map_err(|_| ()).expect("HerculesImmBox cuda_alloc was set unexpectedly");
self.cuda_alloc.get().unwrap().get_ref()
}).clone();
return self
.cuda_ref
.get_or_init(|| {
// Copy data to CUDA device
let alloc = CUDABox::from_cpu_ref(cpu_ref.clone());
self.cuda_alloc
.set(alloc)
.map_err(|_| ())
.expect("HerculesImmBox cuda_alloc was set unexpectedly");
self.cuda_alloc.get().unwrap().get_ref()
})
.clone();
}
panic!("HerculesImmBox has no reference to data")
......@@ -654,7 +665,7 @@ impl<'a, T> From<HerculesCUDARefMut<'a>> for HerculesMutBox<'a, T> {
impl<'a, 'b: 'a, T> HerculesMutBox<'b, T>
where
T: Default + Clone
T: Default + Clone,
{
pub fn as_slice(&'a mut self) -> &'a mut [T] {
self.as_cpu_ref().as_slice()
......@@ -662,42 +673,41 @@ where
pub fn as_cpu_ref(&'a mut self) -> HerculesCPURefMut<'a> {
match self.loc {
HerculesMutBoxLocation::CPU => {
match self.cpu_alloc {
Allocation::None => panic!("No CPU reference"),
Allocation::Reference(ref mut val) => HerculesCPURefMut::from_slice(*val),
Allocation::Allocation(ref mut val) => HerculesCPURefMut::from_slice::<T>(val),
}
}
HerculesMutBoxLocation::CPU => match self.cpu_alloc {
Allocation::None => panic!("No CPU reference"),
Allocation::Reference(ref mut val) => HerculesCPURefMut::from_slice(*val),
Allocation::Allocation(ref mut val) => HerculesCPURefMut::from_slice::<T>(val),
},
#[cfg(feature = "cuda")]
HerculesMutBoxLocation::CUDA => {
let cuda_ref : HerculesCUDARef<'a> =
match self.cuda_alloc {
Allocation::None => panic!("No GPU reference"),
Allocation::Reference(ref mut val) => val.dup().as_ref(),
Allocation::Allocation(ref val) => val.get_ref(),
};
let cuda_ref: HerculesCUDARef<'a> = match self.cuda_alloc {
Allocation::None => panic!("No GPU reference"),
Allocation::Reference(ref mut val) => val.dup().as_ref(),
Allocation::Allocation(ref val) => val.get_ref(),
};
let elements = unsafe { cuda_ref.__size() / size_of::<T>() };
// Allocate host memory (if needed)
let cpu_alloc : Allocation<&'b mut [T], Vec<T>> =
match self.cpu_alloc.take() {
Allocation::Reference(val) if val.len() == elements => Allocation::Reference(val),
Allocation::Allocation(val) if val.len() == elements => Allocation::Allocation(val),
_ => {
let mut alloc = Vec::new();
alloc.resize_with(elements, Default::default);
Allocation::Allocation(alloc)
}
};
let cpu_alloc: Allocation<&'b mut [T], Vec<T>> = match self.cpu_alloc.take() {
Allocation::Reference(val) if val.len() == elements => {
Allocation::Reference(val)
}
Allocation::Allocation(val) if val.len() == elements => {
Allocation::Allocation(val)
}
_ => {
let mut alloc = Vec::new();
alloc.resize_with(elements, Default::default);
Allocation::Allocation(alloc)
}
};
self.cpu_alloc = cpu_alloc;
let cpu_ref : &'a mut [T] =
match &mut self.cpu_alloc {
Allocation::None => panic!(),
Allocation::Reference(val) => val,
Allocation::Allocation(val) => val,
};
let cpu_ref: &'a mut [T] = match &mut self.cpu_alloc {
Allocation::None => panic!(),
Allocation::Reference(val) => val,
Allocation::Allocation(val) => val,
};
// Transfer data from CUDA device
let cpu_ref = cuda_ref.to_cpu_ref(cpu_ref);
......@@ -712,31 +722,32 @@ where
pub fn as_cuda_ref(&'a mut self) -> HerculesCUDARefMut<'a> {
match self.loc {
HerculesMutBoxLocation::CPU => {
let cpu_ref : &'a [T] =
match self.cpu_alloc {
Allocation::None => panic!("No CPU reference"),
Allocation::Reference(ref val) => val,
Allocation::Allocation(ref val) => val,
};
let cpu_ref: &'a [T] = match self.cpu_alloc {
Allocation::None => panic!("No CPU reference"),
Allocation::Reference(ref val) => val,
Allocation::Allocation(ref val) => val,
};
let size = cpu_ref.len() * size_of::<T>();
let (cuda_alloc, copied) =
match self.cuda_alloc.take() {
Allocation::Reference(val) if unsafe { val.__size() == size } => (Allocation::Reference(val), false),
Allocation::Allocation(val) if val.get_bytes() == size => (Allocation::Allocation(val), false),
_ => {
let alloc = CUDABox::from_cpu_ref(HerculesCPURef::from_slice(cpu_ref));
(Allocation::Allocation(alloc), true)
}
};
let (cuda_alloc, copied) = match self.cuda_alloc.take() {
Allocation::Reference(val) if unsafe { val.__size() == size } => {
(Allocation::Reference(val), false)
}
Allocation::Allocation(val) if val.get_bytes() == size => {
(Allocation::Allocation(val), false)
}
_ => {
let alloc = CUDABox::from_cpu_ref(HerculesCPURef::from_slice(cpu_ref));
(Allocation::Allocation(alloc), true)
}
};
self.cuda_alloc = cuda_alloc;
let cuda_ref =
match self.cuda_alloc {
Allocation::None => panic!(),
Allocation::Reference(ref mut val) => val.dup(),
Allocation::Allocation(ref mut val) => val.get_ref_mut(),
};
let cuda_ref = match self.cuda_alloc {
Allocation::None => panic!(),
Allocation::Reference(ref mut val) => val.dup(),
Allocation::Allocation(ref mut val) => val.get_ref_mut(),
};
if !copied {
unsafe {
......@@ -747,13 +758,11 @@ where
self.loc = HerculesMutBoxLocation::CUDA;
cuda_ref
}
HerculesMutBoxLocation::CUDA => {
match self.cuda_alloc {
Allocation::None => panic!("No GPU reference"),
Allocation::Reference(ref mut val) => val.dup(),
Allocation::Allocation(ref mut val) => val.get_ref_mut(),
}
}
HerculesMutBoxLocation::CUDA => match self.cuda_alloc {
Allocation::None => panic!("No GPU reference"),
Allocation::Reference(ref mut val) => val.dup(),
Allocation::Allocation(ref mut val) => val.get_ref_mut(),
},
}
}
}
......@@ -763,7 +772,8 @@ pub trait HerculesImmBoxTo<'a, T> {
}
impl<'a, T> HerculesImmBoxTo<'a, HerculesCPURef<'a>> for HerculesImmBox<'a, T>
where T: Default + Clone
where
T: Default + Clone,
{
fn to(&'a self) -> HerculesCPURef<'a> {
self.as_cpu_ref()
......@@ -772,7 +782,8 @@ where T: Default + Clone
#[cfg(feature = "cuda")]
impl<'a, T> HerculesImmBoxTo<'a, HerculesCUDARef<'a>> for HerculesImmBox<'a, T>
where T: Default + Clone
where
T: Default + Clone,
{
fn to(&'a self) -> HerculesCUDARef<'a> {
self.as_cuda_ref()
......@@ -784,7 +795,8 @@ pub trait HerculesMutBoxTo<'a, T> {
}
impl<'a, 'b: 'a, T> HerculesMutBoxTo<'a, HerculesCPURefMut<'a>> for HerculesMutBox<'b, T>
where T: Default + Clone
where
T: Default + Clone,
{
fn to(&'a mut self) -> HerculesCPURefMut<'a> {
self.as_cpu_ref()
......@@ -793,7 +805,8 @@ where T: Default + Clone
#[cfg(feature = "cuda")]
impl<'a, 'b: 'a, T> HerculesMutBoxTo<'a, HerculesCUDARefMut<'a>> for HerculesMutBox<'b, T>
where T: Default + Clone
where
T: Default + Clone,
{
fn to(&'a mut self) -> HerculesCUDARefMut<'a> {
self.as_cuda_ref()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment