aligned_allocator.h 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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>
S
sneaxiy 已提交
17
#include <utility>
18 19 20 21 22 23
#include "paddle/fluid/memory/allocation/allocator.h"

namespace paddle {
namespace memory {
namespace allocation {

Y
Yu Yang 已提交
24 25 26 27 28 29 30
// The aligned allocation and allocator will wrap a managed allocator,
// and returns the aligned pointer.
//
// NOTE(yy): For speed reason, I just use a template parameter to get
// alignment, however, it can be an private member if necessary.
//
// NOTE(yy): kAlignment must be 2^N. a `static_assert` should be added.
31 32
template <size_t kAlignment>
class AlignedAllocation : public Allocation {
S
sneaxiy 已提交
33 34 35
  static_assert(kAlignment > 0 && (kAlignment & (kAlignment - 1)) == 0,
                "kAlignment must be 2^N");

36
 public:
Y
Yu Yang 已提交
37
  AlignedAllocation(AllocationPtr&& underlying_allocation, size_t size)
Y
Yu Yang 已提交
38 39
      : Allocation(AlignedPtr(underlying_allocation->ptr()),
                   size + kAlignment - Offset(underlying_allocation->ptr()),
40 41 42 43 44
                   underlying_allocation->place()),
        underlying_allocation_(std::move(underlying_allocation)) {}

 private:
  static void* AlignedPtr(void* ptr) {
Y
Yu Yang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(ptr) +
                                   Offset(ptr));
  }

  // Offset to aligned pointer.
  // if ptr is already aligned, returns 0.
  static size_t Offset(void* ptr) {
    auto ptr_addr = reinterpret_cast<intptr_t>(ptr);
    intptr_t aligned_addr = (ptr_addr & ~(kAlignment - 1));
    intptr_t diff = aligned_addr - ptr_addr;
    if (diff == 0) {
      return 0;
    } else {
      return kAlignment + diff;
    }
60 61
  }

Y
Yu Yang 已提交
62
  AllocationPtr underlying_allocation_;
63 64
};

Y
Yu Yang 已提交
65 66 67 68 69 70 71 72
// Thin aligned allocator is trivial and used to generate a small size binary.
//
// NOTE(yy): This is a trick to make a template class. This class extract the
// common code into a `thin` class. So if there are multiple specification of
// the template class, the binary size will not extended too much.
//
// NOTE(yy): This could be an over design. If it harms readability of code, it
// could be removed later.
Y
Yu Yang 已提交
73
class ThinAlignedAllocator : public Allocator {
74 75
 public:
  explicit ThinAlignedAllocator(
Y
Yu Yang 已提交
76
      std::shared_ptr<Allocator> underlyning_allocator);
Y
Yu Yang 已提交
77

S
sneaxiy 已提交
78 79
  bool IsAllocThreadSafe() const;

80
 protected:
Y
Yu Yang 已提交
81
  std::shared_ptr<Allocator> underlying_allocator_;
82 83
};

Y
Yu Yang 已提交
84 85
// An aligned allocator will allocate `size+kAlignment` allocation and adjust
// the pointer offset.
86 87 88 89
template <size_t kAlignment>
class AlignedAllocator : public ThinAlignedAllocator {
 public:
  using ThinAlignedAllocator::ThinAlignedAllocator;
Y
Yu Yang 已提交
90 91

 protected:
92 93
  Allocation* AllocateImpl(size_t size) override {
    auto raw_allocation = underlying_allocator_->Allocate(size + kAlignment);
Y
Yu Yang 已提交
94
    return new AlignedAllocation<kAlignment>(std::move(raw_allocation), size);
95
  }
Z
Zeng Jinle 已提交
96 97

  void FreeImpl(Allocation* allocation) override { delete allocation; }
98 99 100 101 102
};

}  // namespace allocation
}  // namespace memory
}  // namespace paddle