reshape_kernel.cpp 2.3 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 29 30
  auto kernel = this->cl_helper_.KernelAt(0);
  const auto *input = param.InputX();
  auto *output = param.Out();
D
dolphin8 已提交
31 32 33 34
  auto inputImage = input->GetCLImage();
  auto outputImage = output->GetCLImage();
  clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputImage);
  clSetKernelArg(kernel, 1, sizeof(cl_mem), &outputImage);
L
liuruilong 已提交
35 36
  const auto &inputDim = input->dims();
  const auto &outputDim = output->dims();
D
dolphin8 已提交
37 38 39 40 41 42 43 44
  int dims[4] = {1, 1, 1, 1};
  int odims[4] = {1, 1, 1, 1};
  for (int i = 0; i < inputDim.size(); i++) {
    dims[4-inputDim.size()+i] = inputDim[i];
  }
  for (int i = 0; i < outputDim.size(); i++) {
    odims[4-outputDim.size()+i] = outputDim[i];
  }
D
dolphin8 已提交
45
  clSetKernelArg(kernel, 2, sizeof(int), dims);
L
liuruilong 已提交
46 47 48
  clSetKernelArg(kernel, 3, sizeof(int), dims + 1);
  clSetKernelArg(kernel, 4, sizeof(int), dims + 2);
  clSetKernelArg(kernel, 5, sizeof(int), dims + 3);
D
dolphin8 已提交
49
  clSetKernelArg(kernel, 6, sizeof(int), odims);
L
liuruilong 已提交
50 51 52 53
  clSetKernelArg(kernel, 7, sizeof(int), odims + 1);
  clSetKernelArg(kernel, 8, sizeof(int), odims + 2);
  clSetKernelArg(kernel, 9, sizeof(int), odims + 3);
  const size_t work_size[2] = {output->ImageWidth(), output->ImageHeight()};
D
dolphin8 已提交
54 55 56 57

  clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 2, NULL,
                         work_size, NULL, 0, NULL, NULL);
}
D
dolphin8 已提交
58 59 60 61 62

template class ReshapeKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile