memory.h 3.2 KB
Newer Older
S
superjomn 已提交
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
S
superjomn 已提交
16
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
17 18 19 20

namespace paddle {
namespace lite {

S
update  
superjomn 已提交
21
static void* TargetMalloc(TargetType target, size_t size) {
S
superjomn 已提交
22
  void* data{nullptr};
23 24
  switch (target) {
    case TargetType::kHost:
S
superjomn 已提交
25
#ifdef LITE_WITH_X86
26 27
    case TargetType::kX86:
      data = TargetWrapper<TARGET(kHost)>::Malloc(size);
S
superjomn 已提交
28
      break;
S
superjomn 已提交
29
#endif
S
superjomn 已提交
30
#ifdef LITE_WITH_CUDA
31
    case TargetType::kCUDA:
S
superjomn 已提交
32 33
      data =
          TargetWrapper<TARGET(kCUDA), cudaStream_t, cudaEvent_t>::Malloc(size);
S
superjomn 已提交
34
      break;
S
superjomn 已提交
35
#endif  // LITE_WITH_CUDA
S
superjomn 已提交
36
    default:
37
      LOG(FATAL) << "Unknown supported target " << TargetToStr(target);
S
superjomn 已提交
38 39 40 41
  }
  return data;
}

S
update  
superjomn 已提交
42
static void TargetFree(TargetType target, void* data) {
S
superjomn 已提交
43 44 45 46 47 48 49 50 51 52 53 54
  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";
  }
}

S
Superjomn 已提交
55 56
static void TargetCopy(TargetType target, void* dst, const void* src,
                       size_t size) {
57 58 59
  switch (target) {
    case TargetType::kX86:
    case TargetType::kHost:
S
Superjomn 已提交
60 61 62 63
      TargetWrapper<TARGET(kHost)>::MemcpySync(dst, src, size,
                                               IoDirection::DtoD);
      break;

64
    case TargetType::kCUDA:
S
Superjomn 已提交
65 66 67
      TargetWrapper<TARGET(kCUDA)>::MemcpySync(dst, src, size,
                                               IoDirection::DtoD);
      break;
68 69
    default:
      LOG(FATAL) << "unsupported type";
S
Superjomn 已提交
70 71 72
  }
}

S
superjomn 已提交
73 74 75
// Memory buffer manager.
class Buffer {
 public:
S
update  
superjomn 已提交
76
  Buffer() = default;
S
superjomn 已提交
77 78 79
  Buffer(TargetType target, size_t size) : space_(size), target_(target) {}

  void* data() const { return data_; }
S
Superjomn 已提交
80 81
  TargetType target() const { return target_; }
  size_t space() const { return space_; }
S
superjomn 已提交
82 83 84 85

  void ResetLazy(TargetType target, size_t size) {
    if (target != target_ || space_ < size) {
      Free();
86 87 88
      data_ = TargetMalloc(target, size);
      target_ = target;
      space_ = size;
S
superjomn 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101
    }
  }

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

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

S
Superjomn 已提交
102 103 104 105
  void CopyDataFrom(const Buffer& other, size_t nbytes) {
    target_ = other.target_;
    ResizeLazy(nbytes);
    // TODO(Superjomn) support copy between different targets.
S
Superjomn 已提交
106
    TargetCopy(target_, data_, other.data_, nbytes);
S
Superjomn 已提交
107 108
  }

S
superjomn 已提交
109
 private:
S
Superjomn 已提交
110
  // memory it actually malloced.
S
superjomn 已提交
111 112 113 114 115 116 117
  size_t space_{0};
  void* data_{nullptr};
  TargetType target_{TargetType::kHost};
};

}  // namespace lite
}  // namespace paddle