Line data Source code
1 : #include <cmath>
2 : #include <sstream>
3 : #include <string>
4 :
5 : #include "Module/Stateful/Adaptor/Adaptor.hpp"
6 : #include "Tools/Exception/exception.hpp"
7 : #include "Tools/compute_bytes.h"
8 :
9 : namespace spu
10 : {
11 : namespace module
12 : {
13 :
14 : Adaptor::Adaptor(const size_t n_elmts, const std::type_index datatype, const size_t buffer_size)
15 : : Stateful()
16 : , n_elmts(1, n_elmts)
17 : , n_bytes(1, tools::compute_bytes(n_elmts, datatype))
18 : , datatype(1, datatype)
19 : , buffer_size(buffer_size)
20 : , n_sockets(1)
21 : , id(0)
22 : , cur_id(0)
23 : , buffer(new std::vector<std::vector<std::vector<int8_t*>>>(
24 : 1,
25 : std::vector<std::vector<int8_t*>>(1, std::vector<int8_t*>(buffer_size))))
26 : , first(new std::vector<std::atomic<uint64_t>>(1000))
27 : , last(new std::vector<std::atomic<uint64_t>>(1000))
28 : , waiting_canceled(new std::atomic<bool>(false))
29 : , no_copy_pull(false)
30 : , no_copy_push(false)
31 : {
32 : const std::string name = "Adaptor";
33 : this->set_name(name);
34 : this->set_short_name(name);
35 :
36 : if (buffer_size == 0)
37 : {
38 : std::stringstream message;
39 : message << "'buffer_size' has to be greater than 0 ('buffer_size' = " << buffer_size << ").";
40 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
41 : }
42 :
43 : if (n_elmts == 0)
44 : {
45 : std::stringstream message;
46 : message << "'n_elmts' has to be greater than 0 ('n_elmts' = " << n_elmts << ").";
47 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
48 : }
49 :
50 : for (size_t s = 0; s < this->n_sockets; s++)
51 : for (size_t b = 0; b < this->buffer_size; b++)
52 : {
53 : (*this->buffer)[this->id][s][b] = new int8_t[this->n_frames * this->n_bytes[s]];
54 : this->buffer_to_free.push_back((*this->buffer)[this->id][s][b]);
55 : }
56 :
57 : for (auto& a : *this->first.get())
58 : a = 0;
59 : for (auto& a : *this->last.get())
60 : a = 0;
61 :
62 : this->tasks_with_nullptr.resize((size_t)adp::tsk::SIZE);
63 : }
64 :
65 202 : Adaptor::Adaptor(const std::vector<size_t>& n_elmts,
66 : const std::vector<std::type_index>& datatype,
67 202 : const size_t buffer_size)
68 : : Stateful()
69 202 : , n_elmts(n_elmts)
70 202 : , n_bytes(tools::compute_bytes(n_elmts, datatype))
71 202 : , datatype(datatype)
72 202 : , buffer_size(buffer_size)
73 202 : , n_sockets(n_elmts.size())
74 202 : , id(0)
75 202 : , cur_id(0)
76 202 : , buffer(new std::vector<std::vector<std::vector<int8_t*>>>(
77 : 1,
78 404 : std::vector<std::vector<int8_t*>>(n_sockets, std::vector<int8_t*>(buffer_size))))
79 202 : , first(new std::vector<std::atomic<uint64_t>>(1000))
80 202 : , last(new std::vector<std::atomic<uint64_t>>(1000))
81 202 : , waiting_canceled(new std::atomic<bool>(false))
82 202 : , no_copy_pull(false)
83 404 : , no_copy_push(false)
84 : {
85 202 : const std::string name = "Adaptor";
86 202 : this->set_name(name);
87 202 : this->set_short_name(name);
88 :
89 202 : if (buffer_size == 0)
90 : {
91 0 : std::stringstream message;
92 0 : message << "'buffer_size' has to be greater than 0 ('buffer_size' = " << buffer_size << ").";
93 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
94 0 : }
95 :
96 202 : if (n_elmts.size() == 0)
97 : {
98 0 : std::stringstream message;
99 0 : message << "'n_elmts.size()' has to be greater than 0 ('n_elmts.size()' = " << n_elmts.size() << ").";
100 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
101 0 : }
102 :
103 551 : for (size_t e = 0; e < n_elmts.size(); e++)
104 : {
105 349 : if (n_elmts[e] == 0)
106 : {
107 0 : std::stringstream message;
108 0 : message << "'n_elmts[e]' has to be greater than 0 ('e' = " << e << ", 'n_elmts[e]' = " << n_elmts[e]
109 0 : << ").";
110 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
111 0 : }
112 : }
113 :
114 551 : for (size_t s = 0; s < this->n_sockets; s++)
115 51577 : for (size_t b = 0; b < this->buffer_size; b++)
116 : {
117 51228 : (*this->buffer)[this->id][s][b] = new int8_t[this->n_frames * this->n_bytes[s]];
118 51228 : this->buffer_to_free.push_back((*this->buffer)[this->id][s][b]);
119 : }
120 :
121 202202 : for (auto& a : *this->first.get())
122 202000 : a = 0;
123 202202 : for (auto& a : *this->last.get())
124 202000 : a = 0;
125 :
126 202 : this->tasks_with_nullptr.resize((size_t)adp::tsk::SIZE);
127 202 : }
128 :
129 : size_t
130 : Adaptor::get_n_elmts(const size_t sid) const
131 : {
132 : return this->n_elmts[sid];
133 : }
134 :
135 : size_t
136 : Adaptor::get_n_bytes(const size_t sid) const
137 : {
138 : return this->n_bytes[sid];
139 : }
140 :
141 : std::type_index
142 : Adaptor::get_datatype(const size_t sid) const
143 : {
144 : return this->datatype[sid];
145 : }
146 :
147 : bool
148 3395048 : Adaptor::is_empty(const size_t id)
149 : {
150 3395048 : return (*this->first)[id] == (*this->last)[id];
151 : }
152 :
153 : bool
154 1584868 : Adaptor::is_full(const size_t id)
155 : {
156 : // return (size_t)std::abs((int)(*this->last)[id] - (int)(*this->first)[id]) == this->buffer_size;
157 1584868 : return !this->n_free_slots(id);
158 : }
159 :
160 : size_t
161 1582035 : Adaptor::n_fill_slots(const size_t id)
162 : {
163 1582035 : return (*this->last)[id] - (*this->first)[id];
164 : }
165 :
166 : size_t
167 1581755 : Adaptor::n_free_slots(const size_t id)
168 : {
169 1581755 : return this->buffer_size - this->n_fill_slots(id);
170 : }
171 :
172 : }
173 : }
|