40 |
|
*/ |
41 |
|
#include <algorithm> |
42 |
|
#include "brains/BlockSnapshotManager.hpp" |
43 |
+ |
#include "utils/residentMem.h" |
44 |
|
#include "utils/physmem.h" |
45 |
|
#include "utils/Algorithm.hpp" |
46 |
|
#include "brains/SimInfo.hpp" |
47 |
|
#include "io/DumpReader.hpp" |
48 |
|
|
49 |
|
namespace oopse { |
50 |
< |
BlockSnapshotManager::BlockSnapshotManager(SimInfo* info, const std::string& filename, |
51 |
< |
int storageLayout, int blockCapacity) |
52 |
< |
: SnapshotManager(storageLayout), info_(info), blockCapacity_(blockCapacity), activeBlocks_(blockCapacity_, -1) { |
50 |
> |
BlockSnapshotManager::BlockSnapshotManager(SimInfo* info, const std::string& filename, |
51 |
> |
int storageLayout, int blockCapacity) |
52 |
> |
: SnapshotManager(storageLayout), info_(info), blockCapacity_(blockCapacity), |
53 |
> |
activeBlocks_(blockCapacity_, -1), activeRefCount_(blockCapacity_, 0) { |
54 |
|
|
55 |
< |
nAtoms_ = info->getNGlobalAtoms(); |
56 |
< |
nRigidBodies_ = info->getNGlobalRigidBodies(); |
55 |
> |
nAtoms_ = info->getNGlobalAtoms(); |
56 |
> |
nRigidBodies_ = info->getNGlobalRigidBodies(); |
57 |
|
|
58 |
< |
double avalPhysMem = physmem_available(); |
58 |
> |
RealType physMem = physmem_total(); |
59 |
> |
RealType rssMem = residentMem(); |
60 |
> |
RealType avaliablePhysMem = physMem - rssMem; |
61 |
|
|
62 |
< |
int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout); |
62 |
> |
int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout); |
63 |
|
|
64 |
< |
int bytesPerFrame = (nRigidBodies_ + nAtoms_) * bytesPerStuntDouble; |
64 |
> |
int bytesPerFrame = (nRigidBodies_ + nAtoms_) * bytesPerStuntDouble; |
65 |
|
|
66 |
< |
int frameCapacity = int (avalPhysMem / bytesPerFrame); |
66 |
> |
int frameCapacity = int (avaliablePhysMem / bytesPerFrame); |
67 |
|
|
68 |
< |
nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ; |
68 |
> |
nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ; |
69 |
> |
reader_ = new DumpReader(info, filename); |
70 |
> |
nframes_ = reader_->getNFrames(); |
71 |
|
|
72 |
< |
reader_ = new DumpReader(info, filename); |
73 |
< |
nframes_ = reader_->getNFrames(); |
68 |
< |
|
69 |
< |
int nblocks = nframes_ / nSnapshotPerBlock_; |
70 |
< |
if (nframes_ % nSnapshotPerBlock_ != 0) { |
72 |
> |
int nblocks = nframes_ / nSnapshotPerBlock_; |
73 |
> |
if (nframes_ % nSnapshotPerBlock_ != 0) { |
74 |
|
++nblocks; |
75 |
< |
} |
75 |
> |
} |
76 |
|
|
77 |
< |
for (int i = 0; i < nblocks; ++i) { |
77 |
> |
for (int i = 0; i < nblocks; ++i) { |
78 |
|
blocks_.push_back(SnapshotBlock(i*nSnapshotPerBlock_, (i+1)*nSnapshotPerBlock_)); |
79 |
< |
} |
80 |
< |
//the last block may not have nSnapshotPerBlock frames, we need to consider this special situation |
81 |
< |
blocks_.back().second = nframes_; |
79 |
> |
} |
80 |
> |
//the last block may not have nSnapshotPerBlock frames, we need to consider this special situation |
81 |
> |
blocks_.back().second = nframes_; |
82 |
|
|
83 |
< |
snapshots_.insert(snapshots_.begin(), nframes_, static_cast<Snapshot*>(NULL)); |
83 |
> |
snapshots_.insert(snapshots_.begin(), nframes_, static_cast<Snapshot*>(NULL)); |
84 |
> |
|
85 |
> |
// std::cout << "physmem = " << int(physMem) << "\trssMem = "<< int(rssMem) << "\t availablePhysMem = " << int(avaliablePhysMem) <<std::endl; |
86 |
> |
// std::cout << "nSnapshotPerBlock = " << nSnapshotPerBlock_ << "\t total block = " << nblocks << std::endl; |
87 |
|
|
88 |
< |
} |
88 |
> |
} |
89 |
|
|
90 |
|
|
91 |
< |
BlockSnapshotManager::~BlockSnapshotManager() { |
91 |
> |
BlockSnapshotManager::~BlockSnapshotManager() { |
92 |
|
currentSnapshot_ = NULL; |
93 |
|
previousSnapshot_ = NULL; |
94 |
|
|
96 |
|
|
97 |
|
std::vector<int>::iterator i; |
98 |
|
for (i = activeBlocks_.begin(); i != activeBlocks_.end(); ++i) { |
99 |
< |
if (*i != -1) { |
100 |
< |
unloadBlock(*i); |
101 |
< |
} |
99 |
> |
if (*i != -1) { |
100 |
> |
unloadBlock(*i); |
101 |
> |
} |
102 |
|
} |
103 |
< |
} |
103 |
> |
} |
104 |
|
|
105 |
< |
int BlockSnapshotManager::getNActiveBlocks() { |
105 |
> |
int BlockSnapshotManager::getNActiveBlocks() { |
106 |
|
#ifdef __RWSTD |
107 |
|
int count = 0; |
108 |
|
std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1), count); |
110 |
|
#else |
111 |
|
return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1)); |
112 |
|
#endif |
113 |
< |
} |
113 |
> |
} |
114 |
|
|
109 |
– |
bool BlockSnapshotManager::isBlockActive(int block) { |
110 |
– |
return std::find(activeBlocks_.begin(), activeBlocks_.end(), block) != activeBlocks_.end() ? true : false; |
111 |
– |
} |
115 |
|
|
116 |
< |
bool BlockSnapshotManager::loadBlock(int block) { |
116 |
> |
|
117 |
> |
bool BlockSnapshotManager::loadBlock(int block) { |
118 |
> |
std::vector<int>::iterator i = findActiveBlock(block); |
119 |
|
bool loadSuccess; |
120 |
< |
if (isBlockActive(block)) { |
121 |
< |
loadSuccess = true; |
120 |
> |
if (i != activeBlocks_.end()) { |
121 |
> |
//if block is already in memory, just increast the reference count |
122 |
> |
++activeRefCount_[i - activeBlocks_.begin()]; |
123 |
> |
loadSuccess = true; |
124 |
|
} else if (getNActiveBlocks() < blockCapacity_){ |
125 |
< |
|
126 |
< |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
127 |
< |
snapshots_[i] = loadFrame(i); |
128 |
< |
} |
129 |
< |
|
130 |
< |
std::vector<int>::iterator j; |
131 |
< |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), -1); |
132 |
< |
assert(j != activeBlocks_.end()); |
133 |
< |
*j = block; |
134 |
< |
loadSuccess = true; |
135 |
< |
}else { |
136 |
< |
loadSuccess = false; |
125 |
> |
//if number of active blocks is less than the block capacity, just load it |
126 |
> |
internalLoad(block); |
127 |
> |
loadSuccess = true; |
128 |
> |
} else if (hasZeroRefBlock() > 0) { |
129 |
> |
//if already reach the block capacity, need to unload a block with 0 reference |
130 |
> |
int zeroRefBlock = getFirstZeroRefBlock(); |
131 |
> |
assert(zeroRefBlock != -1); |
132 |
> |
internalUnload(zeroRefBlock); |
133 |
> |
internalLoad(block); |
134 |
> |
} else { |
135 |
> |
//reach the capacity and all blocks in memory are not zero reference |
136 |
> |
loadSuccess = false; |
137 |
|
} |
138 |
< |
|
138 |
> |
|
139 |
|
return loadSuccess; |
140 |
< |
} |
140 |
> |
} |
141 |
|
|
142 |
< |
bool BlockSnapshotManager::unloadBlock(int block) { |
142 |
> |
bool BlockSnapshotManager::unloadBlock(int block) { |
143 |
|
bool unloadSuccess; |
144 |
< |
if (!isBlockActive(block)){ |
145 |
< |
unloadSuccess = false; |
144 |
> |
std::vector<int>::iterator i = findActiveBlock(block); |
145 |
> |
|
146 |
> |
if (i != activeBlocks_.end()){ |
147 |
> |
--activeRefCount_[i - activeBlocks_.begin()]; |
148 |
> |
if (activeRefCount_[i - activeBlocks_.begin()] < 0) { |
149 |
> |
//in case, unloadBlock called multiple times |
150 |
> |
activeRefCount_[i - activeBlocks_.begin()] = 0; |
151 |
> |
} |
152 |
> |
|
153 |
> |
unloadSuccess = true; |
154 |
|
} else { |
155 |
< |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
141 |
< |
delete snapshots_[i]; |
142 |
< |
snapshots_[i] = NULL; |
143 |
< |
} |
144 |
< |
std::vector<int>::iterator j; |
145 |
< |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), block); |
146 |
< |
assert(j != activeBlocks_.end()); |
147 |
< |
*j = -1; |
148 |
< |
unloadSuccess = true; |
155 |
> |
unloadSuccess = false; |
156 |
|
} |
157 |
|
|
158 |
|
return unloadSuccess; |
159 |
< |
} |
159 |
> |
} |
160 |
|
|
161 |
< |
std::vector<int> BlockSnapshotManager::getActiveBlocks() { |
161 |
> |
void BlockSnapshotManager::internalLoad(int block) { |
162 |
> |
|
163 |
> |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
164 |
> |
snapshots_[i] = loadFrame(i); |
165 |
> |
} |
166 |
> |
|
167 |
> |
std::vector<int>::iterator j; |
168 |
> |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), -1); |
169 |
> |
assert(j != activeBlocks_.end()); |
170 |
> |
*j = block; |
171 |
> |
++activeRefCount_[j - activeBlocks_.begin()]; |
172 |
> |
} |
173 |
> |
|
174 |
> |
void BlockSnapshotManager::internalUnload(int block) { |
175 |
> |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
176 |
> |
delete snapshots_[i]; |
177 |
> |
snapshots_[i] = NULL; |
178 |
> |
} |
179 |
> |
std::vector<int>::iterator j; |
180 |
> |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), block); |
181 |
> |
assert(j != activeBlocks_.end()); |
182 |
> |
*j = -1; |
183 |
> |
} |
184 |
> |
|
185 |
> |
bool BlockSnapshotManager::hasZeroRefBlock(){ |
186 |
> |
return std::find(activeRefCount_.begin(), activeRefCount_.end(), 0) != activeRefCount_.end() ? true : false; |
187 |
> |
} |
188 |
> |
|
189 |
> |
int BlockSnapshotManager::getFirstZeroRefBlock(){ |
190 |
> |
std::vector<int>::iterator i = std::find(activeRefCount_.begin(), activeRefCount_.end(), 0); |
191 |
> |
return i != activeRefCount_.end() ? activeBlocks_[i - activeRefCount_.begin()] : -1; |
192 |
> |
} |
193 |
> |
|
194 |
> |
std::vector<int> BlockSnapshotManager::getActiveBlocks() { |
195 |
|
std::vector<int> result; |
196 |
|
oopse::copy_if(activeBlocks_.begin(), activeBlocks_.end(), std::back_inserter(result), |
197 |
< |
std::bind2nd(std::not_equal_to<int>(), -1)); |
197 |
> |
std::bind2nd(std::not_equal_to<int>(), -1)); |
198 |
|
return result; |
199 |
< |
} |
199 |
> |
} |
200 |
|
|
201 |
< |
Snapshot* BlockSnapshotManager::loadFrame(int frame){ |
201 |
> |
Snapshot* BlockSnapshotManager::loadFrame(int frame){ |
202 |
|
Snapshot* snapshot = new Snapshot(nAtoms_, nRigidBodies_, getStorageLayout()); |
203 |
|
snapshot->setID(frame); |
204 |
|
|
208 |
|
reader_->readFrame(frame); |
209 |
|
currentSnapshot_ = oldSnapshot; |
210 |
|
return snapshot; |
211 |
< |
} |
211 |
> |
} |
212 |
|
|
213 |
< |
int BlockSnapshotManager::getNFrames() { |
213 |
> |
int BlockSnapshotManager::getNFrames() { |
214 |
|
return reader_->getNFrames(); |
215 |
< |
} |
215 |
> |
} |
216 |
|
|
217 |
+ |
void BlockSnapshotManager::needCOMprops(bool ncp) { |
218 |
+ |
reader_->setNeedCOMprops(ncp); |
219 |
+ |
} |
220 |
+ |
|
221 |
|
} |