fetch_kernel.cpp 3.9 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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/fetch_kernel.h"
Z
zhaojiaying01 已提交
16
#include "framework/cl/cl_tensor.h"
Z
zhaojiaying01 已提交
17 18 19 20 21 22

namespace paddle_mobile {
namespace operators {

template <>
bool FetchKernel<GPU_CL, float>::Init(FetchParam<GPU_CL> *param) {
23 24 25 26 27
  if (this->pre_post_type_ == UINT8_255) {
    this->cl_helper_.AddKernel("fetch_with_post", "fetch_kernel.cl");
  } else {
    this->cl_helper_.AddKernel("fetch", "fetch_kernel.cl");
  }
Z
zhaojiaying01 已提交
28 29 30 31
  return true;
}

template <>
32
void FetchKernel<GPU_CL, float>::Compute(const FetchParam<GPU_CL> &param) {
Z
zhaojiaying01 已提交
33 34 35
  auto kernel = this->cl_helper_.KernelAt(0);
  auto default_work_size = this->cl_helper_.DefaultWorkSize(*param.InputX());

H
hjchen2 已提交
36
  const int col = param.Col();
Z
zhaojiaying01 已提交
37
  auto input = param.InputX()->GetCLImage();
H
hjchen2 已提交
38
  auto *out = &param.Out()->at(col);
Y
yangfei 已提交
39
  out->Resize(param.InputX()->dims());
40 41 42 43 44

  DLOG << "fetch kernel out dims = " << out->dims();
  DLOG << "fetch kernel out memory size = " << out->memory_size();

  auto dim = param.InputX()->dims();
Z
zhaojiaying01 已提交
45 46 47 48 49 50
  size_t new_dims[] = {1, 1, 1, 1};

  for (int j = 0; j < dim.size(); ++j) {
    new_dims[4 - dim.size() + j] = dim[j];
  }

51
  size_t in_ch, in_height, in_width;
Z
zhaojiaying01 已提交
52

53
  in_ch = new_dims[1];
Z
zhaojiaying01 已提交
54
  in_height = new_dims[2];
Y
yangfei 已提交
55
  in_width = new_dims[3];
56 57 58
  int size_ch = in_height * in_width;
  int size_block = size_ch * 4;
  int size_batch = size_ch * in_ch;
Z
zhaojiaying01 已提交
59

60 61
  framework::CLTensor out_cl_tensor(this->cl_helper_.CLContext(),
                                    this->cl_helper_.CLCommandQueue());
Z
zhaojiaying01 已提交
62
  out_cl_tensor.Resize(out->dims());
63 64 65 66 67 68 69 70
  cl_mem outBuffer;
  if (this->pre_post_type_ == UINT8_255) {
    out->mutable_data<uint8_t>();
    outBuffer = out_cl_tensor.mutable_data<uint8_t>();
  } else {
    out->mutable_data<float>();
    outBuffer = out_cl_tensor.mutable_data<float>();
  }
Z
zhaojiaying01 已提交
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  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);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 3, sizeof(cl_mem), &outBuffer);
  CL_CHECK_ERRORS(status);
  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), &in_ch);
  CL_CHECK_ERRORS(status);
Z
zhaojiaying01 已提交
89

L
liuruilong 已提交
90
  //  cl_event wait_event = param.InpdutX()->GetClEvent();
91 92 93 94
  status =
      clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 3, NULL,
                             default_work_size.data(), NULL, 0, NULL, NULL);
  CL_CHECK_ERRORS(status);
L
liuruilong 已提交
95

L
liuruilong 已提交
96
  clFinish(this->cl_helper_.CLCommandQueue());
Z
zhaojiaying01 已提交
97

98 99
  DLOG << "fetch kernel out dims = " << out->dims();
  DLOG << "fetch kernel out memory size = " << out->memory_size();
L
liuruilong 已提交
100

101 102 103
  DLOG << "fetch kernel out_cl_tensor dims = " << out_cl_tensor.dims();
  DLOG << "fetch kernel out_cl_tensor memery size = "
       << out_cl_tensor.memory_size();
104 105 106 107 108 109 110
  if (this->pre_post_type_ == UINT8_255) {
    memcpy(out->data<uint8_t>(), out_cl_tensor.Data<uint8_t>(),
           sizeof(uint8_t) * out->numel());
  } else {
    memcpy(out->data<float>(), out_cl_tensor.Data<float>(),
           sizeof(float) * out->numel());
  }
111
}
Z
zhaojiaying01 已提交
112 113 114 115 116

template class FetchKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile