1 // Copyright David Abrahams, Matthias Troyer, Michael Gauckler
2 // 2005. Distributed under the Boost Software License, Version
3 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/parameter.hpp>
7 #include <boost/timer.hpp>
13 // This test measures the abstraction overhead of using the named
14 // parameter interface. Some actual test results have been recorded
15 // in timings.txt in this source file's directory, or
16 // http://www.boost.org/libs/parameter/test/timings.txt.
20 // 1. This test penalizes the named parameter library slightly, by
21 // passing two arguments through the named interface, while
22 // only passing one through the plain C++ interface.
24 // 2. This test does not measure the case where an ArgumentPack is
25 // so large that it doesn't fit in the L1 cache.
27 // 3. Although we've tried to make this test as general as
28 // possible, we are targeting it at a specific application.
29 // Where that affects design decisions, we've noted it below in
32 // 4. The first time you run this program, the time may not be
33 // representative because of disk and memory cache effects, so
34 // always run it multiple times and ignore the first
35 // measurement. This approach will also allow you to estimate
36 // the statistical error of your test by observing the
37 // variation in the valid times.
39 // 5. Try to run this program on a machine that's otherwise idle,
40 // or other processes and even device hardware interrupts may
41 // interfere by causing caches to be flushed.
43 // Accumulator function object with plain C++ interface
45 struct plain_weight_running_total
47 plain_weight_running_total()
48 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
63 BOOST_PARAMETER_KEYWORD(tag
, weight
)
64 BOOST_PARAMETER_KEYWORD(tag
, value
)
66 // Accumulator function object with named parameter interface
68 struct named_param_weight_running_total
70 named_param_weight_running_total()
71 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
78 template <class ArgumentPack
>
79 void operator()(ArgumentPack
const& variates
)
81 this->sum
+= variates
[weight
];
87 // This value is required to ensure that a smart compiler's dead
88 // code elimination doesn't optimize away anything we're testing.
89 // We'll use it to compute the return code of the executable to make
93 // Call objects of the given Accumulator type repeatedly with x as
95 template <class Accumulator
, class Arg
>
96 void hammer(Arg
const& x
, long const repeats
)
98 // Strategy: because the sum in an accumulator after each call
99 // depends on the previous value of the sum, the CPU's pipeline
100 // might be stalled while waiting for the previous addition to
101 // complete. Therefore, we allocate an array of accumulators,
102 // and update them in sequence, so that there's no dependency
103 // between adjacent addition operations.
105 // Additionally, if there were only one accumulator, the
106 // compiler or CPU might decide to update the value in a
107 // register rather that writing it back to memory. we want each
108 // operation to at least update the L1 cache. *** Note: This
109 // concern is specific to the particular application at which
110 // we're targeting the test. ***
112 // This has to be at least as large as the number of
113 // simultaneous accumulations that can be executing in the
114 // compiler pipeline. A safe number here is larger than the
115 // machine's maximum pipeline depth. If you want to test the L2
116 // or L3 cache, or main memory, you can increase the size of
117 // this array. 1024 is an upper limit on the pipeline depth of
118 // current vector machines.
119 const std::size_t number_of_accumulators
= 1024;
121 Accumulator a
[number_of_accumulators
];
123 for (long iteration
= 0; iteration
< repeats
; ++iteration
)
125 for (Accumulator
* ap
= a
; ap
< a
+ number_of_accumulators
; ++ap
)
131 // Accumulate all the partial sums to avoid dead code
133 for (Accumulator
* ap
= a
; ap
< a
+ number_of_accumulators
; ++ap
)
135 live_code
+= ap
->sum
;
139 // Measure the time required to hammer accumulators of the given
140 // type with the argument x.
141 template <class Accumulator
, class T
>
142 double measure(T
const& x
, long const repeats
)
144 // Hammer accumulators a couple of times to ensure the
145 // instruction cache is full of our test code, and that we don't
146 // measure the cost of a page fault for accessing the data page
147 // containing the memory where the accumulators will be
149 hammer
<Accumulator
>(x
, repeats
);
150 hammer
<Accumulator
>(x
, repeats
);
154 hammer
<Accumulator
>(x
, repeats
); // This time, we'll measure
155 return time
.elapsed();
161 using namespace test
;
163 // first decide how many repetitions to measure
166 while (measured
< 1.0 && repeats
<= 10000000)
172 hammer
<plain_weight_running_total
<double> >(.1, repeats
);
173 hammer
<named_param_weight_running_total
<double> >(
174 (weight
= .1, value
= .2), repeats
);
176 measured
= time
.elapsed();
181 << measure
<plain_weight_running_total
<double> >(.1, repeats
)
185 << "named parameter time: "
186 << measure
<named_param_weight_running_total
<double> >(
187 (weight
= .1, value
= .2), repeats
191 // This is ultimately responsible for preventing all the test code
192 // from being optimized away. Change this to return 0 and you
193 // unplug the whole test's life support system.
194 return live_code
< 0.;