fix cuda dev_ctx allocator cmake deps, test=develop (#19953)
parent
ebff68fa74
commit
37f76407b0
@ -1,47 +0,0 @@
|
||||
// Copyright (c) 2019 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.
|
||||
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocation.h"
|
||||
#include <utility>
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace memory {
|
||||
namespace allocation {
|
||||
|
||||
CUDADeviceContextAllocation::CUDADeviceContextAllocation(
|
||||
AllocationPtr allocation)
|
||||
: Allocation(allocation->ptr(), allocation->size(), allocation->place()),
|
||||
underlying_allocation_(std::move(allocation)) {}
|
||||
|
||||
CUDADeviceContextAllocation::~CUDADeviceContextAllocation() {
|
||||
PADDLE_ENFORCE_NOT_NULL(
|
||||
dev_ctx_, "Didn't set device context for CUDADeviceContextAllocation");
|
||||
auto *p_allocation = underlying_allocation_.release();
|
||||
VLOG(4) << "Adding callback to delete CUDADeviceContextAllocation at "
|
||||
<< p_allocation;
|
||||
dev_ctx_->AddStreamCallback([p_allocation] {
|
||||
VLOG(4) << "Delete CUDADeviceContextAllocation at " << p_allocation;
|
||||
AllocationDeleter()(p_allocation);
|
||||
});
|
||||
}
|
||||
|
||||
void CUDADeviceContextAllocation::SetCUDADeviceContext(
|
||||
const platform::CUDADeviceContext *dev_ctx) {
|
||||
dev_ctx_ = dev_ctx;
|
||||
}
|
||||
|
||||
} // namespace allocation
|
||||
} // namespace memory
|
||||
} // namespace paddle
|
@ -1,42 +0,0 @@
|
||||
// Copyright (c) 2019 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
|
||||
#include "paddle/fluid/memory/allocation/allocator.h"
|
||||
#include "paddle/fluid/platform/device_context.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace memory {
|
||||
namespace allocation {
|
||||
|
||||
/**
|
||||
* CUDADeviceContextAllocation is a wrapper of the underbeneath allocation.
|
||||
* CUDADeviceContextAllocation adds a CUDA stream callback for the underbeneath
|
||||
* allocation so that CUDADeviceContextAllocation can be used in a CUDA stream
|
||||
* which deletes allocation in the callback.
|
||||
*/
|
||||
class CUDADeviceContextAllocation : public Allocation {
|
||||
public:
|
||||
explicit CUDADeviceContextAllocation(AllocationPtr allocation);
|
||||
~CUDADeviceContextAllocation();
|
||||
void SetCUDADeviceContext(const platform::CUDADeviceContext *dev_ctx);
|
||||
|
||||
private:
|
||||
AllocationPtr underlying_allocation_;
|
||||
const platform::CUDADeviceContext *dev_ctx_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace allocation
|
||||
} // namespace memory
|
||||
} // namespace paddle
|
@ -1,66 +0,0 @@
|
||||
// Copyright (c) 2019 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.
|
||||
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h"
|
||||
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocation.h"
|
||||
#include "paddle/fluid/platform/cuda_device_guard.h"
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace memory {
|
||||
namespace allocation {
|
||||
|
||||
CUDADeviceContextAllocator::CUDADeviceContextAllocator(
|
||||
const platform::CUDAPlace place, cudaStream_t default_stream)
|
||||
: place_(place), default_stream_(default_stream) {
|
||||
platform::CUDADeviceGuard guard(place_.device);
|
||||
PADDLE_ENFORCE_CUDA_SUCCESS(
|
||||
cudaEventCreate(&event_, cudaEventDisableTiming),
|
||||
"Create event failed in CUDADeviceContextAllocator");
|
||||
}
|
||||
|
||||
CUDADeviceContextAllocator::~CUDADeviceContextAllocator() {
|
||||
if (event_) {
|
||||
platform::CUDADeviceGuard guard(place_.device);
|
||||
PADDLE_ENFORCE_CUDA_SUCCESS(
|
||||
cudaEventDestroy(event_),
|
||||
"Destory event failed in CUDADeviceContextAllocator destroctor");
|
||||
}
|
||||
}
|
||||
|
||||
Allocation *CUDADeviceContextAllocator::AllocateImpl(size_t size) {
|
||||
PADDLE_ENFORCE_NOT_NULL(
|
||||
default_stream_,
|
||||
"Didn't set default stream for CUDADeviceContextAllocator");
|
||||
platform::CUDADeviceGuard guard(place_.device);
|
||||
auto allocation =
|
||||
new CUDADeviceContextAllocation(memory::Alloc(place_, size));
|
||||
// Wait for the event on stream
|
||||
PADDLE_ENFORCE_CUDA_SUCCESS(
|
||||
cudaEventRecord(event_, default_stream_),
|
||||
"Failed to record event in CUDADeviceContextAllocator");
|
||||
PADDLE_ENFORCE_CUDA_SUCCESS(
|
||||
cudaStreamWaitEvent(default_stream_, event_, 0),
|
||||
"Failed to wait event in CUDADeviceContextAllocator");
|
||||
return allocation;
|
||||
}
|
||||
|
||||
void CUDADeviceContextAllocator::FreeImpl(Allocation *allocation) {
|
||||
delete allocation;
|
||||
}
|
||||
|
||||
} // namespace allocation
|
||||
} // namespace memory
|
||||
} // namespace paddle
|
@ -1,59 +0,0 @@
|
||||
// Copyright (c) 2019 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.
|
||||
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocator_pool.h"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocation.h"
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h"
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace memory {
|
||||
namespace allocation {
|
||||
|
||||
CUDADeviceContextAllocatorPool &CUDADeviceContextAllocatorPool::Instance() {
|
||||
static CUDADeviceContextAllocatorPool pool;
|
||||
return pool;
|
||||
}
|
||||
|
||||
AllocationPtr CUDADeviceContextAllocatorPool::Alloc(
|
||||
const platform::CUDADeviceContext &dev_ctx, size_t size) {
|
||||
auto iter =
|
||||
allocators_.find(boost::get<platform::CUDAPlace>(dev_ctx.GetPlace()));
|
||||
PADDLE_ENFORCE_EQ(iter != allocators_.end(), true,
|
||||
"CUDADeviceContextAllocatorPool initialization error");
|
||||
auto &allocator = iter->second;
|
||||
AllocationPtr allocation = allocator->Allocate(size);
|
||||
static_cast<CUDADeviceContextAllocation *>(allocation.get())
|
||||
->SetCUDADeviceContext(&dev_ctx);
|
||||
return allocation;
|
||||
}
|
||||
|
||||
CUDADeviceContextAllocatorPool::CUDADeviceContextAllocatorPool() {
|
||||
std::vector<int> devices = platform::GetSelectedDevices();
|
||||
for (int i : devices) {
|
||||
auto place = platform::CUDAPlace(i);
|
||||
auto compute_stream =
|
||||
platform::DeviceContextPool::Instance().GetByPlace(place)->stream();
|
||||
auto allocator = std::shared_ptr<CUDADeviceContextAllocator>(
|
||||
new CUDADeviceContextAllocator(place, compute_stream));
|
||||
allocators_.insert(make_pair(place, allocator));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace allocation
|
||||
} // namespace memory
|
||||
} // namespace paddle
|
@ -1,48 +0,0 @@
|
||||
// Copyright (c) 2019 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
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include "paddle/fluid/memory/allocation/allocator.h"
|
||||
#include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h"
|
||||
#include "paddle/fluid/platform/device_context.h"
|
||||
#include "paddle/fluid/platform/place.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace memory {
|
||||
namespace allocation {
|
||||
|
||||
/**
|
||||
* CUDADeviceContextAllocatorPool is a singletion stores mapping from
|
||||
* CUDAPlace(s) to std::shared_ptr<CUDADeviceContextAllocator>. When a
|
||||
* CUDADeviceContext's compute stream isn't default stream, it can call this
|
||||
* class to allocate GPU memory which will be released by a callback after
|
||||
* stream execution.
|
||||
*/
|
||||
class CUDADeviceContextAllocatorPool {
|
||||
public:
|
||||
static CUDADeviceContextAllocatorPool &Instance();
|
||||
|
||||
AllocationPtr Alloc(const platform::CUDADeviceContext &dev_ctx, size_t size);
|
||||
|
||||
private:
|
||||
CUDADeviceContextAllocatorPool();
|
||||
std::map<platform::CUDAPlace, std::shared_ptr<CUDADeviceContextAllocator>>
|
||||
allocators_;
|
||||
};
|
||||
|
||||
} // namespace allocation
|
||||
} // namespace memory
|
||||
} // namespace paddle
|
Loading…
Reference in new issue