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

#include "framework/ddim.h"
#include "framework/tensor.h"
L
liuruilong 已提交
19
#include "CL/cl.h"
D
dolphin8 已提交
20
#include "cl_half.h"
L
liuruilong 已提交
21 22 23 24 25 26

namespace paddle_mobile {
namespace framework {

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

  void Init(cl_context context, float *tensorInput, DDim ddim) {
D
dolphin8 已提交
30 31 32 33 34 35 36 37 38 39 40
    cl_image_format cf = {
      .image_channel_order = CL_RGBA,
      .image_channel_data_type = CL_HALF_FLOAT
    };
    // NCHW -> [W * (C+3)/4, H * N]
    size_t N = tensorDims_[0];
    size_t C = tensorDims_[1];
    size_t H = tensorDims_[2];
    size_t W = tensorDims_[3];
    size_t width = W * ((C + 3) / 4);
    size_t height = H * N;
D
dolphin8 已提交
41
    std::unique_ptr<half_t[]> imageData{};
D
dolphin8 已提交
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
    if (tensorInput != nullptr) {
      imageData.reset(new half_t[width * height * 4]);
      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++) {
              imageData[i2] = float2half(*p);
              i2 += 4;
              p++;
            }
            i1 += width;
          }
        }
        i0 += width * H;
      }
    }
    cl_int err;
    cl_image_ = clCreateImage2D(
      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 // cl_int *errcode_ret
    );
    if (err != CL_SUCCESS) {
      // TODO: error handling
    }
L
liuruilong 已提交
76 77 78
  }

  void Init(cl_context context, DDim ddim) {
D
dolphin8 已提交
79
    Init(context, nullptr, ddim);
L
liuruilong 已提交
80 81
  }

L
liuruilong 已提交
82 83 84 85 86 87
  inline CLImage &Resize(const DDim &dims) {
    tensorDims_ = dims;
    return *this;
  }

  const DDim &dims() const {
Y
yangfei 已提交
88
    return tensorDims_;
L
liuruilong 已提交
89
  }
L
liuruilong 已提交
90

L
liuruilong 已提交
91 92 93 94
  std::vector<size_t> DefaultWorkSize() {
    return {};
  }

L
liuruilong 已提交
95 96 97 98
  cl_mem GetCLImage() {
    return cl_image_;
  }

L
liuruilong 已提交
99
 private:
L
liuruilong 已提交
100
  bool initialized_ = false;
L
liuruilong 已提交
101 102 103 104 105
  cl_mem cl_image_;
  DDim tensorDims_;
  cl_context context_;
};

L
liuruilong 已提交
106 107 108 109 110 111 112
//void TensorToCLImage(Tensor *tensor, CLImage *image) {
//
//}
//
//void CLImageToTensor(CLImage *image, Tensor *tensor) {
//
//}
L
liuruilong 已提交
113 114 115

}
}