MCPcopy Create free account
hub / github.com/Rust-GPU/rust-cuda / copy_to

Method copy_to

crates/cust/src/memory/array.rs:795–835  ·  view source on GitHub ↗

Copy data from the array to the host. **This will not check if the formats match, it does however check for memory size mismatch**. For example, you can copy a `[u32; 2]` value to a `[u8; 8]` array just fine, but not to a `[u8; 10]` array.

(&self, val: &mut U)

Source from the content-addressed store, hash-verified

793 ///
794 /// For example, you can copy a `[u32; 2]` value to a `[u8; 8]` array just fine, but not to a `[u8; 10]` array.
795 pub fn copy_to<T: ArrayPrimitive, U: AsMut<[T]>>(&self, val: &mut U) -> CudaResult<()> {
796 let val = val.as_mut();
797 let desc = self.descriptor()?;
798 let self_size = desc.width()
799 * desc.height().max(1)
800 * desc.depth().max(1)
801 * desc.num_channels() as usize
802 * desc.format().mem_size();
803 let other_size = mem::size_of_val(val);
804 assert_eq!(self_size, other_size, "Array and value sizes don't match");
805 unsafe {
806 if desc.height() == 0 && desc.depth() == 0 {
807 cuMemcpyAtoH_v2(val.as_mut_ptr() as *mut c_void, self.handle, 0, self_size)
808 .to_result()
809 } else if desc.depth() == 0 {
810 let width = desc.width() * desc.num_channels() as usize * desc.format().mem_size();
811 let desc = CUDA_MEMCPY2D {
812 Height: desc.height(),
813 WidthInBytes: width,
814 dstArray: null_mut(),
815 dstDevice: 0,
816 dstHost: val.as_mut_ptr() as *mut c_void,
817 dstMemoryType: cuda::CUmemorytype_enum::CU_MEMORYTYPE_HOST,
818 dstPitch: 0,
819 dstXInBytes: 0,
820 dstY: 0,
821 srcArray: self.handle,
822 srcDevice: 0,
823 srcHost: null(),
824 srcMemoryType: cuda::CUmemorytype_enum::CU_MEMORYTYPE_ARRAY,
825 srcPitch: 0,
826 srcXInBytes: 0,
827 srcY: 0,
828 };
829 cuMemcpy2D_v2(&desc as *const _).to_result()?;
830 Ok(())
831 } else {
832 panic!();
833 }
834 }
835 }
836
837 /// Copy data from the array into a vec on the host. **This will not check if the formats match, it does
838 /// however yield a correct vec**. Format mismatch and especially format size mismatch may yield incorrect (but not unsound!)

Callers 1

as_host_vecMethod · 0.45

Calls 10

depthMethod · 0.80
num_channelsMethod · 0.80
mem_sizeMethod · 0.80
as_mutMethod · 0.45
descriptorMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45
formatMethod · 0.45
to_resultMethod · 0.45
as_mut_ptrMethod · 0.45

Tested by

no test coverage detected