ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/brains/BlockSnapshotManager.cpp
Revision: 2035
Committed: Tue Feb 15 19:36:07 2005 UTC (19 years, 4 months ago) by tim
File size: 7545 byte(s)
Log Message:
BlockSnapshotManager is using reference counting now

File Contents

# Content
1 /*
2 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 *
4 * The University of Notre Dame grants you ("Licensee") a
5 * non-exclusive, royalty free, license to use, modify and
6 * redistribute this software in source and binary code form, provided
7 * that the following conditions are met:
8 *
9 * 1. Acknowledgement of the program authors must be made in any
10 * publication of scientific results based in part on use of the
11 * program. An acceptable form of acknowledgement is citation of
12 * the article in which the program was described (Matthew
13 * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 * Parallel Simulation Engine for Molecular Dynamics,"
16 * J. Comput. Chem. 26, pp. 252-271 (2005))
17 *
18 * 2. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 3. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * This software is provided "AS IS," without a warranty of any
27 * kind. All express or implied conditions, representations and
28 * warranties, including any implied warranty of merchantability,
29 * fitness for a particular purpose or non-infringement, are hereby
30 * excluded. The University of Notre Dame and its licensors shall not
31 * be liable for any damages suffered by licensee as a result of
32 * using, modifying or distributing the software or its
33 * derivatives. In no event will the University of Notre Dame or its
34 * licensors be liable for any lost revenue, profit or data, or for
35 * direct, indirect, special, consequential, incidental or punitive
36 * damages, however caused and regardless of the theory of liability,
37 * arising out of the use of or inability to use software, even if the
38 * University of Notre Dame has been advised of the possibility of
39 * such damages.
40 */
41 #include <algorithm>
42 #include "brains/BlockSnapshotManager.hpp"
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), blockCapacity_(blockCapacity),
52 activeBlocks_(blockCapacity_, -1), activeRefCount_(blockCapacity_, 0) {
53
54 nAtoms_ = info->getNGlobalAtoms();
55 nRigidBodies_ = info->getNGlobalRigidBodies();
56
57 double avalPhysMem = physmem_available();
58
59 int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout);
60
61 int bytesPerFrame = (nRigidBodies_ + nAtoms_) * bytesPerStuntDouble;
62
63 int frameCapacity = int (avalPhysMem / bytesPerFrame);
64
65 nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ;
66
67 reader_ = new DumpReader(info, filename);
68 nframes_ = reader_->getNFrames();
69
70 int nblocks = nframes_ / nSnapshotPerBlock_;
71 if (nframes_ % nSnapshotPerBlock_ != 0) {
72 ++nblocks;
73 }
74
75 for (int i = 0; i < nblocks; ++i) {
76 blocks_.push_back(SnapshotBlock(i*nSnapshotPerBlock_, (i+1)*nSnapshotPerBlock_));
77 }
78 //the last block may not have nSnapshotPerBlock frames, we need to consider this special situation
79 blocks_.back().second = nframes_;
80
81 snapshots_.insert(snapshots_.begin(), nframes_, static_cast<Snapshot*>(NULL));
82
83 }
84
85
86 BlockSnapshotManager::~BlockSnapshotManager() {
87 currentSnapshot_ = NULL;
88 previousSnapshot_ = NULL;
89
90 delete reader_;
91
92 std::vector<int>::iterator i;
93 for (i = activeBlocks_.begin(); i != activeBlocks_.end(); ++i) {
94 if (*i != -1) {
95 unloadBlock(*i);
96 }
97 }
98 }
99
100 int BlockSnapshotManager::getNActiveBlocks() {
101 #ifdef __RWSTD
102 int count = 0;
103 std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1), count);
104 return count;
105 #else
106 return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1));
107 #endif
108 }
109
110
111
112 bool BlockSnapshotManager::loadBlock(int block) {
113 std::vector<int>::iterator i = findActiveBlock(block);
114 bool loadSuccess;
115 if (i != activeBlocks_.end()) {
116 //if block is already in memory, just increast the reference count
117 ++activeRefCount_[i - activeBlocks_.begin()];
118 loadSuccess = true;
119 } else if (getNActiveBlocks() < blockCapacity_){
120 //if number of active blocks is less than the block capacity, just load it
121 internalLoad(block);
122 loadSuccess = true;
123 } else if (hasZeroRefBlock() > 0) {
124 //if already reach the block capacity, need to unload a block with 0 reference
125 int zeroRefBlock = getFirstZeroRefBlock();
126 assert(zeroRefBlock != -1);
127 internalUnload(zeroRefBlock);
128 internalLoad(block);
129 } else {
130 //reach the capacity and all blocks in memory are not zero reference
131 loadSuccess = false;
132 }
133
134 return loadSuccess;
135 }
136
137 bool BlockSnapshotManager::unloadBlock(int block) {
138 bool unloadSuccess;
139 std::vector<int>::iterator i = findActiveBlock(block);
140
141 if (i != activeBlocks_.end()){
142 --activeRefCount_[i - activeBlocks_.begin()];
143 if (activeRefCount_[i - activeBlocks_.begin()] < 0) {
144 //in case, unloadBlock called multiple times
145 activeRefCount_[i - activeBlocks_.begin()] = 0;
146 }
147
148 unloadSuccess = true;
149 } else {
150 unloadSuccess = false;
151 }
152
153 return unloadSuccess;
154 }
155
156 void BlockSnapshotManager::internalLoad(int block) {
157
158 for (int i = blocks_[block].first; i < blocks_[block].second; ++i) {
159 snapshots_[i] = loadFrame(i);
160 }
161
162 std::vector<int>::iterator j;
163 j = std::find(activeBlocks_.begin(), activeBlocks_.end(), -1);
164 assert(j != activeBlocks_.end());
165 *j = block;
166 ++activeRefCount_[j - activeBlocks_.begin()];
167 }
168
169 void BlockSnapshotManager::internalUnload(int block) {
170 for (int i = blocks_[block].first; i < blocks_[block].second; ++i) {
171 delete snapshots_[i];
172 snapshots_[i] = NULL;
173 }
174 std::vector<int>::iterator j;
175 j = std::find(activeBlocks_.begin(), activeBlocks_.end(), block);
176 assert(j != activeBlocks_.end());
177 *j = -1;
178 }
179
180 bool BlockSnapshotManager::hasZeroRefBlock(){
181 return std::find(activeRefCount_.begin(), activeRefCount_.end(), 0);
182 }
183
184 int BlockSnapshotManager::getFirstZeroRefBlock(){
185 std::vector<int>::iterator i = std::find(activeRefCount_.begin(), activeRefCount_.end(), 0);
186 return i != activeRefCount_.end() ? activeBlocks_[i - activeRefCount_.begin()] : -1;
187 }
188
189 std::vector<int> BlockSnapshotManager::getActiveBlocks() {
190 std::vector<int> result;
191 oopse::copy_if(activeBlocks_.begin(), activeBlocks_.end(), std::back_inserter(result),
192 std::bind2nd(std::not_equal_to<int>(), -1));
193 return result;
194 }
195
196 Snapshot* BlockSnapshotManager::loadFrame(int frame){
197 Snapshot* snapshot = new Snapshot(nAtoms_, nRigidBodies_, getStorageLayout());
198 snapshot->setID(frame);
199
200 /** @todo fixed me */
201 Snapshot* oldSnapshot = currentSnapshot_;
202 currentSnapshot_ = snapshot;
203 reader_->readFrame(frame);
204 currentSnapshot_ = oldSnapshot;
205 return snapshot;
206 }
207
208 int BlockSnapshotManager::getNFrames() {
209 return reader_->getNFrames();
210 }
211
212 }