resource_pool.h 2.8 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 23 24 25 26 27 28 29 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
// Copyright (c) 2020 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 <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/macros.h"

namespace paddle {
namespace platform {

template <typename T>
class ResourcePool : public std::enable_shared_from_this<ResourcePool<T>> {
 private:
  struct ResourceDeleter {
   public:
    explicit ResourceDeleter(ResourcePool<T> *pool)
        : instance_(pool->shared_from_this()) {}

    void operator()(T *ptr) const { instance_->Restore(ptr); }

   private:
    std::shared_ptr<ResourcePool<T>> instance_;
  };

 public:
  static std::shared_ptr<ResourcePool<T>> Create(
      const std::function<T *()> &creator,
      const std::function<void(T *)> &deleter) {
    return std::shared_ptr<ResourcePool<T>>(
        new ResourcePool<T>(creator, deleter));
  }

  ~ResourcePool() {
    for (auto *ptr : instances_) {
      deleter_(ptr);
    }
  }

  std::shared_ptr<T> New() {
    std::lock_guard<std::mutex> guard(mtx_);
    T *obj = nullptr;
    if (instances_.empty()) {
      obj = creator_();
      PADDLE_ENFORCE_NOT_NULL(obj,
                              platform::errors::PermissionDenied(
G
GaoWei8 已提交
63
                                  "The creator should not return nullptr."));
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
      VLOG(10) << "Create new instance " << TypePtrName();
    } else {
      obj = instances_.back();
      instances_.pop_back();
      VLOG(10) << "Pop new instance " << TypePtrName()
               << " from pool, size=" << instances_.size();
    }
    return std::shared_ptr<T>(obj, ResourceDeleter(this));
  }

 private:
  static std::string TypePtrName() {
    return platform::demangle(typeid(T *).name());  // NOLINT
  }

 private:
  ResourcePool(const std::function<T *()> &creator,
               const std::function<void(T *)> &deleter)
      : creator_(creator), deleter_(deleter) {}

  void Restore(T *ptr) {
    std::lock_guard<std::mutex> guard(mtx_);
    instances_.emplace_back(ptr);
    VLOG(10) << "Restore " << TypePtrName()
             << " into pool, size=" << instances_.size();
  }

 private:
  std::vector<T *> instances_;
93 94
  const std::function<T *()> creator_;
  const std::function<void(T *)> deleter_;
95 96 97 98 99 100

  std::mutex mtx_;
};

}  // namespace platform
}  // namespace paddle