40 |
|
*/ |
41 |
|
#include <algorithm> |
42 |
|
#include "brains/BlockSnapshotManager.hpp" |
43 |
< |
#include "utlis/physmem.h" |
43 |
> |
#include "utils/physmem.h" |
44 |
> |
#include "utils/Algorithm.hpp" |
45 |
|
#include "brains/SimInfo.hpp" |
46 |
|
#include "io/DumpReader.hpp" |
47 |
|
|
48 |
|
namespace oopse { |
49 |
|
BlockSnapshotManager::BlockSnapshotManager(SimInfo* info, const std::string& filename, |
50 |
|
int storageLayout, int blockCapacity) |
51 |
< |
: SnapshotManager(storageLayout), info_(info), |
51 |
< |
blockCapacity_(blockCapacity), activeBlocks(blockCapacity_, -1) { |
51 |
> |
: SnapshotManager(storageLayout), info_(info), blockCapacity_(blockCapacity), activeBlocks_(blockCapacity_, -1) { |
52 |
|
|
53 |
|
nAtoms_ = info->getNGlobalAtoms(); |
54 |
|
nRigidBodies_ = info->getNGlobalRigidBodies(); |
57 |
|
|
58 |
|
int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout); |
59 |
|
|
60 |
< |
int bytesPerFrame = nStuntDoubles * bytesPerStuntDouble; |
60 |
> |
int bytesPerFrame = (nRigidBodies_ + nAtoms_) * bytesPerStuntDouble; |
61 |
|
|
62 |
|
int frameCapacity = int (avalPhysMem / bytesPerFrame); |
63 |
|
|
64 |
|
nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ; |
65 |
|
|
66 |
|
reader_ = new DumpReader(info, filename); |
67 |
< |
nframes_ = reader->getNFrames(); |
67 |
> |
nframes_ = reader_->getNFrames(); |
68 |
|
|
69 |
< |
int nblocks = nframes / nSnapshotPerBlock_; |
70 |
< |
if (nframes % nSnapshotPerBlock != 0) { |
69 |
> |
int nblocks = nframes_ / nSnapshotPerBlock_; |
70 |
> |
if (nframes_ % nSnapshotPerBlock_ != 0) { |
71 |
|
++nblocks; |
72 |
|
} |
73 |
|
|
74 |
|
for (int i = 0; i < nblocks; ++i) { |
75 |
< |
blocks_.push_back(SnapshotBlock(i, (i+1)*nSnapshotPerBlock_); |
75 |
> |
blocks_.push_back(SnapshotBlock(i*nSnapshotPerBlock_, (i+1)*nSnapshotPerBlock_)); |
76 |
|
} |
77 |
|
//the last block may not have nSnapshotPerBlock frames, we need to consider this special situation |
78 |
< |
blocks.back.second = nframes; |
78 |
> |
blocks_.back().second = nframes_; |
79 |
|
|
80 |
< |
snapshots_.insert(snapshot_.begin(), nframes, NULL); |
80 |
> |
snapshots_.insert(snapshots_.begin(), nframes_, NULL); |
81 |
|
|
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
< |
BlockSnapshotMananger::~BlockSnapshotMananger() { |
85 |
> |
BlockSnapshotManager::~BlockSnapshotManager() { |
86 |
|
currentSnapshot_ = NULL; |
87 |
|
previousSnapshot_ = NULL; |
88 |
|
|
89 |
|
delete reader_; |
90 |
< |
std::for_each(activeBlocks_.begin(), activeBlocks_.end(), unloadBlock); |
90 |
> |
|
91 |
> |
std::vector<int>::iterator i; |
92 |
> |
for (i = activeBlocks_.begin(); i != activeBlocks_.end(); ++i) { |
93 |
> |
if (*i != -1) { |
94 |
> |
unloadBlock(*i); |
95 |
> |
} |
96 |
> |
} |
97 |
|
} |
98 |
|
|
99 |
|
int BlockSnapshotManager::getNActiveBlocks() { |
100 |
< |
return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::not); |
100 |
> |
return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1)); |
101 |
|
} |
102 |
|
|
103 |
|
bool BlockSnapshotManager::isBlockActive(int block) { |
139 |
|
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), block); |
140 |
|
assert(j != activeBlocks_.end()); |
141 |
|
*j = -1; |
142 |
+ |
unloadSuccess = true; |
143 |
|
} |
144 |
+ |
|
145 |
+ |
return unloadSuccess; |
146 |
|
} |
147 |
|
|
148 |
|
std::vector<int> BlockSnapshotManager::getActiveBlocks() { |
149 |
|
std::vector<int> result; |
150 |
< |
std::copy_if(activeBlocks_.begin(), activeBlocks_.end()); |
150 |
> |
oopse::copy_if(activeBlocks_.begin(), activeBlocks_.end(), std::back_inserter(result), |
151 |
> |
std::bind2nd(std::not_equal_to<int>(), -1)); |
152 |
> |
return result; |
153 |
|
} |
154 |
|
|
155 |
|
Snapshot* BlockSnapshotManager::loadFrame(int frame){ |
156 |
|
Snapshot* snapshot = new Snapshot(nAtoms_, nRigidBodies_, getStorageLayout()); |
157 |
|
snapshot->setID(frame); |
158 |
< |
setCurrentSnapshot(snapshot); /** @todo fixed me */ |
158 |
> |
|
159 |
> |
/** @todo fixed me */ |
160 |
> |
Snapshot* oldSnapshot = currentSnapshot_; |
161 |
> |
currentSnapshot_ = snapshot; |
162 |
|
reader_->readFrame(frame); |
163 |
+ |
currentSnapshot_ = oldSnapshot; |
164 |
|
return snapshot; |
165 |
|
} |
166 |
|
|
167 |
+ |
int BlockSnapshotManager::getNFrames() { |
168 |
+ |
return reader_->getNFrames(); |
169 |
|
} |
170 |
+ |
|
171 |
+ |
} |