stream_callback_manager.h 1.5 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 19

#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
20 21
#include <cuda.h>
#include <cuda_runtime.h>
22 23 24 25 26 27
#endif

#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>
#endif

S
sneaxiy 已提交
28
#include <functional>
S
fix bug  
sneaxiy 已提交
29
#include <future>  // NOLINT
S
sneaxiy 已提交
30
#include <memory>
S
fix bug  
sneaxiy 已提交
31 32 33
#include <mutex>  // NOLINT

#include "paddle/fluid/platform/enforce.h"
S
sneaxiy 已提交
34 35 36 37

namespace paddle {
namespace platform {

S
fix bug  
sneaxiy 已提交
38 39
// NOTE(zjl): clean StreamCallbackManager to make compilation faster
// Make StreamCallbackManager thread-safe
S
sneaxiy 已提交
40 41
class StreamCallbackManager {
 public:
42
  explicit StreamCallbackManager(const gpuStream_t stream);
S
sneaxiy 已提交
43

S
fix bug  
sneaxiy 已提交
44
  ~StreamCallbackManager() = default;
S
fix bug  
sneaxiy 已提交
45

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

S
sneaxiy 已提交
48
  void Wait() const;
S
sneaxiy 已提交
49 50

 private:
51
  const gpuStream_t stream_;
S
fix bug  
sneaxiy 已提交
52 53 54
  mutable ::ThreadPool thread_pool_;
  mutable std::mutex mtx_;
  mutable std::future<void> last_future_;
S
sneaxiy 已提交
55 56 57 58
};

}  // namespace platform
}  // namespace paddle