Test-PackedList2.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25 
26 Description
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "argList.H"
31 #include "boolList.H"
32 #include "PackedBoolList.H"
33 #include "HashSet.H"
34 #include "StaticHashTable.H"
35 #include "cpuTime.H"
36 #include <vector>
37 
38 using namespace Foam;
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 
43 // Main program:
44 
45 int main(int argc, char *argv[])
46 {
47  const label n = 1000000;
48  const label nIters = 1000;
49 
50  unsigned int sum = 0;
51 
52  PackedBoolList packed(n, 1);
53  boolList unpacked(n, true);
54  std::vector<bool> stlVector(n, true);
55 
56  labelHashSet emptyHash;
57  labelHashSet fullHash(1000);
58  for (label i = 0; i < n; i++)
59  {
60  fullHash.insert(i);
61  }
62 
63  // fullStaticHash is really slow
64  // give it lots of slots to help
66  StaticHashTable<nil, label, Hash<label> > fullStaticHash(100000);
67  for (label i = 0; i < n; i++)
68  {
69  fullStaticHash.insert(i, nil());
70  }
71 
72  emptyHash.printInfo(Info);
73  fullHash.printInfo(Info);
74  emptyStaticHash.printInfo(Info);
75  fullStaticHash.printInfo(Info);
76 
77 
78  cpuTime timer;
79 
80  for (label iter = 0; iter < nIters; ++iter)
81  {
82  packed.resize(40);
83  packed.shrink();
84  packed.resize(n, 1);
85  }
86  Info<< "resize/shrink/resize:" << timer.cpuTimeIncrement() << " s\n\n";
87 
88  // set every other bit on:
89  Info<< "set every other bit on and count\n";
90  packed.storage() = 0xAAAAAAAAu;
91 
92  // Count packed
93  sum = 0;
94  for (label iter = 0; iter < nIters; ++iter)
95  {
96  forAll(packed, i)
97  {
98  sum += packed[i];
99  }
100  }
101  Info<< "Counting brute-force:" << timer.cpuTimeIncrement()
102  << " s" << endl;
103  Info<< " sum " << sum << endl;
104 
105 
106  // Count packed
107  sum = 0;
108  for (label iter = 0; iter < nIters; ++iter)
109  {
110  sum += packed.count();
111  }
112  Info<< "Counting via count():" << timer.cpuTimeIncrement()
113  << " s" << endl;
114  Info<< " sum " << sum << endl;
115 
116 
117  // Dummy addition
118  sum = 0;
119  for (label iter = 0; iter < nIters; ++iter)
120  {
121  forAll(unpacked, i)
122  {
123  sum += i + 1;
124  }
125  }
126  Info<< "Dummy loop:" << timer.cpuTimeIncrement() << " s" << endl;
127  Info<< " sum " << sum << endl;
128 
129  //
130  // Read
131  //
132 
133  // Read stl
134  sum = 0;
135  for (label iter = 0; iter < nIters; ++iter)
136  {
137  for (unsigned int i = 0; i < stlVector.size(); i++)
138  {
139  sum += stlVector[i];
140  }
141  }
142  Info<< "Reading stl:" << timer.cpuTimeIncrement() << " s" << endl;
143  Info<< " sum " << sum << endl;
144 
145 
146  // Read unpacked
147  sum = 0;
148  for (label iter = 0; iter < nIters; ++iter)
149  {
150  forAll(unpacked, i)
151  {
152  sum += unpacked[i];
153  }
154  }
155  Info<< "Reading unpacked:" << timer.cpuTimeIncrement() << " s" << endl;
156  Info<< " sum " << sum << endl;
157 
158 
159  // Read packed
160  sum = 0;
161  for (label iter = 0; iter < nIters; ++iter)
162  {
163  forAll(packed, i)
164  {
165  sum += packed.get(i);
166  }
167  }
168  Info<< "Reading packed using get:" << timer.cpuTimeIncrement()
169  << " s" << endl;
170  Info<< " sum " << sum << endl;
171 
172 
173  // Read packed
174  sum = 0;
175  for (label iter = 0; iter < nIters; ++iter)
176  {
177  forAll(packed, i)
178  {
179  sum += packed[i];
180  }
181  }
182  Info<< "Reading packed using reference:" << timer.cpuTimeIncrement()
183  << " s" << endl;
184  Info<< " sum " << sum << endl;
185 
186 
187  // Read via iterator
188  sum = 0;
189  for (label iter = 0; iter < nIters; ++iter)
190  {
191  forAllIter(PackedBoolList, packed, it)
192  {
193  sum += it;
194  }
195  }
196  Info<< "Reading packed using iterator:" << timer.cpuTimeIncrement()
197  << " s" << endl;
198  Info<< " sum " << sum << endl;
199 
200 
201  // Read via iterator
202  sum = 0;
203  for (label iter = 0; iter < nIters; ++iter)
204  {
205  forAllConstIter(PackedBoolList, packed, cit)
206  {
207  sum += cit();
208  }
209  }
210  Info<< "Reading packed using const_iterator():" << timer.cpuTimeIncrement()
211  << " s" << endl;
212  Info<< " sum " << sum << endl;
213 
214 
215  // Read empty hash
216  sum = 0;
217  for (label iter = 0; iter < nIters; ++iter)
218  {
219  forAll(unpacked, i)
220  {
221  sum += emptyHash.found(i);
222  }
223  }
224  Info<< "Reading empty labelHashSet:" << timer.cpuTimeIncrement()
225  << " s" << endl;
226  Info<< " sum " << sum << endl;
227 
228 
229  // Read full hash
230  sum = 0;
231  for (label iter = 0; iter < nIters; ++iter)
232  {
233  forAll(unpacked, i)
234  {
235  sum += fullHash.found(i);
236  }
237  }
238  Info<< "Reading full labelHashSet:" << timer.cpuTimeIncrement()
239  << " s" << endl;
240  Info<< " sum " << sum << endl;
241 
242 
243  // Read empty static hash
244  sum = 0;
245  for (label iter = 0; iter < nIters; ++iter)
246  {
247  forAll(unpacked, i)
248  {
249  sum += emptyStaticHash.found(i);
250  }
251  }
252  Info<< "Reading empty StaticHash:" << timer.cpuTimeIncrement()
253  << " s" << endl;
254  Info<< " sum " << sum << endl;
255 
256 #if 0
257  // we can skip this test - it is usually quite slow
258  // Read full static hash
259  sum = 0;
260  for (label iter = 0; iter < nIters; ++iter)
261  {
262  forAll(unpacked, i)
263  {
264  sum += fullStaticHash.found(i);
265  }
266  }
267  Info<< "Reading full StaticHash:" << timer.cpuTimeIncrement()
268  << " s" << endl;
269  Info<< " sum " << sum << endl;
270 #endif
271 
272  Info<< "Starting write tests" << endl;
273 
274  //
275  // Write
276  //
277 
278  // Write stl
279  for (label iter = 0; iter < nIters; ++iter)
280  {
281  for (unsigned int i = 0; i < stlVector.size(); i++)
282  {
283  stlVector[i] = true;
284  }
285  }
286  Info<< "Writing stl:" << timer.cpuTimeIncrement() << " s" << endl;
287 
288  // Write unpacked
289  for (label iter = 0; iter < nIters; ++iter)
290  {
291  forAll(unpacked, i)
292  {
293  unpacked[i] = true;
294  }
295  }
296  Info<< "Writing unpacked:" << timer.cpuTimeIncrement() << " s" << endl;
297 
298 
299  // Write packed
300  for (label iter = 0; iter < nIters; ++iter)
301  {
302  forAll(packed, i)
303  {
304  packed[i] = 1;
305  }
306  }
307  Info<< "Writing packed using reference:" << timer.cpuTimeIncrement()
308  << " s" << endl;
309 
310 
311  // Write packed
312  for (label iter = 0; iter < nIters; ++iter)
313  {
314  forAll(packed, i)
315  {
316  packed.set(i, 1);
317  }
318  }
319  Info<< "Writing packed using set:" << timer.cpuTimeIncrement()
320  << " s" << endl;
321 
322 
323  // Write packed
324  for (label iter = 0; iter < nIters; ++iter)
325  {
326  forAllIter(PackedBoolList, packed, it)
327  {
328  it() = 1;
329  }
330  }
331  Info<< "Writing packed using iterator:" << timer.cpuTimeIncrement()
332  << " s" << endl;
333 
334 
335  // Write packed
336  for (label iter = 0; iter < nIters; ++iter)
337  {
338  packed = 0;
339  }
340  Info<< "Writing packed uniform 0:" << timer.cpuTimeIncrement()
341  << " s" << endl;
342 
343 
344  // Write packed
345  for (label iter = 0; iter < nIters; ++iter)
346  {
347  packed = 1;
348  }
349  Info<< "Writing packed uniform 1:" << timer.cpuTimeIncrement()
350  << " s" << endl;
351 
352 
353  PackedList<3> oddPacked(n, 3);
354 
355  // Write packed
356  for (label iter = 0; iter < nIters; ++iter)
357  {
358  packed = 0;
359  }
360  Info<< "Writing packed<3> uniform 0:" << timer.cpuTimeIncrement()
361  << " s" << endl;
362 
363 
364  // Write packed
365  for (label iter = 0; iter < nIters; ++iter)
366  {
367  packed = 1;
368  }
369  Info<< "Writing packed<3> uniform 1:" << timer.cpuTimeIncrement()
370  << " s" << endl;
371 
372 
373  Info<< "End\n" << endl;
374 
375  return 0;
376 }
377 
378 
379 // ************************************************************************* //
Foam::cpuTime
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
main
int main(int argc, char *argv[])
Definition: Test-PackedList2.C:42
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
boolList.H
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::PackedList::storage
List< unsigned int > & storage()
Return the underlying packed storage.
Definition: PackedListI.H:919
Foam::PackedBoolList::set
void set(const PackedList< 1 > &)
Set specified bits.
Definition: PackedBoolList.C:169
Foam::nil
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::HashSet< label, Hash< label > >
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
StaticHashTable.H
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::StaticHashTable
STL conforming hash table.
Definition: StaticHashTable.H:58
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::HashTable::printInfo
Ostream & printInfo(Ostream &) const
Print information.
Definition: HashTableIO.C:58
Foam::StaticHashTable::printInfo
Ostream & printInfo(Ostream &) const
Print information.
Definition: StaticHashTableIO.C:61
Foam::Info
messageStream Info
PackedBoolList.H
argList.H
Foam::PackedList::count
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
HashSet.H
Foam::HashTable::found
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:109
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
cpuTime.H
Foam::PackedList::get
unsigned int get(const label) const
Get value at index I.
Definition: PackedListI.H:964
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::StaticHashTable::insert
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
Definition: StaticHashTableI.H:60
Foam::PackedList
A dynamically allocatable list of packed unsigned integers.
Definition: PackedList.H:117
Foam::timer
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:333
Foam::PackedList::resize
void resize(const label, const unsigned int &val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:729
Foam::PackedList::shrink
void shrink()
Shrink the allocated space to what is actually used.
Definition: PackedListI.H:908
Foam::StaticHashTable::found
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: StaticHashTable.C:125