cl_image.h 6.3 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 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. */

#pragma once

17 18 19
#include <vector>

#include "CL/cl.h"
L
liuruilong 已提交
20

21
#include "framework/cl/cl_half.h"
L
liuruilong 已提交
22
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
23 24 25 26 27 28 29 30
#include "framework/ddim.h"
#include "framework/tensor.h"

namespace paddle_mobile {
namespace framework {

class CLImage {
 public:
L
liuruilong 已提交
31 32
  CLImage() = default;

L
liuruilong 已提交
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
  /*
   * will not hold input tensor data, memcpy in this method
   * */
  void SetTensorData(float *tensorData, const DDim &dim) {
    int numel = product(dim);
    if (tensor_data_ != nullptr) {
      delete[](tensor_data_);
    }
    tensor_data_ = new float[numel];
    memcpy(tensor_data_, tensorData, numel);
    tensor_dims_ = dim;
  }

  /*
   * need call SetTensorData first
   * */
  void InitCLImage(cl_context context) {
    if (tensor_data_ == nullptr) {
      PADDLE_MOBILE_THROW_EXCEPTION(" need call SetTensorData first");
    }
    InitCLImage(context, tensor_data_, tensor_dims_);
    delete[](tensor_data_);
    tensor_data_ = nullptr;
    initialized_ = true;
  }

  void InitEmptyImage(cl_context context, const DDim &dim) {
    if (tensor_data_ != nullptr) {
      PADDLE_MOBILE_THROW_EXCEPTION(
          " empty image tensor data shouldn't have value");
    }
L
liuruilong 已提交
64
    DLOG << " init empty image ";
L
liuruilong 已提交
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
    InitCLImage(context, nullptr, dim);
    initialized_ = true;
  }

  cl_mem GetCLImage() const { return cl_image_; }

  const DDim &ImageDims() { return image_dims_; }

  inline size_t ImageWidth() const { return image_width_; }

  inline size_t ImageHeight() const { return image_height_; }

  /*
   * block of channels, 4 channel one block
   * */
  inline size_t CBlock() const { return c_block_; }

  /*
   *  width of original tensor
   * */
  inline size_t WidthOfOneBlock() const { return width_of_one_block_; }

  /*
   *  height of original tensor
   * */
  inline size_t HeightOfOneBlock() const { return height_of_one_block_; }

  /*
   *  resize original tensor dim
   * */
  inline CLImage &Resize(const DDim &dims) {
    tensor_dims_ = dims;
    return *this;
  }

  template <typename T>
  T *data() const {
    if (initialized_) {
      PADDLE_MOBILE_THROW_EXCEPTION(
L
liuruilong 已提交
104 105
          " cl image has initialized, tensor data has been deleted, can't use "
          "tensor data");
L
liuruilong 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }
    return reinterpret_cast<T *>(tensor_data_);
  }

  /*
   *  numel of tensor dim
   * */
  inline int64_t numel() const { return product(tensor_dims_); }

  /*
   *  original tensor dim
   * */
  const DDim &dims() const { return tensor_dims_; }

