17 #ifndef LLO_OPERATOR_HPP 18 #define LLO_OPERATOR_HPP 24 using EngineT = std::default_random_engine;
31 void unary (T* out, ade::Shape& outshape,
32 VecRef<T> in, std::function<T(
const T&)> f)
34 if (in.
mapper == ade::identity)
36 for (ade::NElemT i = 0, n = in.
shape.n_elems(); i < n; ++i)
38 out[i] = f(in.
data[i]);
44 for (ade::NElemT i = 0, n = in.
shape.n_elems(); i < n; ++i)
46 in.
mapper->forward(coord.begin(),
47 ade::coordinate(in.
shape, i).begin());
48 out[ade::index(outshape, coord)] = f(in.
data[i]);
54 for (ade::NElemT i = 0, n = outshape.n_elems(); i < n; ++i)
56 in.
mapper->forward(coord.begin(),
57 ade::coordinate(outshape, i).begin());
58 out[i] = f(in.
data[ade::index(in.
shape, coord)]);
68 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::abs(src); });
88 unary<T>(out, in.
shape, in, [](
const T& src) {
return -src; });
105 template <
typename T>
108 unary<T>(out, in.
shape, in, [](
const T& src) {
return !src; });
113 template <
typename T>
116 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::sin(src); });
121 template <
typename T>
124 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::cos(src); });
129 template <
typename T>
132 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::tan(src); });
137 template <
typename T>
140 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::exp(src); });
145 template <
typename T>
148 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::log(src); });
153 template <
typename T>
156 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::sqrt(src); });
161 template <
typename T>
164 unary<T>(out, in.
shape, in, [](
const T& src) {
return std::round(src); });
168 template <
typename OUT,
typename ATYPE,
typename BTYPE>
170 std::function<OUT(
const ATYPE&,
const BTYPE&)> f)
179 for (ade::NElemT i = 0, n = outshape.n_elems(); i < n; ++i)
181 coord = ade::coordinate(outshape, i);
182 a.
mapper->forward(acoord.begin(), coord.begin());
183 b.
mapper->forward(bcoord.begin(), coord.begin());
191 std::vector<ATYPE> tmpdata(outshape.n_elems());
195 for (ade::NElemT i = 0, n = a.
shape.n_elems(); i < n; ++i)
197 a.
mapper->forward(coord.begin(),
198 ade::coordinate(a.
shape, i).begin());
199 tmpdata[ade::index(outshape, coord)] = a.
data[i];
204 for (ade::NElemT i = 0, n = outshape.n_elems(); i < n; ++i)
206 a.
mapper->forward(coord.begin(),
207 ade::coordinate(outshape, i).begin());
208 tmpdata[i] = a.
data[ade::index(a.
shape, coord)];
213 for (ade::NElemT i = 0, n = b.
shape.n_elems(); i < n; ++i)
215 b.
mapper->forward(coord.begin(),
216 ade::coordinate(b.
shape, i).begin());
217 ade::NElemT outidx = ade::index(outshape, coord);
218 out[outidx] = f(tmpdata[outidx], b.
data[i]);
223 for (ade::NElemT i = 0, n = outshape.n_elems(); i < n; ++i)
225 b.
mapper->forward(coord.begin(),
226 ade::coordinate(outshape, i).begin());
227 out[i] = f(tmpdata[i], b.
data[ade::index(b.
shape, coord)]);
236 template <
typename T>
239 binary<T,T,T>(out, outshape, a, b,
240 [](
const T& b,
const T& x) {
return std::pow(b, x); });
246 template <
typename T>
249 binary<T,T,T>(out, outshape, a, b,
250 [](
const T& a,
const T& b) {
return a - b; });
256 template <
typename T>
259 binary<T,T,T>(out, outshape, a, b,
260 [](
const T& a,
const T& b) {
return a / b; });
266 template <
typename T>
269 binary<T,T,T>(out, outshape, a, b,
270 [](
const T& a,
const T& b) {
return a == b; });
276 template <
typename T>
279 binary<T,T,T>(out, outshape, a, b,
280 [](
const T& a,
const T& b) {
return a != b; });
286 template <
typename T>
289 binary<T,T,T>(out, outshape, a, b,
290 [](
const T& a,
const T& b) {
return a < b; });
296 template <
typename T>
299 binary<T,T,T>(out, outshape, a, b,
300 [](
const T& a,
const T& b) {
return a > b; });
306 template <
typename T>
309 binary<T,T,double>(out, outshape, a, b,
310 [](
const T& a,
const double& b)
312 std::binomial_distribution<T> dist(a, b);
319 ade::Shape& outshape, VecRef<double> a, VecRef<double> b);
323 ade::Shape& outshape, VecRef<float> a, VecRef<double> b);
328 template <
typename T>
332 binary<T,T,T>(out, outshape, a, b,
333 [](
const T& a,
const T& b)
335 std::uniform_int_distribution<T> dist(a, b);
342 ade::Shape& outshape, VecRef<double> a, VecRef<double> b);
346 ade::Shape& outshape, VecRef<float> a, VecRef<float> b);
351 template <
typename T>
354 throw std::bad_function_call();
359 ade::Shape& outshape, VecRef<double> a, VecRef<double> b);
363 ade::Shape& outshape, VecRef<float> a, VecRef<float> b);
366 template <
typename T>
368 std::function<
void(T&,
const T&)> acc)
370 ade::NElemT nout = outshape.n_elems();
372 std::memset(visited,
false, nout);
378 for (ade::NElemT i = 0, n = arg.shape.n_elems(); i < n; ++i)
380 arg.mapper->forward(coord.begin(),
381 ade::coordinate(arg.shape, i).begin());
382 ade::NElemT outidx = ade::index(outshape, coord);
385 acc(out[outidx], arg.data[i]);
389 out[outidx] = arg.data[i];
390 visited[outidx] =
true;
396 for (ade::NElemT i = 0, n = outshape.n_elems(); i < n; ++i)
398 arg.mapper->forward(coord.begin(),
399 ade::coordinate(outshape, i).begin());
400 ade::NElemT inidx = ade::index(arg.shape, coord);
403 acc(out[i], arg.data[inidx]);
407 out[i] = arg.data[inidx];
418 template <
typename T>
421 nnary<T>(out, outshape, args,
422 [](T& out,
const T& val) { out += val; });
427 template <
typename T>
430 nnary<T>(out, outshape, args,
431 [](T& out,
const T& val) { out *= val; });
436 template <
typename T>
439 nnary<T>(out, outshape, args,
440 [](T& out,
const T& val) { out =
std::min(out, val); });
445 template <
typename T>
448 nnary<T>(out, outshape, args,
449 [](T& out,
const T& val) { out =
std::max(out, val); });
454 #endif // LLO_OPERATOR_HPP void abs< uint64_t >(uint64_t *out, VecRef< uint64_t > in)
void gt(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:297
const T * data
Raw input data.
Definition: data.hpp:249
void tan(T *out, VecRef< T > in)
Definition: operator.hpp:130
void rand_binom< double >(double *out, ade::Shape &outshape, VecRef< double > a, VecRef< double > b)
void neg< uint8_t >(uint8_t *out, VecRef< uint8_t > in)
void nnary(T *out, ade::Shape &outshape, std::vector< VecRef< T >> args, std::function< void(T &, const T &)> acc)
Generic n-nary operation.
Definition: operator.hpp:367
void neg< uint16_t >(uint16_t *out, VecRef< uint16_t > in)
void exp(T *out, VecRef< T > in)
Definition: operator.hpp:138
EngineT & get_engine(void)
Return global random generator.
void eq(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:267
void abs< uint32_t >(uint32_t *out, VecRef< uint32_t > in)
std::default_random_engine EngineT
RNG engine used.
Definition: operator.hpp:24
void add(T *out, ade::Shape &outshape, std::vector< VecRef< T >> args)
Definition: operator.hpp:419
void unary(T *out, ade::Shape &outshape, VecRef< T > in, std::function< T(const T &)> f)
Generic unary operation assuming identity mapping.
Definition: operator.hpp:31
void binary(OUT *out, ade::Shape &outshape, VecRef< ATYPE > a, VecRef< BTYPE > b, std::function< OUT(const ATYPE &, const BTYPE &)> f)
Generic binary operation assuming identity mapping.
Definition: operator.hpp:169
void cos(T *out, VecRef< T > in)
Definition: operator.hpp:122
void rand_normal< double >(double *out, ade::Shape &outshape, VecRef< double > a, VecRef< double > b)
void neq(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:277
void round(T *out, VecRef< T > in)
Definition: operator.hpp:162
void rand_binom< float >(float *out, ade::Shape &outshape, VecRef< float > a, VecRef< double > b)
void min(T *out, ade::Shape &outshape, std::vector< VecRef< T >> args)
Definition: operator.hpp:437
void rand_uniform(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:329
void rand_uniform< float >(float *out, ade::Shape &outshape, VecRef< float > a, VecRef< float > b)
void abs< uint8_t >(uint8_t *out, VecRef< uint8_t > in)
void rand_uniform< double >(double *out, ade::Shape &outshape, VecRef< double > a, VecRef< double > b)
void sqrt(T *out, VecRef< T > in)
Definition: operator.hpp:154
void neg< uint64_t >(uint64_t *out, VecRef< uint64_t > in)
void rand_normal(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:352
bool push
Definition: data.hpp:259
ade::Shape shape
Shape info of the raw input.
Definition: data.hpp:252
void sub(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:247
void log(T *out, VecRef< T > in)
Definition: operator.hpp:146
void abs< uint16_t >(uint16_t *out, VecRef< uint16_t > in)
void max(T *out, ade::Shape &outshape, std::vector< VecRef< T >> args)
Definition: operator.hpp:446
void abs(T *out, VecRef< T > in)
Definition: operator.hpp:66
void mul(T *out, ade::Shape &outshape, std::vector< VecRef< T >> args)
Definition: operator.hpp:428
void sin(T *out, VecRef< T > in)
Definition: operator.hpp:114
void lt(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:287
void div(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:257
void neg(T *out, VecRef< T > in)
Definition: operator.hpp:86
ade::CoordptrT mapper
Coordinate mapper of input to output.
Definition: data.hpp:255
void rand_normal< float >(float *out, ade::Shape &outshape, VecRef< float > a, VecRef< float > b)
void pow(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< T > b)
Definition: operator.hpp:237
void rand_binom(T *out, ade::Shape &outshape, VecRef< T > a, VecRef< double > b)
Definition: operator.hpp:307
void bit_not(T *out, VecRef< T > in)
Definition: operator.hpp:106
void neg< uint32_t >(uint32_t *out, VecRef< uint32_t > in)