1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TRANSFER_MANAGER_H_
17#define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TRANSFER_MANAGER_H_
18
19#include <vector>
20
21#include "tensorflow/compiler/xla/service/cpu/xfeed_manager.h"
22#include "tensorflow/compiler/xla/service/generic_transfer_manager.h"
23#include "tensorflow/compiler/xla/service/transfer_manager.h"
24#include "tensorflow/compiler/xla/statusor.h"
25#include "tensorflow/compiler/xla/xla_data.pb.h"
26#include "tensorflow/core/lib/gtl/array_slice.h"
27#include "tensorflow/core/platform/macros.h"
28#include "tensorflow/core/platform/stream_executor_no_cuda.h"
29#include "tensorflow/core/platform/types.h"
30
31namespace xla {
32
33// An implementation of the XLA GenericTransferManager that
34// handles CPU-specific infeed.
35class CpuTransferManager : public GenericTransferManager {
36 public:
37  CpuTransferManager();
38  ~CpuTransferManager() override {}
39
40  Status TransferLiteralToInfeed(perftools::gputools::StreamExecutor* executor,
41                                 const Literal& literal) override;
42  Status TransferBufferToInfeed(perftools::gputools::StreamExecutor* executor,
43                                int64 size, const void* source) override;
44  Status TransferLiteralFromOutfeed(
45      perftools::gputools::StreamExecutor* executor, const Shape& literal_shape,
46      Literal* literal) override;
47
48 private:
49  // Transfers infeed data to device. InfeedBuffer->Done() must be
50  // called to clean up the memory allocated for InfeedBuffer.
51  StatusOr<cpu::runtime::XfeedBuffer*> TransferBufferToInfeedInternal(
52      perftools::gputools::StreamExecutor* executor, int64 size,
53      const void* source);
54
55  // Helper that transfers a tuple of element buffers from the device's outfeed.
56  StatusOr<Shape> TransferTupleBuffersFromOutfeed(
57      perftools::gputools::StreamExecutor* executor,
58      tensorflow::gtl::ArraySlice<std::pair<void*, int64>> buffer_data);
59
60  // Helper that transfers an array buffer from the device's outfeed.
61  StatusOr<Shape> TransferArrayBufferFromOutfeed(
62      perftools::gputools::StreamExecutor* executor, void* destination,
63      int64 size_bytes);
64
65  // On success, returns the shape that was transferred from the outfeed -- if
66  // is_tuple is true, the returned shape will be a tuple of the returned shapes
67  // for the given buffers.
68  StatusOr<Shape> TransferBuffersFromOutfeedInternal(
69      perftools::gputools::StreamExecutor* executor,
70      tensorflow::gtl::ArraySlice<std::pair<void*, int64>> buffer_data,
71      bool is_tuple);
72
73  TF_DISALLOW_COPY_AND_ASSIGN(CpuTransferManager);
74};
75
76}  // namespace xla
77
78#endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TRANSFER_MANAGER_H_
79