allocator_facade.cc 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "paddle/fluid/memory/allocation/allocator.h"
S
sneaxiy 已提交
16
#include <gflags/gflags.h>
17
#include <map>
Y
Yu Yang 已提交
18
#include <string>
S
sneaxiy 已提交
19
#include <unordered_map>
S
sneaxiy 已提交
20
#include <utility>
21 22 23
#include <vector>
#include "paddle/fluid/memory/allocation/aligned_allocator.h"
#include "paddle/fluid/memory/allocation/allocator_facade.h"
Y
Yu Yang 已提交
24
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
Y
Yu Yang 已提交
25
#include "paddle/fluid/memory/allocation/auto_increment_allocator.h"
26
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
Y
Yu Yang 已提交
27
#include "paddle/fluid/memory/allocation/conditional_allocator.h"
28
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
Y
Yu Yang 已提交
29
#include "paddle/fluid/memory/allocation/legacy_allocator.h"
30
#include "paddle/fluid/memory/allocation/locked_allocator.h"
S
sneaxiy 已提交
31
#include "paddle/fluid/memory/allocation/retry_allocator.h"
S
sneaxiy 已提交
32
#include "paddle/fluid/platform/cpu_info.h"
S
sneaxiy 已提交
33
#include "paddle/fluid/platform/enforce.h"
34 35 36
#include "paddle/fluid/platform/place.h"
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/memory/allocation/cuda_allocator.h"
S
sneaxiy 已提交
37 38 39
#include "paddle/fluid/memory/allocation/pinned_allocator.h"
#include "paddle/fluid/platform/cuda_device_guard.h"
#include "paddle/fluid/platform/gpu_info.h"
40 41
#endif

S
sneaxiy 已提交
42
DEFINE_int64(
S
sneaxiy 已提交
43 44 45 46
    gpu_allocator_retry_time, 0,
    "The retry time (milliseconds) when allocator fails "
    "to allocate memory. No retry if this value is not greater than 0");

