Line data Source code
1 : #include <sstream> 2 : #include <string> 3 : 4 : #include "Module/Stateful/Finalizer/Finalizer.hpp" 5 : 6 : using namespace spu; 7 : using namespace spu::module; 8 : 9 : template<typename T> 10 138 : Finalizer<T>::Finalizer(const size_t n_elmts, const size_t history_size, const size_t ns) 11 : : Stateful() 12 138 : , data(history_size, std::vector<std::vector<T>>(this->get_n_frames(), std::vector<T>(n_elmts, 0))) 13 138 : , next_stream_id(0) 14 138 : , ns(ns) 15 : { 16 138 : const std::string name = "Finalizer"; 17 138 : this->set_name(name); 18 138 : this->set_short_name(name); 19 : 20 138 : if (n_elmts == 0) 21 : { 22 0 : std::stringstream message; 23 0 : message << "'n_elmts' has to be greater than 0."; 24 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str()); 25 0 : } 26 : 27 138 : if (history_size == 0) 28 : { 29 0 : std::stringstream message; 30 0 : message << "'history_size' has to be greater than 0."; 31 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str()); 32 0 : } 33 : 34 138 : auto& p = this->create_task("finalize"); 35 138 : auto ps_in = this->template create_socket_in<T>(p, "in", n_elmts); 36 138 : this->create_codelet(p, 37 389250 : [ps_in](Module& m, runtime::Task& t, const size_t frame_id) -> int 38 : { 39 389250 : auto& fin = static_cast<Finalizer&>(m); 40 389250 : fin._finalize(t[ps_in].template get_dataptr<const T>(), frame_id); 41 390399 : return runtime::status_t::SUCCESS; 42 : }); 43 138 : } 44 : 45 : template<typename T> 46 : Finalizer<T>* 47 2661 : Finalizer<T>::clone() const 48 : { 49 2661 : auto m = new Finalizer(*this); 50 2661 : m->deep_copy(*this); 51 2661 : return m; 52 : } 53 : 54 : template<typename T> 55 : size_t 56 0 : Finalizer<T>::get_ns() const 57 : { 58 0 : return this->ns; 59 : } 60 : 61 : template<typename T> 62 : void 63 32 : Finalizer<T>::set_ns(const size_t ns) 64 : { 65 32 : this->ns = ns; 66 32 : } 67 : 68 : template<typename T> 69 : const std::vector<std::vector<T>>& 70 24305 : Finalizer<T>::get_final_data() const 71 : { 72 24305 : auto last_stream_id = this->next_stream_id ? this->next_stream_id - 1 : this->data.size() - 1; 73 24305 : return this->data[last_stream_id]; 74 : } 75 : 76 : template<typename T> 77 : const std::vector<std::vector<std::vector<T>>>& 78 4590 : Finalizer<T>::get_histo_data() const 79 : { 80 4590 : return this->data; 81 : } 82 : 83 : template<typename T> 84 : size_t 85 0 : Finalizer<T>::get_next_stream_id() const 86 : { 87 0 : return this->next_stream_id; 88 : } 89 : 90 : template<typename T> 91 : void 92 3722 : Finalizer<T>::set_n_frames(const size_t n_frames) 93 : { 94 3722 : const auto old_n_frames = this->get_n_frames(); 95 3722 : if (old_n_frames != n_frames) 96 : { 97 3722 : Module::set_n_frames(n_frames); 98 9238 : for (size_t s = 0; s < this->data.size(); s++) 99 : { 100 5516 : this->data[s].resize(n_frames); 101 30766 : for (size_t f = old_n_frames; f < n_frames; f++) 102 25250 : this->data[s][f].resize(this->data[s][0].size()); 103 : } 104 : } 105 3722 : } 106 : 107 : template<typename T> 108 : void 109 0 : Finalizer<T>::finalize(const T* in, const int frame_id, const bool managed_memory) 110 : { 111 0 : (*this)[fin::sck::finalize::in].bind(in); 112 0 : (*this)[fin::tsk::finalize].exec(frame_id, managed_memory); 113 0 : } 114 : 115 : template<typename T> 116 : void 117 389281 : Finalizer<T>::_finalize(const T* in, const size_t frame_id) 118 : { 119 389281 : std::chrono::time_point<std::chrono::steady_clock> t_start; 120 389281 : if (this->ns) t_start = std::chrono::steady_clock::now(); 121 : 122 389281 : std::copy(in, in + this->data[this->next_stream_id][0].size(), this->data[this->next_stream_id][frame_id].begin()); 123 : 124 390532 : if (frame_id == this->get_n_frames() - 1) this->next_stream_id = (this->next_stream_id + 1) % this->data.size(); 125 : 126 390403 : if (this->ns) 127 : { 128 3517 : std::chrono::nanoseconds duration = std::chrono::steady_clock::now() - t_start; 129 244616 : while ((size_t)duration.count() < this->ns) // active waiting 130 241094 : duration = std::chrono::steady_clock::now() - t_start; 131 : } 132 390400 : } 133 : 134 : template<typename T> 135 : void 136 1024 : Finalizer<T>::reset() 137 : { 138 1024 : this->next_stream_id = 0; 139 1024 : } 140 : 141 : // ==================================================================================== explicit template instantiation 142 : template class spu::module::Finalizer<int8_t>; 143 : template class spu::module::Finalizer<uint8_t>; 144 : template class spu::module::Finalizer<int16_t>; 145 : template class spu::module::Finalizer<uint16_t>; 146 : template class spu::module::Finalizer<int32_t>; 147 : template class spu::module::Finalizer<uint32_t>; 148 : template class spu::module::Finalizer<int64_t>; 149 : template class spu::module::Finalizer<uint64_t>; 150 : template class spu::module::Finalizer<float>; 151 : template class spu::module::Finalizer<double>; 152 : // ==================================================================================== explicit template instantiation