1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman
Copyright (c) 2005-2006 Dan Marsden
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <boost/array.hpp>
#include <boost/timer.hpp>
#include <boost/fusion/algorithm/iteration/accumulate.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/algorithm/transformation/zip.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/adapted/array.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iostream>
#include <cmath>
#include <limits>
#ifdef _MSC_VER
// inline aggressively
# pragma inline_recursion(on) // turn on inline recursion
# pragma inline_depth(255) // max inline depth
#endif
int const REPEAT_COUNT = 10;
double const duration = 0.5;
namespace
{
struct poly_add
{
template<typename Sig>
struct result;
template<typename Lhs, typename Rhs>
struct result<poly_add(Lhs, Rhs)>
: boost::remove_reference<Lhs>
{};
template<typename Lhs, typename Rhs>
Lhs operator()(const Lhs& lhs, const Rhs& rhs) const
{
return lhs + rhs;
}
};
struct poly_mult
{
template<typename Sig>
struct result;
template<typename Lhs, typename Rhs>
struct result<poly_mult(Lhs, Rhs)>
: boost::remove_reference<Lhs>
{};
template<typename Lhs, typename Rhs>
Lhs operator()(const Lhs& lhs, const Rhs& rhs) const
{
return lhs * rhs;
}
};
template<int N>
double time_for_std_inner_product(int& j)
{
boost::timer tim;
int i = 0;
long long iter = 65536;
long long counter, repeats;
double result = (std::numeric_limits<double>::max)();
double runtime = 0;
double run;
boost::array<int, N> arr1;
boost::array<int, N> arr2;
std::generate(arr1.begin(), arr1.end(), rand);
std::generate(arr2.begin(), arr2.end(), rand);
do
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0);
static_cast<void>(i);
}
runtime = tim.elapsed();
iter *= 2;
} while(runtime < duration);
iter /= 2;
// repeat test and report least value for consistency:
for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0);
j += i;
}
run = tim.elapsed();
result = (std::min)(run, result);
}
std::cout << i << std::endl;
return result / iter;
}
template<int N>
double time_for_fusion_inner_product(int& j)
{
boost::timer tim;
int i = 0;
long long iter = 65536;
long long counter, repeats;
double result = (std::numeric_limits<double>::max)();
double runtime = 0;
double run;
boost::array<int, N> arr1;
boost::array<int, N> arr2;
std::generate(arr1.begin(), arr1.end(), rand);
std::generate(arr2.begin(), arr2.end(), rand);
do
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = boost::fusion::accumulate(
boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add());
static_cast<void>(i);
}
runtime = tim.elapsed();
iter *= 2;
} while(runtime < duration);
iter /= 2;
// repeat test and report least value for consistency:
for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = boost::fusion::accumulate(
boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add());
j += i;
}
run = tim.elapsed();
result = (std::min)(run, result);
}
std::cout << i << std::endl;
return result / iter;
}
}
int main()
{
int total = 0;
int res;
std::cout << "short inner_product std test " << time_for_std_inner_product<8>(res) << std::endl;
total += res;
std::cout << "short inner_product fusion test " << time_for_fusion_inner_product<8>(res) << std::endl;
total += res;
std::cout << "medium inner_product std test " << time_for_std_inner_product<64>(res) << std::endl;
total += res;
std::cout << "medium inner_product fusion test " << time_for_fusion_inner_product<64>(res) << std::endl;
total += res;
std::cout << "long inner_product std test " << time_for_std_inner_product<128>(res) << std::endl;
total += res;
std::cout << "long inner_product fusion test " << time_for_fusion_inner_product<128>(res) << std::endl;
total += res;
return total;
}
|