mxnet
mask.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_MASK_H_
8 #define MSHADOW_EXTENSION_MASK_H_
9 
10 #include "../extension.h"
11 
12 namespace mshadow {
13 namespace expr {
14 
20 template<typename IndexExp, typename SrcExp, typename DType>
21 struct MaskExp: public Exp<MaskExp<IndexExp, SrcExp, DType>,
22  DType, type::kChainer> {
24  const IndexExp &index_;
26  const SrcExp &src_;
28  MaskExp(const IndexExp &index, const SrcExp &src)
29  : index_(index), src_(src) {}
30 }; // struct MaskExp
31 
32 
33 
34 template<typename IndexExp,
35  typename SrcExp,
36  typename DType,
37  int e1, int e2>
40  const Exp<SrcExp, DType, e2> &src) {
41  return MaskExp<IndexExp, SrcExp, DType>(index.self(), src.self());
42 }
43 
44 
45 //----------------------
46 // Execution plan
47 //----------------------
48 
49 template<typename IndexExp, typename SrcExp, typename DType>
50 struct Plan<MaskExp<IndexExp, SrcExp, DType>, DType> {
51  public:
53  : index_(MakePlan(e.index_)), src_(MakePlan(e.src_)) {
54  }
55 
56  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
57  return static_cast<DType>(src_.Eval(y, x) * index_.Eval(0, y));
58  }
59 
60  private:
63 }; // struct Plan
64 
65 template<typename IndexExp, typename SrcExp, typename DType>
68  return Plan<MaskExp<IndexExp, SrcExp, DType>, DType>(exp);
69 }
70 
71 template<int dim, typename IndexExp, typename SrcExp, typename DType>
72 struct ShapeCheck<dim, MaskExp<IndexExp, SrcExp, DType> > {
73  inline static Shape<dim>
75  CHECK(dim == 2)
76  << "MaskExp only support 2D output";
79  CHECK_EQ(dshape[0], wshape[0]) << "MaskExp require inputs in same first dimention";
80  Shape<dim> ret;
81  ret[0] = wshape[0];
82  ret[1] = wshape[1];
83  return ret;
84  }
85 };
86 
87 
88 template<typename IndexExp, typename SrcExp, typename DType>
89 struct ExpInfo<MaskExp<IndexExp, SrcExp, DType> > {
90  static const int kDim = 2;
91  static const int kDevMask = ExpInfo<IndexExp>::kDevMask;
92 };
93 
94 } // namespace expr
95 } // namespace mshadow
96 
97 #endif // MSHADOW_EXTENSION_MASK_H_
MaskExp(const IndexExp &index, const SrcExp &src)
Definition: mask.h:28
Definition: expr_engine-inl.h:40
const SrcExp & src_
matrix oprand
Definition: mask.h:26
static Shape< dim > Check(const MaskExp< IndexExp, SrcExp, DType > &t)
Definition: mask.h:74
const IndexExp & index_
index oprand
Definition: mask.h:24
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:244
MaskExp< IndexExp, SrcExp, DType > mask(const Exp< IndexExp, DType, e1 > &index, const Exp< SrcExp, DType, e2 > &src)
Definition: mask.h:39
Plan(const MaskExp< IndexExp, SrcExp, DType > &e)
Definition: mask.h:52
int32_t index_t
type that will be used for index
Definition: base.h:291
Broadcast a mask and do element-wise multiplication.
Definition: mask.h:21
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
const SubType & self(void) const
Definition: expression.h:64
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:221
namespace for mshadow
Definition: base.h:282
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: mask.h:56