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

namespace paddle {
namespace memory {
namespace allocation {

Y
Yu Yang 已提交
23 24 25 26 27 28 29
// 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.
30 31
template <size_t kAlignment>
class AlignedAllocation : public Allocation {
S
sneaxiy 已提交
32 33 34
  static_assert(kAlignment > 0 && (kAlignment & (kAlignment - 1)) == 0,
                "kAlignment must be 2^N");

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

 private:
  static void* AlignedPtr(void* ptr) {
Y
Yu Yang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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;
    }
59 60
  }

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

Y
Yu Yang 已提交
64 65 66 67 68 69 70 71
// 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 已提交
72
class ThinAlignedAllocator : public Allocator {
73 74
 public:
  explicit ThinAlignedAllocator(
Y
Yu Yang 已提交
75
      std::shared_ptr<Allocator> underlyning_allocator);
Y
Yu Yang 已提交
76

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

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

Y
Yu Yang 已提交
83 84
// An aligned allocator will allocate `size+kAlignment` allocation and adjust
// the pointer offset.
85 86 87 88
template <size_t kAlignment>
class AlignedAllocator : public ThinAlignedAllocator {
 public:
  using ThinAlignedAllocator::ThinAlignedAllocator;
Y
Yu Yang 已提交
89
  AllocationPtr Allocate(size_t size, Attr attr) override {
90 91
    auto raw_allocation =
        underlying_allocator_->Allocate(size + kAlignment, attr);
Y
Yu Yang 已提交
92
    return AllocationPtr(
93 94 95 96 97 98 99
        new AlignedAllocation<kAlignment>(std::move(raw_allocation), size));
  }
};

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