target_wrapper.cc 13.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle 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.

15
#include "lite/backends/opencl/target_wrapper.h"
Y
Yan Chunwei 已提交
16
#include <algorithm>
17 18 19
#include "lite/backends/opencl/cl_include.h"
#include "lite/backends/opencl/cl_runtime.h"
#include "lite/backends/opencl/cl_utility.h"
Y
Yan Chunwei 已提交
20 21 22 23 24 25 26
namespace paddle {
namespace lite {

static cl_channel_type GetCLChannelType(const PrecisionType type) {
  switch (type) {
    case PRECISION(kFloat):
      return CL_FLOAT;
27 28
    case PRECISION(kFP16):
      return CL_HALF_FLOAT;
Y
Yan Chunwei 已提交
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
    case PRECISION(kInt32):
      return CL_SIGNED_INT32;
    case PRECISION(kInt8):
      return CL_SIGNED_INT8;
    default:
      LOG(FATAL) << "Unsupported image channel type: " << PrecisionToStr(type);
      return 0;
  }
}

void *TargetWrapperCL::Malloc(size_t size) {
  cl_int status;
  cl::Buffer *buffer = new cl::Buffer(CLRuntime::Global()->context(),
                                      CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
                                      size,
                                      nullptr,
                                      &status);
  if (status != CL_SUCCESS) {
    delete buffer;
    buffer = nullptr;
  }
  CL_CHECK_FATAL(status);
  return buffer;
}

void TargetWrapperCL::Free(void *ptr) {
  if (ptr != nullptr) {
    cl::Buffer *cl_buffer = static_cast<cl::Buffer *>(ptr);
    delete cl_buffer;
  }
}

61
template <>
62
void *TargetWrapperCL::MallocImage<float>(const size_t cl_image2d_width,
63 64
                                          const size_t cl_image2d_height,
                                          void *host_ptr) {
65 66
  cl::ImageFormat img_format(CL_RGBA, GetCLChannelType(PRECISION(kFloat)));
  cl_int status;
67 68 69 70 71 72 73 74 75 76
  cl::Image2D *cl_image = new cl::Image2D(
      CLRuntime::Global()->context(),
      (host_ptr ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE) |
          (host_ptr ? CL_MEM_COPY_HOST_PTR : CL_MEM_ALLOC_HOST_PTR),
      img_format,
      cl_image2d_width,
      cl_image2d_height,
      0,
      host_ptr,
      &status);
77 78 79 80 81 82 83 84
  if (status != CL_SUCCESS) {
    delete cl_image;
    cl_image = nullptr;
  }
  CL_CHECK_FATAL(status);
  return cl_image;
}

85 86 87 88
template <>  // use uint16_t represents half float
void *TargetWrapperCL::MallocImage<uint16_t>(const size_t cl_image2d_width,
                                             const size_t cl_image2d_height,
                                             void *host_ptr) {
89
  cl::ImageFormat img_format(CL_RGBA, GetCLChannelType(PRECISION(kFP16)));
90
  cl_int status;
91 92 93 94 95 96 97 98 99 100
  cl::Image2D *cl_image = new cl::Image2D(
      CLRuntime::Global()->context(),
      (host_ptr ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE) |
          (host_ptr ? CL_MEM_COPY_HOST_PTR : CL_MEM_ALLOC_HOST_PTR),
      img_format,
      cl_image2d_width,
      cl_image2d_height,
      0,
      host_ptr,
      &status);
101 102 103 104 105 106 107 108 109
  if (status != CL_SUCCESS) {
    delete cl_image;
    cl_image = nullptr;
  }
  CL_CHECK_FATAL(status);
  return cl_image;
}

template <>
110
void *TargetWrapperCL::MallocImage<int32_t>(const size_t cl_image2d_width,
111 112
                                            const size_t cl_image2d_height,
                                            void *host_ptr) {
113
  cl::ImageFormat img_format(CL_RGBA, GetCLChannelType(PRECISION(kInt32)));
Y
Yan Chunwei 已提交
114
  cl_int status;
115 116 117 118 119 120 121 122 123 124
  cl::Image2D *cl_image = new cl::Image2D(
      CLRuntime::Global()->context(),
      (host_ptr ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE) |
          (host_ptr ? CL_MEM_COPY_HOST_PTR : CL_MEM_ALLOC_HOST_PTR),
      img_format,
      cl_image2d_width,
      cl_image2d_height,
      0,
      host_ptr,
      &status);
Y
Yan Chunwei 已提交
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
  if (status != CL_SUCCESS) {
    delete cl_image;
    cl_image = nullptr;
  }
  CL_CHECK_FATAL(status);
  return cl_image;
}

void TargetWrapperCL::FreeImage(void *image) {
  if (image != nullptr) {
    cl::Image2D *cl_image = static_cast<cl::Image2D *>(image);
    delete cl_image;
  }
}

void *TargetWrapperCL::Map(void *buffer, size_t offset, size_t size) {
  cl::Buffer *cl_buffer = static_cast<cl::Buffer *>(buffer);
  cl_int status;
  void *mapped_ptr = CLRuntime::Global()->command_queue().enqueueMapBuffer(
      *cl_buffer,
      CL_TRUE,
      CL_MAP_READ | CL_MAP_WRITE,
      offset,
      size,
      nullptr,
      nullptr,
      &status);
  if (status != CL_SUCCESS) {
    mapped_ptr = nullptr;
  }
  CL_CHECK_FATAL(status);
  return mapped_ptr;
}

void *TargetWrapperCL::MapImage(void *image,
160 161 162 163
                                const size_t cl_image2d_width,
                                const size_t cl_image2d_height,
                                size_t cl_image2d_row_pitch,
                                size_t cl_image2d_slice_pitch) {
Y
Yan Chunwei 已提交
164
  cl::Image2D *cl_image = static_cast<cl::Image2D *>(image);
165 166
  cl::array<size_t, 3> origin = {0, 0, 0};
  cl::array<size_t, 3> region = {cl_image2d_width, cl_image2d_height, 1};
Y
Yan Chunwei 已提交
167 168 169 170 171 172 173
  cl_int status;
  void *mapped_ptr = CLRuntime::Global()->command_queue().enqueueMapImage(
      *cl_image,
      CL_TRUE,
      CL_MAP_READ | CL_MAP_WRITE,
      origin,
      region,
174 175
      &cl_image2d_row_pitch,
      &cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
      nullptr,
      nullptr,
      &status);
  if (status != CL_SUCCESS) {
    mapped_ptr = nullptr;
  }
  CL_CHECK_FATAL(status);
  return mapped_ptr;
}

void TargetWrapperCL::Unmap(void *cl_obj, void *mapped_ptr) {
  cl::Memory *mem_obj = static_cast<cl::Memory *>(cl_obj);
  cl_int status = CLRuntime::Global()->command_queue().enqueueUnmapMemObject(
      *mem_obj, mapped_ptr, nullptr, nullptr);
  CL_CHECK_FATAL(status);
}

void TargetWrapperCL::MemcpySync(void *dst,
                                 const void *src,
                                 size_t size,
                                 IoDirection dir) {
  cl_int status;
  auto stream = CLRuntime::Global()->command_queue();
  switch (dir) {
    case IoDirection::DtoD:
      status = stream.enqueueCopyBuffer(*static_cast<const cl::Buffer *>(src),
                                        *static_cast<cl::Buffer *>(dst),
                                        0,
                                        0,
                                        size,
                                        nullptr,
X
xiebaiyuan 已提交
207
                                        nullptr);
Y
Yan Chunwei 已提交
208
      CL_CHECK_FATAL(status);
X
xiebaiyuan 已提交
209
      CLRuntime::Global()->command_queue().finish();
Y
Yan Chunwei 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      break;
    case IoDirection::HtoD:
      status = stream.enqueueWriteBuffer(*static_cast<cl::Buffer *>(dst),
                                         CL_TRUE,
                                         0,
                                         size,
                                         src,
                                         nullptr,
                                         nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::DtoH:
      status = stream.enqueueReadBuffer(*static_cast<const cl::Buffer *>(src),
                                        CL_TRUE,
                                        0,
                                        size,
                                        dst,
                                        nullptr,
                                        nullptr);
      CL_CHECK_FATAL(status);
      break;
    default:
      LOG(FATAL) << "Unsupported IoDirection " << static_cast<int>(dir);
  }
}

void TargetWrapperCL::MemcpyAsync(void *dst,
                                  const void *src,
                                  size_t size,
                                  IoDirection dir,
                                  const stream_t &stream) {
  cl_int status;
  switch (dir) {
    case IoDirection::DtoD:
      status = stream.enqueueCopyBuffer(*static_cast<const cl::Buffer *>(src),
                                        *static_cast<cl::Buffer *>(dst),
                                        0,
                                        0,
                                        size,
                                        nullptr,
                                        nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::HtoD:
      status = stream.enqueueWriteBuffer(*static_cast<cl::Buffer *>(dst),
                                         CL_FALSE,
                                         0,
                                         size,
                                         src,
                                         nullptr,
                                         nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::DtoH:
      status = stream.enqueueReadBuffer(*static_cast<const cl::Buffer *>(src),
                                        CL_FALSE,
                                        0,
                                        size,
                                        dst,
                                        nullptr,
                                        nullptr);
      CL_CHECK_FATAL(status);
      break;
    default:
      LOG(FATAL) << "Unsupported IoDirection " << static_cast<int>(dir);
  }
}

void TargetWrapperCL::ImgcpySync(void *dst,
                                 const void *src,
280 281 282 283
                                 const size_t cl_image2d_width,
                                 const size_t cl_image2d_height,
                                 const size_t cl_image2d_row_pitch,
                                 const size_t cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
284
                                 IoDirection dir) {
285 286
  cl::array<size_t, 3> origin = {0, 0, 0};
  cl::array<size_t, 3> region = {cl_image2d_width, cl_image2d_height, 1};
Y
Yan Chunwei 已提交
287 288 289 290 291 292 293 294 295 296
  cl_int status;
  auto stream = CLRuntime::Global()->command_queue();
  switch (dir) {
    case IoDirection::DtoD:
      status = stream.enqueueCopyImage(*static_cast<const cl::Image2D *>(src),
                                       *static_cast<cl::Image2D *>(dst),
                                       origin,
                                       origin,
                                       region,
                                       nullptr,
X
xiebaiyuan 已提交
297
                                       nullptr);
Y
Yan Chunwei 已提交
298
      CL_CHECK_FATAL(status);
X
xiebaiyuan 已提交
299
      CLRuntime::Global()->command_queue().finish();
Y
Yan Chunwei 已提交
300 301 302 303 304 305
      break;
    case IoDirection::HtoD:
      status = stream.enqueueWriteImage(*static_cast<cl::Image2D *>(dst),
                                        CL_TRUE,
                                        origin,
                                        region,
306 307
                                        cl_image2d_row_pitch,
                                        cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
308 309 310 311 312 313 314 315 316 317
                                        src,
                                        nullptr,
                                        nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::DtoH:
      status = stream.enqueueReadImage(*static_cast<const cl::Image2D *>(src),
                                       CL_TRUE,
                                       origin,
                                       region,
318 319
                                       cl_image2d_row_pitch,
                                       cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
320 321 322 323 324 325 326 327 328 329 330 331
                                       dst,
                                       nullptr,
                                       nullptr);
      CL_CHECK_FATAL(status);
      break;
    default:
      LOG(FATAL) << "Unsupported IoDirection " << static_cast<int>(dir);
  }
}

void TargetWrapperCL::ImgcpyAsync(void *dst,
                                  const void *src,
332 333 334 335
                                  const size_t cl_image2d_width,
                                  const size_t cl_image2d_height,
                                  const size_t cl_image2d_row_pitch,
                                  const size_t cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
336 337
                                  IoDirection dir,
                                  const stream_t &stream) {
338 339
  cl::array<size_t, 3> origin = {0, 0, 0};
  cl::array<size_t, 3> region = {cl_image2d_width, cl_image2d_height, 1};
Y
Yan Chunwei 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  cl_int status;
  switch (dir) {
    case IoDirection::DtoD:
      status = stream.enqueueCopyImage(*static_cast<const cl::Image2D *>(src),
                                       *static_cast<cl::Image2D *>(dst),
                                       origin,
                                       origin,
                                       region,
                                       nullptr,
                                       nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::HtoD:
      status = stream.enqueueWriteImage(*static_cast<cl::Image2D *>(dst),
                                        CL_FALSE,
                                        origin,
                                        region,
357 358
                                        cl_image2d_row_pitch,
                                        cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
359 360 361 362 363 364 365 366 367 368
                                        src,
                                        nullptr,
                                        nullptr);
      CL_CHECK_FATAL(status);
      break;
    case IoDirection::DtoH:
      status = stream.enqueueReadImage(*static_cast<const cl::Image2D *>(src),
                                       CL_FALSE,
                                       origin,
                                       region,
369 370
                                       cl_image2d_row_pitch,
                                       cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
371 372 373 374 375 376 377 378 379 380 381 382
                                       dst,
                                       nullptr,
                                       nullptr);
      CL_CHECK_FATAL(status);
      break;
    default:
      LOG(FATAL) << "Unsupported IoDirection " << static_cast<int>(dir);
  }
}

}  // namespace lite
}  // namespace paddle