47 48 49 50
namespace paddle {
namespace memory {
namespace allocation {

Z
Zeng Jinle 已提交
51 52 53 54 55 56 57 58 59 60 61
static inline std::shared_ptr<Allocator> WrapRetryAllocator(
    std::shared_ptr<Allocator> allocator, int64_t retry_time) {
  if (retry_time > 0) {
    auto* retry_allocator =
        new RetryAllocator(std::move(allocator), retry_time);
    allocator.reset(retry_allocator);
  }

  return allocator;
}

Y
Yu Yang 已提交
62
// TODO(yy): Dirty code here. This class should be configurable in runtime.
Y
Yu Yang 已提交
63
class CPUManagedAllocator : public Allocator {
Y
Yu Yang 已提交
64
 public:
Y
Yu Yang 已提交
65
  CPUManagedAllocator() : normal_allocator_(new CPUAllocator()) {}
Y
Yu Yang 已提交
66

Y
Yu Yang 已提交
67
  bool IsAllocThreadSafe() const override { return true; }
Y
Yu Yang 已提交
68

Y
Yu Yang 已提交
69
 protected:
70 71
  Allocation* AllocateImpl(size_t size) override {
    return normal_allocator_->Allocate(size).release();
Y
Yu Yang 已提交
72 73
  }

Y
Yu Yang 已提交
74
 private:
Y
Yu Yang 已提交
75
  std::shared_ptr<Allocator> normal_allocator_;
Y
Yu Yang 已提交
76 77
};

Y
Yu Yang 已提交
78
// TODO(yy): Dirty code here. This class should be configurable in runtime.
Y
Yu Yang 已提交
79
class ChunkedAllocator : public Allocator {
80
 public:
Y
Yu Yang 已提交
81 82 83
  explicit ChunkedAllocator(std::unique_ptr<Allocator> system_allocator,
                            size_t max_chunk_size, size_t capacity = 1,
                            int64_t retry_time = -1)
S
sneaxiy 已提交
84
      : max_chunk_size_(max_chunk_size), retry_time_(retry_time) {
Y
Yu Yang 已提交
85
    raw_allocator_ = std::move(system_allocator);
S
sneaxiy 已提交
86 87 88 89 90

    if (max_chunk_size_ == 0) {
      default_allocator_ = raw_allocator_;
    } else {
      if (capacity == 1) {
M
minqiyang 已提交
91 92
        VLOG(1) << "Create BestFitAllocator with chunk_size "
                << max_chunk_size_;
Y
Yu Yang 已提交
93
        default_allocator_ = CreateAllocatorWithChunk();
S
sneaxiy 已提交
94
      } else {
M
minqiyang 已提交
95 96
        VLOG(1) << "Create AutoIncrementAllocator with chunk_size "
                << max_chunk_size_ << " and capacity " << capacity;
S
sneaxiy 已提交
97
        default_allocator_ = std::make_shared<AutoIncrementAllocator>(
G
Gabor Buella 已提交
98
            [this] { return CreateAllocatorWithChunk(); }, capacity);
S
sneaxiy 已提交
99 100
      }
    }
Y
Yu Yang 已提交
101 102 103

    auto* cond_allocator = new ConditionalAllocator();
    cond_allocator
104 105
        ->AddAllocator([this](size_t size) { return size < max_chunk_size_; },
                       default_allocator_)
Y
Yu Yang 已提交
106
        .AddAllocator(
107
            [](size_t size) {
Y
Yu Yang 已提交
108 109 110 111
              return true;  // default case
            },
            raw_allocator_);
    default_allocator_.reset(cond_allocator);
Y
Yu Yang 已提交
112
  }
113

Y
Yu Yang 已提交
114
  ~ChunkedAllocator() override {
115
    // Specify destruct order.
Y
Yu Yang 已提交
116 117 118 119 120
    default_allocator_.reset();
    chunks_.clear();
    raw_allocator_.reset();
  }

Y
Yu Yang 已提交
121
  std::shared_ptr<Allocator> CreateAllocatorWithChunk() {
Y
Yu Yang 已提交
122 123
    chunks_.emplace_back(raw_allocator_->Allocate(max_chunk_size_));
    auto* allocation = chunks_.back().get();
Z
Zeng Jinle 已提交
124 125
    std::shared_ptr<Allocator> allocator(new LockedAllocator(
        std::shared_ptr<Allocator>(new BestFitAllocator(allocation))));
S
sneaxiy 已提交
126

Z
Zeng Jinle 已提交
127
    allocator = WrapRetryAllocator(allocator, retry_time_);
Y
Yu Yang 已提交
128 129

    return std::make_shared<AlignedAllocator<64u>>(std::move(allocator));
130
  }
S
sneaxiy 已提交
131

Y
Yu Yang 已提交
132 133
  bool IsAllocThreadSafe() const override { return true; }

Y
Yu Yang 已提交
134
 protected:
135 136
  Allocation* AllocateImpl(size_t size) override {
    return default_allocator_->Allocate(size).release();
Y
Yu Yang 已提交
137 138
  }

S
sneaxiy 已提交
139
 protected:
Y
Yu Yang 已提交
140
  size_t max_chunk_size_;
S
sneaxiy 已提交
141
  int64_t retry_time_;
Y
Yu Yang 已提交
142
  std::vector<AllocationPtr> chunks_;
Y
Yu Yang 已提交
143 144
  std::shared_ptr<Allocator> raw_allocator_;
  std::shared_ptr<Allocator> default_allocator_;
Y
Yu Yang 已提交
145
};
S
sneaxiy 已提交
146 147 148

#ifdef PADDLE_WITH_CUDA

Y
Yu Yang 已提交
149
class CUDAChunkedAllocator : public ChunkedAllocator {
S
sneaxiy 已提交
150
 public:
Y
Yu Yang 已提交
151 152 153 154 155
  explicit CUDAChunkedAllocator(int dev_id)
      : ChunkedAllocator(std::unique_ptr<Allocator>(
                             new CUDAAllocator(platform::CUDAPlace(dev_id))),
                         GetMaxChunkSize(dev_id), GetCapcity(dev_id),
                         GetRetryTime()) {}
S
sneaxiy 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

 private:
  static size_t GetMaxChunkSize(int dev_id) {
    platform::CUDADeviceGuard guard(dev_id);
    return platform::GpuMaxChunkSize();
  }

  static size_t GetCapcity(int dev_id) {
    platform::CUDADeviceGuard guard(dev_id);
    size_t available, total;
    platform::GpuMemoryUsage(&available, &total);
    size_t max_chunk_size = platform::GpuMaxChunkSize();
    return max_chunk_size == 0 ? 0 : available / max_chunk_size;
  }

  static int64_t GetRetryTime() { return FLAGS_gpu_allocator_retry_time; }
};

Y
Yu Yang 已提交
174
class CUDAPinnedChunkedAllocator : public ChunkedAllocator {
S
sneaxiy 已提交
175
 public:
Y
Yu Yang 已提交
176 177 178 179
  CUDAPinnedChunkedAllocator()
      : ChunkedAllocator(std::unique_ptr<Allocator>(new CPUPinnedAllocator()),
                         platform::CUDAPinnedMaxChunkSize(), GetCapacity(),
                         -1) {}  // never retry
S
sneaxiy 已提交
180 181 182 183 184 185 186 187 188

