reshape_kernel.cpp 3.7 KB
Newer Older
D
dolphin8 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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. */

#include "operators/kernel/reshape_kernel.h"

namespace paddle_mobile {
namespace operators {

template <>
bool ReshapeKernel<GPU_CL, float>::Init(ReshapeParam<GPU_CL> *param) {
D
dolphin8 已提交
22
  this->cl_helper_.AddKernel("reshape", "reshape.cl");
D
dolphin8 已提交
23 24 25 26
  return true;
}

template <>
D
dolphin8 已提交
27
void ReshapeKernel<GPU_CL, float>::Compute(const ReshapeParam<GPU_CL> &param) {
L
liuruilong 已提交
28
  auto kernel = this->cl_helper_.KernelAt(0);
Y
yangfei 已提交
29
  auto default_work_size = this->cl_helper_.DefaultWorkSize(*param.Out());
L
liuruilong 已提交
30 31
  const auto *input = param.InputX();
  auto *output = param.Out();
Y
yangfei 已提交
32 33
  auto input_image = input->GetCLImage();
  auto output_image = output->GetCLImage();
L
liuruilong 已提交
34 35
  const auto &inputDim = input->dims();
  const auto &outputDim = output->dims();
Y
yangfei 已提交
36 37
  int input_dims[4] = {1, 1, 1, 1};
  int output_dims[4] = {1, 1, 1, 1};
L
liuruilong 已提交
38
  // 1 1000 1 1
D
dolphin8 已提交
39
  for (int i = 0; i < inputDim.size(); i++) {
Y
yangfei 已提交
40
    input_dims[4 - inputDim.size() + i] = inputDim[i];
D
dolphin8 已提交
41
  }
L
liuruilong 已提交
42 43

  // 1 1 1 1000
D
dolphin8 已提交
44
  for (int i = 0; i < outputDim.size(); i++) {
Y
yangfei 已提交
45
    output_dims[4 - outputDim.size() + i] = outputDim[i];
D
dolphin8 已提交
46
  }
D
dolphin8 已提交
47

Y
yangfei 已提交
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  int out_C = output_dims[1];
  int out_H = output_dims[2];
  int out_W = output_dims[3];
  int in_W = input_dims[3];
  int in_H = input_dims[2];
  int in_Stride0 = in_W;
  int in_Stride1 = input_dims[2] * input_dims[3];
  int in_Stride2 = input_dims[1] * input_dims[2] * input_dims[3];
  int out_Stride0 = out_W;
  int out_Stride1 = out_H * out_W;
  int out_Stride2 = out_C * out_H * out_W;
  DLOG << "out_C=" << out_C;
  DLOG << "out_H=" << out_H;
  DLOG << "out_W=" << out_W;
  DLOG << "in_W=" << in_W;
  DLOG << "default_work_size=" << default_work_size;
  DLOG << "in_Stride0=" << in_Stride0;
  DLOG << "in_Stride1=" << in_Stride1;
  DLOG << "out_Stride0=" << out_Stride0;
  DLOG << "out_Stride1=" << out_Stride1;
  cl_int status;
  status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input_image);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_image);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 2, sizeof(int), &out_C);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 3, sizeof(int), &out_H);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 4, sizeof(int), &out_W);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 5, sizeof(int), &in_W);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 6, sizeof(int), &in_H);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 7, sizeof(int), &in_Stride0);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 8, sizeof(int), &in_Stride1);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 9, sizeof(int), &in_Stride2);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 10, sizeof(int), &out_Stride0);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 11, sizeof(int), &out_Stride1);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 12, sizeof(int), &out_Stride2);
  CL_CHECK_ERRORS(status);
  status = clEnqueueNDRangeKernel(
      this->cl_helper_.CLCommandQueue(), kernel, default_work_size.size(), NULL,
      default_work_size.data(), NULL, 0, NULL, NULL);
  CL_CHECK_ERRORS(status);
D
dolphin8 已提交
99
}
D
dolphin8 已提交
100 101 102 103 104

template class ReshapeKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile