memory.h 5.9 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 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
16
#include <algorithm>
17
#include <string>
Y
Yan Chunwei 已提交
18 19
#include "lite/api/paddle_place.h"
#include "lite/core/target_wrapper.h"
20
#include "lite/utils/logging.h"
Y
Yan Chunwei 已提交
21 22 23
#include "lite/utils/macros.h"

#ifdef LITE_WITH_OPENCL
24
#include "lite/backends/opencl/target_wrapper.h"
Y
Yan Chunwei 已提交
25 26
#endif  // LITE_WITH_OPENCL

27
#ifdef LITE_WITH_CUDA
28
#include "lite/backends/cuda/target_wrapper.h"
29 30
#endif  // LITE_WITH_CUDA

31 32 33 34
#ifdef LITE_WITH_BM
#include "lite/backends/bm/target_wrapper.h"
#endif  // LITE_WITH_BM

35 36 37 38
#ifdef LITE_WITH_MLU
#include "lite/backends/mlu/target_wrapper.h"
#endif  // LITE_WITH_MLU

39 40 41 42
#ifdef LITE_WITH_XPU
#include "lite/backends/xpu/target_wrapper.h"
#endif  // LITE_WITH_XPU

Y
Yan Chunwei 已提交
43 44 45 46 47 48 49 50 51
namespace paddle {
namespace lite {

// Malloc memory for a specific Target. All the targets should be an element in
// the `switch` here.
LITE_API void* TargetMalloc(TargetType target, size_t size);

// Free memory for a specific Target. All the targets should be an element in
// the `switch` here.
52 53 54
void LITE_API TargetFree(TargetType target,
                         void* data,
                         std::string free_flag = "");
Y
Yan Chunwei 已提交
55 56 57

// Copy a buffer from host to another target.
void TargetCopy(TargetType target, void* dst, const void* src, size_t size);
58 59 60 61
#ifdef LITE_WITH_OPENCL
void TargetCopyImage2D(TargetType target,
                       void* dst,
                       const void* src,
62 63 64 65
                       const size_t cl_image2d_width,
                       const size_t cl_image2d_height,
                       const size_t cl_image2d_row_pitch,
                       const size_t cl_image2d_slice_pitch);
66
#endif  // LITE_WITH_OPENCL
Y
Yan Chunwei 已提交
67 68

template <TargetType Target>
69
void CopySync(void* dst, const void* src, size_t size, IoDirection dir) {
Y
Yan Chunwei 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  switch (Target) {
    case TARGET(kX86):
    case TARGET(kHost):
    case TARGET(kARM):
      TargetWrapper<TARGET(kHost)>::MemcpySync(
          dst, src, size, IoDirection::HtoH);
      break;
#ifdef LITE_WITH_CUDA
    case TARGET(kCUDA):
      TargetWrapperCuda::MemcpySync(dst, src, size, dir);
      break;
#endif
#ifdef LITE_WITH_OPENCL
    case TargetType::kOpenCL:
      TargetWrapperCL::MemcpySync(dst, src, size, dir);
      break;
#endif  // LITE_WITH_OPENCL
87 88 89 90 91
#ifdef LITE_WITH_MLU
    case TARGET(kMLU):
      TargetWrapperMlu::MemcpySync(dst, src, size, dir);
      break;
#endif
Y
Yan Chunwei 已提交
92 93 94 95
#ifdef LITE_WITH_FPGA
    case TARGET(kFPGA):
      TargetWrapper<TARGET(kFPGA)>::MemcpySync(dst, src, size, dir);
      break;
96 97 98 99 100
#endif
#ifdef LITE_WITH_BM
    case TARGET(kBM):
      TargetWrapper<TARGET(kBM)>::MemcpySync(dst, src, size, dir);
      break;
Y
Yan Chunwei 已提交
101
#endif
102 103 104
    default:
      LOG(FATAL)
          << "The copy function of this target has not been implemented yet.";
Y
Yan Chunwei 已提交
105 106 107 108 109 110 111 112
  }
}

// Memory buffer manager.
class Buffer {
 public:
  Buffer() = default;
  Buffer(TargetType target, size_t size) : space_(size), target_(target) {}
113 114
  Buffer(void* data, TargetType target, size_t size)
      : space_(size), data_(data), own_data_(false), target_(target) {}
Y
Yan Chunwei 已提交
115 116 117 118

  void* data() const { return data_; }
  TargetType target() const { return target_; }
  size_t space() const { return space_; }
119
  bool own_data() const { return own_data_; }
Y
Yan Chunwei 已提交
120 121 122

  void ResetLazy(TargetType target, size_t size) {
    if (target != target_ || space_ < size) {
123
      CHECK_EQ(own_data_, true) << "Can not reset unowned buffer.";
Y
Yan Chunwei 已提交
124 125 126 127
      Free();
      data_ = TargetMalloc(target, size);
      target_ = target;
      space_ = size;
128 129 130
#ifdef LITE_WITH_OPENCL
      cl_use_image2d_ = false;
#endif
Y
Yan Chunwei 已提交
131 132 133 134 135
    }
  }

  void ResizeLazy(size_t size) { ResetLazy(target_, size); }

136 137 138
#ifdef LITE_WITH_OPENCL
  template <typename T>
  void ResetLazyImage2D(TargetType target,
139 140
                        const size_t img_w_req,
                        const size_t img_h_req,
141
                        void* host_ptr = nullptr) {
142 143
    if (target != target_ || cl_image2d_width_ < img_w_req ||
        cl_image2d_height_ < img_h_req || host_ptr != nullptr) {
144
      CHECK_EQ(own_data_, true) << "Can not reset unowned buffer.";
145 146
      cl_image2d_width_ = std::max(cl_image2d_width_, img_w_req);
      cl_image2d_height_ = std::max(cl_image2d_height_, img_h_req);
147
      Free();
148 149
      data_ = TargetWrapperCL::MallocImage<T>(
          cl_image2d_width_, cl_image2d_height_, host_ptr);
150
      target_ = target;
151
      space_ = sizeof(T) * cl_image2d_width_ * cl_image2d_height_ *
152 153
               4;  // un-used for opencl Image2D, 4 for RGBA,
      cl_use_image2d_ = true;
154 155 156 157
    }
  }
#endif

Y
Yan Chunwei 已提交
158
  void Free() {
159
    if (space_ > 0 && own_data_) {
160 161 162 163 164
      if (!cl_use_image2d_) {
        TargetFree(target_, data_);
      } else {
        TargetFree(target_, data_, "cl_use_image2d_");
      }
Y
Yan Chunwei 已提交
165
    }
166
    data_ = nullptr;
Y
Yan Chunwei 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    target_ = TargetType::kHost;
    space_ = 0;
  }

  void CopyDataFrom(const Buffer& other, size_t nbytes) {
    target_ = other.target_;
    ResizeLazy(nbytes);
    // TODO(Superjomn) support copy between different targets.
    TargetCopy(target_, data_, other.data_, nbytes);
  }

  ~Buffer() { Free(); }

 private:
  // memory it actually malloced.
  size_t space_{0};
183
  bool cl_use_image2d_{false};   // only used for OpenCL Image2D
184 185
  size_t cl_image2d_width_{0};   // only used for OpenCL Image2D
  size_t cl_image2d_height_{0};  // only used for OpenCL Image2D
Y
Yan Chunwei 已提交
186
  void* data_{nullptr};
187
  bool own_data_{true};
Y
Yan Chunwei 已提交
188 189 190 191 192
  TargetType target_{TargetType::kHost};
};

}  // namespace lite
}  // namespace paddle