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

#include <memory>
#include <string>

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

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

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

  bool Init();

L
liuruilong 已提交
34 35 36 37 38
  std::unique_ptr<_cl_context, CLContextDeleter> CreateContext() {
    cl_context c = clCreateContext(NULL, 1, devices_, NULL, NULL, NULL);
    std::unique_ptr<_cl_context, CLContextDeleter> context_ptr(c);
    return std::move(context_ptr);
  }
L
liuruilong 已提交
39

40 41
  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter>
  CreateClCommandQueue() {
L
liuruilong 已提交
42
    cl_int status;
43 44 45 46
    cl_command_queue queue =
        clCreateCommandQueue(context_.get(), devices_[0], 0, &status);
    std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_ptr(
        queue);
L
liuruilong 已提交
47 48
    return std::move(command_queue_ptr);
  }
L
liuruilong 已提交
49

50 51
  std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith(
      cl_context context, std::string file_name) {
L
liuruilong 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    FILE *file = fopen(file_name.c_str(), "rb");
    PADDLE_MOBILE_ENFORCE(file != nullptr, "can't open file: %s ",
                          filename.c_str());
    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 已提交
67
    size_t sourceSize[] = {strlen(source)};
68 69
    cl_program p =
        clCreateProgramWithSource(context, 1, &source, sourceSize, NULL);
L
liuruilong 已提交
70 71 72
    std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p);
    return std::move(program_ptr);
  }
L
liuruilong 已提交
73

L
liuruilong 已提交
74
  bool BuildProgram(cl_program program) {
L
liuruilong 已提交
75 76 77 78 79
    cl_int status;
    status = clBuildProgram(program, 0, 0, "-cl-fast-relaxed-math", 0, 0);
    CL_CHECK_ERRORS(status);
    return true;
  }
L
liuruilong 已提交
80 81 82 83 84 85 86 87 88

 private:
  CLEngine() { initialized_ = false; }

  bool SetPlatform();

  bool SetClDeviceId();

  bool initialized_;
L
liuruilong 已提交
89

L
liuruilong 已提交
90
  cl_platform_id platform_;
L
liuruilong 已提交
91

L
liuruilong 已提交
92
  cl_device_id *devices_;
L
liuruilong 已提交
93

L
liuruilong 已提交
94
  std::unique_ptr<_cl_context, CLContextDeleter> context_;
L
liuruilong 已提交
95

L
liuruilong 已提交
96
  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_;
L
liuruilong 已提交
97 98

  std::unique_ptr<_cl_program, CLProgramDeleter> program_;
L
liuruilong 已提交
99

100
  //  bool SetClContext();
L
liuruilong 已提交
101

102
  //  bool SetClCommandQueue();
L
liuruilong 已提交
103

104
  //  bool LoadKernelFromFile(const char *kernel_file);
L
liuruilong 已提交
105

106
  //  bool BuildProgram();
L
liuruilong 已提交
107 108 109 110
};

}  // namespace framework
}  // namespace paddle_mobile