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 20 21 22 23
/* 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 <CL/cl.h>
// #include "CL/cl.h"
#include <fstream>
#include <iostream>
#include <memory>
#include <string>

L
liuruilong 已提交
24 25 26
#include "common/enforce.h"
#include "framework/cl/cl_deleter.h"

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

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

  bool Init();

L
liuruilong 已提交
36 37 38 39 40
  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 已提交
41

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

L
liuruilong 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith(cl_context context, std::string file_name) {
    const char *kernel_file = file_name.c_str();
    size_t size;
    char *str;
    std::fstream f(kernel_file, (std::fstream::in | std::fstream::binary));

    PADDLE_MOBILE_ENFORCE(f.is_open(), " file open failed")

    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];

    PADDLE_MOBILE_ENFORCE(str != NULL, " str null")

    f.read(str, fileSize);
    f.close();
    str[size] = '\0';
    const char *source = str;
    size_t sourceSize[] = {strlen(source)};
    cl_program p = clCreateProgramWithSource(context, 1, &source, sourceSize, NULL);
    std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p);
    return std::move(program_ptr);
  }
L
liuruilong 已提交
74

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

 private:
  CLEngine() { initialized_ = false; }

  bool SetPlatform();

  bool SetClDeviceId();

L
liuruilong 已提交
89
//  bool SetClContext();
L
liuruilong 已提交
90

L
liuruilong 已提交
91
//  bool SetClCommandQueue();
L
liuruilong 已提交
92

L
liuruilong 已提交
93
//  bool LoadKernelFromFile(const char *kernel_file);
L
liuruilong 已提交
94

L
liuruilong 已提交
95
//  bool BuildProgram();
L
liuruilong 已提交
96 97

  bool initialized_;
L
liuruilong 已提交
98

L
liuruilong 已提交
99
  cl_platform_id platform_;
L
liuruilong 已提交
100

L
liuruilong 已提交
101
  cl_device_id *devices_;
L
liuruilong 已提交
102

L
liuruilong 已提交
103
  std::unique_ptr<_cl_context, CLContextDeleter> context_;
L
liuruilong 已提交
104

L
liuruilong 已提交
105
  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_;
L
liuruilong 已提交
106 107

  std::unique_ptr<_cl_program, CLProgramDeleter> program_;
L
liuruilong 已提交
108 109 110 111
};

}  // namespace framework
}  // namespace paddle_mobile