cl_engine.h 5.4 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17
#include <cstring>
L
liuruilong 已提交
18 19 20
#include <memory>
#include <string>

21
#include "CL/cl.h"
L
liuruilong 已提交
22
#include "common/enforce.h"
L
liuruilong 已提交
23
#include "common/log.h"
L
liuruilong 已提交
24
#include "framework/cl/cl_deleter.h"
L
liuruilong 已提交
25
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
26

L
liuruilong 已提交
27 28 29 30 31 32 33 34
namespace paddle_mobile {
namespace framework {

class CLEngine {
 public:
  static CLEngine *Instance();

  bool Init();
xiebaiyuan's avatar
xiebaiyuan 已提交
35
  bool isInitSuccess();
L
liuruilong 已提交
36
  std::unique_ptr<_cl_context, CLContextDeleter> CreateContext() {
L
liuruilong 已提交
37 38
    cl_int status;
    cl_context c = clCreateContext(NULL, 1, devices_, NULL, NULL, &status);
L
liuruilong 已提交
39
    std::unique_ptr<_cl_context, CLContextDeleter> context_ptr(c);
L
liuruilong 已提交
40
    CL_CHECK_ERRORS(status);
L
liuruilong 已提交
41 42
    return std::move(context_ptr);
  }
L
liuruilong 已提交
43

L
liuruilong 已提交
44 45
  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> CreateClCommandQueue(
      cl_context context) {
L
liuruilong 已提交
46
    cl_int status;
47
    cl_command_queue queue =
L
liuruilong 已提交
48
        clCreateCommandQueue(context, devices_[0], 0, &status);
49 50
    std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_ptr(
        queue);
L
liuruilong 已提交
51
    CL_CHECK_ERRORS(status);
L
liuruilong 已提交
52 53
    return std::move(command_queue_ptr);
  }
L
liuruilong 已提交
54

xiebaiyuan's avatar
xiebaiyuan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  cl_context getContext() {
    if (context_ == nullptr) {
      context_ = CreateContext();
    }
    return context_.get();
  }

  cl_command_queue getClCommandQueue() {
    if (command_queue_ == nullptr) {
      command_queue_ = CreateClCommandQueue(getContext());
    }
    return command_queue_.get();
  }

69 70
  std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith(
      cl_context context, std::string file_name) {
L
liuruilong 已提交
71 72
    FILE *file = fopen(file_name.c_str(), "rb");
    PADDLE_MOBILE_ENFORCE(file != nullptr, "can't open file: %s ",
L
liuruilong 已提交
73
                          file_name.c_str());
L
liuruilong 已提交
74 75 76 77 78 79 80 81 82 83 84 85
    fseek(file, 0, SEEK_END);
    int64_t size = ftell(file);
    PADDLE_MOBILE_ENFORCE(size > 0, "size is too small");
    rewind(file);
    char *data = new char[size + 1];
    size_t bytes_read = fread(data, 1, size, file);
    data[size] = '\0';
    PADDLE_MOBILE_ENFORCE(bytes_read == size,
                          "read binary file bytes do not match with fseek");
    fclose(file);

    const char *source = data;
L
liuruilong 已提交
86
    size_t sourceSize[] = {strlen(source)};
87
    cl_program p =
L
liuruilong 已提交
88 89 90 91 92 93
        clCreateProgramWithSource(context, 1, &source, sourceSize, &status_);

    DLOG << " cl kernel file name: " << file_name;
    DLOG << " source size: " << sourceSize[0];
    CL_CHECK_ERRORS(status_);

L
liuruilong 已提交
94
    std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p);
L
liuruilong 已提交
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    return std::move(program_ptr);
  }

  std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWithSource(
      cl_context context, const char *source) {
    size_t sourceSize[] = {strlen(source)};
    cl_program p =
        clCreateProgramWithSource(context, 1, &source, sourceSize, &status_);

    DLOG << " cl kernel from source";
    DLOG << " source size: " << sourceSize[0];
    CL_CHECK_ERRORS(status_);

    std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p);

L
liuruilong 已提交
111 112
    return std::move(program_ptr);
  }
L
liuruilong 已提交
113

L
liuruilong 已提交
114
  std::unique_ptr<_cl_event, CLEventDeleter> CreateEvent(cl_context context) {
L
liuruilong 已提交
115
    cl_event event = clCreateUserEvent(context, &status_);
L
liuruilong 已提交
116
    std::unique_ptr<_cl_event, CLEventDeleter> event_ptr(event);
L
liuruilong 已提交
117
    CL_CHECK_ERRORS(status_);
L
liuruilong 已提交
118 119 120
    return std::move(event_ptr);
  }

121
  bool BuildProgram(cl_program program, const std::string &options = "") {
L
liuruilong 已提交
122
    cl_int status;
123
    std::string path = options + " -cl-fast-relaxed-math -I " +
124 125 126
                       CLEngine::Instance()->GetCLPath() + "/cl_kernel";

    status = clBuildProgram(program, 0, 0, path.c_str(), 0, 0);
L
liuruilong 已提交
127

L
liuruilong 已提交
128
    CL_CHECK_ERRORS(status);
L
liuruilong 已提交
129 130 131 132 133

    if (status_ == CL_BUILD_PROGRAM_FAILURE) {
      size_t log_size;
      clGetProgramBuildInfo(program, CLEngine::Instance()->DeviceID(),
                            CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
L
liuruilong 已提交
134
      char *log = reinterpret_cast<char *>(malloc(log_size));
L
liuruilong 已提交
135 136 137 138 139 140 141 142 143 144
      clGetProgramBuildInfo(program, CLEngine::Instance()->DeviceID(),
                            CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
      DLOG << " program build error: " << log;
    }

    if (status == CL_SUCCESS) {
      return true;
    } else {
      return false;
    }
L
liuruilong 已提交
145
  }
L
liuruilong 已提交
146

L
liuruilong 已提交
147 148
  cl_device_id DeviceID(int index = 0) { return devices_[index]; }

Y
yangfei 已提交
149 150 151
  std::string GetCLPath() { return cl_path_; }
  void setClPath(std::string cl_path) { cl_path_ = cl_path; }

L
liuruilong 已提交
152 153 154 155 156 157 158 159
 private:
  CLEngine() { initialized_ = false; }

  bool SetPlatform();

  bool SetClDeviceId();

  bool initialized_;
L
liuruilong 已提交
160

L
liuruilong 已提交
161
  cl_platform_id platform_;
L
liuruilong 已提交
162

L
liuruilong 已提交
163
  cl_device_id *devices_;
L
liuruilong 已提交
164

L
liuruilong 已提交
165 166
  cl_int status_;

167
  std::string cl_path_;
L
liuruilong 已提交
168
  std::unique_ptr<_cl_program, CLProgramDeleter> program_;
L
liuruilong 已提交
169

xiebaiyuan's avatar
xiebaiyuan 已提交
170 171 172 173 174
  std::unique_ptr<_cl_context, CLContextDeleter> context_ = nullptr;

  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_ =
      nullptr;

175
  //  bool SetClContext();
L
liuruilong 已提交
176

177
  //  bool SetClCommandQueue();
L
liuruilong 已提交
178

179
  //  bool LoadKernelFromFile(const char *kernel_file);
L
liuruilong 已提交
180

181
  //  bool BuildProgram();
xiebaiyuan's avatar
xiebaiyuan 已提交
182
  bool is_init_success_ = false;
L
liuruilong 已提交
183 184 185 186
};

}  // namespace framework
}  // namespace paddle_mobile