|
| 1 | +#ifndef __RCPP_PARALLEL_TIMER__ |
| 2 | +#define __RCPP_PARALLEL_TIMER__ |
| 3 | + |
| 4 | +namespace RcppParallel { |
| 5 | + |
| 6 | + template <typename Timer> |
| 7 | + class SingleTimer { |
| 8 | + public: |
| 9 | + SingleTimer() : timer(){} |
| 10 | + |
| 11 | + inline operator SEXP(){ |
| 12 | + Rcpp::List out = Rcpp::List::create(timer) ; |
| 13 | + out.attr("class") = Rcpp::CharacterVector::create( "SingleTimer", "Timer" ); |
| 14 | + return out ; |
| 15 | + } |
| 16 | + |
| 17 | + inline void step( const char* name ){ |
| 18 | + timer.step(name) ; |
| 19 | + } |
| 20 | + |
| 21 | + private: |
| 22 | + Timer timer ; |
| 23 | + } ; |
| 24 | + |
| 25 | + template <typename Timer> |
| 26 | + class FixedSizeTimers { |
| 27 | + public: |
| 28 | + FixedSizeTimers( int n ) : timers(n){} |
| 29 | + |
| 30 | + inline Timer& get(int i) { |
| 31 | + return timers[i] ; |
| 32 | + } |
| 33 | + |
| 34 | + inline operator SEXP(){ |
| 35 | + Rcpp::List out = wrap(timers) ; |
| 36 | + out.attr("class") = Rcpp::CharacterVector::create("FixedSizeTimers", "Timer") ; |
| 37 | + return out ; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + private: |
| 42 | + std::vector<Timer> timers ; |
| 43 | + } ; |
| 44 | + |
| 45 | + template <typename Timer, typename Mutex, typename Locker> |
| 46 | + class TimersList { |
| 47 | + public: |
| 48 | + |
| 49 | + TimersList(): timers(){} |
| 50 | + |
| 51 | + Timer& get_new_timer(){ |
| 52 | + Locker lock(mutex) ; |
| 53 | + if( timers.empty() ) { |
| 54 | + timers.push_back( Timer() ) ; |
| 55 | + } else { |
| 56 | + timers.push_back( Timer( timers.front().origin() ) ) ; |
| 57 | + } |
| 58 | + std::cout << timers.size() << " timers" << std::endl ; |
| 59 | + return timers.back() ; |
| 60 | + } |
| 61 | + |
| 62 | + inline operator SEXP(){ |
| 63 | + Rcpp::List data = wrap(timers) ; |
| 64 | + data.attr("class") = Rcpp::CharacterVector::create( "TimersList" , "Timer" ) ; |
| 65 | + return data ; |
| 66 | + } |
| 67 | + |
| 68 | + private: |
| 69 | + TimersList( const TimersList& ) ; |
| 70 | + std::list<Timer> timers ; |
| 71 | + Mutex mutex ; |
| 72 | + |
| 73 | + } ; |
| 74 | + |
| 75 | + |
| 76 | +} // namespace RcppParallel |
| 77 | + |
| 78 | + |
| 79 | +#endif // __RCPP_PARALLEL_COMMON__ |
0 commit comments