Skip to content

Commit

Permalink
fix gpu callback (#40445)
Browse files Browse the repository at this point in the history
* fix gpu conetxt callback

* fix gpu callback

* fix callback early destruct problem
  • Loading branch information
zhiqiu authored Mar 14, 2022
1 parent bb80196 commit 2c21d24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions paddle/phi/backends/gpu/gpu_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,17 @@ struct GPUContext::Impl {
}

void AddStreamCallback(const std::function<void()>& callback) const {
// TODO(wilber): Do we need ThreadPool?
auto* func = new std::function<void()>([this, callback] {
// NOTE(zhiqiu): better use threadpool here, otherwise "std::async" may
// launch too
// many threads and result in thread oversubscription.
auto* callback_func = new std::function<void()>(std::move(callback));
auto* func = new std::function<void()>([this, callback_func] {
std::lock_guard<std::mutex> lock(stream_call_back_mtx_);
last_future_ = std::async(std::launch::deferred, [&]() { callback(); });
VLOG(4) << "Stream callback";
last_future_ = std::async(std::launch::async, [callback_func]() {
std::unique_ptr<std::function<void()>> releaser(callback_func);
(*callback_func)();
});
});

#ifdef PADDLE_WITH_HIP
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/kernels/funcs/concat_and_split_functor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ struct ConcatFunctor<phi::GPUContext, T> {
auto* data_alloc_released = data_alloc.release();
auto* col_alloc_released = col_alloc.release();
context.AddStreamCallback([data_alloc_released, col_alloc_released] {
VLOG(4) << "Delete cuda pinned at " << data_alloc_released;
VLOG(4) << "Delete cuda pinned at " << col_alloc_released;
paddle::memory::allocation::Allocator::AllocationDeleter(
data_alloc_released);
paddle::memory::allocation::Allocator::AllocationDeleter(
Expand Down

0 comments on commit 2c21d24

Please sign in to comment.