Created
January 9, 2012 17:07
-
-
Save pesterie/1583914 to your computer and use it in GitHub Desktop.
Bench problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================== | |
// Copyright 2003 - 2011 LASMEA UMR 6602 CNRS/Univ. Clermont II | |
// Copyright 2009 - 2011 LRI UMR 8623 CNRS/Univ Paris Sud XI | |
// | |
// Distributed under the Boost Software License, Version 1.0. | |
// See accompanying file LICENSE.txt or copy at | |
// http://www.boost.org/LICENSE_1_0.txt | |
//============================================================================== | |
#include <cstdlib> | |
#include <vector> | |
#include <algorithm> | |
#include <iostream> | |
#include <nt2/sdk/unit/perform_benchmark.hpp> | |
#include <boost/simd/sdk/memory/allocator.hpp> | |
#include <boost/simd/sdk/simd/iterator.hpp> | |
#include <boost/simd/sdk/simd/pack.hpp> | |
#include <boost/simd/include/functions/plus.hpp> | |
#include <boost/simd/include/functions/multiplies.hpp> | |
#include <nt2/include/timing.hpp> | |
template<class T> | |
struct Taxpy | |
{ | |
public: | |
typedef T value_type; | |
typedef std::vector<T, boost::simd::memory::allocator<T> > vector_type; | |
Taxpy(std::size_t const& s, T const& a) : size(s), alpha(a), alpha_(boost::simd::pack<T>(a)) | |
{ | |
X.resize(s); Y.resize(s); Z.resize(s); | |
} | |
void run_scalar(std::size_t const& samples) | |
{ | |
std::cout << "======== SCAL ========\n"; | |
typename vector_type::iterator itz; | |
typename vector_type::iterator itx; | |
typename vector_type::iterator ity; | |
for(int i = 0; i<size; ++i) X[i] = Y[i] = T(i); | |
double time, cycle; | |
std::vector<double> cycles,timings; | |
for(int i = 0; i < samples; ++i) | |
{ | |
itz = Z.begin(); | |
itx = X.begin(); | |
ity = Y.begin(); | |
time = nt2::details::now(); | |
cycle = nt2::details::read_cycles(); | |
for(;itz != Z.end(); ++itz) | |
{ | |
*itz = alpha*(*itx++) + *ity++; | |
} | |
cycle = nt2::details::read_cycles() - cycle; | |
time = nt2::details::now() - time; | |
cycles.push_back(cycle); | |
timings.push_back(time); | |
} | |
std::cout << "# samples : " << cycles.size() << "\n\t" | |
<< "time : " << nt2::unit::median(timings.begin(),timings.end())/timings.size() << "\n\t" | |
<< "cycles/elem: " << nt2::unit::median(cycles.begin(),cycles.end())/Z.size() << "\n"; | |
} | |
void run_simd(std::size_t const& samples) | |
{ | |
std::cout << "======== SIMD ========\n"; | |
typedef typename boost::simd::iterator<T>::iterator it_; | |
it_ itz_; | |
it_ itx_; | |
it_ ity_; | |
it_ end_ = boost::simd::end(Z.end()); | |
for(int i = 0; i<size; ++i) X[i] = Y[i] = T(i); | |
double time_, cycle_; | |
std::vector<double> cycles_,timings_; | |
for(int i = 0; i < samples; ++i) | |
{ | |
itz_ = boost::simd::begin(Z.begin()); | |
itx_ = boost::simd::begin(X.begin()); | |
ity_ = boost::simd::begin(Y.begin()); | |
time_ = nt2::details::now(); | |
cycle_ = nt2::details::read_cycles(); | |
for(; itz_ != end_; ++itz_) | |
{ | |
*itz_ = alpha_*(*(itx_++)) + *(ity_++); | |
} | |
cycle_ = nt2::details::read_cycles() - cycle_; | |
time_ = nt2::details::now() - time_; | |
cycles_.push_back(cycle_); | |
timings_.push_back(time_); | |
} | |
std::cout << "# samples : " << cycles_.size() << "\n\t" | |
<< "time : " << nt2::unit::median(timings_.begin(),timings_.end())/timings_.size() << "\n\t" | |
<< "cycles : " << nt2::unit::median(cycles_.begin(),cycles_.end()) << "\n"; | |
} | |
private: | |
T alpha; | |
boost::simd::pack<T> alpha_; | |
std::size_t size; | |
vector_type X; | |
vector_type Y; | |
vector_type Z; | |
}; | |
int main(int argc, char** argv) | |
{ | |
std::size_t size = atoi(argv[1]); | |
Taxpy<float> saxpy(size, 6.558943); | |
saxpy.run_scalar(50); | |
saxpy.run_simd(50); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment