cl_tensor.h 5.2 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 <vector>

L
liuruilong 已提交
21
#include "CL/cl.h"
22 23 24
#include "framework/cl/cl_deleter.h"
#include "framework/cl/cl_engine.h"
#include "framework/tensor_base.h"
L
liuruilong 已提交
25 26 27 28

namespace paddle_mobile {
namespace framework {

L
liuruilong 已提交
29
class CLTensor : TensorBase {
L
liuruilong 已提交
30
 public:
L
liuruilong 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
  CLTensor(cl_context context, cl_command_queue command_queue)
      : context_(context), command_queue_(command_queue) {}

  CLTensor() = default;

  /*
   * if init method haven't set context and command_queue, need set
   * */
  void SetContextAndCommandQueue(cl_context context,
                                 cl_command_queue command_queue) {
    context_ = context;
    command_queue_ = command_queue;
  }
L
liuruilong 已提交
44 45 46 47 48 49 50

  /*! Resize the dimensions of the memory block. */
  inline CLTensor &Resize(const DDim &dims) {
    dims_ = dims;
    return *this;
  }

L
liuruilong 已提交
51 52 53 54 55
  template <typename T>
  inline cl_mem mutable_with_data(const T *data) {
    int64_t size = numel() * sizeof(T);

    holder_.reset(new PlaceholderImpl(
56
        size, reinterpret_cast<void *>(const_cast<T *>(data)), type_id<T>(),
L
liuruilong 已提交
57 58
        context_, command_queue_));
    return reinterpret_cast<cl_mem>(holder_->ptr());
Y
yangfei 已提交
59
  }
L
liuruilong 已提交
60

61
  inline cl_mem mutable_data(std::string type) {
L
liuruilong 已提交
62 63 64 65 66 67
    if (holder_ != nullptr) {
      holder_->set_type(type);
    }
    PADDLE_MOBILE_ENFORCE(numel() >= 0, "the Tensor's numel must >=0.")
    int64_t size = numel() * SizeOfType(type);
    if (holder_ == nullptr || holder_->size() < size + offset_) {
L
liuruilong 已提交
68
      holder_.reset(new PlaceholderImpl(size, type, context_, command_queue_));
L
liuruilong 已提交
69 70
      offset_ = 0;
    }
L
liuruilong 已提交
71
    return reinterpret_cast<cl_mem>(holder_->ptr());
L
liuruilong 已提交
72 73 74
  }

  /**
L
liuruilong 已提交
75
   * @brief   Return a pointer to cl buffer.
L
liuruilong 已提交
76 77 78
   * @note    If not exist, then allocation.
   */
  template <typename T>
L
liuruilong 已提交
79
  inline cl_mem mutable_data() {
80
    return reinterpret_cast<cl_mem>(mutable_data(type_id<T>()));
L
liuruilong 已提交
81 82 83
  }

  /**
L
liuruilong 已提交
84
   * @brief     Return a pointer to cl buffer.
L
liuruilong 已提交
85 86 87 88 89 90 91
   *
   * @param[in] dims    The dimensions of the memory block.
   * @param[in] place   The place of the memory block.
   *
   * @note      If not exist, then allocation.
   */
  template <typename T>
L
liuruilong 已提交
92
  inline cl_mem mutable_data(DDim dims) {
L
liuruilong 已提交
93 94 95 96
    Resize(dims);
    return mutable_data<T>();
  }

L
liuruilong 已提交
97 98 99
  inline cl_mem CLBuffer() {
    check_memory_size();
    return reinterpret_cast<cl_mem>(
L
liuruilong 已提交
100
        reinterpret_cast<uintptr_t>(holder_->ptr()));
L
liuruilong 已提交
101
  }
L
liuruilong 已提交
102

L
liuruilong 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
  template <typename T>
  inline T *Data() {
    if (host_ptr_) {
      delete (host_ptr_);
      host_ptr_ = nullptr;
    }
    cl_mem buffer = CLBuffer();
    host_ptr_ = new char[holder_->size()];
    cl_int status;
    status = clEnqueueReadBuffer(command_queue_, buffer, CL_TRUE, 0,
                                 holder_->size(), host_ptr_, 0, NULL, NULL);
    CL_CHECK_ERRORS(status);
    return reinterpret_cast<T *>(host_ptr_);
  }
L
liuruilong 已提交
117

L
liuruilong 已提交
118
  int memorySize() { return holder_->size(); }
L
liuruilong 已提交
119

L
liuruilong 已提交
120
  ~CLTensor() {
L
liuruilong 已提交
121
    DLOG << "~CLTensor";
L
liuruilong 已提交
122
    if (host_ptr_) {
L
liuruilong 已提交
123
      DLOG << " delete host ptr ";
L
liuruilong 已提交
124 125 126 127
      delete (host_ptr_);
      host_ptr_ = nullptr;
    }
  }
L
liuruilong 已提交
128

L
liuruilong 已提交
129 130 131
 private:
  cl_context context_;
  cl_command_queue command_queue_;
L
liuruilong 已提交
132
  void *host_ptr_ = nullptr;
L
liuruilong 已提交
133 134

  struct PlaceholderImpl : public Placeholder {
135
    PlaceholderImpl(size_t size, void *input, std::string type,
L
liuruilong 已提交
136
                    cl_context context, cl_command_queue command_queue)
137 138
        : ptr_(clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                              size, reinterpret_cast<void *>(input), NULL)),
L
liuruilong 已提交
139
          size_(size),
140
          capatity_(size),
L
liuruilong 已提交
141
          type_(type),
142
          context_(context),
L
liuruilong 已提交
143
          command_queue_(command_queue) {}
L
liuruilong 已提交
144

145
    PlaceholderImpl(size_t size, std::string type, cl_context context,
L
liuruilong 已提交
146
                    cl_command_queue command_queue)
147
        : ptr_(clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, NULL)),
L
liuruilong 已提交
148
          size_(size),
149
          capatity_(size),
L
liuruilong 已提交
150
          type_(type),
151
          context_(context),
L
liuruilong 已提交
152
          command_queue_(command_queue) {}
L
liuruilong 已提交
153 154 155

    virtual size_t size() const { return size_; }

L
liuruilong 已提交
156
    virtual void *ptr() const { return static_cast<void *>(ptr_.get()); }
L
liuruilong 已提交
157

158
    virtual std::string type() const { return type_; }
L
liuruilong 已提交
159

160
    virtual void set_type(std::string type) { type_ = type; }
L
liuruilong 已提交
161

162 163 164 165 166 167 168 169 170
    virtual void resize(size_t size) {
      if (size > capatity_) {
        capatity_ = size;
        ptr_.reset(
            clCreateBuffer(context_, CL_MEM_READ_WRITE, capatity_, NULL, NULL));
      }
      size_ = size;
    }

L
liuruilong 已提交
171
    std::unique_ptr<_cl_mem, CLMemDeleter> ptr_;
L
liuruilong 已提交
172 173 174

    size_t size_;

175 176
    size_t capatity_;

L
liuruilong 已提交
177
    /* the current type of memory */
178
    std::string type_;
L
liuruilong 已提交
179

180
    cl_context context_;
L
liuruilong 已提交
181
    cl_command_queue command_queue_;
L
liuruilong 已提交
182 183 184 185 186
  };
};

}  // namespace framework
}  // namespace paddle_mobile