cl_image.cpp 5.6 KB
Newer Older
Y
yangfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

L
liuruilong 已提交
15
#include "framework/cl/cl_image.h"
Y
yangfei 已提交
16
#include "framework/cl/cl_tensor.h"
L
liuruilong 已提交
17

Y
yangfei 已提交
18
namespace paddle_mobile {
L
liuruilong 已提交
19
namespace framework {
L
liuruilong 已提交
20

Y
yangfei 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
void CLImageToTensor(CLImage *cl_image, Tensor *tensor, cl_context context,
                     cl_command_queue commandQueue, cl_kernel kernel) {
  tensor->mutable_data<float>();
  const auto &dim = cl_image->dims();
  size_t new_dims[] = {1, 1, 1, 1};
  for (int j = 0; j < dim.size(); ++j) {
    new_dims[4 - dim.size() + j] = dim[j];
  }
  size_t C, in_height, in_width;

  C = new_dims[1];
  in_height = new_dims[2];
  in_width = new_dims[3];

  CLTensor out_cl_tensor(context, commandQueue);
  out_cl_tensor.Resize(tensor->dims());
  cl_mem outBuffer = out_cl_tensor.mutable_data<float>();

  auto input_image = cl_image->GetCLImage();

41 42 43 44 45 46 47 48 49
  cl_int status;
  status = clSetKernelArg(kernel, 0, sizeof(int), &in_height);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 1, sizeof(int), &in_width);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 2, sizeof(cl_mem), &input_image);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 3, sizeof(cl_mem), &outBuffer);
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
50 51 52
  int size_ch = in_height * in_width;
  int size_block = size_ch * 4;
  int size_batch = size_ch * C;
53 54 55 56 57 58 59 60
  status = clSetKernelArg(kernel, 4, sizeof(int), &size_ch);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 5, sizeof(int), &size_block);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 6, sizeof(int), &size_batch);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 7, sizeof(int), &C);
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
61 62
  size_t global_work_size[3] = {(new_dims[1] + 3) / 4, new_dims[3],
                                new_dims[0] * new_dims[2]};
63 64 65
  status = clEnqueueNDRangeKernel(commandQueue, kernel, 3, NULL,
                                  global_work_size, NULL, 0, NULL, NULL);
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
66 67
  memcpy(tensor->data<float>(), out_cl_tensor.Data<float>(),
         tensor->memory_size());
L
liuruilong 已提交
68
}
L
liuruilong 已提交
69

Y
yangfei 已提交
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
void TensorToCLImage(Tensor *tensor, CLImage *cl_image, cl_context context,
                     cl_command_queue commandQueue, cl_kernel kernel) {
  const auto &dim = cl_image->dims();
  size_t new_dims[] = {1, 1, 1, 1};
  for (int j = 0; j < dim.size(); ++j) {
    new_dims[4 - dim.size() + j] = dim[j];
  }
  cl_int status;
  auto output = cl_image;
  const Tensor *input = tensor;
  const float *input_data = input->data<float>();
  auto output_image = output->GetCLImage();
  const int out_C = new_dims[1];
  const int out_H = new_dims[2];
  const int out_W = new_dims[3];
  const int Stride2 = out_C * out_H * out_W;
  const int Stride1 = out_H * out_W;
  const int Stride0 = out_W;
  DLOG << out_C;
  DLOG << out_H;
  DLOG << out_W;
  CLTensor input_cl_tensor(context, commandQueue);
  input_cl_tensor.Resize(input->dims());
  cl_mem inputBuffer = input_cl_tensor.mutable_with_data<float>(input_data);

  status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputBuffer);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_image);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 2, sizeof(cl_int), &out_H);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 3, sizeof(cl_int), &out_W);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 4, sizeof(cl_int), &out_C);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 5, sizeof(cl_int), &Stride0);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 6, sizeof(cl_int), &Stride1);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 7, sizeof(cl_int), &Stride2);
  CL_CHECK_ERRORS(status);

  size_t global_work_size[3] = {(new_dims[1] + 3) / 4, new_dims[3],
                                new_dims[0] * new_dims[2]};
  status = clEnqueueNDRangeKernel(commandQueue, kernel, 3, NULL,
                                  global_work_size, NULL, 0, NULL, NULL);

  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
118
}
L
liuruilong 已提交
119

Y
yangfei 已提交
120
#ifdef PADDLE_MOBILE_DEBUG
L
liuruilong 已提交
121
Print &operator<<(Print &printer, const CLImage &cl_image) {
122 123
  size_t width = cl_image.ImageDims()[0];
  size_t height = cl_image.ImageDims()[1];
L
liuruilong 已提交
124

L
liuruilong 已提交
125 126 127 128 129 130 131
  half_t *image_data = new half_t[height * width * 4];
  cl_int err;
  cl_mem image = cl_image.GetCLImage();
  size_t origin[3] = {0, 0, 0};
  size_t region[3] = {width, height, 1};
  err = clEnqueueReadImage(cl_image.CommandQueue(), image, CL_TRUE, origin,
                           region, 0, 0, image_data, 0, NULL, NULL);
Y
yangfei 已提交
132

L
liuruilong 已提交
133
  CL_CHECK_ERRORS(err);
Y
yangfei 已提交
134

135 136
  PADDLE_MOBILE_ENFORCE(cl_image.numel() != 0,
                        "cl_image numel should not be 0 ");
L
liuruilong 已提交
137 138 139 140 141 142
  float *tensor_data = new float[cl_image.numel()];
  auto converter = cl_image.Converter();
  converter->ImageToNCHW(image_data, tensor_data, cl_image.ImageDims(),
                         cl_image.dims());
  int stride = cl_image.numel() / 20;
  stride = stride > 0 ? stride : 1;
L
liuruilong 已提交
143

L
liuruilong 已提交
144
  printer << " dims: " << cl_image.dims() << "\n";
L
liuruilong 已提交
145
  for (int i = 0; i < cl_image.numel(); i += stride) {
L
liuruilong 已提交
146
    printer << tensor_data[i] << " ";
Y
yangfei 已提交
147
  }
L
liuruilong 已提交
148 149 150 151

  delete[](tensor_data);
  delete[](image_data);

Y
yangfei 已提交
152
  return printer;
L
liuruilong 已提交
153
}
Y
yangfei 已提交
154
#endif
L
liuruilong 已提交
155 156
}  // namespace framework
}  // namespace paddle_mobile