ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/brains/BlockSnapshotManager.cpp
Revision: 2007
Committed: Sun Feb 13 15:56:10 2005 UTC (19 years, 4 months ago) by tim
File size: 5570 byte(s)
Log Message:
dynamicProps in progress

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