 private:
  static size_t GetCapacity() {
    size_t total = platform::CpuTotalPhysicalMemory();
    size_t max_chunk_size = platform::CUDAPinnedMaxChunkSize();
    return max_chunk_size == 0 ? 0 : total / max_chunk_size;
  }
};

Y
Refine  
Yu Yang 已提交
189
#endif
Y
Yu Yang 已提交
190 191 192

class AllocatorFacadePrivate {
 public:
193
  AllocatorFacadePrivate() {
Z
Zeng Jinle 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    auto strategy = GetAllocatorStrategy();
    switch (strategy) {
      case AllocatorStrategy::kLegacy: {
        InitLegacyAllocator();
        break;
      }
      case AllocatorStrategy::kNaiveBestFit: {
        InitCPUAllocator();
        InitCUDAAllocator();
        InitCUDAPinnedAllocator();
        break;
      }
      default: {
        PADDLE_THROW("Unsupported allocator strategy: %d",
                     static_cast<int>(strategy));
      }
Y
Yu Yang 已提交
210
    }
Z
Zeng Jinle 已提交
211 212 213 214 215 216 217 218 219 220 221 222
    InitZeroSizeAllocators();
  }

  inline const std::shared_ptr<Allocator>& GetAllocator(
      const platform::Place& place, size_t size) {
    const auto& allocators = (size > 0 ? allocators_ : zero_size_allocators_);
    auto iter = allocators.find(place);
    if (iter == allocators.end()) {
      throw BadAlloc(
          string::Sprintf("No such allocator for the place, %s", place));
    }
    return iter->second;
223 224 225
  }

 private:
Y
Yu Yang 已提交
226 227 228 229 230 231
  void InitLegacyAllocator() {
    std::vector<platform::Place> places{platform::CPUPlace()};
#ifdef PADDLE_WITH_CUDA
    for (int dev_id = 0; dev_id < platform::GetCUDADeviceCount(); ++dev_id) {
      places.emplace_back(platform::CUDAPlace(dev_id));
    }
Y
Yu Yang 已提交
232
    places.emplace_back(platform::CUDAPinnedPlace());
Y
Yu Yang 已提交
233 234 235 236 237 238
#endif
    for (auto& p : places) {
      allocators_[p] = std::make_shared<LegacyAllocator>(p);
    }
  }

239
  void InitCPUAllocator() {
Y
Yu Yang 已提交
240
    allocators_[platform::CPUPlace()] = std::make_shared<CPUManagedAllocator>();
241 242 243 244
  }

  void InitCUDAAllocator() {
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
245 246
    int device_count = platform::GetCUDADeviceCount();
    for (int dev_id = 0; dev_id < device_count; ++dev_id) {
247
      allocators_[platform::CUDAPlace(dev_id)] =
Y
Yu Yang 已提交
248
          std::make_shared<CUDAChunkedAllocator>(dev_id);
249 250 251
    }
#endif
  }
Y
Yu Yang 已提交
252

S
sneaxiy 已提交
253 254 255
  void InitCUDAPinnedAllocator() {
#ifdef PADDLE_WITH_CUDA
    allocators_[platform::CUDAPinnedPlace()] =
Y
Yu Yang 已提交
256
        std::make_shared<CUDAPinnedChunkedAllocator>();
S
sneaxiy 已提交
257 258 259
#endif
  }

Z
Zeng Jinle 已提交
260 261 262 263 264
  class ZeroSizeAllocator : public Allocator {
   public:
    explicit ZeroSizeAllocator(platform::Place place) : place_(place) {}

   protected:
265
    Allocation* AllocateImpl(size_t size) override {
Z
Zeng Jinle 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
      return new Allocation(nullptr, 0, place_);
    }

    void FreeImpl(Allocation* allocation) override { delete allocation; }

   private:
    platform::Place place_;
  };

  void InitZeroSizeAllocators() {
    std::vector<platform::Place> places;
    places.emplace_back(platform::CPUPlace());
#ifdef PADDLE_WITH_CUDA
    int device_count = platform::GetCUDADeviceCount();
    for (int dev_id = 0; dev_id < device_count; ++dev_id) {
      places.emplace_back(platform::CUDAPlace(dev_id));
    }
    places.emplace_back(platform::CUDAPinnedPlace());
#endif

    for (auto& p : places) {
      zero_size_allocators_[p] = std::make_shared<ZeroSizeAllocator>(p);
Y
Yu Yang 已提交
288 289
    }
  }
Z
Zeng Jinle 已提交
290 291 292 293

 private:
  std::map<platform::Place, std::shared_ptr<Allocator>> allocators_;
  std::map<platform::Place, std::shared_ptr<Allocator>> zero_size_allocators_;
294 295
};

Y
Refine  
Yu Yang 已提交
296
// Pimpl. Make interface clean.
297 298 299 300 301 302 303 304 305
AllocatorFacade::AllocatorFacade() : m_(new AllocatorFacadePrivate()) {}
AllocatorFacade::~AllocatorFacade() { delete m_; }

AllocatorFacade& AllocatorFacade::Instance() {
  static AllocatorFacade instance;
  return instance;
}

std::shared_ptr<Allocation> AllocatorFacade::AllocShared(
306 307
    const platform::Place& place, size_t size) {
  return std::shared_ptr<Allocation>(Alloc(place, size));
308 309
}

310 311 312
AllocationPtr AllocatorFacade::Alloc(const platform::Place& place,
                                     size_t size) {
  return m_->GetAllocator(place, size)->Allocate(size);
313 314 315 316 317
}

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