Skip to content

Commit 29ceb93

Browse files
committed
Use malloc and free in JeMalloc
test=develop
1 parent e33427d commit 29ceb93

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

paddle/fluid/memory/allocation/legacy_allocator.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
// limitations under the License.
1414

1515
#include "paddle/fluid/memory/allocation/legacy_allocator.h"
16+
1617
#include <string>
1718
#include <vector>
19+
20+
#ifdef WITH_JEMALLOC
21+
#include <jemalloc/jemalloc.h>
22+
#endif
23+
1824
#include "glog/logging.h"
1925
#include "paddle/fluid/memory/detail/buddy_allocator.h"
2026
#include "paddle/fluid/memory/detail/system_allocator.h"
@@ -89,7 +95,11 @@ struct NaiveAllocator {
8995
template <>
9096
void *Alloc<platform::CPUPlace>(const platform::CPUPlace &place, size_t size) {
9197
VLOG(10) << "Allocate " << size << " bytes on " << platform::Place(place);
98+
#ifdef WITH_JEMALLOC
99+
void *p = malloc(size);
100+
#else
92101
void *p = GetCPUBuddyAllocator()->Alloc(size);
102+
#endif
93103
if (FLAGS_init_allocated_mem) {
94104
memset(p, 0xEF, size);
95105
}
@@ -100,12 +110,21 @@ void *Alloc<platform::CPUPlace>(const platform::CPUPlace &place, size_t size) {
100110
template <>
101111
void Free<platform::CPUPlace>(const platform::CPUPlace &place, void *p) {
102112
VLOG(10) << "Free pointer=" << p << " on " << platform::Place(place);
113+
#ifdef WITH_JEMALLOC
114+
free(p);
115+
#else
103116
GetCPUBuddyAllocator()->Free(p);
117+
#endif
104118
}
105119

106120
template <>
107121
size_t Used<platform::CPUPlace>(const platform::CPUPlace &place) {
122+
#ifdef WITH_JEMALLOC
123+
// fake the result of used memory when WITH_JEMALLOC is ON
124+
return 0U;
125+
#else
108126
return GetCPUBuddyAllocator()->Used();
127+
#endif
109128
}
110129

111130
#ifdef PADDLE_WITH_CUDA

0 commit comments

Comments
 (0)