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

Method copy_from

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

Copy data from the host to the array on the device. **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.

(&mut self, val: &U)

Source from the content-addressed store, hash-verified

747 ///
748 /// For example, you can copy a `[u32; 2]` value to a `[u8; 8]` array just fine, but not to a `[u8; 10]` array.
749 pub fn copy_from<T: ArrayPrimitive, U: AsRef<[T]>>(&mut self, val: &U) -> CudaResult<()> {
750 let val = val.as_ref();
751 let desc = self.descriptor()?;
752 let self_size = desc.width()
753 * desc.height().max(1)
754 * desc.depth().max(1)
755 * desc.num_channels() as usize
756 * desc.format().mem_size();
757 let other_size = mem::size_of_val(val);
758 assert_eq!(self_size, other_size, "Array and value sizes don't match");
759 unsafe {
760 if desc.height() == 0 && desc.depth() == 0 {
761 cuMemcpyHtoA_v2(self.handle, 0, val.as_ptr() as *const c_void, self_size)
762 .to_result()
763 } else if desc.depth() == 0 {
764 let desc = CUDA_MEMCPY2D {
765 Height: desc.height(),
766 WidthInBytes: desc.width()
767 * desc.num_channels() as usize
768 * desc.format().mem_size(),
769 dstArray: self.handle,
770 dstDevice: 0,
771 dstHost: null_mut(),
772 dstMemoryType: cuda::CUmemorytype_enum::CU_MEMORYTYPE_ARRAY,
773 dstPitch: 0,
774 dstXInBytes: 0,
775 dstY: 0,
776 srcArray: null_mut(),
777 srcDevice: 0,
778 srcHost: val.as_ptr() as *const c_void,
779 srcMemoryType: cuda::CUmemorytype_enum::CU_MEMORYTYPE_HOST,
780 srcPitch: 0,
781 srcXInBytes: 0,
782 srcY: 0,
783 };
784 cuMemcpy2D_v2(&desc as *const _).to_result()
785 } else {
786 panic!();
787 }
788 }
789 }
790
791 /// Copy data from the array to the host. **This will not check if the formats match, it does
792 /// however check for memory size mismatch**.

Callers 2

as_1d_arrayMethod · 0.45
as_2d_arrayMethod · 0.45

Calls 10

depthMethod · 0.80
num_channelsMethod · 0.80
mem_sizeMethod · 0.80
as_ptrMethod · 0.80
as_refMethod · 0.45
descriptorMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45
formatMethod · 0.45
to_resultMethod · 0.45

Tested by

no test coverage detected