Storage.cpp 2.8 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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/utils/Util.h"
#include "Allocator.h"
#include "Storage.h"

P_DEFINE_int32(pool_limit_size, 536870912,
               "maximum memory size managed by a memory pool, default is 512M");

namespace paddle {

// Initialization StorageEngine singleton.
// Other modules may rely on storage management,
// so StorageEngine need to be initialized before other modules.
28 29
static InitFunction __init_storage_engine([](){StorageEngine::singleton();},
                                          std::numeric_limits<int>::max());
Z
zhangjinchao01 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

StorageEngine::StorageEngine() : cpuAllocator_(nullptr) {
}

StorageEngine::~StorageEngine() {
  if (cpuAllocator_) {
    delete cpuAllocator_;
  }
  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_);
    if (deviceId < static_cast<int>(gpuAllocator_.size())
        && (gpuAllocator_[deviceId] != nullptr)) {
      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 =
        "gpu" + std::to_string(deviceId) + std::string("_pool");
      gpuAllocator_[deviceId] = new PoolAllocator(
        new GpuAllocator(), FLAGS_pool_limit_size, name);
    }
    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(
          new CudaHostAllocator(), FLAGS_pool_limit_size, "cuda_host_pool");
      } else {
        cpuAllocator_ = new PoolAllocator(
          new CpuAllocator(), FLAGS_pool_limit_size, "cpu_pool");
      }
    }
    return cpuAllocator_;
  }
}

}  // namespace paddle