• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <string>
17 
18 #include "tensorflow/core/framework/allocator_registry.h"
19 #include "tensorflow/core/platform/logging.h"
20 
21 namespace tensorflow {
22 
23 // static
singleton()24 AllocatorFactoryRegistry* AllocatorFactoryRegistry::singleton() {
25   static AllocatorFactoryRegistry* singleton = new AllocatorFactoryRegistry;
26   return singleton;
27 }
28 
29 const AllocatorFactoryRegistry::FactoryEntry*
FindEntry(const string & name,int priority) const30 AllocatorFactoryRegistry::FindEntry(const string& name, int priority) const {
31   for (auto& entry : factories_) {
32     if (!name.compare(entry.name) && priority == entry.priority) {
33       return &entry;
34     }
35   }
36   return nullptr;
37 }
38 
Register(const char * source_file,int source_line,const string & name,int priority,AllocatorFactory * factory)39 void AllocatorFactoryRegistry::Register(const char* source_file,
40                                         int source_line, const string& name,
41                                         int priority,
42                                         AllocatorFactory* factory) {
43   mutex_lock l(mu_);
44   CHECK(!first_alloc_made_) << "Attempt to register an AllocatorFactory "
45                             << "after call to GetAllocator()";
46   CHECK(!name.empty()) << "Need a valid name for Allocator";
47   CHECK_GE(priority, 0) << "Priority needs to be non-negative";
48 
49   const FactoryEntry* existing = FindEntry(name, priority);
50   if (existing != nullptr) {
51     // Duplicate registration is a hard failure.
52     LOG(FATAL) << "New registration for AllocatorFactory with name=" << name
53                << " priority=" << priority << " at location " << source_file
54                << ":" << source_line
55                << " conflicts with previous registration at location "
56                << existing->source_file << ":" << existing->source_line;
57   }
58 
59   FactoryEntry entry;
60   entry.source_file = source_file;
61   entry.source_line = source_line;
62   entry.name = name;
63   entry.priority = priority;
64   entry.factory.reset(factory);
65   factories_.push_back(std::move(entry));
66 }
67 
GetAllocator()68 Allocator* AllocatorFactoryRegistry::GetAllocator() {
69   mutex_lock l(mu_);
70   first_alloc_made_ = true;
71   FactoryEntry* best_entry = nullptr;
72   for (auto& entry : factories_) {
73     if (best_entry == nullptr) {
74       best_entry = &entry;
75     } else if (entry.priority > best_entry->priority) {
76       best_entry = &entry;
77     }
78   }
79   if (best_entry) {
80     if (!best_entry->allocator) {
81       best_entry->allocator.reset(best_entry->factory->CreateAllocator());
82     }
83     return best_entry->allocator.get();
84   } else {
85     LOG(FATAL) << "No registered CPU AllocatorFactory";
86     return nullptr;
87   }
88 }
89 
GetSubAllocator(int numa_node)90 SubAllocator* AllocatorFactoryRegistry::GetSubAllocator(int numa_node) {
91   mutex_lock l(mu_);
92   first_alloc_made_ = true;
93   FactoryEntry* best_entry = nullptr;
94   for (auto& entry : factories_) {
95     if (best_entry == nullptr) {
96       best_entry = &entry;
97     } else if (best_entry->factory->NumaEnabled()) {
98       if (entry.factory->NumaEnabled() &&
99           (entry.priority > best_entry->priority)) {
100         best_entry = &entry;
101       }
102     } else {
103       DCHECK(!best_entry->factory->NumaEnabled());
104       if (entry.factory->NumaEnabled() ||
105           (entry.priority > best_entry->priority)) {
106         best_entry = &entry;
107       }
108     }
109   }
110   if (best_entry) {
111     int index = 0;
112     if (numa_node != port::kNUMANoAffinity) {
113       CHECK_LE(numa_node, port::NUMANumNodes());
114       index = 1 + numa_node;
115     }
116     if (best_entry->sub_allocators.size() < (index + 1)) {
117       best_entry->sub_allocators.resize(index + 1);
118     }
119     if (!best_entry->sub_allocators[index].get()) {
120       best_entry->sub_allocators[index].reset(
121           best_entry->factory->CreateSubAllocator(numa_node));
122     }
123     return best_entry->sub_allocators[index].get();
124   } else {
125     LOG(FATAL) << "No registered CPU AllocatorFactory";
126     return nullptr;
127   }
128 }
129 
130 }  // namespace tensorflow
131