cl_caller.cc 1.9 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "lite/backends/opencl/cl_caller.h"
Y
Yan Chunwei 已提交
16
#include <string>
17 18 19 20
#include "lite/backends/opencl/cl_context.h"
#include "lite/backends/opencl/cl_image.h"
#include "lite/backends/opencl/cl_runtime.h"
#include "lite/backends/opencl/cl_utility.h"
Y
Yan Chunwei 已提交
21 22 23 24 25
#include "lite/core/tensor.h"
#include "lite/utils/string.h"

namespace paddle {
namespace lite {
26

Y
Yan Chunwei 已提交
27 28 29 30 31 32 33 34
static void CopyImageData(CLContext* context,
                          const CLImage& cl_image,
                          float* out) {
  int width = cl_image.image_dims()[0];
  int height = cl_image.image_dims()[1];

  float* image_data = new float[height * width * 4];
  cl::Image* image = cl_image.cl_image();
35 36
  cl::array<size_t, 3> origin = {0, 0, 0};
  cl::array<size_t, 3> region = {
Y
Yan Chunwei 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      static_cast<size_t>(width), static_cast<size_t>(height), 1};
  cl_int err = context->GetCommandQueue().enqueueReadImage(
      *image, CL_TRUE, origin, region, 0, 0, image_data, nullptr, nullptr);
  CL_CHECK_FATAL(err);

  auto* converter = cl_image.image_converter();
  converter->ImageToNCHW(
      image_data, out, cl_image.image_dims(), cl_image.tensor_dims());

  delete[] image_data;
}

bool InitOpenCLRuntime(std::string cl_path) {
  auto* runtime = CLRuntime::Global();
  runtime->set_cl_path(cl_path);
  return runtime->IsInitSuccess();
}

}  // namespace lite
}  // namespace paddle