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_COPY_TENSOR_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_COPY_TENSOR_H_
18 
19 #include "tensorflow/core/common_runtime/device.h"
20 #include "tensorflow/core/framework/allocator.h"
21 #include "tensorflow/core/framework/device_base.h"
22 #include "tensorflow/core/framework/tensor.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow/core/platform/types.h"
26 
27 namespace tensorflow {
28 
29 class CopyTensor {
30  public:
31   typedef void (*CopyFunction)(DeviceContext* send_dev_context,
32                                DeviceContext* recv_dev_context, Device* src,
33                                Device* dst,
34                                const AllocatorAttributes src_alloc_attr,
35                                const AllocatorAttributes dst_alloc_attr,
36                                const Tensor* input, Tensor* output,
37                                StatusCallback done);
38 
39   // Copies "input" to "output" between devices accessible to the
40   // local process via some DMA-like method.  "edge_name" is the name
41   // of the tensor being copied, for debugging purposes. Depending on
42   // the type of devices and memory in use, the copy may be performed
43   // synchronously or asynchronously.  'done' will be invoked only
44   // after the copy is actually complete.
45   static void ViaDMA(StringPiece edge_name, DeviceContext* send_dev_context,
46                      DeviceContext* recv_dev_context, Device* src, Device* dst,
47                      const AllocatorAttributes src_alloc_attr,
48                      const AllocatorAttributes dst_alloc_attr,
49                      const Tensor* input, Tensor* output, StatusCallback done);
50 
51   // Object used to call Register() at static-initialization time.
52   // Note: This should only ever be used as a global-static object; no stack
53   // or heap instances.
54   class Registration {
55    public:
Registration(DeviceType sender_device_type,DeviceType receiver_device_type,CopyFunction copy_function)56     Registration(DeviceType sender_device_type, DeviceType receiver_device_type,
57                  CopyFunction copy_function) {
58       TF_QCHECK_OK(
59           Register(sender_device_type, receiver_device_type, copy_function));
60     }
61   };
62 
63  private:
64   // Register a function for copying between two specific DeviceTypes.
65   // Note: This should only be called via the constructor of
66   // CopyTensor::Registration.
67   static Status Register(DeviceType sender_device_type,
68                          DeviceType receiver_device_type,
69                          CopyFunction copy_function);
70 };
71 
72 }  // namespace tensorflow
73 
74 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_COPY_TENSOR_H_
75