Skip to content

Commit 1cb6e44

Browse files
committed
temp for allocate pinned for rust bindings
1 parent af575b7 commit 1cb6e44

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

wrappers/rust/icicle-core/src/msm/tests.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ where
160160
.into();
161161
assert!(msm_res_affine.is_on_curve());
162162

163-
let allocated_pinned_points = HostSlice::allocate_pinned(points.len(), CudaHostAllocFlags::DEFAULT).unwrap();
164-
allocated_pinned_points
165-
.as_mut_slice()
166-
.clone_from_slice(points.as_slice());
163+
points.allocate_pinned(points.len(), CudaHostAllocFlags::DEFAULT).unwrap();
164+
// let allocated_pinned_points = HostSlice::allocate_pinned(points.len(), CudaHostAllocFlags::DEFAULT).unwrap();
165+
// allocated_pinned_points
166+
// .as_mut_slice()
167+
// .clone_from_slice(points.as_slice());
167168

168-
msm(&scalars_d[..], allocated_pinned_points, &cfg, &mut msm_results[..]).unwrap();
169+
msm(&scalars_d[..], points, &cfg, &mut msm_results[..]).unwrap();
169170

170171
let mut msm_host_result = vec![Projective::<C>::zero(); 1];
171172
msm_results
@@ -179,7 +180,7 @@ where
179180
.to_ark()
180181
.into();
181182
assert!(msm_res_affine.is_on_curve());
182-
allocated_pinned_points.free_pinned();
183+
points.free_pinned();
183184

184185
stream
185186
.destroy()

wrappers/rust/icicle-cuda-runtime/src/memory.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,28 @@ impl<T> HostSlice<T> {
165165
// }
166166
// }
167167

168-
pub fn allocate_pinned(count: usize, flags: CudaHostAllocFlags) -> CudaResult<&'static mut Self> {
168+
pub fn allocate_pinned(&self, count: usize, flags: CudaHostAllocFlags) -> CudaResult<()> {
169169
let size = count
170170
.checked_mul(size_of::<T>())
171171
.unwrap_or(0);
172172
if size == 0 {
173173
return Err(CudaError::cudaErrorMemoryAllocation); //TODO: only CUDA backend should return CudaError
174174
}
175175

176-
let mut pinned_host_ptr = MaybeUninit::<*mut c_void>::uninit();
176+
// let mut pinned_host_ptr = MaybeUninit::<*mut c_void>::uninit();
177+
178+
// unsafe {
179+
// cudaHostAlloc(pinned_host_ptr.as_mut_ptr(), size, flags.bits).wrap()?;
180+
// let pinned_host_slice = from_raw_parts_mut(pinned_host_ptr.assume_init() as *mut T, count);
181+
// Ok(Self::from_mut_slice(pinned_host_slice))
182+
// }
177183

178184
unsafe {
179-
cudaHostAlloc(pinned_host_ptr.as_mut_ptr(), size, flags.bits).wrap()?;
180-
let pinned_host_slice = from_raw_parts_mut(pinned_host_ptr.assume_init() as *mut T, count);
181-
Ok(Self::from_mut_slice(pinned_host_slice))
185+
let p_host = self.as_ptr() as *mut *mut c_void;
186+
cudaHostAlloc(p_host, size, flags.bits()).wrap()?;
182187
}
188+
189+
Ok(())
183190
}
184191

185192
pub fn free_pinned(&self) -> CudaResult<()> {
@@ -534,19 +541,19 @@ pub(crate) mod tests {
534541
// assert_eq!(err, CudaError::cudaErrorInvalidValue);
535542
// }
536543

537-
#[test]
538-
fn test_allocated_pinned_memory() {
539-
let data = vec![1, 2, 3, 4, 5, 7, 8, 9];
540-
let data_host_slice = HostSlice::from_slice(&data);
541-
let newly_allocated_pinned_host_slice: &HostSlice<i32> =
542-
HostSlice::allocate_pinned(data_host_slice.len(), CudaHostAllocFlags::DEFAULT)
543-
.expect("Allocating new pinned memory failed");
544-
newly_allocated_pinned_host_slice
545-
.free_pinned()
546-
.expect("Freeing pinned memory failed");
547-
let err = newly_allocated_pinned_host_slice
548-
.free_pinned()
549-
.expect_err("Freeing non-pinned memory succeeded");
550-
assert_eq!(err, CudaError::cudaErrorInvalidValue);
551-
}
544+
// #[test]
545+
// fn test_allocated_pinned_memory() {
546+
// let data = vec![1, 2, 3, 4, 5, 7, 8, 9];
547+
// let data_host_slice = HostSlice::from_slice(&data);
548+
// let newly_allocated_pinned_host_slice: &HostSlice<i32> =
549+
// HostSlice::allocate_pinned(data_host_slice.len(), CudaHostAllocFlags::DEFAULT)
550+
// .expect("Allocating new pinned memory failed");
551+
// newly_allocated_pinned_host_slice
552+
// .free_pinned()
553+
// .expect("Freeing pinned memory failed");
554+
// let err = newly_allocated_pinned_host_slice
555+
// .free_pinned()
556+
// .expect_err("Freeing non-pinned memory succeeded");
557+
// assert_eq!(err, CudaError::cudaErrorInvalidValue);
558+
// }
552559
}

0 commit comments

Comments
 (0)