TensorIndicesStress_test.cc
Go to the documentation of this file.
1 /**
2  * @file TensorIndicesStress_test.cc
3  * @brief Stress test for TensorIndices class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date June 28, 2016
6  * @see TensorIndices.h
7  *
8  * This test performs repeated queries to a TensorIndices object of rank 5.
9  * Usage:
10  * ~~~~
11  * TensorIndicesStress_test [DimSize]
12  * ~~~~
13  * where `DimSize` is the dimension of each size (default: 16 and 32 for debug
14  * and non-debug compilation respectively).
15  * This test is not extremely representative of a real use case in that the loop
16  * content is small and can likely kept in the processor cache, which is not
17  * often the case in real scenarios.
18  *
19  */
20 
21 // LArSoft libraries
23 
24 // C/C++ standard libraries
25 #include <array>
26 #include <chrono>
27 #include <sstream>
28 #include <iostream>
29 
30 
31 //------------------------------------------------------------------------------
32 //--- Test code
33 //---
34 
35 int main(int argc, char** argv) {
36 
37  // debug being much slower, by default we ask for less cycles
38 #ifdef NDEBUG
39  unsigned int dimSize = 80; // default value
40 #else // !NDEBUG
41  unsigned int dimSize = 32; // default value
42 #endif // ?NDEBUG
43 
44  //
45  // command line argument parsing
46  //
47  if (argc > 1) {
48  std::istringstream sstr(argv[1]);
49  sstr >> dimSize;
50  if (!sstr || (dimSize <= 0)) {
51  std::cerr << "Invalid dimension size: '" << argv[1] << "'." << std::endl;
52  return 1;
53  }
54  }
55 
56 
57  //
58  // set up
59  //
60  auto indices
61  = util::makeTensorIndices(dimSize, dimSize, dimSize, dimSize, dimSize);
62 
63  std::cout << "Running through " << indices.dim<0>()
64  << "x" << indices.dim<1>()
65  << "x" << indices.dim<2>()
66  << "x" << indices.dim<3>()
67  << "x" << indices.dim<4>()
68  << " = " << indices.size() << " tensor elements"
69  << std::endl;
70 
71  //
72  // run
73  //
74  auto startTime = std::chrono::high_resolution_clock::now();
75  using index_t = decltype(indices)::Index_t;
76  std::array<index_t, indices.rank()> i;
77  decltype(indices)::DimSize_t count = 0;
78  for (i[0] = 0; i[0] < (index_t) indices.dim<0>(); ++(i[0])) {
79  for (i[1] = 0; i[1] < (index_t) indices.dim<1>(); ++(i[1])) {
80  for (i[2] = 0; i[2] < (index_t) indices.dim<2>(); ++(i[2])) {
81  for (i[3] = 0; i[3] < (index_t) indices.dim<3>(); ++(i[3])) {
82  for (i[4] = 0; i[4] < (index_t) indices.dim<4>(); ++(i[4])) {
83 
84  auto linIndex = indices(i[0], i[1], i[2], i[3], i[4]);
85  if (count != linIndex) {
86  std::cerr << "Error: ["
87  << i[0] << "][" << i[1] << "][" << i[2] << "]["
88  << i[3] << "][" << i[4]
89  << "] => " << linIndex << " (expected: " << count << ")"
90  << std::endl;
91  return 1;
92  }
93  ++count;
94  } // loop 4
95  } // loop 3
96  } // loop 2
97  } // loop 1
98  } // loop 0
100 
101  std::chrono::duration<double> elapsed = stopTime - startTime;
102  std::cout << "Iterating through all " << count << " indices took "
103  << (elapsed.count() * 1000.) << " milliseconds." << std::endl;
104 
105  return 0;
106 } // main()
TensorIndices class to flatten multi-dimension indices into linear.
int main(int argc, char **argv)
unsigned int Index_t
Type to denote the index of the flag.
Definition: BitMask.h:60
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
auto makeTensorIndices(DIMS...dims)
Instantiates a TensorIndices class with the specified dimensions.
QTextStream & endl(QTextStream &s)