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 |
gezelter |
1938 |
#ifdef IS_MPI |
44 |
|
|
#include <mpi.h> |
45 |
|
|
#endif |
46 |
|
|
|
47 |
gezelter |
1390 |
#include <algorithm> |
48 |
|
|
#include <cassert> |
49 |
|
|
#include <string> |
50 |
gezelter |
1782 |
#include <iterator> |
51 |
gezelter |
1390 |
|
52 |
|
|
#include "utils/OpenMDBitSet.hpp" |
53 |
|
|
#include "utils/Algorithm.hpp" |
54 |
|
|
|
55 |
|
|
namespace OpenMD { |
56 |
|
|
int OpenMDBitSet::countBits() { |
57 |
|
|
#ifdef __RWSTD |
58 |
|
|
//For the compiler(Sun, MSVC6.0) binding with RougeWave STL Library, we need to use old-style |
59 |
|
|
// std::count which is error-prone. |
60 |
|
|
int count = 0; |
61 |
|
|
std::count(bitset_.begin(), bitset_.end(), true, count); |
62 |
|
|
return count; |
63 |
|
|
#else |
64 |
|
|
return std::count(bitset_.begin(), bitset_.end(), true); |
65 |
|
|
#endif |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
void OpenMDBitSet::flip(int fromIndex, int toIndex) { |
69 |
|
|
assert(fromIndex <= toIndex); |
70 |
|
|
assert(fromIndex >=0); |
71 |
|
|
assert(toIndex <= size()); |
72 |
gezelter |
1782 |
std::vector<bool>::iterator first = bitset_.begin() + fromIndex; |
73 |
|
|
std::vector<bool>::iterator last = bitset_.begin() + toIndex; |
74 |
gezelter |
1390 |
|
75 |
|
|
std::transform(first, last, first, std::logical_not<bool>()); |
76 |
|
|
|
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
OpenMDBitSet OpenMDBitSet::get(int fromIndex, int toIndex) { |
80 |
|
|
assert(fromIndex <= toIndex); |
81 |
|
|
assert(fromIndex >=0); |
82 |
|
|
assert(toIndex <= size()); |
83 |
gezelter |
1782 |
std::vector<bool>::iterator first = bitset_.begin() + fromIndex; |
84 |
|
|
std::vector<bool>::iterator last = bitset_.begin() + toIndex; |
85 |
gezelter |
1390 |
|
86 |
|
|
OpenMDBitSet result; |
87 |
|
|
std::copy(first, last, std::back_inserter(result.bitset_)); |
88 |
|
|
return result; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
bool OpenMDBitSet::none() { |
92 |
gezelter |
1782 |
std::vector<bool>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); |
93 |
gezelter |
1390 |
return i == bitset_.end() ? true : false; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
int OpenMDBitSet::nextOffBit(int fromIndex) const { |
97 |
|
|
if (fromIndex <= -1) { |
98 |
|
|
//in case -1 or other negative number is passed to this function |
99 |
|
|
return -1; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
++fromIndex; |
103 |
|
|
while (fromIndex < size()) { |
104 |
|
|
if (!bitset_[fromIndex]) { |
105 |
|
|
return fromIndex; |
106 |
|
|
} |
107 |
|
|
++fromIndex; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
return -1; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
int OpenMDBitSet::nextOnBit(int fromIndex) const { |
114 |
|
|
if (fromIndex <= -1) { |
115 |
|
|
//in case -1 or other negative number is passed to this function |
116 |
|
|
return -1; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
++fromIndex; |
120 |
|
|
while (fromIndex < size()) { |
121 |
|
|
if (bitset_[fromIndex]) { |
122 |
|
|
return fromIndex; |
123 |
|
|
} |
124 |
|
|
++fromIndex; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
return -1; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
void OpenMDBitSet::andOperator (const OpenMDBitSet& bs) { |
131 |
|
|
assert(size() == bs.size()); |
132 |
|
|
|
133 |
|
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
void OpenMDBitSet::orOperator (const OpenMDBitSet& bs) { |
137 |
|
|
assert(size() == bs.size()); |
138 |
|
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>()); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
void OpenMDBitSet::xorOperator (const OpenMDBitSet& bs) { |
142 |
|
|
assert(size() == bs.size()); |
143 |
|
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), OpenMD::logical_xor<bool>()); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
void OpenMDBitSet::setBits(int fromIndex, int toIndex, bool value) { |
147 |
|
|
assert(fromIndex <= toIndex); |
148 |
|
|
assert(fromIndex >=0); |
149 |
|
|
assert(toIndex <= size()); |
150 |
gezelter |
1782 |
std::vector<bool>::iterator first = bitset_.begin() + fromIndex; |
151 |
|
|
std::vector<bool>::iterator last = bitset_.begin() + toIndex; |
152 |
gezelter |
1390 |
std::fill(first, last, value); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
void OpenMDBitSet::resize(int nbits) { |
156 |
|
|
int oldSize = size(); |
157 |
|
|
bitset_.resize(nbits); |
158 |
|
|
if (nbits > oldSize) { |
159 |
|
|
std::fill(bitset_.begin()+oldSize, bitset_.end(), false); |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
OpenMDBitSet operator| (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { |
164 |
|
|
assert(bs1.size() == bs2.size()); |
165 |
|
|
|
166 |
|
|
OpenMDBitSet result(bs1); |
167 |
|
|
result |= bs2; |
168 |
|
|
return result; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
OpenMDBitSet operator& (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { |
172 |
|
|
assert(bs1.size() == bs2.size()); |
173 |
|
|
|
174 |
|
|
OpenMDBitSet result(bs1); |
175 |
|
|
result &= bs2; |
176 |
|
|
return result; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
OpenMDBitSet operator^ (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { |
180 |
|
|
assert(bs1.size() == bs2.size()); |
181 |
|
|
|
182 |
|
|
OpenMDBitSet result(bs1); |
183 |
|
|
result ^= bs2; |
184 |
|
|
return result; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
OpenMDBitSet operator- (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2) { |
188 |
|
|
assert(bs1.size() == bs2.size()); |
189 |
|
|
|
190 |
|
|
OpenMDBitSet result(bs1); |
191 |
|
|
result -= bs2; |
192 |
|
|
return result; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
bool operator== (const OpenMDBitSet & bs1, const OpenMDBitSet &bs2) { |
196 |
|
|
assert(bs1.size() == bs2.size()); |
197 |
|
|
return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); |
198 |
gezelter |
1802 |
} |
199 |
|
|
|
200 |
|
|
OpenMDBitSet OpenMDBitSet::parallelReduce() { |
201 |
|
|
OpenMDBitSet result; |
202 |
|
|
|
203 |
|
|
#ifdef IS_MPI |
204 |
|
|
|
205 |
|
|
// This is necessary because std::vector<bool> isn't really a |
206 |
|
|
// std::vector, so we can't pass the address of the first element |
207 |
|
|
// to the MPI call that follows. We first have to convert to a |
208 |
|
|
// std::vector<int> to do the logical_or Allreduce call, then back |
209 |
|
|
// convert it into the vector<bool>. |
210 |
|
|
|
211 |
|
|
std::vector<int> bsInt(bitset_.begin(), bitset_.end()); |
212 |
|
|
|
213 |
|
|
MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &bsInt[0], |
214 |
|
|
bsInt.size(), MPI::INT, MPI::LOR); |
215 |
|
|
|
216 |
|
|
std::transform(bsInt.begin(), bsInt.end(), |
217 |
|
|
std::back_inserter( result.bitset_ ), to_bool<int>()); |
218 |
|
|
#else |
219 |
|
|
|
220 |
|
|
// Not in MPI? Just return a copy of the current bitset: |
221 |
|
|
std::copy(bitset_.begin(), bitset_.end(), |
222 |
|
|
std::back_inserter( result.bitset_ )); |
223 |
|
|
#endif |
224 |
|
|
|
225 |
|
|
return result; |
226 |
gezelter |
1390 |
} |
227 |
|
|
|
228 |
|
|
//std::istream& operator>> ( std::istream& is, const OpenMDBitSet& bs) { |
229 |
|
|
// |
230 |
|
|
// return is; |
231 |
|
|
//} |
232 |
|
|
|
233 |
|
|
std::ostream& operator<< ( std::ostream& os, const OpenMDBitSet& bs) { |
234 |
gezelter |
1782 |
for (unsigned int i = 0; i < bs.bitset_.size(); ++i) { |
235 |
gezelter |
1390 |
std::string val = bs[i] ? "true" : "false"; |
236 |
|
|
os << "OpenMDBitSet[" << i <<"] = " << val << std::endl; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
return os; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
} |