1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/xla/service/gpu/copy_thunk.h"
17
18 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
19 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
20
21 namespace xla {
22 namespace gpu {
23
HostToDeviceCopyThunk(const void * source_address,const BufferAllocation::Slice & destination_buffer,uint64 mem_size,const HloInstruction * hlo_instruction)24 HostToDeviceCopyThunk::HostToDeviceCopyThunk(
25 const void* source_address,
26 const BufferAllocation::Slice& destination_buffer, uint64 mem_size,
27 const HloInstruction* hlo_instruction)
28 : Thunk(Kind::kCopy, hlo_instruction),
29 source_address_(source_address),
30 destination_buffer_(destination_buffer),
31 mem_size_(mem_size) {}
32
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)33 Status HostToDeviceCopyThunk::ExecuteOnStream(
34 const BufferAllocations& buffer_allocations, se::Stream* stream,
35 HloExecutionProfiler* profiler) {
36 se::DeviceMemoryBase destination_data =
37 buffer_allocations.GetDeviceAddress(destination_buffer_);
38 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
39 stream->ThenMemcpy(&destination_data, source_address_, mem_size_);
40 return Status::OK();
41 }
42
DeviceToDeviceCopyThunk(const BufferAllocation::Slice & source_buffer,const BufferAllocation::Slice & destination_buffer,uint64 mem_size,const HloInstruction * hlo_instruction)43 DeviceToDeviceCopyThunk::DeviceToDeviceCopyThunk(
44 const BufferAllocation::Slice& source_buffer,
45 const BufferAllocation::Slice& destination_buffer, uint64 mem_size,
46 const HloInstruction* hlo_instruction)
47 : Thunk(Kind::kCopy, hlo_instruction),
48 source_buffer_(source_buffer),
49 destination_buffer_(destination_buffer),
50 mem_size_(mem_size) {}
51
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)52 Status DeviceToDeviceCopyThunk::ExecuteOnStream(
53 const BufferAllocations& buffer_allocations, se::Stream* stream,
54 HloExecutionProfiler* profiler) {
55 se::DeviceMemoryBase destination_data =
56 buffer_allocations.GetDeviceAddress(destination_buffer_);
57 se::DeviceMemoryBase source_data =
58 buffer_allocations.GetDeviceAddress(source_buffer_);
59 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
60 stream->ThenMemcpy(&destination_data, source_data, mem_size_);
61 return Status::OK();
62 }
63 } // namespace gpu
64 } // namespace xla
65