cl_tensor.h 4.7 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;
  }

Y
yangfei 已提交
51
  template <typename T>
Y
yangfei 已提交
52
  inline T mutable_with_data(void *data) {
Y
yangfei 已提交
53
    int64_t size = numel() * sizeof(float);
L
liuruilong 已提交
54 55
    holder_.reset(
        new PlaceholderImpl(size, data, typeid(T), context_, command_queue_));
Y
yangfei 已提交
56
    return reinterpret_cast<T>(
Y
yangfei 已提交
57 58
        reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(holder_->ptr())));
  }
L
liuruilong 已提交
59 60 61 62 63 64 65 66

  inline void *mutable_data(std::type_index type) {
    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 已提交
67
      holder_.reset(new PlaceholderImpl(size, type, context_, command_queue_));
L
liuruilong 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      offset_ = 0;
    }
    return reinterpret_cast<void *>(
        reinterpret_cast<uintptr_t>(holder_->ptr()) + offset_);
  }

  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
  inline T *mutable_data() {
    static_assert(std::is_pod<T>::value, "T must be POD");
    return reinterpret_cast<T *>(mutable_data(typeid(T)));
  }

  /**
   * @brief     Return a pointer to mutable memory block.
   *
   * @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>
  inline T *mutable_data(DDim dims) {
    static_assert(std::is_pod<T>::value, "T must be POD");
    Resize(dims);
    return mutable_data<T>();
  }

 private:
L
liuruilong 已提交
100
  cl_context context_;
L
liuruilong 已提交
101
  cl_command_queue command_queue_;
L
liuruilong 已提交
102

L
liuruilong 已提交
103 104 105 106 107 108 109 110 111 112 113 114
  /*
   *   virtual ~Placeholder() = default;

    virtual void *ptr() const = 0;

    virtual size_t size() const = 0;

    virtual std::type_index type() const = 0;

    virtual void set_type(std::type_index type) = 0;
   * */
  struct PlaceholderImpl : public Placeholder {
115
    PlaceholderImpl(size_t size, void *input, std::type_index type,
L
liuruilong 已提交
116
                    cl_context context, cl_command_queue command_queue)
117 118
        : ptr_(clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                              size, reinterpret_cast<void *>(input), NULL)),
L
liuruilong 已提交
119
          size_(size),
L
liuruilong 已提交
120 121
          type_(type),
          command_queue_(command_queue) {}
L
liuruilong 已提交
122

L
liuruilong 已提交
123 124
    PlaceholderImpl(size_t size, std::type_index type, cl_context context,
                    cl_command_queue command_queue)
125
        : ptr_(clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, NULL)),
L
liuruilong 已提交
126
          size_(size),
L
liuruilong 已提交
127 128
          type_(type),
          command_queue_(command_queue) {}
L
liuruilong 已提交
129 130 131

    virtual size_t size() const { return size_; }

L
liuruilong 已提交
132 133 134 135 136 137 138 139 140
    virtual void *ptr() const {
      if (host_ptr_) {
        delete (host_ptr_);
      }
      char *host_ptr = new char[size_];
      clEnqueueReadBuffer(command_queue_, ptr_.get(), CL_TRUE, 0, size_,
                          host_ptr, 0, NULL, NULL);
      return static_cast<void *>(host_ptr);
    }
L
liuruilong 已提交
141 142 143 144 145

    virtual std::type_index type() const { return type_; }

    virtual void set_type(std::type_index type) { type_ = type; }

L
liuruilong 已提交
146
    std::unique_ptr<_cl_mem, CLMemDeleter> ptr_;
L
liuruilong 已提交
147 148 149 150 151

    size_t size_;

    /* the current type of memory */
    std::type_index type_;
L
liuruilong 已提交
152 153 154 155 156 157 158 159 160 161 162

    cl_command_queue command_queue_;

    ~PlaceholderImpl() {
      if (host_ptr_) {
        delete (host_ptr_);
      }
    }

   private:
    void *host_ptr_;
L
liuruilong 已提交
163 164 165 166 167
  };
};

}  // namespace framework
}  // namespace paddle_mobile