| 1 | gezelter | 1390 | /* | 
| 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. Redistributions of source code must retain the above copyright | 
| 10 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 |  |  | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 |  |  | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | gezelter | 1879 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | gezelter | 1782 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | gezelter | 1390 | */ | 
| 42 |  |  |  | 
| 43 |  |  | #include <algorithm> | 
| 44 |  |  | #include <cassert> | 
| 45 |  |  | #include <string> | 
| 46 | gezelter | 1782 | #include <iterator> | 
| 47 | gezelter | 1390 |  | 
| 48 |  |  | #include "utils/OpenMDBitSet.hpp" | 
| 49 |  |  | #include "utils/Algorithm.hpp" | 
| 50 | gezelter | 1802 | #ifdef IS_MPI | 
| 51 |  |  | #include <mpi.h> | 
| 52 |  |  | #endif | 
| 53 | gezelter | 1390 |  | 
| 54 |  |  | namespace OpenMD { | 
| 55 |  |  | int OpenMDBitSet::countBits() { | 
| 56 |  |  | #ifdef __RWSTD | 
| 57 |  |  | //For the compiler(Sun, MSVC6.0) binding with RougeWave STL Library, we need to use old-style | 
| 58 |  |  | // std::count which is error-prone. | 
| 59 |  |  | int count = 0; | 
| 60 |  |  | std::count(bitset_.begin(), bitset_.end(), true, count); | 
| 61 |  |  | return count; | 
| 62 |  |  | #else | 
| 63 |  |  | return std::count(bitset_.begin(), bitset_.end(), true); | 
| 64 |  |  | #endif | 
| 65 |  |  | } | 
| 66 |  |  |  | 
| 67 |  |  | void OpenMDBitSet::flip(int fromIndex, int toIndex) { | 
| 68 |  |  | assert(fromIndex <= toIndex); | 
| 69 |  |  | assert(fromIndex >=0); | 
| 70 |  |  | assert(toIndex <= size()); | 
| 71 | gezelter | 1782 | std::vector<bool>::iterator first = bitset_.begin() + fromIndex; | 
| 72 |  |  | std::vector<bool>::iterator last = bitset_.begin() + toIndex; | 
| 73 | gezelter | 1390 |  | 
| 74 |  |  | std::transform(first, last, first, std::logical_not<bool>()); | 
| 75 |  |  |  | 
| 76 |  |  | } | 
| 77 |  |  |  | 
| 78 |  |  | OpenMDBitSet OpenMDBitSet::get(int fromIndex, int toIndex) { | 
| 79 |  |  | assert(fromIndex <= toIndex); | 
| 80 |  |  | assert(fromIndex >=0); | 
| 81 |  |  | assert(toIndex <= size()); | 
| 82 | gezelter | 1782 | std::vector<bool>::iterator first = bitset_.begin() + fromIndex; | 
| 83 |  |  | std::vector<bool>::iterator last = bitset_.begin() + toIndex; | 
| 84 | gezelter | 1390 |  | 
| 85 |  |  | OpenMDBitSet result; | 
| 86 |  |  | std::copy(first, last, std::back_inserter(result.bitset_)); | 
| 87 |  |  | return result; | 
| 88 |  |  | } | 
| 89 |  |  |  | 
| 90 |  |  | bool OpenMDBitSet::none() { | 
| 91 | gezelter | 1782 | std::vector<bool>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); | 
| 92 | gezelter | 1390 | return i == bitset_.end() ? true : false; | 
| 93 |  |  | } | 
| 94 |  |  |  | 
| 95 |  |  | int OpenMDBitSet::nextOffBit(int fromIndex) const { | 
| 96 |  |  | if (fromIndex <= -1) { | 
| 97 |  |  | //in case -1 or other negative number is passed to this function | 
| 98 |  |  | return -1; | 
| 99 |  |  | } | 
| 100 |  |  |  | 
| 101 |  |  | ++fromIndex; | 
| 102 |  |  | while (fromIndex < size()) { | 
| 103 |  |  | if (!bitset_[fromIndex]) { | 
| 104 |  |  | return fromIndex; | 
| 105 |  |  | } | 
| 106 |  |  | ++fromIndex; | 
| 107 |  |  | } | 
| 108 |  |  |  | 
| 109 |  |  | return -1; | 
| 110 |  |  | } | 
| 111 |  |  |  | 
| 112 |  |  | int OpenMDBitSet::nextOnBit(int fromIndex) const { | 
| 113 |  |  | if (fromIndex <= -1) { | 
| 114 |  |  | //in case -1 or other negative number is passed to this function | 
| 115 |  |  | return -1; | 
| 116 |  |  | } | 
| 117 |  |  |  | 
| 118 |  |  | ++fromIndex; | 
| 119 |  |  | while (fromIndex < size()) { | 
| 120 |  |  | if (bitset_[fromIndex]) { | 
| 121 |  |  | return fromIndex; | 
| 122 |  |  | } | 
| 123 |  |  | ++fromIndex; | 
| 124 |  |  | } | 
| 125 |  |  |  | 
| 126 |  |  | return -1; | 
| 127 |  |  | } | 
| 128 |  |  |  | 
| 129 |  |  | void OpenMDBitSet::andOperator (const OpenMDBitSet& bs) { | 
| 130 |  |  | assert(size() == bs.size()); | 
| 131 |  |  |  | 
| 132 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); | 
| 133 |  |  | } | 
| 134 |  |  |  | 
| 135 |  |  | void OpenMDBitSet::orOperator (const OpenMDBitSet& bs) { | 
| 136 |  |  | assert(size() == bs.size()); | 
| 137 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>()); | 
| 138 |  |  | } | 
| 139 |  |  |  | 
| 140 |  |  | void OpenMDBitSet::xorOperator (const OpenMDBitSet& bs) { | 
| 141 |  |  | assert(size() == bs.size()); | 
| 142 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), OpenMD::logical_xor<bool>()); | 
| 143 |  |  | } | 
| 144 |  |  |  | 
| 145 |  |  | void OpenMDBitSet::setBits(int fromIndex, int toIndex, bool value) { | 
| 146 |  |  | assert(fromIndex <= toIndex); | 
| 147 |  |  | assert(fromIndex >=0); | 
| 148 |  |  | assert(toIndex <= size()); | 
| 149 | gezelter | 1782 | std::vector<bool>::iterator first = bitset_.begin() + fromIndex; | 
| 150 |  |  | std::vector<bool>::iterator last = bitset_.begin() + toIndex; | 
| 151 | gezelter | 1390 | std::fill(first, last, value); | 
| 152 |  |  | } | 
| 153 |  |  |  | 
| 154 |  |  | void OpenMDBitSet::resize(int nbits) { | 
| 155 |  |  | int oldSize = size(); | 
| 156 |  |  | bitset_.resize(nbits); | 
| 157 |  |  | if (nbits > oldSize) { | 
| 158 |  |  | std::fill(bitset_.begin()+oldSize, bitset_.end(), false); | 
| 159 |  |  | } | 
| 160 |  |  | } | 
| 161 |  |  |  | 
| 162 |  |  | OpenMDBitSet operator| (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { | 
| 163 |  |  | assert(bs1.size() == bs2.size()); | 
| 164 |  |  |  | 
| 165 |  |  | OpenMDBitSet result(bs1); | 
| 166 |  |  | result |= bs2; | 
| 167 |  |  | return result; | 
| 168 |  |  | } | 
| 169 |  |  |  | 
| 170 |  |  | OpenMDBitSet operator& (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { | 
| 171 |  |  | assert(bs1.size() == bs2.size()); | 
| 172 |  |  |  | 
| 173 |  |  | OpenMDBitSet result(bs1); | 
| 174 |  |  | result &= bs2; | 
| 175 |  |  | return result; | 
| 176 |  |  | } | 
| 177 |  |  |  | 
| 178 |  |  | OpenMDBitSet operator^ (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { | 
| 179 |  |  | assert(bs1.size() == bs2.size()); | 
| 180 |  |  |  | 
| 181 |  |  | OpenMDBitSet result(bs1); | 
| 182 |  |  | result ^= bs2; | 
| 183 |  |  | return result; | 
| 184 |  |  | } | 
| 185 |  |  |  | 
| 186 |  |  | OpenMDBitSet operator- (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { | 
| 187 |  |  | assert(bs1.size() == bs2.size()); | 
| 188 |  |  |  | 
| 189 |  |  | OpenMDBitSet result(bs1); | 
| 190 |  |  | result -= bs2; | 
| 191 |  |  | return result; | 
| 192 |  |  | } | 
| 193 |  |  |  | 
| 194 |  |  | bool operator== (const OpenMDBitSet & bs1, const OpenMDBitSet &bs2) { | 
| 195 |  |  | assert(bs1.size() == bs2.size()); | 
| 196 |  |  | return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); | 
| 197 | gezelter | 1802 | } | 
| 198 |  |  |  | 
| 199 |  |  | OpenMDBitSet OpenMDBitSet::parallelReduce() { | 
| 200 |  |  | OpenMDBitSet result; | 
| 201 |  |  |  | 
| 202 |  |  | #ifdef IS_MPI | 
| 203 |  |  |  | 
| 204 |  |  | // This is necessary because std::vector<bool> isn't really a | 
| 205 |  |  | // std::vector, so we can't pass the address of the first element | 
| 206 |  |  | // to the MPI call that follows.  We first have to convert to a | 
| 207 |  |  | // std::vector<int> to do the logical_or Allreduce call, then back | 
| 208 |  |  | // convert it into the vector<bool>. | 
| 209 |  |  |  | 
| 210 |  |  | std::vector<int> bsInt(bitset_.begin(), bitset_.end()); | 
| 211 |  |  |  | 
| 212 |  |  | MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &bsInt[0], | 
| 213 |  |  | bsInt.size(), MPI::INT, MPI::LOR); | 
| 214 |  |  |  | 
| 215 |  |  | std::transform(bsInt.begin(), bsInt.end(), | 
| 216 |  |  | std::back_inserter( result.bitset_ ), to_bool<int>()); | 
| 217 |  |  | #else | 
| 218 |  |  |  | 
| 219 |  |  | // Not in MPI?  Just return a copy of the current bitset: | 
| 220 |  |  | std::copy(bitset_.begin(), bitset_.end(), | 
| 221 |  |  | std::back_inserter( result.bitset_ )); | 
| 222 |  |  | #endif | 
| 223 |  |  |  | 
| 224 |  |  | return result; | 
| 225 | gezelter | 1390 | } | 
| 226 |  |  |  | 
| 227 |  |  | //std::istream& operator>> ( std::istream& is, const OpenMDBitSet& bs) { | 
| 228 |  |  | // | 
| 229 |  |  | //    return is; | 
| 230 |  |  | //} | 
| 231 |  |  |  | 
| 232 |  |  | std::ostream& operator<< ( std::ostream& os, const OpenMDBitSet& bs) { | 
| 233 | gezelter | 1782 | for (unsigned int i = 0; i < bs.bitset_.size(); ++i) { | 
| 234 | gezelter | 1390 | std::string val = bs[i] ? "true" : "false"; | 
| 235 |  |  | os << "OpenMDBitSet[" << i <<"] = " << val << std::endl; | 
| 236 |  |  | } | 
| 237 |  |  |  | 
| 238 |  |  | return os; | 
| 239 |  |  | } | 
| 240 |  |  |  | 
| 241 |  |  | } |