|
1 | 1 | use crate::bindings::{
|
2 |
| - cudaDeviceAttr, cudaFree, cudaFreeHost, cudaHostAlloc, cudaHostAllocDefault, cudaHostAllocPortable, |
3 |
| - cudaHostGetFlags, cudaHostRegister, cudaHostRegisterDefault, cudaHostRegisterPortable, cudaHostUnregister, |
4 |
| - cudaMalloc, cudaMallocAsync, cudaMemPool_t, cudaMemcpy, cudaMemcpyAsync, cudaMemcpyKind, |
| 2 | + cudaFree, cudaMalloc, cudaMallocAsync, cudaMemPool_t, cudaMemcpy, cudaMemcpyAsync, cudaMemcpyKind, |
5 | 3 | };
|
6 |
| -use crate::device::{check_device, get_device_attribute, get_device_from_pointer}; |
| 4 | +use crate::device::{check_device, get_device_from_pointer}; |
7 | 5 | use crate::error::{CudaError, CudaResult, CudaResultWrap};
|
8 | 6 | use crate::stream::CudaStream;
|
9 |
| -use bitflags::bitflags; |
10 | 7 | use std::mem::{size_of, ManuallyDrop, MaybeUninit};
|
11 | 8 | use std::ops::{
|
12 | 9 | Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
|
13 | 10 | };
|
14 |
| -use std::os::raw::{c_uint, c_void}; |
| 11 | +use std::os::raw::c_void; |
15 | 12 | use std::slice::from_raw_parts_mut;
|
16 | 13 | use std::slice::SliceIndex;
|
17 | 14 |
|
18 |
| -bitflags! { |
19 |
| - pub struct CudaHostAllocFlags: u32 { |
20 |
| - const DEFAULT = cudaHostAllocDefault; |
21 |
| - const PORTABLE = cudaHostAllocPortable; |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -bitflags! { |
26 |
| - pub struct CudaHostRegisterFlags: u32 { |
27 |
| - const DEFAULT = cudaHostRegisterDefault; |
28 |
| - const PORTABLE = cudaHostRegisterPortable; |
29 |
| - } |
30 |
| -} |
31 |
| - |
32 | 15 | #[derive(Debug)]
|
33 | 16 | pub struct HostSlice<T>([T]);
|
34 | 17 | pub struct DeviceVec<T>(ManuallyDrop<Box<[T]>>);
|
@@ -134,78 +117,6 @@ impl<T> HostSlice<T> {
|
134 | 117 | self.0
|
135 | 118 | .iter_mut()
|
136 | 119 | }
|
137 |
| - |
138 |
| - // TODO: @jeremy Fix the issue where ptr pinned by cudaHostRegister cannot be used in primitives |
139 |
| - // pub fn is_pinnable(&self) -> bool { |
140 |
| - // let pinnable = get_device_attribute(cudaDeviceAttr::cudaDevAttrHostRegisterSupported, 0).unwrap(); |
141 |
| - // let lockable = |
142 |
| - // get_device_attribute(cudaDeviceAttr::cudaDevAttrPageableMemoryAccessUsesHostPageTables, 0).unwrap(); |
143 |
| - |
144 |
| - // pinnable == 1 && lockable == 0 |
145 |
| - // } |
146 |
| - |
147 |
| - // pub fn pin(&self, flags: CudaHostRegisterFlags) -> CudaResult<()> { |
148 |
| - // if self.is_pinnable() { |
149 |
| - // unsafe { |
150 |
| - // let ptr = self.as_ptr() as *mut c_void; |
151 |
| - // let flags_to_set = flags.bits(); |
152 |
| - // cudaHostRegister(ptr, self.len(), flags_to_set as c_uint).wrap() |
153 |
| - // } |
154 |
| - // } else { |
155 |
| - // Ok(()) |
156 |
| - // } |
157 |
| - // } |
158 |
| - |
159 |
| - // pub fn unpin(&self) -> CudaResult<()> { |
160 |
| - // unsafe { |
161 |
| - // let mut flags = 0; |
162 |
| - // let ptr = self.as_ptr() as *mut c_void; |
163 |
| - // cudaHostGetFlags(&mut flags, ptr).wrap()?; |
164 |
| - // cudaHostUnregister(ptr).wrap() |
165 |
| - // } |
166 |
| - // } |
167 |
| - |
168 |
| - pub fn allocate_pinned(&self, count: usize, flags: CudaHostAllocFlags) -> CudaResult<()> { |
169 |
| - let size = count |
170 |
| - .checked_mul(size_of::<T>()) |
171 |
| - .unwrap_or(0); |
172 |
| - if size == 0 { |
173 |
| - return Err(CudaError::cudaErrorMemoryAllocation); //TODO: only CUDA backend should return CudaError |
174 |
| - } |
175 |
| - |
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 |
| - // } |
183 |
| - |
184 |
| - unsafe { |
185 |
| - let p_host = self.as_ptr() as *mut *mut c_void; |
186 |
| - cudaHostAlloc(p_host, size, flags.bits()).wrap()?; |
187 |
| - } |
188 |
| - |
189 |
| - Ok(()) |
190 |
| - } |
191 |
| - |
192 |
| - pub fn free_pinned(&self) -> CudaResult<()> { |
193 |
| - unsafe { |
194 |
| - let mut flags: u32 = 0; |
195 |
| - let ptr = self.as_ptr() as *mut c_void; |
196 |
| - cudaHostGetFlags(&mut flags, ptr).wrap()?; |
197 |
| - cudaFreeHost(ptr).wrap() |
198 |
| - } |
199 |
| - } |
200 |
| - |
201 |
| - pub fn get_memory_flags(&self) -> CudaResult<u32> { |
202 |
| - unsafe { |
203 |
| - let mut flags: u32 = 1234; |
204 |
| - let ptr = self.as_ptr() as *mut c_void; |
205 |
| - cudaHostGetFlags(&mut flags, ptr).wrap()?; |
206 |
| - Ok(flags) |
207 |
| - } |
208 |
| - } |
209 | 120 | }
|
210 | 121 |
|
211 | 122 | impl<T> DeviceSlice<T> {
|
@@ -513,47 +424,3 @@ impl<T> Drop for DeviceVec<T> {
|
513 | 424 |
|
514 | 425 | #[allow(non_camel_case_types)]
|
515 | 426 | pub type CudaMemPool = cudaMemPool_t;
|
516 |
| - |
517 |
| -pub(crate) mod tests { |
518 |
| - use crate::error::CudaError; |
519 |
| - use crate::memory::{CudaHostAllocFlags, HostOrDeviceSlice, HostSlice}; |
520 |
| - |
521 |
| - // TODO: @jeremy Fix the issue where ptr pinned by cudaHostRegister cannot be used in primitives |
522 |
| - // #[test] |
523 |
| - // fn test_pin_memory() { |
524 |
| - // let data = vec![1, 2, 3, 4, 5, 7, 8, 9]; |
525 |
| - // let data_host_slice = HostSlice::from_slice(&data); |
526 |
| - |
527 |
| - // data_host_slice |
528 |
| - // .pin(CudaHostRegisterFlags::DEFAULT) |
529 |
| - // .expect("Registering host mem failed"); |
530 |
| - // let err = data_host_slice |
531 |
| - // .pin(CudaHostRegisterFlags::DEFAULT) |
532 |
| - // .expect_err("Registering already registered memory succeeded"); |
533 |
| - // assert_eq!(err, CudaError::cudaErrorHostMemoryAlreadyRegistered); |
534 |
| - |
535 |
| - // data_host_slice |
536 |
| - // .unpin() |
537 |
| - // .expect("Unregistering pinned memory failed"); |
538 |
| - // let err = data_host_slice |
539 |
| - // .unpin() |
540 |
| - // .expect_err("Unregistering non-registered pinned memory succeeded"); |
541 |
| - // assert_eq!(err, CudaError::cudaErrorInvalidValue); |
542 |
| - // } |
543 |
| - |
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 |
| - // } |
559 |
| -} |
0 commit comments