Skip to content

Commit 4e6c62c

Browse files
authored
[Allocator] Fix PMEM allocator build failure and refactor code. (#78)
1 parent ae152f6 commit 4e6c62c

7 files changed

+25
-23
lines changed

tensorflow/core/framework/allocator.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Allocator* pmem_allocator() {
9494
}
9595

9696
Allocator* experimental_pmem_allocator(const std::string& pmem_path, size_t allocator_size) {
97+
#ifdef TF_ENABLE_PMEM
9798
static Allocator* experimental_pmem_allocator =
9899
AllocatorFactoryRegistry::singleton()->GetExperimentalPMEMAllocator(pmem_path, allocator_size);
99100
if (experimental_pmem_allocator && cpu_allocator_collect_full_stats &&
@@ -102,6 +103,9 @@ Allocator* experimental_pmem_allocator(const std::string& pmem_path, size_t allo
102103
new TrackingAllocator(experimental_pmem_allocator, true);
103104
}
104105
return experimental_pmem_allocator;
106+
#else
107+
return nullptr;
108+
#endif
105109
}
106110

107111
Allocator* ev_allocator() {

tensorflow/core/framework/allocator_registry.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ limitations under the License.
1616
#include <string>
1717

1818
#include "tensorflow/core/framework/allocator_registry.h"
19+
#ifdef TF_ENABLE_PMEM
20+
#include "tensorflow/core/framework/experimental_pmem_allocator.h"
21+
#endif
1922
#include "tensorflow/core/platform/logging.h"
20-
#include "experimental_pmem_allocator.h"
2123

2224
namespace tensorflow {
2325

@@ -88,7 +90,9 @@ Allocator* AllocatorFactoryRegistry::GetAllocator() {
8890
}
8991
}
9092

91-
Allocator* AllocatorFactoryRegistry::GetExperimentalPMEMAllocator(const std::string& pmem_path, size_t pmem_size) {
93+
#ifdef TF_ENABLE_PMEM
94+
Allocator* AllocatorFactoryRegistry::GetExperimentalPMEMAllocator(
95+
const std::string& pmem_path, size_t pmem_size) {
9296
mutex_lock l(mu_);
9397
first_alloc_made_ = true;
9498
FactoryEntry* best_entry = nullptr;
@@ -101,7 +105,8 @@ Allocator* AllocatorFactoryRegistry::GetExperimentalPMEMAllocator(const std::str
101105

102106
if (best_entry) {
103107
if (!best_entry->allocator) {
104-
static_cast<ExperimentalPMEMAllocatorFactory*>(best_entry->factory.get())->Init(pmem_path, pmem_size);
108+
static_cast<ExperimentalPMEMAllocatorFactory*>(
109+
best_entry->factory.get())->Init(pmem_path, pmem_size);
105110
best_entry->allocator.reset(best_entry->factory->CreateAllocator());
106111
}
107112
return best_entry->allocator.get();
@@ -110,6 +115,7 @@ Allocator* AllocatorFactoryRegistry::GetExperimentalPMEMAllocator(const std::str
110115
return nullptr;
111116
}
112117
}
118+
#endif
113119

114120
Allocator* AllocatorFactoryRegistry::GetPMEMAllocator() {
115121
mutex_lock l(mu_);

tensorflow/core/framework/allocator_registry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ class AllocatorFactoryRegistry {
8080
//If use PMEMallocator, then factory pick this one
8181
Allocator* GetPMEMAllocator();
8282

83+
#ifdef TF_ENABLE_PMEM
8384
Allocator* GetExperimentalPMEMAllocator(const std::string& pmem_path, size_t pmem_size);
85+
#endif
8486

8587
Allocator* GetEVAllocator();
8688

tensorflow/core/framework/experimental_pmem_allocator.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
#include "experimental_pmem_allocator.h"
2-
1+
#include "libpmem.h"
32
#include <string.h>
43
#include <unistd.h>
5-
6-
#include <mutex>
7-
8-
#include "libpmem.h"
4+
#include "tensorflow/core/framework/experimental_pmem_allocator.h"
95

106
namespace tensorflow {
117

tensorflow/core/framework/experimental_pmem_allocator.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#ifndef TENSORFLOW_CORE_FRAMEWORK_EXPERIMENTAL_PMEM_ALLOCATOR_H_
22
#define TENSORFLOW_CORE_FRAMEWORK_EXPERIMENTAL_PMEM_ALLOCATOR_H_
33

4-
#include <assert.h>
4+
#include <atomic>
55
#include <fcntl.h>
66
#include <sys/mman.h>
7-
8-
#include <atomic>
9-
#include <thread>
107
#include <vector>
118

12-
#include "experimental_pmem_allocator_utils.h"
139
#include "tensorflow/core/framework/allocator.h"
1410
#include "tensorflow/core/framework/allocator_registry.h"
11+
#include "tensorflow/core/framework/experimental_pmem_allocator_utils.h"
1512
#include "tensorflow/core/lib/core/spin_lock.h"
1613
#include "tensorflow/core/platform/env.h"
1714

@@ -393,4 +390,4 @@ class ExperimentalPMEMAllocatorFactory : public AllocatorFactory {
393390
};
394391
} // namespace tensorflow
395392

396-
#endif
393+
#endif

tensorflow/core/framework/experimental_pmem_allocator_utils.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <assert.h>
22

3-
#include <mutex>
4-
5-
#include "experimental_pmem_allocator.h"
3+
#include "tensorflow/core/framework/experimental_pmem_allocator.h"
64
#include "tensorflow/core/lib/core/spin_lock.h"
75
namespace tensorflow {
86

@@ -45,4 +43,4 @@ void ThreadManager::Release(const AllocatorThread& t) {
4543
usable_id_.insert(t.id);
4644
}
4745
}
48-
} // namespace tensorflow
46+
} // namespace tensorflow

tensorflow/core/framework/experimental_pmem_allocator_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#ifndef TENSORFLOW_CORE_FRAMEWORK_EXPERIMENTAL_PMEM_ALLOCATOR_UTILS_H_
22
#define TENSORFLOW_CORE_FRAMEWORK_EXPERIMENTAL_PMEM_ALLOCATOR_UTILS_H_
33

4-
#include <string.h>
5-
#include <sys/stat.h>
6-
74
#include <atomic>
85
#include <memory>
6+
#include <string.h>
7+
#include <sys/stat.h>
98
#include <unordered_set>
109

1110
#include "tensorflow/core/lib/core/spin_lock.h"
@@ -108,4 +107,4 @@ inline int create_dir_if_missing(const std::string& name) {
108107

109108
} // namespace tensorflow
110109

111-
#endif
110+
#endif

0 commit comments

Comments
 (0)