 private:
  void InitCLImage(cl_context context, float *tensor_data, const DDim &dim) {
L
liuruilong 已提交
122
    DLOG << " tensor dim: " << dim;
123 124
    cl_image_format cf = {.image_channel_order = CL_RGBA,
                          .image_channel_data_type = CL_HALF_FLOAT};
D
dolphin8 已提交
125
    // NCHW -> [W * (C+3)/4, H * N]
Y
yangfei 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    tensor_dims_ = dim;
    if (tensor_data) {
      tensor_data_ = tensor_data;
    } else {
      int numel = 1;
      for (int i = 0; i < dim.size(); i++) {
        numel *= dim[i];
      }
      tensor_data_ = static_cast<float *>(
          paddle_mobile::memory::Alloc(sizeof(float) * numel));
      for (int i = 0; i < numel; i++) {
        tensor_data_[i] = 0;
      }
    }
L
liuruilong 已提交
140

L
liuruilong 已提交
141
    size_t new_dims[] = {1, 1, 1, 1};
L
liuruilong 已提交
142

L
liuruilong 已提交
143 144
    for (int j = 0; j < dim.size(); ++j) {
      new_dims[4 - dim.size() + j] = dim[j];
Y
yangfei 已提交
145 146
    }

L
liuruilong 已提交
147 148 149 150 151 152 153 154 155 156
    size_t N, C, H, W;

    N = new_dims[0];
    C = new_dims[1];
    H = new_dims[2];
    W = new_dims[3];

    width_of_one_block_ = W;
    height_of_one_block_ = H;

D
dolphin8 已提交
157 158
    size_t width = W * ((C + 3) / 4);
    size_t height = H * N;
L
liuruilong 已提交
159 160 161

    image_width_ = width;
    image_height_ = height;
L
liuruilong 已提交
162
    image_dims_ = make_ddim({image_width_, image_height_});
L
liuruilong 已提交
163

D
dolphin8 已提交
164
    std::unique_ptr<half_t[]> imageData{};
165
    int count = 0;
L
liuruilong 已提交
166
    if (tensor_data != nullptr) {
D
dolphin8 已提交
167
      imageData.reset(new half_t[width * height * 4]);
L
liuruilong 已提交
168
      float *p = tensor_data;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
      size_t i0 = 0;
      for (int n = 0; n < N; n++) {
        for (int c = 0; c < C; c++) {
          size_t i1 = i0;
          for (int h = 0; h < H; h++) {
            size_t i2 = (i1 << 2) + c % 4;
            for (int w = 0; w < W; w++) {
              if (i2 >= width * height * 4) {
                printf("%d > %d ----> %d, %d, %d, %d --- %d, %d, %d\n", i2,
                       width * height * 4, n, c, h, w, i0, i1, i2);
              }
              assert(i2 < width * height * 4);

              imageData[i2] = float2half(*p);
              i2 += 4;
              p++;
              //              count++;
              //              DLOG<<count;
            }
            i1 += width;
          }
        }
        i0 += width * H;
      }
D
dolphin8 已提交
193 194
    }
    cl_int err;
L
liuruilong 已提交
195 196
    DLOG << " image width: " << width;
    DLOG << " image height: " << height;
D
dolphin8 已提交
197
    cl_image_ = clCreateImage2D(
L
liuruilong 已提交
198 199 200
        context,  // cl_context context
        CL_MEM_READ_WRITE |
            (imageData ? CL_MEM_COPY_HOST_PTR : 0),  // cl_mem_flags flags
201 202 203 204 205 206
        &cf,     // const cl_image_format *image_format
        width,   // size_t image_width
        height,  // size_t image_height
        0,       // size_t image_row_pitch
        reinterpret_cast<void *>(imageData.get()),  // void *host_ptr
        &err);
L
liuruilong 已提交
207

D
dolphin8 已提交
208
    if (err != CL_SUCCESS) {
209
      // TODO(HaiPeng): error handling
L
liuruilong 已提交
210
      CL_CHECK_ERRORS(err);
L
liuruilong 已提交
211
      PADDLE_MOBILE_THROW_EXCEPTION(" create image 2d error ");
D
dolphin8 已提交
212
    }
L
liuruilong 已提交
213 214
  }

L
liuruilong 已提交
215
  bool initialized_ = false;
L
liuruilong 已提交
216
  cl_mem cl_image_;
L
liuruilong 已提交
217 218 219 220 221
  size_t image_width_;
  size_t width_of_one_block_;
  size_t height_of_one_block_;
  size_t image_height_;
  size_t c_block_;
222
  DDim tensor_dims_;
L
liuruilong 已提交
223 224
  DDim image_dims_;
  float *tensor_data_;
L
liuruilong 已提交
225 226 227
  cl_context context_;
};

L
liuruilong 已提交
228 229
void TensorToCLImage(Tensor *tensor, CLImage *image,
                     cl_command_queue commandQueue);
Y
yangfei 已提交
230

L
liuruilong 已提交
231 232
void CLImageToTensor(CLImage *image, Tensor *tensor,
                     cl_command_queue commandQueue);
L
liuruilong 已提交
233

L
liuruilong 已提交
234 235 236 237
#ifdef PADDLE_MOBILE_DEBUG
Print &operator<<(Print &printer, const CLImage &image);
#endif

238 239
}  // namespace framework
}  // namespace paddle_mobile