stream_callback_manager.h 1.4 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#pragma once

P
peizhilin 已提交
17
#include <ThreadPool.h>
18
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
19 20
#include <cuda.h>
#include <cuda_runtime.h>
21
#endif
S
sneaxiy 已提交
22
#include <functional>
S
fix bug  
sneaxiy 已提交
23
#include <future>  // NOLINT
S
sneaxiy 已提交
24
#include <memory>
S
fix bug  
sneaxiy 已提交
25 26 27
#include <mutex>  // NOLINT

#include "paddle/fluid/platform/enforce.h"
S
sneaxiy 已提交
28 29 30 31

namespace paddle {
namespace platform {

S
fix bug  
sneaxiy 已提交
32 33
// NOTE(zjl): clean StreamCallbackManager to make compilation faster
// Make StreamCallbackManager thread-safe
34
template <typename Stream>
S
sneaxiy 已提交
35 36
class StreamCallbackManager {
 public:
37
  explicit StreamCallbackManager(const Stream stream);
S
sneaxiy 已提交
38

S
fix bug  
sneaxiy 已提交
39
  ~StreamCallbackManager() = default;
S
fix bug  
sneaxiy 已提交
40

S
sneaxiy 已提交
41
  void AddCallback(std::function<void()> callback) const;
S
sneaxiy 已提交
42

S
sneaxiy 已提交
43
  void Wait() const;
S
sneaxiy 已提交
44 45

 private:
46
  const Stream stream_;
S
fix bug  
sneaxiy 已提交
47 48 49
  mutable ::ThreadPool thread_pool_;
  mutable std::mutex mtx_;
  mutable std::future<void> last_future_;
S
sneaxiy 已提交
50 51 52 53
};

}  // namespace platform
}  // namespace paddle