MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / CopyGPUTensorToCPU

Method CopyGPUTensorToCPU

tensorflow/core/common_runtime/gpu/gpu_util.cc:280–330  ·  view source on GitHub ↗

static

Source from the content-addressed store, hash-verified

278
279// static
280void 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 */
333void GPUUtil::CopyCPUTensorToGPU(const Tensor* cpu_tensor,

Callers

nothing calls this directly

Calls 8

PrepareCopyFunction · 0.85
InternalFunction · 0.85
device_to_host_streamMethod · 0.80
GetBaseFunction · 0.70
okMethod · 0.45
TotalBytesMethod · 0.45
ThenExecuteMethod · 0.45
UnrefMethod · 0.45

Tested by

no test coverage detected