Test-PackedList.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 "uLabel.H"
32 #include "IOobject.H"
33 #include "IOstreams.H"
34 #include "IFstream.H"
35 #include "PackedBoolList.H"
36 #include <climits>
37 
38 
39 using namespace Foam;
40 
41 template<unsigned nBits>
42 inline void reportInfo()
43 {
44  unsigned offset = PackedList<nBits>::packing();
45 
46  unsigned useSHL = ((1u << (nBits * offset)) - 1);
47  unsigned useSHR = (~0u >> (sizeof(unsigned)*CHAR_BIT - nBits * offset));
48 
49  Info<< nl
50  << "PackedList<" << nBits << ">" << nl
51  << " max_value: " << PackedList<nBits>::max_value() << nl
52  << " packing: " << PackedList<nBits>::packing() << nl
53  << " utilization: " << (nBits * offset) << nl;
54 
55  Info<< " Masking:" << nl
56  << " shift << "
57  << unsigned(nBits * offset) << nl
58  << " shift >> "
59  << unsigned((sizeof(unsigned)*CHAR_BIT) - nBits * offset)
60  << nl;
61 
62  hex(Info);
63  Info<< " maskLower: "
65  << nl
66  << " useSHL: " << useSHL << nl
67  << " useSHR: " << useSHR << nl;
68 
69  if (useSHL != useSHR)
70  {
71  Info<< "WARNING: different results for SHL and SHR" << nl;
72  }
73 
74  Info<< nl;
75  dec(Info);
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80 // Main program:
81 
82 int main(int argc, char *argv[])
83 {
85  argList::validArgs.insert("file .. fileN");
86 
87  argList::addBoolOption("mask", "report information about the bit masks");
88  argList::addBoolOption("count", "test the count() method");
90  (
91  "info",
92  "print an ascii representation of the storage"
93  );
94 
95  argList args(argc, argv, false, true);
96 
97 
98  if (args.optionFound("mask"))
99  {
100  Info<< "bit width: " << unsigned(sizeof(unsigned)*CHAR_BIT) << endl;
101  reportInfo<1>();
102  reportInfo<2>();
103  reportInfo<3>();
104  reportInfo<4>();
105  reportInfo<5>();
106  reportInfo<6>();
107  reportInfo<7>();
108  reportInfo<8>();
109  reportInfo<9>();
110  reportInfo<10>();
111  reportInfo<11>();
112  reportInfo<12>();
113  reportInfo<13>();
114  reportInfo<14>();
115  reportInfo<15>();
116  reportInfo<16>();
117 
118  return 0;
119  }
120  else if (args.size() <= 1)
121  {
122  args.printUsage();
123  }
124 
125 
126  for (label argI=1; argI < args.size(); ++argI)
127  {
128  const string& srcFile = args[argI];
129  Info<< nl << "reading " << srcFile << nl;
130 
131  IFstream ifs(srcFile);
132  List<label> rawLst(ifs);
133 
134  PackedBoolList packLst(rawLst);
135 
136  Info<< "size: " << packLst.size() << nl;
137 
138  if (args.optionFound("count"))
139  {
140  unsigned int rawCount = 0;
141  forAll(rawLst, elemI)
142  {
143  if (rawLst[elemI])
144  {
145  rawCount++;
146  }
147  }
148  Info<< "raw count: " << rawCount << nl
149  << "packed count: " << packLst.count() << nl;
150  }
151 
152  if (args.optionFound("info"))
153  {
154  packLst.printInfo(Info);
155  }
156 
157  Info<< nl;
159  }
160 
161  return 0;
162 }
163 
164 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::argList::size
label size() const
Return the number of arguments.
Definition: argListI.H:84
Foam::PackedList::packing
static unsigned int packing()
The number of entries per packed storage element.
Definition: PackedListI.H:46
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
main
int main(int argc, char *argv[])
Definition: Test-PackedList.C:79
reportInfo
void reportInfo()
Definition: Test-PackedList.C:39
Foam::PackedList::maskLower
static unsigned int maskLower(unsigned offset)
Masking for all bits below the offset.
Definition: PackedListI.H:53
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::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:558
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
PackedBoolList.H
argList.H
Foam::argList::printUsage
void printUsage() const
Print usage.
Definition: argList.C:1045
Foam::PackedList::count
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
IFstream.H
IOobject.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::PackedList::printInfo
Ostream & printInfo(Ostream &, const bool fullOutput=false) const
Print information and bit patterns (with printBits)
Definition: PackedList.C:235
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
Foam::PackedList::max_value
static unsigned int max_value()
The max. value for an entry, which simultaneously the bit-mask.
Definition: PackedListI.H:39
Foam::List< label >
Foam::PackedList
A dynamically allocatable list of packed unsigned integers.
Definition: PackedList.H:117
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::PackedList::size
label size() const
Number of entries.
Definition: PackedListI.H:714
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
uLabel.H
args
Foam::argList args(argc, argv)