static
| 278 | |
| 279 | // static |
| 280 | void GPUUtil::CopyGPUTensorToCPU(Device* gpu_device, |
| 281 | const DeviceContext* device_context, |
| 282 | const Tensor* gpu_tensor, Tensor* cpu_tensor, |
| 283 | StatusCallback done) { |
| 284 | VLOG(1) << "CopyGPUTensorToCPU"; |
| 285 | const DeviceBase::GpuDeviceInfo* dev_info = nullptr; |
| 286 | se::Stream* send_stream = nullptr; |
| 287 | Status s = PrepareCopy(gpu_device, device_context, *gpu_tensor, cpu_tensor, |
| 288 | &dev_info, &send_stream); |
| 289 | if (!s.ok()) { |
| 290 | done(s); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | se::Stream* send_device_to_host_stream = nullptr; |
| 295 | if (MergeComputeAndCopyStream()) { |
| 296 | send_device_to_host_stream = send_stream; |
| 297 | } else { |
| 298 | send_device_to_host_stream = |
| 299 | static_cast<const GPUDeviceContext*>(device_context) |
| 300 | ->device_to_host_stream(); |
| 301 | if (send_device_to_host_stream == nullptr) { |
| 302 | done(errors::Internal("No send gpu copy-out-stream is available.")); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Wait for the sender's main stream to make sure the data are available. |
| 307 | if (send_device_to_host_stream != send_stream) { |
| 308 | send_device_to_host_stream->ThenWaitFor(send_stream); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | const int64 total_bytes = gpu_tensor->TotalBytes(); |
| 313 | if (total_bytes > 0) { |
| 314 | void* src_ptr = GetBase(gpu_tensor); |
| 315 | DeviceMemoryBase gpu_src_ptr(src_ptr, total_bytes); |
| 316 | void* dst_ptr = GetBase(cpu_tensor); |
| 317 | send_device_to_host_stream->ThenMemcpy(dst_ptr, gpu_src_ptr, total_bytes); |
| 318 | } |
| 319 | // Use of the input may outlive stack scope, so keep a ref. |
| 320 | TensorReference input_ref(*gpu_tensor); |
| 321 | dev_info->event_mgr->ThenExecute( |
| 322 | send_device_to_host_stream, |
| 323 | [send_device_to_host_stream, done, input_ref]() { |
| 324 | if (!send_device_to_host_stream->ok()) { |
| 325 | LOG(FATAL) << "GPU->CPU Memcpy failed"; |
| 326 | } |
| 327 | input_ref.Unref(); |
| 328 | done(Status::OK()); |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | /* static */ |
| 333 | void GPUUtil::CopyCPUTensorToGPU(const Tensor* cpu_tensor, |
nothing calls this directly
no test coverage detected