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>
S
sneaxiy 已提交
18 19 20
#include <cuda.h>
#include <cuda_runtime.h>
#include <functional>
S
fix bug  
sneaxiy 已提交
21
#include <future>  // NOLINT
S
sneaxiy 已提交
22
#include <memory>
S
fix bug  
sneaxiy 已提交
23 24 25
#include <mutex>  // NOLINT

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

namespace paddle {
namespace platform {

S
fix bug  
sneaxiy 已提交
30 31
// NOTE(zjl): clean StreamCallbackManager to make compilation faster
// Make StreamCallbackManager thread-safe
S
sneaxiy 已提交
32 33
class StreamCallbackManager {
 public:
S
sneaxiy 已提交
34
  explicit StreamCallbackManager(const cudaStream_t stream);
S
sneaxiy 已提交
35

S
fix bug  
sneaxiy 已提交
36 37
  ~StreamCallbackManager();

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

S
sneaxiy 已提交
40
  void Wait() const;
S
sneaxiy 已提交
41 42 43

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

}  // namespace platform
}  // namespace paddle