feed_kernel.cpp 3.1 KB
Newer Older
Y
yangfei 已提交
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/feed_kernel.h"
Y
yangfei 已提交
16
#include "framework/cl/cl_tensor.h"
H
hjchen2 已提交
17

Y
yangfei 已提交
18
namespace paddle_mobile {
L
liuruilong 已提交
19 20 21 22 23
namespace operators {

template <>
bool FeedKernel<GPU_CL, float>::Init(FeedParam<GPU_CL> *param) {
  DLOG << "Init feed";
24 25 26 27 28
  if (this->pre_post_type_ == UINT8_255) {
    this->cl_helper_.AddKernel("feed_with_pre", "feed_kernel.cl");
  } else {
    this->cl_helper_.AddKernel("feed", "feed_kernel.cl");
  }
L
liuruilong 已提交
29 30 31 32 33
  return true;
}

template <>
void FeedKernel<GPU_CL, float>::Compute(const FeedParam<GPU_CL> &param) {
H
hjchen2 已提交
34
  const int col = param.Col();
L
liuruilong 已提交
35
  auto kernel = this->cl_helper_.KernelAt(0);
Y
yangfei 已提交
36
  auto default_work_size = this->cl_helper_.DefaultWorkSize(*(param.Out()));
L
liuruilong 已提交
37 38
  cl_int status;
  auto output = param.Out();
H
hjchen2 已提交
39
  const Tensor *input = &param.InputX()->at(col);
L
liuruilong 已提交
40
  //  DLOG << *input;
41

Y
yangfei 已提交
42
  int numel = input->numel();
Y
yangfei 已提交
43 44 45 46 47 48 49
  cl_mem output_image = output->GetCLImage();
  const int out_C = output->dims()[1];
  const int out_H = output->dims()[2];
  const int out_W = output->dims()[3];
  const int Stride2 = out_C * out_H * out_W;
  const int Stride1 = out_H * out_W;
  const int Stride0 = out_W;
H
hjchen2 已提交
50 51
  framework::CLTensor input_cl_tensor(this->cl_helper_.CLContext(),
                                      this->cl_helper_.CLCommandQueue());
Y
yangfei 已提交
52
  input_cl_tensor.Resize(input->dims());
53 54 55 56 57 58 59 60
  cl_mem inputBuffer;
  if (this->pre_post_type_ == UINT8_255) {
    inputBuffer =
        input_cl_tensor.mutable_with_data<uint8_t>(input->data<uint8_t>());
  } else {
    inputBuffer =
        input_cl_tensor.mutable_with_data<float>(input->data<float>());
  }
L
liuruilong 已提交
61

Y
yangfei 已提交
62
  status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputBuffer);
L
liuruilong 已提交
63
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
64
  status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_image);
L
liuruilong 已提交
65
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
66
  status = clSetKernelArg(kernel, 2, sizeof(cl_int), &out_H);
Y
yangfei 已提交
67
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
68
  status = clSetKernelArg(kernel, 3, sizeof(cl_int), &out_W);
L
liuruilong 已提交
69
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
70 71 72 73 74 75 76
  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);
77
  CL_CHECK_ERRORS(status);
L
liuruilong 已提交
78

Y
yangfei 已提交
79 80 81
  status = clEnqueueNDRangeKernel(
      this->cl_helper_.CLCommandQueue(), kernel, default_work_size.size(), NULL,
      default_work_size.data(), NULL, 0, NULL, NULL);
Y
yangfei 已提交
82

L
liuruilong 已提交
83
  CL_CHECK_ERRORS(status);
L
liuruilong 已提交
84 85 86 87 88
}

template class FeedKernel<GPU_CL, float>;

}  // namespace operators
Y
yangfei 已提交
89
}  // namespace paddle_mobile