25 #ifndef MXNET_COMMON_LAZY_ALLOC_ARRAY_H_ 26 #define MXNET_COMMON_LAZY_ALLOC_ARRAY_H_ 28 #include <dmlc/logging.h> 38 template<
typename TElem>
48 template<
typename FCreate>
49 inline std::shared_ptr<TElem>
Get(
int index, FCreate creator);
54 template<
typename FVisit>
55 inline void ForEach(FVisit fvisit);
62 template<
typename SyncObject>
65 explicit unique_unlock(std::unique_lock<SyncObject> *lock)
77 std::unique_lock<SyncObject> *lock_;
81 static constexpr std::size_t kInitSize = 16;
83 std::mutex create_mutex_;
85 std::array<std::shared_ptr<TElem>, kInitSize> head_;
87 std::vector<std::shared_ptr<TElem> > more_;
89 std::atomic<bool> exit_now_;
92 template<
typename TElem>
98 template<
typename TElem>
99 template<
typename FCreate>
102 size_t idx =
static_cast<size_t>(index);
103 if (idx < kInitSize) {
104 std::shared_ptr<TElem> ptr = head_[idx];
108 std::lock_guard<std::mutex> lock(create_mutex_);
109 if (!exit_now_.load()) {
110 std::shared_ptr<TElem> ptr = head_[idx];
114 ptr = head_[idx] = std::shared_ptr<TElem>(creator());
119 std::lock_guard<std::mutex> lock(create_mutex_);
120 if (!exit_now_.load()) {
122 if (more_.size() <= idx) {
123 more_.reserve(idx + 1);
124 while (more_.size() <= idx) {
125 more_.push_back(std::shared_ptr<TElem>(
nullptr));
128 std::shared_ptr<TElem> ptr = more_[idx];
132 ptr = more_[idx] = std::shared_ptr<TElem>(creator());
139 template<
typename TElem>
141 std::unique_lock<std::mutex> lock(create_mutex_);
142 exit_now_.store(
true);
146 for (
size_t i = 0; i < head_.size(); ++i) {
147 std::shared_ptr<TElem> p = head_[i];
148 head_[i] = std::shared_ptr<TElem>(
nullptr);
149 unique_unlock<std::mutex> unlocker(&lock);
150 p = std::shared_ptr<TElem>(
nullptr);
152 for (
size_t i = 0; i < more_.size(); ++i) {
153 std::shared_ptr<TElem> p = more_[i];
154 more_[i] = std::shared_ptr<TElem>(
nullptr);
155 unique_unlock<std::mutex> unlocker(&lock);
156 p = std::shared_ptr<TElem>(
nullptr);
160 template<
typename TElem>
161 template<
typename FVisit>
163 std::lock_guard<std::mutex> lock(create_mutex_);
164 for (
size_t i = 0; i < head_.size(); ++i) {
165 if (head_[i].
get() !=
nullptr) {
166 fvisit(i, head_[i].
get());
169 for (
size_t i = 0; i < more_.size(); ++i) {
170 if (more_[i].
get() !=
nullptr) {
171 fvisit(i + kInitSize, more_[i].
get());
176 template<
typename TElem>
178 std::lock_guard<std::mutex> lock(create_mutex_);
179 exit_now_.store(
true);
184 #endif // MXNET_COMMON_LAZY_ALLOC_ARRAY_H_ std::shared_ptr< TElem > Get(int index, FCreate creator)
Get element of corresponding index, if it is not created create by creator.
Definition: lazy_alloc_array.h:100
namespace of mxnet
Definition: base.h:126
void Clear()
clear all the allocated elements in array
Definition: lazy_alloc_array.h:140
void ForEach(FVisit fvisit)
for each not null element of the array, call fvisit
Definition: lazy_alloc_array.h:162
Definition: lazy_alloc_array.h:39
void SignalForKill()
Definition: lazy_alloc_array.h:177
LazyAllocArray()
Definition: lazy_alloc_array.h:93