aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/common_runtime/sycl/sycl_device_context.cc
blob: b49420b1b5300381ad4f5ba8095c7a37d25388ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#if TENSORFLOW_USE_SYCL

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

#include "tensorflow/core/common_runtime/sycl/sycl_device_context.h"
#include "tensorflow/core/common_runtime/dma_helper.h"

namespace tensorflow {

void SYCLDeviceContext::CopyCPUTensorToDevice(const Tensor *cpu_tensor,
                                              Device *device,
                                              Tensor *device_tensor,
                                              StatusCallback done) const {
  const int64 total_bytes = cpu_tensor->TotalBytes();
  if (total_bytes > 0) {
    const void *src_ptr = DMAHelper::base(cpu_tensor);
    void *dst_ptr = DMAHelper::base(device_tensor);
    switch (cpu_tensor->dtype()) {
    case DT_FLOAT:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<float *>(dst_ptr), static_cast<const float *>(src_ptr),
          total_bytes);
      break;
    case DT_DOUBLE:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<double *>(dst_ptr), static_cast<const double *>(src_ptr),
          total_bytes);
      break;
    case DT_INT32:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<int32 *>(dst_ptr), static_cast<const int32 *>(src_ptr),
          total_bytes);
      break;
    case DT_INT64:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<int64 *>(dst_ptr), static_cast<const int64 *>(src_ptr),
          total_bytes);
      break;
    case DT_HALF:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<Eigen::half *>(dst_ptr),
          static_cast<const Eigen::half *>(src_ptr), total_bytes);
      break;
    case DT_COMPLEX64:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<std::complex<float> *>(dst_ptr),
          static_cast<const std::complex<float> *>(src_ptr), total_bytes);
      break;
    case DT_COMPLEX128:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<std::complex<double> *>(dst_ptr),
          static_cast<const std::complex<double> *>(src_ptr), total_bytes);
      break;
    case DT_INT8:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<int8 *>(dst_ptr), static_cast<const int8 *>(src_ptr),
          total_bytes);
      break;
    case DT_INT16:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<int16 *>(dst_ptr), static_cast<const int16 *>(src_ptr),
          total_bytes);
      break;
    case DT_UINT8:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<uint8 *>(dst_ptr), static_cast<const uint8 *>(src_ptr),
          total_bytes);
      break;
    case DT_UINT16:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<uint16 *>(dst_ptr), static_cast<const uint16 *>(src_ptr),
          total_bytes);
      break;
    case DT_BOOL:
      device->eigen_sycl_device()->memcpyHostToDevice(
          static_cast<bool *>(dst_ptr), static_cast<const bool *>(src_ptr),
          total_bytes);
      break;
    default:
      assert(false && "unsupported type");
    }
  }
  done(Status::OK());
}

void SYCLDeviceContext::CopyDeviceTensorToCPU(const Tensor *device_tensor,
                                              StringPiece edge_name,
                                              Device *device,
                                              Tensor *cpu_tensor,
                                              StatusCallback done) {
  const int64 total_bytes = device_tensor->TotalBytes();
  if (total_bytes > 0) {
    const void* src_ptr = DMAHelper::base(device_tensor);
    void* dst_ptr = DMAHelper::base(cpu_tensor);
    switch (device_tensor->dtype()) {
    case DT_FLOAT:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<float *>(dst_ptr), static_cast<const float *>(src_ptr),
          total_bytes);
      break;
    case DT_DOUBLE:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<double *>(dst_ptr), static_cast<const double *>(src_ptr),
          total_bytes);
      break;
    case DT_INT32:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<int32 *>(dst_ptr), static_cast<const int32 *>(src_ptr),
          total_bytes);
      break;
    case DT_INT64:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<int64 *>(dst_ptr), static_cast<const int64 *>(src_ptr),
          total_bytes);
      break;
    case DT_HALF:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<Eigen::half *>(dst_ptr),
          static_cast<const Eigen::half *>(src_ptr), total_bytes);
      break;
    case DT_COMPLEX64:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<std::complex<float> *>(dst_ptr),
          static_cast<const std::complex<float> *>(src_ptr), total_bytes);
      break;
    case DT_COMPLEX128:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<std::complex<double> *>(dst_ptr),
          static_cast<const std::complex<double> *>(src_ptr), total_bytes);
      break;
    case DT_INT8:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<int8 *>(dst_ptr), static_cast<const int8 *>(src_ptr),
          total_bytes);
      break;
    case DT_INT16:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<int16 *>(dst_ptr), static_cast<const int16 *>(src_ptr),
          total_bytes);
      break;
    case DT_UINT8:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<uint8 *>(dst_ptr), static_cast<const uint8 *>(src_ptr),
          total_bytes);
      break;
    case DT_UINT16:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<uint16 *>(dst_ptr), static_cast<const uint16 *>(src_ptr),
          total_bytes);
      break;
    case DT_BOOL:
      device->eigen_sycl_device()->memcpyDeviceToHost(
          static_cast<bool *>(dst_ptr), static_cast<const bool *>(src_ptr),
          total_bytes);
      break;
    default:
      assert(false && "unsupported type");
    }
  }
  done(Status::OK());
}

}  // namespace tensorflow
#endif // TENSORFLOW_USE_SYCL