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