1 //---------------------------------------------------------------------------// 2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com> 3 // 4 // Distributed under the Boost Software License, Version 1.0 5 // See accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt 7 // 8 // See http://boostorg.github.com/compute for more information. 9 //---------------------------------------------------------------------------// 10 11 #ifndef PERF_HPP 12 #define PERF_HPP 13 14 // this header contains general purpose functions and variables used by 15 // the boost.compute performance benchmarks. 16 17 #include <vector> 18 #include <cstdlib> 19 #include <algorithm> 20 21 #include <boost/lexical_cast.hpp> 22 #include <boost/timer/timer.hpp> 23 24 static size_t PERF_N = 1024; 25 static size_t PERF_TRIALS = 3; 26 27 // parses command line arguments and sets the corresponding perf variables perf_parse_args(int argc,char * argv[])28inline void perf_parse_args(int argc, char *argv[]) 29 { 30 if(argc >= 2){ 31 PERF_N = boost::lexical_cast<size_t>(argv[1]); 32 } 33 34 if(argc >= 3){ 35 PERF_TRIALS = boost::lexical_cast<size_t>(argv[2]); 36 } 37 } 38 39 // generates a vector of random numbers 40 template<class T> generate_random_vector(const size_t size)41std::vector<T> generate_random_vector(const size_t size) 42 { 43 std::vector<T> vector(size); 44 std::generate(vector.begin(), vector.end(), rand); 45 return vector; 46 } 47 48 // a simple timer wrapper which records multiple time entries 49 class perf_timer 50 { 51 public: 52 typedef boost::timer::nanosecond_type nanosecond_type; 53 perf_timer()54 perf_timer() 55 { 56 timer.stop(); 57 } 58 start()59 void start() 60 { 61 timer.start(); 62 } 63 stop()64 void stop() 65 { 66 timer.stop(); 67 times.push_back(timer.elapsed().wall); 68 } 69 trials() const70 size_t trials() const 71 { 72 return times.size(); 73 } 74 clear()75 void clear() 76 { 77 times.clear(); 78 } 79 last_time() const80 nanosecond_type last_time() const 81 { 82 return times.back(); 83 } 84 min_time() const85 nanosecond_type min_time() const 86 { 87 return *std::min_element(times.begin(), times.end()); 88 } 89 max_time() const90 nanosecond_type max_time() const 91 { 92 return *std::max_element(times.begin(), times.end()); 93 } 94 95 boost::timer::cpu_timer timer; 96 std::vector<boost::timer::nanosecond_type> times; 97 }; 98 99 // returns the rate (in MB/s) for processing 'count' items of type 'T' 100 // in 'time' nanoseconds 101 template<class T> perf_rate(const size_t count,perf_timer::nanosecond_type time)102double perf_rate(const size_t count, perf_timer::nanosecond_type time) 103 { 104 const size_t byte_count = count * sizeof(T); 105 106 return (double(byte_count) / 1024 / 1024) / (time / 1e9); 107 } 108 109 #endif // PERF_HPP 110