memory.h 2.5 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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
#include <glog/logging.h>
#include "target_wrapper.h"

namespace paddle {
namespace lite {

S
update  
superjomn 已提交
22
static void* TargetMalloc(TargetType target, size_t size) {
S
superjomn 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  void* data{nullptr};
  switch (static_cast<int>(target)) {
    case static_cast<int>(TargetType::kX86):
      data = TargetWrapper<TARGET(kX86)>::Malloc(size);
      break;
    case static_cast<int>(TargetType::kCUDA):
      data = TargetWrapper<TARGET(kCUDA)>::Malloc(size);
      break;
    case static_cast<int>(TargetType::kHost):
      data = TargetWrapper<TARGET(kHost)>::Malloc(size);
      break;
    default:
      LOG(FATAL) << "Unknown type";
  }
  return data;
}

S
update  
superjomn 已提交
40
static void TargetFree(TargetType target, void* data) {
S
superjomn 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  switch (static_cast<int>(target)) {
    case static_cast<int>(TargetType::kX86):
      TargetWrapper<TARGET(kX86)>::Free(data);
      break;
    case static_cast<int>(TargetType::kCUDA):
      TargetWrapper<TARGET(kX86)>::Free(data);
      break;
    default:
      LOG(FATAL) << "Unknown type";
  }
}

// Memory buffer manager.
class Buffer {
 public:
S
update  
superjomn 已提交
56
  Buffer() = default;
S
superjomn 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  Buffer(TargetType target, size_t size) : space_(size), target_(target) {}

  void* data() const { return data_; }

  void ResetLazy(TargetType target, size_t size) {
    if (target != target_ || space_ < size) {
      Free();
    }

    if (size < space_) return;
    data_ = TargetMalloc(target, size);
    target_ = target;
    space_ = size;
  }

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

  void Free() {
    if (space_ > 0) {
      TargetFree(target_, data_);
    }
    target_ = TargetType::kHost;
    space_ = 0;
  }

S
Superjomn 已提交
82 83 84 85 86 87 88
  void CopyDataFrom(const Buffer& other, size_t nbytes) {
    target_ = other.target_;
    ResizeLazy(nbytes);
    // TODO(Superjomn) support copy between different targets.
    memcpy(data_, other.data_, nbytes);
  }

S
superjomn 已提交
89 90 91 92 93 94 95 96
 private:
  size_t space_{0};
  void* data_{nullptr};
  TargetType target_{TargetType::kHost};
};

}  // namespace lite
}  // namespace paddle