• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_GPU_DEVICE_CONTEXT_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GPU_DEVICE_CONTEXT_H_
18 
19 #include "tensorflow/core/common_runtime/device.h"
20 #include "tensorflow/core/framework/device_base.h"
21 #include "tensorflow/core/lib/gtl/inlined_vector.h"
22 
23 namespace stream_executor {
24 class Stream;
25 }  // namespace stream_executor
26 
27 namespace tensorflow {
28 
29 class GPUDeviceContext : public DeviceContext {
30  public:
31   // Does not take ownership of streams.
GPUDeviceContext(int stream_id,se::Stream * stream,se::Stream * host_to_device_stream,se::Stream * device_to_host_stream,gtl::InlinedVector<se::Stream *,4> device_to_device_stream)32   GPUDeviceContext(int stream_id, se::Stream* stream,
33                    se::Stream* host_to_device_stream,
34                    se::Stream* device_to_host_stream,
35                    gtl::InlinedVector<se::Stream*, 4> device_to_device_stream)
36       : stream_id_(stream_id),
37         stream_(stream),
38         host_to_device_stream_(host_to_device_stream),
39         device_to_host_stream_(device_to_host_stream),
40         device_to_device_stream_(device_to_device_stream) {}
41 
~GPUDeviceContext()42   ~GPUDeviceContext() override {}
43 
stream()44   se::Stream* stream() const override { return stream_; }
host_to_device_stream()45   se::Stream* host_to_device_stream() const { return host_to_device_stream_; }
device_to_host_stream()46   se::Stream* device_to_host_stream() const { return device_to_host_stream_; }
device_to_device_stream(int index)47   se::Stream* device_to_device_stream(int index) const {
48     return device_to_device_stream_[index % device_to_device_stream_.size()];
49   }
stream_id()50   int stream_id() const { return stream_id_; }
51 
52   void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
53                              Tensor* device_tensor,
54                              StatusCallback done) const override;
55 
56   void CopyDeviceTensorToCPU(const Tensor* device_tensor, StringPiece edge_name,
57                              Device* device, Tensor* cpu_tensor,
58                              StatusCallback done) override;
59 
60   void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device,
61                               Tensor* output_tensor,
62                               StatusCallback done) const override;
63 
MaintainLifetimeOnStream(const Tensor * t,se::Stream * stream)64   void MaintainLifetimeOnStream(const Tensor* t,
65                                 se::Stream* stream) const override {}
66 
67   Status ThenExecute(Device* device, se::Stream* stream,
68                      std::function<void()> func) override;
69 
70  private:
71   int stream_id_;
72   // The default primary stream to use for this context.
73   // All the memory belongs to this stream.
74   se::Stream* stream_;
75   // The stream to use for copying data from host into GPU.
76   se::Stream* host_to_device_stream_;
77   // The stream to use for copying data from GPU to host.
78   se::Stream* device_to_host_stream_;
79   // Streams to use for copying data between GPUs.
80   gtl::InlinedVector<se::Stream*, 4> device_to_device_stream_;
81 };
82 
83 }  // namespace tensorflow
84 
85 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_DEVICE_CONTEXT_H_
86