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 21 22 23 24 25
#include <cuda.h>
#include <cuda_runtime.h>
#include <functional>
#include <memory>

namespace paddle {
namespace platform {

S
sneaxiy 已提交
26
// NOTE(zjl): clean StreamCallback to make compilation faster
S
sneaxiy 已提交
27 28
class StreamCallbackManager {
 public:
S
sneaxiy 已提交
29
  explicit StreamCallbackManager(const cudaStream_t stream);
S
sneaxiy 已提交
30

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

S
sneaxiy 已提交
33
  void Wait() const;
S
sneaxiy 已提交
34 35 36

 private:
  const cudaStream_t stream_;
S
sneaxiy 已提交
37
  mutable std::unique_ptr<::ThreadPool> thread_pool_;
S
sneaxiy 已提交
38

S
sneaxiy 已提交
39
#if CUDA_VERSION >= 10000
S
sneaxiy 已提交
40
  static void CUDART_CB StreamCallbackFunc(void *user_data);
S
sneaxiy 已提交
41
#else
S
sneaxiy 已提交
42
  static void CUDART_CB StreamCallbackFunc(cudaStream_t stream,
S
sneaxiy 已提交
43
                                           cudaError_t status, void *user_data);
S
sneaxiy 已提交
44
#endif
S
sneaxiy 已提交
45 46 47 48
};

}  // namespace platform
}  // namespace paddle