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
  int dims[4] = {1, 1, 1, 1};
  int odims[4] = {1, 1, 1, 1};
L
liuruilong 已提交
39
  // 1 1000 1 1
D
dolphin8 已提交
40
  for (int i = 0; i < inputDim.size(); i++) {
L
liuruilong 已提交
41
    dims[4 - inputDim.size() + i] = inputDim[i];
D
dolphin8 已提交
42
  }
L
liuruilong 已提交
43 44

  // 1 1 1 1000
D
dolphin8 已提交
45
  for (int i = 0; i < outputDim.size(); i++) {
L
liuruilong 已提交
46
    odims[4 - outputDim.size() + i] = outputDim[i];
D
dolphin8 已提交
47
  }
Y
yangfei 已提交
48 49 50 51 52 53 54 55
  clSetKernelArg(kernel, 2, sizeof(cl_int), &dims);
  clSetKernelArg(kernel, 3, sizeof(cl_int), &dims[1]);
  clSetKernelArg(kernel, 4, sizeof(cl_int), &dims[2]);
  clSetKernelArg(kernel, 5, sizeof(cl_int), &dims[3]);
  clSetKernelArg(kernel, 6, sizeof(cl_int), &odims);
  clSetKernelArg(kernel, 7, sizeof(cl_int), &odims[1]);
  clSetKernelArg(kernel, 8, sizeof(cl_int), &odims[1]);
  clSetKernelArg(kernel, 9, sizeof(cl_int), &odims[1]);
L
liuruilong 已提交
56
  const size_t work_size[2] = {output->ImageWidth(), output->ImageHeight()};
D
dolphin8 已提交
57 58 59 60

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

template class ReshapeKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile