Skip to content

Commit 4fcc293

Browse files
authored
memory module (#12931)
* memory module * "fix ci"
1 parent 3c58b87 commit 4fcc293

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

paddle/fluid/inference/api/demo_ci/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ include_directories("${PADDLE_LIB}")
2323
include_directories("${PADDLE_LIB}/third_party/install/protobuf/include")
2424
include_directories("${PADDLE_LIB}/third_party/install/glog/include")
2525
include_directories("${PADDLE_LIB}/third_party/install/gflags/include")
26+
if (NOT WIN32)
2627
include_directories("${PADDLE_LIB}/third_party/install/snappy/include")
2728
include_directories("${PADDLE_LIB}/third_party/install/snappystream/include")
2829
include_directories("${PADDLE_LIB}/third_party/install/zlib/include")
30+
endif(NOT WIN32)
2931

3032
include_directories("${PADDLE_LIB}/third_party/boost")
3133
include_directories("${PADDLE_LIB}/third_party/eigen3")

paddle/fluid/memory/detail/system_allocator.cc

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14+
#define GLOG_NO_ABBREVIATED_SEVERITIES
1415

1516
#include "paddle/fluid/memory/detail/system_allocator.h"
1617

17-
#include <stdlib.h> // for malloc and free
18+
#ifdef _WIN32
19+
#include <malloc.h>
20+
#include <windows.h> // VirtualLock/VirtualUnlock
21+
#else
1822
#include <sys/mman.h> // for mlock and munlock
19-
#include <algorithm> // for std::max
23+
#endif
24+
#include <stdlib.h> // for malloc and free
25+
#include <algorithm> // for std::max
2026

2127
#include "gflags/gflags.h"
2228
#include "paddle/fluid/platform/assert.h"
@@ -35,31 +41,42 @@ namespace paddle {
3541
namespace memory {
3642
namespace detail {
3743

38-
void* CPUAllocator::Alloc(size_t* index, size_t size) {
39-
// According to http://www.cplusplus.com/reference/cstdlib/malloc/,
40-
// malloc might not return nullptr if size is zero, but the returned
41-
// pointer shall not be dereferenced -- so we make it nullptr.
42-
if (size <= 0) return nullptr;
43-
44-
*index = 0; // unlock memory
45-
44+
void* AlignedMalloc(size_t size) {
4645
void* p = nullptr;
47-
46+
size_t alignment = 32ul;
4847
#ifdef PADDLE_WITH_MKLDNN
4948
// refer to https://github.com/01org/mkl-dnn/blob/master/include/mkldnn.hpp
5049
// memory alignment
51-
PADDLE_ENFORCE_EQ(posix_memalign(&p, 4096ul, size), 0, "Alloc %ld error!",
52-
size);
50+
alignment = 4096ul;
51+
#endif
52+
#ifdef _WIN32
53+
p = _aligned_malloc(size, alignment);
5354
#else
54-
PADDLE_ENFORCE_EQ(posix_memalign(&p, 32ul, size), 0, "Alloc %ld error!",
55+
PADDLE_ENFORCE_EQ(posix_memalign(&p, alignment, size), 0, "Alloc %ld error!",
5556
size);
5657
#endif
5758
PADDLE_ENFORCE(p, "Fail to allocate CPU memory: size = %d .", size);
59+
return p;
60+
}
61+
62+
void* CPUAllocator::Alloc(size_t* index, size_t size) {
63+
// According to http://www.cplusplus.com/reference/cstdlib/malloc/,
64+
// malloc might not return nullptr if size is zero, but the returned
65+
// pointer shall not be dereferenced -- so we make it nullptr.
66+
if (size <= 0) return nullptr;
67+
68+
*index = 0; // unlock memory
69+
70+
void* p = AlignedMalloc(size);
5871

5972
if (p != nullptr) {
6073
if (FLAGS_use_pinned_memory) {
6174
*index = 1;
75+
#ifdef _WIN32
76+
VirtualLock(p, size);
77+
#else
6278
mlock(p, size); // lock memory
79+
#endif
6380
}
6481
}
6582

@@ -68,7 +85,11 @@ void* CPUAllocator::Alloc(size_t* index, size_t size) {
6885

6986
void CPUAllocator::Free(void* p, size_t size, size_t index) {
7087
if (p != nullptr && index == 1) {
88+
#ifdef _WIN32
89+
VirtualUnlock(p, size);
90+
#else
7191
munlock(p, size);
92+
#endif
7293
}
7394
free(p);
7495
}

0 commit comments

Comments
 (0)