cl_engine.cpp 3.5 KB
Newer Older
L
liuruilong 已提交
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 "framework/cl/cl_engine.h"
L
liuruilong 已提交
16
#include "CL/cl.h"
L
liuruilong 已提交
17
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
18 19 20 21 22 23 24 25

#include <cstdlib>
#include <cstring>

namespace paddle_mobile {
namespace framework {

bool CLEngine::Init() {
L
liuruilong 已提交
26 27 28
  if (initialized_) {
    return true;
  }
L
liuruilong 已提交
29
  cl_int status;
L
liuruilong 已提交
30 31
  SetPlatform();
  SetClDeviceId();
L
liuruilong 已提交
32

L
liuruilong 已提交
33 34
  initialized_ = true;
  return initialized_;
35 36 37 38
  //  setClCommandQueue();
  //  std::string filename = "./HelloWorld_Kernel.cl";
  //  loadKernelFromFile(filename.c_str());
  //  buildProgram();
L
liuruilong 已提交
39 40 41 42
}

CLEngine *CLEngine::Instance() {
  static CLEngine cl_engine_;
L
liuruilong 已提交
43
  cl_engine_.Init();
L
liuruilong 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  return &cl_engine_;
}

bool CLEngine::SetPlatform() {
  platform_ = NULL;      // the chosen platform
  cl_uint numPlatforms;  // the NO. of platforms
  cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms);

  /**For clarity, choose the first available platform. */
  if (numPlatforms > 0) {
    cl_platform_id *platforms = reinterpret_cast<cl_platform_id *>(
        malloc(numPlatforms * sizeof(cl_platform_id)));
    status = clGetPlatformIDs(numPlatforms, platforms, NULL);
    platform_ = platforms[0];
    free(platforms);
    return true;
  } else {
    return false;
  }
}

bool CLEngine::SetClDeviceId() {
  cl_uint numDevices = 0;
  devices_ = NULL;
  cl_int status =
      clGetDeviceIDs(platform_, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);

  if (numDevices > 0) {
L
liuruilong 已提交
72
    devices_ = reinterpret_cast<cl_device_id *>(
L
liuruilong 已提交
73 74 75 76 77 78 79 80
        malloc(numDevices * sizeof(cl_device_id)));
    status = clGetDeviceIDs(platform_, CL_DEVICE_TYPE_GPU, numDevices, devices_,
                            NULL);
    return true;
  }
  return false;
}

81
// std::unique_ptr<_cl_kernel, clKernel_deleter> CLEngine::GSetKernel(
L
liuruilong 已提交
82 83 84 85 86 87
//    const std::string &kernel_name) {
//  std::unique_ptr<_cl_kernel, clKernel_deleter> kernel(
//      clCreateKernel(program_.get(), kernel_name.c_str(), NULL));
//  return std::move(kernel);
//}
//
88
// bool CLEngine::SetClCommandQueue() {
L
liuruilong 已提交
89 90 91 92 93 94
//  cl_int status;
//  command_queue_.reset(
//          clCreateCommandQueue(context_.get(), devices_[0], 0, &status));
//  return true;
//}

95
// bool CLEngine::SetClContext() {
L
liuruilong 已提交
96 97 98 99
//  context_.reset(clCreateContext(NULL, 1, devices_, NULL, NULL, NULL));
//  return true;
//}

100
// bool CLEngine::LoadKernelFromFile(const char *kernel_file) {
L
liuruilong 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
//  size_t size;
//  char *str;
//  std::fstream f(kernel_file, (std::fstream::in | std::fstream::binary));
//
//  if (!f.is_open()) {
//    return false;
//  }
//
//  size_t fileSize;
//  f.seekg(0, std::fstream::end);
//  size = fileSize = (size_t)f.tellg();
//  f.seekg(0, std::fstream::beg);
//  str = new char[size + 1];
//  if (!str) {
//    f.close();
//    return 0;
//  }
//
//  f.read(str, fileSize);
//  f.close();
//  str[size] = '\0';
//  const char *source = str;
//  size_t sourceSize[] = {strlen(source)};
//  program_.reset(
125 126
//      clCreateProgramWithSource(context_.get(), 1, &source, sourceSize,
//      NULL));
L
liuruilong 已提交
127 128
//  return true;
//}
L
liuruilong 已提交
129 130 131

}  // namespace framework
}  // namespace paddle_mobile