Skip to content

Commit

Permalink
Revert "Enable thp(transparent huge pages) for buffer sizes >=2MB (py…
Browse files Browse the repository at this point in the history
…torch#95963)"

This reverts commit 3bb16a0.

Reverted pytorch#95963 on behalf of https://github.com/izaitsevfb due to Breaks internal android builds: unused function c10_compute_alignment  [-Werror,-Wunused-function]
  • Loading branch information
pytorchmergebot committed Mar 14, 2023
1 parent 86a9fe8 commit a22b92d
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 49 deletions.
4 changes: 0 additions & 4 deletions c10/core/alignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ constexpr size_t gAlignment = 16;
constexpr size_t gAlignment = 64;
#endif

constexpr size_t gPagesize = 4096;
// since the default thp pagesize is 2MB, enable thp only
// for buffers of size 2MB or larger to avoid memory bloating
constexpr size_t gAlloc_threshold_thp = 2 * 1024 * 1024;
} // namespace c10
41 changes: 1 addition & 40 deletions c10/core/impl/alloc_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,6 @@ void memset_junk(void* data, size_t num) {
}
}

#ifdef __linux__
static inline bool is_thp_alloc_enabled() {
static bool value = [&] {
const char* ptr = std::getenv("THP_MEM_ALLOC_ENABLE");
return ptr != nullptr ? std::atoi(ptr) : 0;
}();
return value;
}

inline size_t c10_compute_alignment(size_t nbytes) {
static const auto pagesize = sysconf(_SC_PAGESIZE);
// for kernels that don't provide page size, default it to 4K
const size_t thp_alignment = (gPagesize < 0 ? gPagesize : pagesize);
return (is_thp_alloc_enabled() ? thp_alignment : gAlignment);
}

inline bool is_thp_alloc(size_t nbytes) {
// enable thp (transparent huge pages) for larger buffers
return (is_thp_alloc_enabled() && (nbytes >= gAlloc_threshold_thp));
}
#elif !defined(__ANDROID__) && !defined(_MSC_VER)
constexpr size_t c10_compute_alignment(C10_UNUSED size_t nbytes) {
return gAlignment;
}

constexpr bool is_thp_alloc(C10_UNUSED size_t nbytes) {
return false;
}
#endif
} // namespace

void* alloc_cpu(size_t nbytes) {
Expand Down Expand Up @@ -100,7 +71,7 @@ void* alloc_cpu(size_t nbytes) {
nbytes,
" bytes.");
#else
int err = posix_memalign(&data, c10_compute_alignment(nbytes), nbytes);
int err = posix_memalign(&data, gAlignment, nbytes);
CAFFE_ENFORCE(
err == 0,
"DefaultCPUAllocator: can't allocate memory: you tried to allocate ",
Expand All @@ -110,16 +81,6 @@ void* alloc_cpu(size_t nbytes) {
" (",
strerror(err),
")");
if (is_thp_alloc(nbytes)) {
#ifdef __linux__
// MADV_HUGEPAGE advise is available only for linux.
// general posix compliant systems can check POSIX_MADV_SEQUENTIAL advise.
int ret = madvise(data, nbytes, MADV_HUGEPAGE);
if (ret != 0) {
TORCH_WARN_ONCE("thp madvise for HUGEPAGE failed with ", strerror(errno));
}
#endif
}
#endif

// move data to a thread's NUMA node
Expand Down
5 changes: 0 additions & 5 deletions c10/core/impl/alloc_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

#include <cstddef>

#ifdef __linux__
#include <sys/mman.h>
#include <unistd.h>
#endif

namespace c10 {

C10_API void* alloc_cpu(size_t nbytes);
Expand Down

0 comments on commit a22b92d

Please sign in to comment.