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 #if !TENSORFLOW_USE_SYCL
17 #error This file must only be included when building TensorFlow with SYCL support
18 #endif
19
20 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_SYCL_SYCL_UTIL_H_
21 #define TENSORFLOW_CORE_COMMON_RUNTIME_SYCL_SYCL_UTIL_H_
22
23 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
24 #include "tensorflow/core/common_runtime/device.h"
25 // For DMA helper
26 #include "tensorflow/core/common_runtime/dma_helper.h"
27 #include "tensorflow/core/framework/tensor.h"
28
29 namespace tensorflow {
GetBase(const Tensor * src)30 inline void const* GetBase(const Tensor* src) { return DMAHelper::base(src); }
GetBase(Tensor * dst)31 inline void* GetBase(Tensor* dst) { return DMAHelper::base(dst); }
32
SYCLmemcpy(Eigen::SyclDevice const & device,Tensor const & src_tensor,Tensor * dst_tensor)33 inline void SYCLmemcpy(Eigen::SyclDevice const& device,
34 Tensor const& src_tensor, Tensor* dst_tensor) {
35 const size_t size = src_tensor.TotalBytes();
36 void* dst_ptr = GetBase(dst_tensor);
37 void const* src_ptr = GetBase(&src_tensor);
38
39 #define COPY_WITH_TYPE(T) \
40 device.memcpy(dst_ptr, static_cast<T const*>(src_ptr), size);
41 switch (src_tensor.dtype()) {
42 case DT_COMPLEX128:
43 COPY_WITH_TYPE(cl::sycl::cl_ulong2);
44 break;
45 case DT_DOUBLE:
46 case DT_COMPLEX64:
47 case DT_INT64:
48 COPY_WITH_TYPE(cl::sycl::cl_ulong);
49 break;
50 case DT_FLOAT:
51 case DT_INT32:
52 case DT_QINT32:
53 COPY_WITH_TYPE(cl::sycl::cl_uint);
54 break;
55 case DT_INT16:
56 case DT_UINT16:
57 case DT_BFLOAT16:
58 case DT_QINT16:
59 case DT_QUINT16:
60 case DT_HALF:
61 COPY_WITH_TYPE(cl::sycl::cl_ushort);
62 break;
63 case DT_BOOL:
64 COPY_WITH_TYPE(bool);
65 break;
66 case DT_UINT8:
67 case DT_INT8:
68 case DT_QINT8:
69 case DT_QUINT8:
70 COPY_WITH_TYPE(cl::sycl::cl_uchar);
71 break;
72 default:
73 LOG(FATAL) << "Unknown data type " << src_tensor.dtype();
74 break;
75 }
76 #undef COPY_WITH_TYPE
77 }
78 } // namespace tensorflow
79
80 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_SYCL_SYCL_UTIL_H_
81