-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CUDAPinnedPlace #9380
Add CUDAPinnedPlace #9380
Changes from 5 commits
18eb773
158d6c4
ab601c1
58a9f9f
ffa6397
2514d70
766c740
e099b18
51c22fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ limitations under the License. */ | |
|
||
#include "paddle/fluid/memory/detail/system_allocator.h" | ||
#include "paddle/fluid/platform/assert.h" | ||
#include "paddle/fluid/platform/cpu_info.h" | ||
#include "paddle/fluid/platform/enforce.h" | ||
#include "paddle/fluid/platform/gpu_info.h" | ||
|
||
|
@@ -123,20 +124,22 @@ bool GPUAllocator::UseGpu() const { return true; } | |
// memory. It’s locked to a physical address. | ||
void* CUDAPinnedAllocator::Alloc(size_t& index, size_t size) { | ||
if (size <= 0) return nullptr; | ||
void* p; | ||
// NOTE: here, we use GpuMaxAllocSize() as the maximum memory size | ||
|
||
// NOTE: here, we use CUDAPinnedMaxAllocSize as the maximum memory size | ||
// of host pinned allocation. Allocates too much would reduce | ||
// the amount of memory available to the underlying system for paging. | ||
|
||
size_t usable = paddle::platform::GpuMaxAllocSize() - fallback_alloc_size_; | ||
size_t usable = | ||
paddle::platform::CUDAPinnedMaxAllocSize() - cuda_pinnd_alloc_size_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems default pinned memory max size is determined by system settings, can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your review!
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the detailed information, quiet useful! |
||
|
||
if (size > usable) return nullptr; | ||
|
||
void* p; | ||
// PINNED memory is visible to all CUDA contexts. | ||
cudaError_t result = cudaMallocHost(&p, size); | ||
|
||
if (result == cudaSuccess) { | ||
index = 1; | ||
fallback_alloc_size_ += size; | ||
index = 1; // PINNED memory | ||
cuda_pinnd_alloc_size_ += size; | ||
return p; | ||
} | ||
|
||
|
@@ -147,8 +150,8 @@ void CUDAPinnedAllocator::Free(void* p, size_t size, size_t index) { | |
cudaError_t err; | ||
PADDLE_ASSERT(index == 1); | ||
|
||
PADDLE_ASSERT(fallback_alloc_size_ >= size); | ||
fallback_alloc_size_ -= size; | ||
PADDLE_ASSERT(cuda_pinnd_alloc_size_ >= size); | ||
cuda_pinnd_alloc_size_ -= size; | ||
err = cudaFreeHost(p); | ||
|
||
// Purposefully allow cudaErrorCudartUnloading, because | ||
|
@@ -161,7 +164,7 @@ void CUDAPinnedAllocator::Free(void* p, size_t size, size_t index) { | |
} | ||
} | ||
|
||
bool CUDAPinnedAllocator::UseGpu() const { return true; } | ||
bool CUDAPinnedAllocator::UseGpu() const { return false; } | ||
|
||
#endif | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,9 +59,7 @@ class CUDAPinnedAllocator : public SystemAllocator { | |
virtual bool UseGpu() const; | ||
|
||
private: | ||
size_t gpu_alloc_size_ = | ||
0; // TODO(zcd): how to define the upper limit of CUDAPinnedMemory? | ||
size_t fallback_alloc_size_ = 0; | ||
size_t cuda_pinnd_alloc_size_ = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments at line 24 should be modified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks! |
||
}; | ||
#endif | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is line 58
FLAGS_use_pinned_memory
useful now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes