cl_image.cpp 2.0 KB
Newer Older
Y
yangfei 已提交
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. */

L
liuruilong 已提交
15 16
#include "framework/cl/cl_image.h"

Y
yangfei 已提交
17
namespace paddle_mobile {
L
liuruilong 已提交
18
namespace framework {
L
liuruilong 已提交
19

L
liuruilong 已提交
20 21
void CLImageToTensor(CLImage *cl_image, Tensor *tensor,
                     cl_command_queue commandQueue) {
L
liuruilong 已提交
22
  // TODO(yangfei): need imp
L
liuruilong 已提交
23
}
L
liuruilong 已提交
24

L
liuruilong 已提交
25 26
void TensorToCLImage(const Tensor *tensor, CLImage *cl_image,
                     cl_command_queue commandQueue) {
L
liuruilong 已提交
27
  // TODO(yangfei): need imp
Y
yangfei 已提交
28
}
L
liuruilong 已提交
29

Y
yangfei 已提交
30
#ifdef PADDLE_MOBILE_DEBUG
L
liuruilong 已提交
31
Print &operator<<(Print &printer, const CLImage &cl_image) {
L
liuruilong 已提交
32 33
  int width = cl_image.ImageDims()[0];
  int height = cl_image.ImageDims()[1];
L
liuruilong 已提交
34

L
liuruilong 已提交
35 36 37 38 39 40 41
  half_t *image_data = new half_t[height * width * 4];
  cl_int err;
  cl_mem image = cl_image.GetCLImage();
  size_t origin[3] = {0, 0, 0};
  size_t region[3] = {width, height, 1};
  err = clEnqueueReadImage(cl_image.CommandQueue(), image, CL_TRUE, origin,
                           region, 0, 0, image_data, 0, NULL, NULL);
Y
yangfei 已提交
42

L
liuruilong 已提交
43
  CL_CHECK_ERRORS(err);
Y
yangfei 已提交
44

L
liuruilong 已提交
45 46 47 48 49 50
  float *tensor_data = new float[cl_image.numel()];
  auto converter = cl_image.Converter();
  converter->ImageToNCHW(image_data, tensor_data, cl_image.ImageDims(),
                         cl_image.dims());
  int stride = cl_image.numel() / 20;
  stride = stride > 0 ? stride : 1;
L
liuruilong 已提交
51

L
liuruilong 已提交
52
  printer << " dims: " << cl_image.dims() << "\n";
L
liuruilong 已提交
53
  for (int i = 0; i < cl_image.numel(); i += stride) {
L
liuruilong 已提交
54
    printer << tensor_data[i] << " ";
Y
yangfei 已提交
55
  }
L
liuruilong 已提交
56 57 58 59

  delete[](tensor_data);
  delete[](image_data);

Y
yangfei 已提交
60
  return printer;
L
liuruilong 已提交
61
}
Y
yangfei 已提交
62
#endif
L
liuruilong 已提交
63 64
}  // namespace framework
}  // namespace paddle_mobile