Storage.cpp 2.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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 "Storage.h"
Y
Yu Yang 已提交
16
#include "Allocator.h"
17
#include "paddle/utils/StringUtil.h"
Y
Yu Yang 已提交
18
#include "paddle/utils/Util.h"
Z
zhangjinchao01 已提交
19

20 21 22
DEFINE_int32(pool_limit_size,
             536870912,
             "maximum memory size managed by a memory pool, default is 512M");
Z
zhangjinchao01 已提交
23 24 25 26 27 28

namespace paddle {

// Initialization StorageEngine singleton.
// Other modules may rely on storage management,
// so StorageEngine need to be initialized before other modules.
29
static InitFunction __init_storage_engine([]() { StorageEngine::singleton(); },
30
                                          std::numeric_limits<int>::max());
Z
zhangjinchao01 已提交
31

32
StorageEngine::StorageEngine() : cpuAllocator_(nullptr) {}
Z
zhangjinchao01 已提交
33 34

StorageEngine::~StorageEngine() {
H
hedaoyuan 已提交
35
  delete cpuAllocator_;
Z
zhangjinchao01 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
  for (auto it : gpuAllocator_) {
    delete it;
  }
}

StorageEngine* StorageEngine::singleton() {
  static StorageEngine storage;
  return &storage;
}

PoolAllocator* StorageEngine::getGpuAllocator(int deviceId) {
  {
    // if gpuAllocator_ has been constructed
    ReadLockGuard guard(lock_);
50 51
    if (deviceId < static_cast<int>(gpuAllocator_.size()) &&
        (gpuAllocator_[deviceId] != nullptr)) {
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62 63
      return gpuAllocator_[deviceId];
    }
  }

  {
    // Construct gpuAllocator_
    std::lock_guard<RWLock> guard(lock_);
    if (deviceId >= static_cast<int>(gpuAllocator_.size())) {
      gpuAllocator_.resize(deviceId + 1);
    }
    if (gpuAllocator_[deviceId] == nullptr) {
      std::string name =
64
          "gpu" + str::to_string(deviceId) + std::string("_pool");
65 66
      gpuAllocator_[deviceId] =
          new PoolAllocator(new GpuAllocator(), FLAGS_pool_limit_size, name);
Z
zhangjinchao01 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }
    return gpuAllocator_[deviceId];
  }
}

PoolAllocator* StorageEngine::getCpuAllocator() {
  {
    // if cpuAllocator_ has been constructed
    ReadLockGuard guard(lock_);
    if (cpuAllocator_ != nullptr) {
      return cpuAllocator_;
    }
  }

  {
    // Construct cpuAllocator_
    std::lock_guard<RWLock> guard(lock_);
    if (cpuAllocator_ == nullptr) {
      if (FLAGS_use_gpu) {
        cpuAllocator_ = new PoolAllocator(
87
            new CudaHostAllocator(), FLAGS_pool_limit_size, "cuda_host_pool");
Z
zhangjinchao01 已提交
88 89
      } else {
        cpuAllocator_ = new PoolAllocator(
90
            new CpuAllocator(), FLAGS_pool_limit_size, "cpu_pool");
Z
zhangjinchao01 已提交
91 92 93 94 95 96 97
      }
    }
    return cpuAllocator_;
  }
}

}  // namespace paddle