Files
openfoam/applications/test/PackedList2/Test-PackedList2.C
Mark Olesen bac943e6fc ENH: new bitSet class and improved PackedList class (closes #751)
- The bitSet class replaces the old PackedBoolList class.
  The redesign provides better block-wise access and reduced method
  calls. This helps both in cases where the bitSet may be relatively
  sparse, and in cases where advantage of contiguous operations can be
  made. This makes it easier to work with a bitSet as top-level object.

  In addition to the previously available count() method to determine
  if a bitSet is being used, now have simpler queries:

    - all()  - true if all bits in the addressable range are empty
    - any()  - true if any bits are set at all.
    - none() - true if no bits are set.

  These are faster than count() and allow early termination.

  The new test() method tests the value of a single bit position and
  returns a bool without any ambiguity caused by the return type
  (like the get() method), nor the const/non-const access (like
  operator[] has). The name corresponds to what std::bitset uses.

  The new find_first(), find_last(), find_next() methods provide a faster
  means of searching for bits that are set.

  This can be especially useful when using a bitSet to control an
  conditional:

  OLD (with macro):

      forAll(selected, celli)
      {
          if (selected[celli])
          {
              sumVol += mesh_.cellVolumes()[celli];
          }
      }

  NEW (with const_iterator):

      for (const label celli : selected)
      {
          sumVol += mesh_.cellVolumes()[celli];
      }

      or manually

      for
      (
          label celli = selected.find_first();
          celli != -1;
          celli = selected.find_next()
      )
      {
          sumVol += mesh_.cellVolumes()[celli];
      }

- When marking up contiguous parts of a bitset, an interval can be
  represented more efficiently as a labelRange of start/size.
  For example,

  OLD:

      if (isA<processorPolyPatch>(pp))
      {
          forAll(pp, i)
          {
              ignoreFaces.set(i);
          }
      }

  NEW:

      if (isA<processorPolyPatch>(pp))
      {
          ignoreFaces.set(pp.range());
      }
2018-03-07 11:21:48 +01:00

385 lines
8.8 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "boolList.H"
#include "bitSet.H"
#include "HashSet.H"
#include "cpuTime.H"
#include <vector>
#include <unordered_set>
using namespace Foam;
#undef TEST_STD_BOOLLIST
#undef TEST_STD_UNORDERED_SET
template<class T>
void printInfo(const std::unordered_set<T, Foam::Hash<T>>& ht)
{
Info<<"std::unordered_set elements:"
<< ht.size()
<< " buckets:" << ht.bucket_count()
<< " load_factor: " << ht.load_factor()
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
const label n = 1000000;
const label nIters = 1000;
unsigned long sum = 0;
bitSet packed(n, true);
boolList unpacked(n, true);
#ifdef TEST_STD_BOOLLIST
std::vector<bool> stdBoolList(n, true);
#endif
cpuTime timer;
labelHashSet emptyHash;
labelHashSet fullHash(1024);
for (label i = 0; i < n; i++)
{
fullHash.insert(i);
}
Info<< "populated labelHashSet in "
<< timer.cpuTimeIncrement() << " s\n\n";
#ifdef TEST_STD_UNORDERED_SET
std::unordered_set<label, Foam::Hash<label>> emptyStdHash;
std::unordered_set<label, Foam::Hash<label>> fullStdHash;
fullStdHash.reserve(1024);
for (label i = 0; i < n; i++)
{
fullStdHash.insert(i);
}
Info<< "populated unordered_set in "
<< timer.cpuTimeIncrement() << " s\n\n";
#endif
emptyHash.printInfo(Info);
fullHash.printInfo(Info);
#ifdef TEST_STD_UNORDERED_SET
printInfo(emptyStdHash);
printInfo(fullStdHash);
#endif
for (label iter = 0; iter < nIters; ++iter)
{
packed.resize(40);
packed.shrink();
packed.resize(n, 1);
}
Info<< "resize/shrink/resize:" << timer.cpuTimeIncrement() << " s\n\n";
Info<< "packed bool size=" << packed.size() << nl;
// Neither of these should affect the size
packed.unset(2*n-1);
packed.set(2*n-1, 0);
Info<< "packed bool size=" << packed.size() << nl;
// set every other bit on:
Info<< "set every other bit on and count\n";
packed.storage() = 0xAAAAAAAAu;
// Count packed
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(packed, i)
{
sum += packed[i];
}
}
std::cout
<< "Counting brute-force:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Count packed
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += packed.count();
}
std::cout
<< "Counting via count():" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Dummy addition
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += i + 1;
}
}
std::cout
<< "Dummy loop:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << " (sum is meaningless)" << nl;
//
// Read
//
#ifdef TEST_STD_BOOLLIST
// Read stl
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
for (unsigned int i = 0; i < stdBoolList.size(); i++)
{
sum += stdBoolList[i];
}
}
std::cout
<< "Reading stdBoolList:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << nl;
#endif
// Read unpacked
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += unpacked[i];
}
}
std::cout
<< "Reading unpacked:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << nl;
// Read packed
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(packed, i)
{
sum += packed.get(i);
}
}
std::cout
<< "Reading packed using get:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read packed
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(packed, i)
{
sum += packed[i];
}
}
std::cout
<< "Reading packed using reference:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read empty hash
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += emptyHash.found(i);
}
}
std::cout
<< "Reading empty labelHashSet:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read full hash
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += fullHash.found(i);
}
}
std::cout
<< "Reading full labelHashSet:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
#ifdef TEST_STD_UNORDERED_SET
// Read empty stl set
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += (emptyStdHash.find(i) != emptyStdHash.cend());
}
}
std::cout
<< "Reading empty std::unordered_set:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read full stl set
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += (fullStdHash.find(i) != fullStdHash.cend());
}
}
std::cout
<< "Reading full std::unordered_set:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
#endif
Info<< "Starting write tests" << nl;
//
// Write
//
#ifdef TEST_STD_BOOLLIST
// Read stl
for (label iter = 0; iter < nIters; ++iter)
{
for (unsigned int i = 0; i < stdBoolList.size(); i++)
{
stdBoolList[i] = true;
}
}
Info<< "Writing stdBoolList:" << timer.cpuTimeIncrement() << " s" << nl;
#endif
// Write unpacked
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
unpacked[i] = true;
}
}
Info<< "Writing unpacked:" << timer.cpuTimeIncrement() << " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
forAll(packed, i)
{
packed[i] = 1;
}
}
Info<< "Writing packed using reference:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
forAll(packed, i)
{
packed.set(i, 1);
}
}
Info<< "Writing packed using set:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
packed = 0;
}
Info<< "Writing packed uniform 0:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
packed = 1;
}
Info<< "Writing packed uniform 1:" << timer.cpuTimeIncrement()
<< " s" << nl;
PackedList<3> oddPacked(n, 3);
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
packed = 0;
}
Info<< "Writing packed<3> uniform 0:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
packed = 1;
}
Info<< "Writing packed<3> uniform 1:" << timer.cpuTimeIncrement()
<< " s" << nl;
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //