cl_image.h 4.5 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 20
#include <vector>

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

namespace paddle_mobile {
namespace framework {

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

  void Init(cl_context context, float *tensorInput, DDim ddim) {
32 33 34
    tensor_dims_ = ddim;
    cl_image_format cf = {.image_channel_order = CL_RGBA,
                          .image_channel_data_type = CL_HALF_FLOAT};
D
dolphin8 已提交
35
    // NCHW -> [W * (C+3)/4, H * N]
36 37 38 39 40
    DLOG << tensor_dims_;
    size_t N, C, H, W;
    if (tensor_dims_.size() == 4) {
      N = tensor_dims_[0];
      if (N < 0) {
Y
yangfei 已提交
41
        N = 1;
42 43 44 45
      }
      C = tensor_dims_[1];
      H = tensor_dims_[2];
      W = tensor_dims_[3];
L
liuruilong 已提交
46 47 48 49

      width_of_one_block_ = W;
      height_of_one_block_ = H;

50 51 52 53 54
    } else if (tensor_dims_.size() == 1) {
      N = 1;
      C = tensor_dims_[0];
      H = 1;
      W = 1;
L
liuruilong 已提交
55 56 57

      width_of_one_block_ = W;
      height_of_one_block_ = H;
Y
yangfei 已提交
58 59
    }

60 61
    DLOG << "-------InitMemory-------";

D
dolphin8 已提交
62 63
    size_t width = W * ((C + 3) / 4);
    size_t height = H * N;
L
liuruilong 已提交
64 65 66 67

    image_width_ = width;
    image_height_ = height;

D
dolphin8 已提交
68
    std::unique_ptr<half_t[]> imageData{};
69
    int count = 0;
D
dolphin8 已提交
70 71
    if (tensorInput != nullptr) {
      imageData.reset(new half_t[width * height * 4]);
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
      float *p = tensorInput;
      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 已提交
97
    }
98
    DLOG << "-------InitMemory-------";
D
dolphin8 已提交
99 100
    cl_int err;
    cl_image_ = clCreateImage2D(
101 102 103 104 105 106 107 108
        context,                                   // cl_context context
        CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,  // cl_mem_flags flags
        &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 已提交
109

D
dolphin8 已提交
110
    if (err != CL_SUCCESS) {
111
      // TODO(HaiPeng): error handling
L
liuruilong 已提交
112
      PADDLE_MOBILE_THROW_EXCEPTION(" create image 2d error ");
D
dolphin8 已提交
113
    }
L
liuruilong 已提交
114 115

    initialized_ = true;
L
liuruilong 已提交
116 117
  }

118
  void Init(cl_context context, DDim ddim) { Init(context, nullptr, ddim); }
L
liuruilong 已提交
119

L
liuruilong 已提交
120
  inline CLImage &Resize(const DDim &dims) {
121
    tensor_dims_ = dims;
L
liuruilong 已提交
122 123 124
    return *this;
  }

125
  const DDim &dims() const { return tensor_dims_; }
L
liuruilong 已提交
126

127
  cl_mem GetCLImage() const { return cl_image_; }
L
liuruilong 已提交
128

129 130 131
  template <typename T>
  T *data() const {
    return reinterpret_cast<T *>(tensor_input_);
L
liuruilong 已提交
132 133
  }

134 135
  inline int64_t numel() const { return product(tensor_dims_); }

L
liuruilong 已提交
136
  inline size_t ImageWidth() const { return image_width_; }
137

L
liuruilong 已提交
138
  inline size_t ImageHeight() const { return image_height_; }
139

L
liuruilong 已提交
140
  inline size_t CBlock() const { return c_block_; }
141

L
liuruilong 已提交
142
  inline size_t WidthOfOneBlock() const { return width_of_one_block_; }
143

L
liuruilong 已提交
144
  inline size_t HeightOfOneBlock() const { return height_of_one_block_; }
145

L
liuruilong 已提交
146
 private:
L
liuruilong 已提交
147
  bool initialized_ = false;
L
liuruilong 已提交
148
  cl_mem cl_image_;
L
liuruilong 已提交
149 150 151 152 153
  size_t image_width_;
  size_t width_of_one_block_;
  size_t height_of_one_block_;
  size_t image_height_;
  size_t c_block_;
154 155
  DDim tensor_dims_;
  float *tensor_input_;
L
liuruilong 已提交
156 157 158
  cl_context context_;
};

159
// void TensorToCLImage(Tensor *tensor, CLImage *image) {
L
liuruilong 已提交
160 161 162
//
//}
//
163
// void CLImageToTensor(CLImage *image, Tensor *tensor) {
L
liuruilong 已提交
164 165
//
//}
L
liuruilong 已提交
166

167 168
}  // namespace framework
}  // namespace paddle_mobile