cl_scope.h 4.6 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 <map>
L
liuruilong 已提交
18 19 20
#include <memory>
#include <string>
#include <unordered_map>
21
#include <utility>
22
#include <vector>
L
liuruilong 已提交
23

L
liuruilong 已提交
24
#include "CL/cl.h"
25 26 27
#include "framework/cl/cl_deleter.h"
#include "framework/cl/cl_engine.h"
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
28 29

namespace paddle_mobile {
30 31

extern const std::map<std::string, std::vector<unsigned char>> opencl_kernels;
32
extern const std::map<std::string, std::vector<unsigned char>> opencl_headers;
33

L
liuruilong 已提交
34 35 36 37 38
namespace framework {

class CLScope {
 public:
  CLScope() {
xiebaiyuan's avatar
xiebaiyuan 已提交
39 40 41
    CLEngine *engine = CLEngine::Instance();
    context_ = engine->getContext();
    command_queue_ = engine->getClCommandQueue();
42
    localWorkSizeInfo_ = engine->getLocalWorkSizeInfo();
L
liuruilong 已提交
43 44
  }

xiebaiyuan's avatar
xiebaiyuan 已提交
45
  cl_command_queue CommandQueue() { return command_queue_; }
L
liuruilong 已提交
46

47
  std::unique_ptr<_cl_kernel, CLKernelDeleter> GetKernel(
48 49
      const std::string &kernel_name, const std::string &file_name,
      const std::string &options) {
50
    LOG(kLOG_DEBUG2) << " to get program " << file_name;
51
    auto program = Program(file_name, kernel_name, options);
52 53
    LOG(kLOG_DEBUG2) << " end get program ~ ";
    LOG(kLOG_DEBUG2) << " to create kernel: " << kernel_name;
54
    std::unique_ptr<_cl_kernel, CLKernelDeleter> kernel(
L
liuruilong 已提交
55 56
        clCreateKernel(program, kernel_name.c_str(), &status_));
    CL_CHECK_ERRORS(status_);
57
    LOG(kLOG_DEBUG2) << " end create kernel ~ ";
L
liuruilong 已提交
58 59 60
    return std::move(kernel);
  }

xiebaiyuan's avatar
xiebaiyuan 已提交
61
  cl_context Context() { return context_; }
L
liuruilong 已提交
62

63 64 65 66 67 68 69 70 71 72 73 74
  cl_program Program(const std::string &file_name,
                     const std::string &kernel_name,
                     const std::string &options) {
    if (opencl_kernels.find(kernel_name) != opencl_kernels.end() &&
        opencl_headers.find(file_name) != opencl_headers.end()) {
      std::string program_key = file_name + kernel_name;
      if (!options.empty()) {
        program_key += options;
      }
      auto it = programs_.find(program_key);
      if (it != programs_.end()) {
        return it->second.get();
75
      }
76 77 78 79 80
      auto src_it = opencl_kernels.find(kernel_name);
      std::string source(src_it->second.begin(), src_it->second.end());
      auto header_it = opencl_headers.find(file_name);
      std::string header(header_it->second.begin(), header_it->second.end());
      source = header + "\n" + source;
81 82 83
      auto program = CLEngine::Instance()->CreateProgramWithSource(
          context_, source.c_str());

84 85
      LOG(kLOG_DEBUG3) << " --- begin build program -> " << program_key
                       << " --- ";
86
      CLEngine::Instance()->BuildProgram(program.get(), options);
87 88
      LOG(kLOG_DEBUG3) << " --- end build program -> " << program_key
                       << " --- ";
89 90

      programs_[program_key] = std::move(program);
91
      return programs_[program_key].get();
92
    } else {
93 94 95 96 97 98 99 100
      std::string program_key = file_name;
      if (!options.empty()) {
        program_key += options;
      }
      auto it = programs_.find(program_key);
      if (it != programs_.end()) {
        return it->second.get();
      }
101 102 103 104
      auto program = CLEngine::Instance()->CreateProgramWith(
          context_,
          CLEngine::Instance()->GetCLPath() + "/cl_kernel/" + file_name);

105 106
      LOG(kLOG_DEBUG3) << " --- begin build program ele-> " << program_key
                       << " --- ";
107
      CLEngine::Instance()->BuildProgram(program.get(), options);
108 109
      LOG(kLOG_DEBUG3) << " --- end build program ele-> " << program_key
                       << " --- ";
110 111

      programs_[program_key] = std::move(program);
112
      return programs_[program_key].get();
113
    }
L
liuruilong 已提交
114 115
  }

116
  CLLocalWorkSizeInfo LocalWorkSizeInfo() { return localWorkSizeInfo_; }
117 118 119 120
  size_t KernelWorkSize(cl_kernel kernel) {
    size_t kernel_work_size = CLEngine::Instance()->GetKernelWorkSize(kernel);
    return kernel_work_size;
  }
121

L
liuruilong 已提交
122
 private:
123
  cl_int status_;
xiebaiyuan's avatar
xiebaiyuan 已提交
124 125
  cl_context context_;
  cl_command_queue command_queue_;
126 127 128
  std::unordered_map<std::string,
                     std::unique_ptr<_cl_program, CLProgramDeleter>>
      programs_;
129
  CLLocalWorkSizeInfo localWorkSizeInfo_;
L
liuruilong 已提交
130 131
};

132 133
}  // namespace framework
}  // namespace paddle_mobile