fetch_kernel.cpp 3.3 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"
L
liuruilong 已提交
17 18
// #include "common/common.h"
// #include <iostream>
Z
zhaojiaying01 已提交
19 20 21 22 23 24

namespace paddle_mobile {
namespace operators {

template <>
bool FetchKernel<GPU_CL, float>::Init(FetchParam<GPU_CL> *param) {
Z
zhaojiaying01 已提交
25 26 27 28 29
  if (param->InputX()->dims().size() <= 2) {
    this->cl_helper_.AddKernel("fetch_2d", "fetch_kernel.cl");
  } else {
    this->cl_helper_.AddKernel("fetch", "fetch_kernel.cl");
  }
Z
zhaojiaying01 已提交
30 31 32 33
  return true;
}

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

  auto input = param.InputX()->GetCLImage();
  auto *out = param.Out();
40
  out->mutable_data<float>();
Z
zhaojiaying01 已提交
41 42 43 44 45 46 47
  const auto &dim = param.InputX()->dims();
  size_t new_dims[] = {1, 1, 1, 1};

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

Z
zhaojiaying01 已提交
48
  size_t C, in_height, in_width;
Z
zhaojiaying01 已提交
49 50 51

  C = new_dims[1];
  in_height = new_dims[2];
Z
zhaojiaying01 已提交
52 53 54 55 56
  if (dim.size() <= 2) {
    in_width = param.InputX()->ImageWidth();
  } else {
    in_width = new_dims[3];
  }
Z
zhaojiaying01 已提交
57 58 59 60 61 62 63 64

  CLTensor out_cl_tensor(this->cl_helper_.CLContext(),
                         this->cl_helper_.CLCommandQueue());
  out_cl_tensor.Resize(out->dims());
  cl_mem outBuffer = out_cl_tensor.mutable_data<float>();

  clSetKernelArg(kernel, 0, sizeof(int), &in_height);
  clSetKernelArg(kernel, 1, sizeof(int), &in_width);
Z
zhaojiaying01 已提交
65 66 67 68 69 70
  clSetKernelArg(kernel, 2, sizeof(cl_mem), &input);
  clSetKernelArg(kernel, 3, sizeof(cl_mem), &outBuffer);
  if (dim.size() > 2) {
    int size_ch = in_height * in_width;
    int size_block = size_ch * 4;
    int size_batch = size_ch * C;
71
    int out_c = new_dims[1];
Z
zhaojiaying01 已提交
72 73 74
    clSetKernelArg(kernel, 4, sizeof(int), &size_ch);
    clSetKernelArg(kernel, 5, sizeof(int), &size_block);
    clSetKernelArg(kernel, 6, sizeof(int), &size_batch);
75
    clSetKernelArg(kernel, 7, sizeof(int), &out_c);
Z
zhaojiaying01 已提交
76
  }
Z
zhaojiaying01 已提交
77

L
liuruilong 已提交
78
  //  cl_event wait_event = param.InpdutX()->GetClEvent();
Z
zhaojiaying01 已提交
79
  clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 3, NULL,
L
liuruilong 已提交
80 81
                         default_work_size.data(), NULL, 0, NULL, NULL);

L
liuruilong 已提交
82
  //  auto time1 = paddle_mobile::time();
L
liuruilong 已提交
83

L
liuruilong 已提交
84 85
  //  printf(" before finish \n");
  //  clFlsh(this->cl_helper_.CLCommandQueue());
L
liuruilong 已提交
86
  clFinish(this->cl_helper_.CLCommandQueue());
L
liuruilong 已提交
87
  //  printf(" after finish \n");
Z
zhaojiaying01 已提交
88

L
liuruilong 已提交
89 90 91 92 93
  //  auto time2 = paddle_mobile::time();
  //
  //
  //  std::cout << " finish  cost :" << paddle_mobile::time_diff(time1, time2)
  //            << "ms" << std::endl;
L
liuruilong 已提交
94

Z
zhaojiaying01 已提交
95
  memcpy(out->data<float>(), out_cl_tensor.Data<float>(), out->memory_size());
96 97
  DLOG << *param.InputX();
  DLOG << *out;
98
}
Z
zhaojiaying01 已提交
99 100 101 102 103

template class FetchKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile