cl_scope.h 2.9 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
/* 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>
#include <unordered_map>

L
liuruilong 已提交
21
#include "CL/cl.h"
22 23 24
#include "framework/cl/cl_deleter.h"
#include "framework/cl/cl_engine.h"
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
25 26 27 28 29 30 31 32 33

namespace paddle_mobile {
namespace framework {

class CLScope {
 public:
  CLScope() {
    CLEngine *engin = CLEngine::Instance();
    context_ = engin->CreateContext();
L
liuruilong 已提交
34
    command_queue_ = engin->CreateClCommandQueue(context_.get());
L
liuruilong 已提交
35 36
  }

37
  cl_command_queue CommandQueue() { return command_queue_.get(); }
L
liuruilong 已提交
38

39 40
  std::unique_ptr<_cl_kernel, CLKernelDeleter> GetKernel(
      const std::string &kernel_name, const std::string &file_name) {
L
liuruilong 已提交
41
    auto program = Program(file_name);
L
liuruilong 已提交
42
    DLOG << " get program ~ ";
43
    std::unique_ptr<_cl_kernel, CLKernelDeleter> kernel(
L
liuruilong 已提交
44 45
        clCreateKernel(program, kernel_name.c_str(), &status_));
    CL_CHECK_ERRORS(status_);
L
liuruilong 已提交
46
    DLOG << " create kernel ~ ";
L
liuruilong 已提交
47 48 49
    return std::move(kernel);
  }

50
  cl_context Context() { return context_.get(); }
L
liuruilong 已提交
51 52 53 54 55 56 57

  cl_program Program(const std::string &file_name) {
    auto it = programs_.find(file_name);
    if (it != programs_.end()) {
      return it->second.get();
    }

Y
yangfei 已提交
58 59
    auto program = CLEngine::Instance()->CreateProgramWith(
        context_.get(), "./cl_kernel/" + file_name);
L
liuruilong 已提交
60

L
liuruilong 已提交
61
    DLOG << " --- begin build program -> " << file_name << " --- ";
L
liuruilong 已提交
62 63
    status_ =
        clBuildProgram(program.get(), 0, 0, "-cl-fast-relaxed-math", 0, 0);
L
liuruilong 已提交
64

L
liuruilong 已提交
65
    CL_CHECK_ERRORS(status_);
L
liuruilong 已提交
66

L
liuruilong 已提交
67 68 69 70 71 72 73 74 75 76 77 78
    if (status_ == CL_BUILD_PROGRAM_FAILURE) {
      size_t log_size;
      clGetProgramBuildInfo(program.get(), CLEngine::Instance()->DeviceID(),
                            CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
      char *log = (char *)malloc(log_size);
      clGetProgramBuildInfo(program.get(), CLEngine::Instance()->DeviceID(),
                            CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
      DLOG << " program build error: " << log;
    }

    DLOG << " --- end build program -> " << file_name << " --- ";

L
liuruilong 已提交
79 80
    programs_[file_name] = std::move(program);

L
liuruilong 已提交
81
    return programs_[file_name].get();
L
liuruilong 已提交
82 83 84
  }

 private:
85
  cl_int status_;
L
liuruilong 已提交
86 87
  std::unique_ptr<_cl_context, CLContextDeleter> context_;
  std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_;
88 89 90
  std::unordered_map<std::string,
                     std::unique_ptr<_cl_program, CLProgramDeleter>>
      programs_;
L
liuruilong 已提交
91 92
};

93 94
}  // namespace framework
}  // namespace paddle_mobile