| 1 | tim | 318 | /* | 
| 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 | tim | 354 | #include "utils/residentMem.h" | 
| 44 | tim | 322 | #include "utils/physmem.h" | 
| 45 | tim | 324 | #include "utils/Algorithm.hpp" | 
| 46 | tim | 318 | #include "brains/SimInfo.hpp" | 
| 47 |  |  | #include "io/DumpReader.hpp" | 
| 48 |  |  |  | 
| 49 |  |  | namespace oopse { | 
| 50 | tim | 324 | BlockSnapshotManager::BlockSnapshotManager(SimInfo* info, const std::string& filename, | 
| 51 | tim | 318 | int storageLayout, int blockCapacity) | 
| 52 | tim | 351 | : SnapshotManager(storageLayout), info_(info), blockCapacity_(blockCapacity), | 
| 53 |  |  | activeBlocks_(blockCapacity_, -1), activeRefCount_(blockCapacity_, 0) { | 
| 54 | tim | 318 |  | 
| 55 |  |  | nAtoms_ = info->getNGlobalAtoms(); | 
| 56 |  |  | nRigidBodies_ = info->getNGlobalRigidBodies(); | 
| 57 |  |  |  | 
| 58 | tim | 354 | double physMem = physmem_total(); | 
| 59 |  |  | double rssMem = residentMem(); | 
| 60 | tim | 355 | double avaliablePhysMem = physMem - rssMem; | 
| 61 |  |  |  | 
| 62 | tim | 318 | int bytesPerStuntDouble = DataStorage::getBytesPerStuntDouble(storageLayout); | 
| 63 |  |  |  | 
| 64 | tim | 323 | int bytesPerFrame = (nRigidBodies_ + nAtoms_) * bytesPerStuntDouble; | 
| 65 | tim | 318 |  | 
| 66 | tim | 355 | int frameCapacity = int (avaliablePhysMem / bytesPerFrame); | 
| 67 | tim | 318 |  | 
| 68 |  |  | nSnapshotPerBlock_ = frameCapacity /blockCapacity_ ; | 
| 69 |  |  | reader_ = new DumpReader(info, filename); | 
| 70 | tim | 323 | nframes_ = reader_->getNFrames(); | 
| 71 | tim | 318 |  | 
| 72 | tim | 323 | int nblocks = nframes_ / nSnapshotPerBlock_; | 
| 73 |  |  | if (nframes_ % nSnapshotPerBlock_ != 0) { | 
| 74 | tim | 318 | ++nblocks; | 
| 75 |  |  | } | 
| 76 |  |  |  | 
| 77 |  |  | for (int i = 0; i < nblocks; ++i) { | 
| 78 | tim | 337 | blocks_.push_back(SnapshotBlock(i*nSnapshotPerBlock_, (i+1)*nSnapshotPerBlock_)); | 
| 79 | tim | 318 | } | 
| 80 |  |  | //the last block may not have nSnapshotPerBlock frames, we need to consider this special situation | 
| 81 | tim | 324 | blocks_.back().second = nframes_; | 
| 82 | tim | 318 |  | 
| 83 | tim | 347 | snapshots_.insert(snapshots_.begin(), nframes_, static_cast<Snapshot*>(NULL)); | 
| 84 | tim | 358 |  | 
| 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 | tim | 318 |  | 
| 88 |  |  | } | 
| 89 |  |  |  | 
| 90 |  |  |  | 
| 91 | tim | 324 | BlockSnapshotManager::~BlockSnapshotManager() { | 
| 92 | tim | 318 | currentSnapshot_ = NULL; | 
| 93 |  |  | previousSnapshot_ = NULL; | 
| 94 |  |  |  | 
| 95 |  |  | delete reader_; | 
| 96 | tim | 324 |  | 
| 97 |  |  | std::vector<int>::iterator i; | 
| 98 |  |  | for (i = activeBlocks_.begin(); i != activeBlocks_.end(); ++i) { | 
| 99 |  |  | if (*i != -1) { | 
| 100 |  |  | unloadBlock(*i); | 
| 101 |  |  | } | 
| 102 |  |  | } | 
| 103 | tim | 318 | } | 
| 104 |  |  |  | 
| 105 |  |  | int BlockSnapshotManager::getNActiveBlocks() { | 
| 106 | tim | 347 | #ifdef __RWSTD | 
| 107 |  |  | int count = 0; | 
| 108 |  |  | std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1), count); | 
| 109 |  |  | return count; | 
| 110 |  |  | #else | 
| 111 | tim | 324 | return std::count_if(activeBlocks_.begin(), activeBlocks_.end(), std::bind2nd(std::not_equal_to<int>(), -1)); | 
| 112 | tim | 347 | #endif | 
| 113 | tim | 318 | } | 
| 114 |  |  |  | 
| 115 |  |  |  | 
| 116 | tim | 351 |  | 
| 117 | tim | 318 | bool BlockSnapshotManager::loadBlock(int block) { | 
| 118 | tim | 351 | std::vector<int>::iterator i = findActiveBlock(block); | 
| 119 | tim | 318 | bool loadSuccess; | 
| 120 | tim | 351 | if (i != activeBlocks_.end()) { | 
| 121 |  |  | //if block is already in memory, just increast the reference count | 
| 122 |  |  | ++activeRefCount_[i - activeBlocks_.begin()]; | 
| 123 | tim | 318 | loadSuccess = true; | 
| 124 |  |  | } else if (getNActiveBlocks() < blockCapacity_){ | 
| 125 | tim | 351 | //if number of active blocks is less than the block capacity, just load it | 
| 126 |  |  | internalLoad(block); | 
| 127 | tim | 318 | loadSuccess = true; | 
| 128 | tim | 351 | } 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 | tim | 318 | loadSuccess = false; | 
| 137 |  |  | } | 
| 138 | tim | 351 |  | 
| 139 | tim | 318 | return loadSuccess; | 
| 140 |  |  | } | 
| 141 |  |  |  | 
| 142 |  |  | bool BlockSnapshotManager::unloadBlock(int block) { | 
| 143 |  |  | bool unloadSuccess; | 
| 144 | tim | 351 | 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 | tim | 318 | } | 
| 152 | tim | 351 |  | 
| 153 | tim | 324 | unloadSuccess = true; | 
| 154 | tim | 351 | } else { | 
| 155 |  |  | unloadSuccess = false; | 
| 156 | tim | 318 | } | 
| 157 | tim | 324 |  | 
| 158 |  |  | return unloadSuccess; | 
| 159 | tim | 318 | } | 
| 160 |  |  |  | 
| 161 | tim | 351 | 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 | tim | 352 | return std::find(activeRefCount_.begin(), activeRefCount_.end(), 0) != activeRefCount_.end() ?  true : false; | 
| 187 | tim | 351 | } | 
| 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 | tim | 318 | std::vector<int> BlockSnapshotManager::getActiveBlocks() { | 
| 195 |  |  | std::vector<int> result; | 
| 196 | tim | 324 | oopse::copy_if(activeBlocks_.begin(), activeBlocks_.end(), std::back_inserter(result), | 
| 197 |  |  | std::bind2nd(std::not_equal_to<int>(), -1)); | 
| 198 |  |  | return result; | 
| 199 | tim | 318 | } | 
| 200 |  |  |  | 
| 201 |  |  | Snapshot* BlockSnapshotManager::loadFrame(int frame){ | 
| 202 |  |  | Snapshot* snapshot = new Snapshot(nAtoms_, nRigidBodies_, getStorageLayout()); | 
| 203 |  |  | snapshot->setID(frame); | 
| 204 | tim | 324 |  | 
| 205 |  |  | /** @todo fixed me */ | 
| 206 |  |  | Snapshot* oldSnapshot = currentSnapshot_; | 
| 207 |  |  | currentSnapshot_ = snapshot; | 
| 208 | tim | 318 | reader_->readFrame(frame); | 
| 209 | tim | 324 | currentSnapshot_ = oldSnapshot; | 
| 210 | tim | 318 | return snapshot; | 
| 211 |  |  | } | 
| 212 |  |  |  | 
| 213 | tim | 322 | int BlockSnapshotManager::getNFrames() { | 
| 214 |  |  | return reader_->getNFrames(); | 
| 215 | tim | 318 | } | 
| 216 | tim | 322 |  | 
| 217 |  |  | } |