Copy host array to device array. Args: host_array: Host array to copy. device_array: Device array to copy to, __cuda_array_interface__ or object with interface.
(
host_array: np.ndarray,
device_array: int | dict | object,
)
| 78 | |
| 79 | # docs_tag: being_cuda_memcpy_h2d |
| 80 | def cuda_memcpy_h2d( |
| 81 | host_array: np.ndarray, |
| 82 | device_array: int | dict | object, |
| 83 | ) -> None: |
| 84 | """ |
| 85 | Copy host array to device array. |
| 86 | |
| 87 | Args: |
| 88 | host_array: Host array to copy. |
| 89 | device_array: Device array to copy to, __cuda_array_interface__ or object with interface. |
| 90 | """ |
| 91 | if hasattr(device_array, "__cuda_array_interface__"): |
| 92 | device_array = device_array.__cuda_array_interface__["data"][0] |
| 93 | elif isinstance(device_array, dict) and "data" in device_array: |
| 94 | device_array = device_array["data"][0] |
| 95 | elif not isinstance(device_array, int): |
| 96 | raise ValueError("Invalid device array") |
| 97 | (err,) = cudart.cudaMemcpy( |
| 98 | device_array, |
| 99 | host_array.ctypes.data, |
| 100 | host_array.nbytes, |
| 101 | cudart.cudaMemcpyKind.cudaMemcpyHostToDevice, |
| 102 | ) |
| 103 | if err != cudart.cudaError_t.cudaSuccess: |
| 104 | raise RuntimeError(f"cudaMemcpy failed: {err}") |
| 105 | |
| 106 | |
| 107 | # docs_tag: end_cuda_memcpy_h2d |
no outgoing calls
no test coverage detected