Skip to content
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

Always Use cudaMallocHost to allocate and pin CPU memory #3060

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions include/caffe/syncedmem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@

namespace caffe {

// If CUDA is available and in GPU mode, host memory will be allocated pinned,
// If CUDA is available, host memory will be allocated pinned,
// using cudaMallocHost. It avoids dynamic pinning for transfers (DMA).
// The improvement in performance seems negligible in the single GPU case,
// but might be more significant for parallel training. Most importantly,
// it improved stability for large models on many GPUs.
inline void CaffeMallocHost(void** ptr, size_t size) {
#ifndef CPU_ONLY
if (Caffe::mode() == Caffe::GPU) {
CUDA_CHECK(cudaMallocHost(ptr, size));
return;
}
#endif
// Always use cudaMallocHost to allocate cpu memory (unless in CPU_ONLY build)
// so that memory allocation and free do not depend on Caffe::mode()
CUDA_CHECK(cudaMallocHost(ptr, size));
#else
*ptr = malloc(size);
CHECK(*ptr) << "host allocation of size " << size << " failed";
#endif
}

inline void CaffeFreeHost(void* ptr) {
#ifndef CPU_ONLY
if (Caffe::mode() == Caffe::GPU) {
CUDA_CHECK(cudaFreeHost(ptr));
return;
}
#endif
CUDA_CHECK(cudaFreeHost(ptr));
#else
free(ptr);
#endif
}


Expand Down