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 "utlis/physmem.h" |
44 |
#include "brains/SimInfo.hpp" |
45 |
#include "io/DumpReader.hpp" |
46 |
|
47 |
namespace oopse { |
48 |
BlockSnapshotManager::BlockSnapshotManager(SimInfo* info, const std::string& filename, |
49 |
int storageLayout, int blockCapacity) |
50 |
: SnapshotManager(storageLayout), info_(info), |
51 |
blockCapacity_(blockCapacity), activeBlocks(blockCapacity_, -1) { |
52 |
|
53 |
nAtoms_ = info->getNGlobalAtoms(); |
54 |
nRigidBodies_ = info->getNGlobalRigidBodies(); |
55 |
|
56 |
double avalPhysMem = physmem_available(); |
57 |
|
58 |
int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout); |
59 |
|
60 |
int bytesPerFrame = nStuntDoubles * bytesPerStuntDouble; |
61 |
|
62 |
int frameCapacity = int (avalPhysMem / bytesPerFrame); |
63 |
|
64 |
nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ; |
65 |
|
66 |
reader_ = new DumpReader(info, filename); |
67 |
nframes_ = reader->getNFrames(); |
68 |
|
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_); |
76 |
} |
77 |
//the last block may not have nSnapshotPerBlock frames, we need to consider this special situation |
78 |
blocks.back.second = nframes; |
79 |
|
80 |
snapshots_.insert(snapshot_.begin(), nframes, NULL); |
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
BlockSnapshotMananger::~BlockSnapshotMananger() { |
86 |
currentSnapshot_ = NULL; |
87 |
previousSnapshot_ = NULL; |
88 |
|
89 |
delete reader_; |
90 |
std::for_each(activeBlocks_.begin(), activeBlocks_.end(), unloadBlock); |
91 |
} |
92 |
|
93 |
int BlockSnapshotManager::getNActiveBlocks() { |
94 |
return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::not); |
95 |
} |
96 |
|
97 |
bool BlockSnapshotManager::isBlockActive(int block) { |
98 |
return std::find(activeBlocks_.begin(), activeBlocks_.end(), block) != activeBlocks_.end() ? true : false; |
99 |
} |
100 |
|
101 |
bool BlockSnapshotManager::loadBlock(int block) { |
102 |
bool loadSuccess; |
103 |
if (isBlockActive(block)) { |
104 |
loadSuccess = true; |
105 |
} else if (getNActiveBlocks() < blockCapacity_){ |
106 |
|
107 |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
108 |
snapshots_[i] = loadFrame(i); |
109 |
} |
110 |
|
111 |
std::vector<int>::iterator j; |
112 |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), -1); |
113 |
assert(j != activeBlocks_.end()); |
114 |
*j = block; |
115 |
loadSuccess = true; |
116 |
}else { |
117 |
loadSuccess = false; |
118 |
} |
119 |
|
120 |
return loadSuccess; |
121 |
} |
122 |
|
123 |
bool BlockSnapshotManager::unloadBlock(int block) { |
124 |
bool unloadSuccess; |
125 |
if (!isBlockActive(block)){ |
126 |
unloadSuccess = false; |
127 |
} else { |
128 |
for (int i = blocks_[block].first; i < blocks_[block].second; ++i) { |
129 |
delete snapshots_[i]; |
130 |
snapshots_[i] = NULL; |
131 |
} |
132 |
std::vector<int>::iterator j; |
133 |
j = std::find(activeBlocks_.begin(), activeBlocks_.end(), block); |
134 |
assert(j != activeBlocks_.end()); |
135 |
*j = -1; |
136 |
} |
137 |
} |
138 |
|
139 |
std::vector<int> BlockSnapshotManager::getActiveBlocks() { |
140 |
std::vector<int> result; |
141 |
std::copy_if(activeBlocks_.begin(), activeBlocks_.end()); |
142 |
} |
143 |
|
144 |
Snapshot* BlockSnapshotManager::loadFrame(int frame){ |
145 |
Snapshot* snapshot = new Snapshot(nAtoms_, nRigidBodies_, getStorageLayout()); |
146 |
snapshot->setID(frame); |
147 |
setCurrentSnapshot(snapshot); /** @todo fixed me */ |
148 |
reader_->readFrame(frame); |
149 |
return snapshot; |
150 |
} |
151 |
|
152 |
} |