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

Method CopyCPUTensorToGPU

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

static */

Source from the content-addressed store, hash-verified

331
332/* static */
333void GPUUtil::CopyCPUTensorToGPU(const Tensor* cpu_tensor,
334 const DeviceContext* device_context,
335 Device* gpu_device, Tensor* gpu_tensor,
336 StatusCallback done, bool sync_dst_compute) {
337 VLOG(1) << "CopyCPUTensorToGPU";
338 const DeviceBase::GpuDeviceInfo* dev_info = nullptr;
339 se::Stream* recv_stream = nullptr;
340 Status s = PrepareCopy(gpu_device, device_context, *cpu_tensor, gpu_tensor,
341 &dev_info, &recv_stream);
342 if (!s.ok()) {
343 done(s);
344 return;
345 }
346
347 se::Stream* recv_host_to_device_stream = nullptr;
348 if (MergeComputeAndCopyStream()) {
349 recv_host_to_device_stream = recv_stream;
350 } else {
351 recv_host_to_device_stream =
352 static_cast<const GPUDeviceContext*>(device_context)
353 ->host_to_device_stream();
354 if (recv_host_to_device_stream == nullptr) {
355 done(errors::Internal("No send gpu copy-out-stream is available."));
356 return;
357 }
358 // Wait for the recv-stream to make sure the buffer is truly available.
359 if (sync_dst_compute) {
360 if (recv_host_to_device_stream != recv_stream) {
361 recv_host_to_device_stream->ThenWaitFor(recv_stream);
362 }
363 }
364 }
365
366 const int64 total_bytes = cpu_tensor->TotalBytes();
367 // Note that 0-size tensors have no backing buffer.
368 if (total_bytes > 0) {
369 void* src_ptr = GetBase(cpu_tensor);
370 void* dst_ptr = GetBase(gpu_tensor);
371 DeviceMemoryBase gpu_dst_ptr(dst_ptr, total_bytes);
372 recv_host_to_device_stream->ThenMemcpy(&gpu_dst_ptr, src_ptr, total_bytes);
373 }
374
375 if (MergeComputeAndCopyStream()) {
376 done(Status::OK());
377 } else {
378 // Use of cpu_tensor may outlive stack scope, so keep a ref.
379 TensorReference input_ref(*cpu_tensor);
380 dev_info->event_mgr->ThenExecute(
381 recv_host_to_device_stream,
382 [recv_host_to_device_stream, done, input_ref]() {
383 input_ref.Unref();
384 if (!recv_host_to_device_stream->ok()) {
385 LOG(FATAL) << "CPU->GPU Memcpy failed";
386 }
387 done(Status::OK());
388 });
389 }
390}

Callers

nothing calls this directly

Calls 8

PrepareCopyFunction · 0.85
InternalFunction · 0.85
GetBaseFunction · 0.70
okMethod · 0.45
host_to_device_streamMethod · 0.45
TotalBytesMethod · 0.45
ThenExecuteMethod · 0.45
UnrefMethod · 0.45

Tested by

no test coverage detected