You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.6 KiB
148 lines
4.6 KiB
7 years ago
|
/* Copyright (c) 2016 PaddlePaddle Authors. 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. */
|
||
|
|
||
|
#pragma once
|
||
7 years ago
|
|
||
7 years ago
|
#include <condition_variable>
|
||
|
#include <functional>
|
||
7 years ago
|
#include <future>
|
||
7 years ago
|
#include <mutex>
|
||
|
#include <queue>
|
||
|
#include <thread>
|
||
7 years ago
|
#include <vector>
|
||
7 years ago
|
#include "glog/logging.h"
|
||
|
#include "paddle/platform/enforce.h"
|
||
7 years ago
|
#include "paddle/platform/macros.h" // for DISABLE_COPY_AND_ASSIGN
|
||
7 years ago
|
|
||
|
namespace paddle {
|
||
|
namespace framework {
|
||
|
|
||
7 years ago
|
// ThreadPool maintains a queue of tasks, and runs them using a fixed
|
||
|
// number of threads.
|
||
7 years ago
|
class ThreadPool {
|
||
|
public:
|
||
7 years ago
|
using Task = std::packaged_task<std::unique_ptr<platform::EnforceNotMet>()>;
|
||
7 years ago
|
|
||
7 years ago
|
// Returns the singleton of ThreadPool.
|
||
|
static ThreadPool* GetInstance();
|
||
7 years ago
|
|
||
7 years ago
|
~ThreadPool();
|
||
7 years ago
|
|
||
7 years ago
|
// Returns the number of threads created by the constructor.
|
||
|
size_t Threads() const { return total_threads_; }
|
||
7 years ago
|
|
||
7 years ago
|
// Returns the number of currently idle threads.
|
||
|
size_t IdleThreads() {
|
||
7 years ago
|
std::unique_lock<std::mutex> lock(mutex_);
|
||
7 years ago
|
return idle_threads_;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Run pushes a function to the task queue and returns a std::future
|
||
|
// object. To wait for the completion of the task, call
|
||
|
// std::future::wait().
|
||
7 years ago
|
template <typename Callback>
|
||
|
std::future<void> Run(Callback fn) {
|
||
7 years ago
|
auto f = this->RunAndGetException(fn);
|
||
|
return std::async(std::launch::deferred, ExceptionHandler(std::move(f)));
|
||
|
}
|
||
|
|
||
|
template <typename Callback>
|
||
|
std::future<std::unique_ptr<platform::EnforceNotMet>> RunAndGetException(
|
||
|
Callback fn) {
|
||
7 years ago
|
std::unique_lock<std::mutex> lock(mutex_);
|
||
7 years ago
|
Task task([fn]() -> std::unique_ptr<platform::EnforceNotMet> {
|
||
|
try {
|
||
|
fn();
|
||
|
return nullptr;
|
||
|
} catch (platform::EnforceNotMet ex) {
|
||
|
return std::unique_ptr<platform::EnforceNotMet>(
|
||
|
new platform::EnforceNotMet(ex));
|
||
|
} catch (...) {
|
||
|
LOG(FATAL)
|
||
|
<< "Unexpected exception is catched in thread pool. All "
|
||
|
"throwable exception in Fluid should be an EnforceNotMet.";
|
||
|
}
|
||
|
});
|
||
|
std::future<std::unique_ptr<platform::EnforceNotMet>> f = task.get_future();
|
||
7 years ago
|
tasks_.push(std::move(task));
|
||
7 years ago
|
lock.unlock();
|
||
|
scheduled_.notify_one();
|
||
7 years ago
|
return f;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Wait until all the tasks are completed.
|
||
|
void Wait();
|
||
7 years ago
|
|
||
|
private:
|
||
7 years ago
|
struct ExceptionHandler {
|
||
|
mutable std::future<std::unique_ptr<platform::EnforceNotMet>> future_;
|
||
|
explicit ExceptionHandler(
|
||
|
std::future<std::unique_ptr<platform::EnforceNotMet>>&& f)
|
||
|
: future_(std::move(f)) {}
|
||
|
void operator()() const {
|
||
|
auto ex = this->future_.get();
|
||
|
if (ex != nullptr) {
|
||
|
LOG(FATAL) << "The exception is thrown inside the thread pool. You "
|
||
|
"should use RunAndGetException to handle the exception.\n"
|
||
|
"The default exception handler is LOG(FATAL)."
|
||
|
<< ex->what();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
DISABLE_COPY_AND_ASSIGN(ThreadPool);
|
||
7 years ago
|
|
||
7 years ago
|
explicit ThreadPool(int num_threads);
|
||
7 years ago
|
|
||
7 years ago
|
// If the task queue is empty and avaialbe is equal to the number of
|
||
|
// threads, means that all tasks are completed. Note: this function
|
||
|
// is not thread-safe. Returns true if all tasks are completed.
|
||
|
// Note: don't delete the data member total_threads_ and use
|
||
|
// threads_.size() instead; because you'd need to lock the mutex
|
||
|
// before accessing threads_.
|
||
|
bool Done() { return tasks_.empty() && idle_threads_ == total_threads_; }
|
||
7 years ago
|
|
||
7 years ago
|
// The constructor starts threads to run TaskLoop, which retrieves
|
||
|
// and runs tasks from the queue.
|
||
|
void TaskLoop();
|
||
|
|
||
|
// Init is called by GetInstance.
|
||
|
static void Init();
|
||
7 years ago
|
|
||
|
private:
|
||
7 years ago
|
static std::unique_ptr<ThreadPool> threadpool_;
|
||
|
static std::once_flag init_flag_;
|
||
7 years ago
|
|
||
|
std::vector<std::unique_ptr<std::thread>> threads_;
|
||
7 years ago
|
const size_t total_threads_;
|
||
|
size_t idle_threads_;
|
||
|
|
||
|
std::queue<Task> tasks_;
|
||
7 years ago
|
std::mutex mutex_;
|
||
7 years ago
|
bool running_;
|
||
7 years ago
|
std::condition_variable scheduled_;
|
||
|
std::condition_variable completed_;
|
||
|
};
|
||
|
|
||
7 years ago
|
// Run a function asynchronously.
|
||
|
// NOTE: The function must return void. If the function need to return a value,
|
||
|
// you can use lambda to capture a value pointer.
|
||
7 years ago
|
template <typename Callback>
|
||
|
std::future<void> Async(Callback callback) {
|
||
|
return ThreadPool::GetInstance()->Run(callback);
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
} // namespace framework
|
||
|
} // namespace paddle
|