feed_kernel.cpp 2.7 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"
Y
yangfei 已提交
17
namespace paddle_mobile {
L
liuruilong 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31
namespace operators {

template <>
bool FeedKernel<GPU_CL, float>::Init(FeedParam<GPU_CL> *param) {
  DLOG << "Init feed";
  this->cl_helper_.AddKernel("feed", "feed_kernel.cl");
  return true;
}

template <>
void FeedKernel<GPU_CL, float>::Compute(const FeedParam<GPU_CL> &param) {
  auto kernel = this->cl_helper_.KernelAt(0);
  cl_int status;
  auto output = param.Out();
Y
yangfei 已提交
32
  const Tensor *input = param.InputX();
Y
yangfei 已提交
33 34 35
  const float *input_data = input->data<float>();
  int numel = input->numel();
  DLOG << "numel = " << numel;
L
liuruilong 已提交
36 37 38
  cl_mem cl_image = output->GetCLImage();
  int height = output->dims()[2];
  int width = output->dims()[3];
Y
yangfei 已提交
39
  DLOG << output->dims();
Y
yangfei 已提交
40 41 42 43 44
  CLTensor input_cl_tensor(this->cl_helper_.CLContext());
  input_cl_tensor.Resize(input->dims());
  cl_mem *inputBuffer =
      input_cl_tensor.mutable_with_data<cl_mem>((void *)input_data);
  DLOG << "yangfei";
L
liuruilong 已提交
45

Y
yangfei 已提交
46
  status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);
L
liuruilong 已提交
47
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
48
  status = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&cl_image);
L
liuruilong 已提交
49
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
50 51 52
  status = clSetKernelArg(kernel, 2, sizeof(cl_int), (void *)&width);
  CL_CHECK_ERRORS(status);
  status = clSetKernelArg(kernel, 3, sizeof(cl_int), (void *)&height);
L
liuruilong 已提交
53
  CL_CHECK_ERRORS(status);
L
liuruilong 已提交
54 55

  size_t global_work_size[2] = {height, width};
Y
yangfei 已提交
56
  DLOG << "yangfei";
L
liuruilong 已提交
57 58
  status = clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 2,
                                  NULL, global_work_size, NULL, 0, NULL, NULL);
L
liuruilong 已提交
59
  CL_CHECK_ERRORS(status);
Y
yangfei 已提交
60 61 62 63 64 65 66 67 68 69 70

  int len = 4 * 224 * 224;
  half *out = new half[len];
  DLOG << "yangfei";
  cl_command_queue commandQueue = this->cl_helper_.CLCommandQueue();
  size_t origin[3] = {0, 0, 0};
  size_t region[3] = {height, width, 1};
  clEnqueueReadImage(commandQueue, cl_image, CL_TRUE, origin, region, 0, 0, out,
                     0, NULL, NULL);
  DLOG << "yangfei";
  for (int i = 0; i < 100; i++) DLOG << out[i];
L
liuruilong 已提交
71 72 73 74 75
}

template class FeedKernel<GPU_CL, float>;

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