Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
tensor_base.h
Go to the documentation of this file.
1 #ifndef MSHADOW_TENSOR_BASE_H
2 #define MSHADOW_TENSOR_BASE_H
3 
9 #include <cmath>
10 #include <cstdio>
11 #include <cfloat>
12 #include <climits>
13 #include <algorithm>
14 // macro defintiions
15 
17 #ifndef MSHADOW_STAND_ALONE
18  #define MSHADOW_STAND_ALONE 0
19 #endif
20 
22 #ifndef MSHADOW_ALLOC_PAD
23  #define MSHADOW_ALLOC_PAD true
24 #endif
25 
31 #ifndef MSHADOW_MIN_PAD_RATIO
32  #define MSHADOW_MIN_PAD_RATIO 2
33 #endif
34 
35 #if MSHADOW_STAND_ALONE
36  #define MSHADOW_USE_CBLAS 0
37  #define MSHADOW_USE_MKL 0
38  #define MSHADOW_USE_CUDA 0
39 #endif
40 
42 #ifndef MSHADOW_USE_CBLAS
43  #define MSHADOW_USE_CBLAS 0
44 #endif
45 
46 #ifndef MSHADOW_USE_MKL
47  #define MSHADOW_USE_MKL 1
48 #endif
49 
50 #ifndef MSHADOW_USE_CUDA
51  #define MSHADOW_USE_CUDA 1
52 #endif
53 
54 #ifndef MSHADOW_SINGLE_PRECISION
55  #define MSHADOW_SINGLE_PRECISION 1
56 #endif
57 
58 #ifndef MSHADOW_USE_SSE
59  #define MSHADOW_USE_SSE 1
60 #endif
61 
62 #ifndef MSHADOW_USE_NVML
63  #define MSHADOW_USE_NVML 0
64 #endif
65 // SSE is conflict with cudacc
66 #ifdef __CUDACC__
67  #undef MSHADOW_USE_SSE
68  #define MSHADOW_USE_SSE 0
69 #endif
70 
71 #if MSHADOW_USE_CBLAS
72 extern "C"{
73  #include <cblas.h>
74 }
75 #elif MSHADOW_USE_MKL
76  #include <mkl.h>
77  #include <mkl_cblas.h>
78  #include <mkl_vsl.h>
79  #include <mkl_vsl_functions.h>
80 #endif
81 
82 #if MSHADOW_USE_CUDA
83  #include <cublas.h>
84  #include <curand.h>
85 #endif
86 
87 #if MSHADOW_USE_NVML
88  #include <nvml.h>
89 #endif
90 // --------------------------------
91 // MSHADOW_XINLINE is used for inlining template code for both CUDA and CPU code.
92 #ifdef MSHADOW_XINLINE
93  #error "MSHADOW_XINLINE must not be defined"
94 #endif
95 #ifdef __CUDACC__
96  #define MSHADOW_XINLINE inline __attribute__((always_inline)) __device__ __host__
97 #else
98  #define MSHADOW_XINLINE inline __attribute__((always_inline))
99 #endif
100 
101 #define MSHADOW_CINLINE inline __attribute__((always_inline))
102 
103 #if defined(__GXX_EXPERIMENTAL_CXX0X) || defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
104  #define MSHADOW_CONSTEXPR constexpr
105 #else
106  #define MSHADOW_CONSTEXPR const
107 #endif
108 
110 namespace mshadow {
112  const unsigned kRandBufferSize = 1000000;
114  const float kPi = 3.1415926f;
115 
116 #if MSHADOW_SINGLE_PRECISION
117 
118  typedef float real_t;
119 #else
120  typedef double real_t;
121 #endif
122 
123  typedef unsigned index_t;
124 }; // namespace mshadow
125 
126 namespace mshadow {
128  namespace op {
129  // binary operator
131  struct mul{
133  MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
134  return a * b;
135  }
136  };
138  struct plus {
140  MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
141  return a + b;
142  }
143  };
145  struct minus {
147  MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
148  return a - b;
149  }
150  };
152  struct div {
154  MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
155  return a / b;
156  }
157  };
159  struct right {
161  MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
162  return b;
163  }
164  };
165  }; // namespace op
166 
168  namespace sv {
170  struct saveto {
172  MSHADOW_XINLINE static void Save(real_t& a, real_t b) {
173  a = b;
174  }
176  MSHADOW_CONSTEXPR static real_t kAlphaBLAS = 1.0f;
178  MSHADOW_CONSTEXPR static real_t kBetaBLAS = 0.0f;
180  typedef op::right OPType;
181  };
183  struct plusto {
185  MSHADOW_XINLINE static void Save(real_t& a, real_t b) {
186  a += b;
187  }
189  MSHADOW_CONSTEXPR static real_t kAlphaBLAS = 1.0f;
191  MSHADOW_CONSTEXPR static real_t kBetaBLAS = 1.0f;
193  typedef op::plus OPType;
194  };
196  struct minusto {
198  MSHADOW_XINLINE static void Save(real_t& a, real_t b) {
199  a -= b;
200  }
202  MSHADOW_CONSTEXPR static real_t kAlphaBLAS = -1.0f;
204  MSHADOW_CONSTEXPR static real_t kBetaBLAS = 1.0f;
206  typedef op::minus OPType;
207  };
209  struct multo {
211  MSHADOW_XINLINE static void Save(real_t& a, real_t b) {
212  a *= b;
213  }
215  typedef op::mul OPType;
216  };
218  struct divto {
220  MSHADOW_XINLINE static void Save(real_t& a, real_t b) {
221  a /= b;
222  }
224  typedef op::div OPType;
225  };
226  }; // namespace sv
227 
228 
229  namespace op {
230  // unary operator/ function: example
231  // these operators can be defined by user, in the same style as binary and unary operator
232  // to use, simply write F<op::identity>( src )
234  struct identity{
236  MSHADOW_XINLINE static real_t Map(real_t a) {
237  return a;
238  }
239  };
240  }; // namespace op
241 
243  namespace red {
245  struct sum {
247  MSHADOW_XINLINE static void Reduce( volatile real_t& dst, volatile real_t src ) {
248  dst += src;
249  }
251  MSHADOW_XINLINE static real_t PartialGrad( real_t redres, real_t redsrc ) {
252  return 1.0f;
253  }
255  MSHADOW_CONSTEXPR static real_t kInitV = 0.0f;
256  };
258  struct maximum {
260  MSHADOW_XINLINE static void Reduce( volatile real_t& dst, volatile real_t src ) {
261  using namespace std;
262  dst = max( dst, src );
263  }
265  MSHADOW_XINLINE static real_t PartialGrad( real_t redres, real_t redsrc ) {
266  return redres == redsrc ? 1.0f: 0.0f;
267  }
269 #if MSHADOW_SINGLE_PRECISION
270  MSHADOW_CONSTEXPR static real_t kInitV = -FLT_MAX;
271 #else
272  MSHADOW_CONSTEXPR static real_t kInitV = -DBL_MAX;
273 #endif
274  };
275  };
276 
278  namespace utils{
280  inline void Error( const char *msg ){
281  fprintf( stderr, "Error:%s\n",msg );
282  exit( -1 );
283  }
285  inline void Assert( bool exp ){
286  if( !exp ) Error( "AssertError" );
287  }
289  inline void Assert( bool exp, const char *msg ){
290  if( !exp ) Error( msg );
291  }
293  inline void Warning( const char *msg ){
294  fprintf( stderr, "warning:%s\n",msg );
295  }
296  }; // namespace utils
297 }; // namespace mshadow
298 #endif // TENSOR_BASE_H
static MSHADOW_XINLINE real_t Map(real_t a)
map a to result using defined operation
Definition: tensor_base.h:236
void Warning(const char *msg)
warning
Definition: tensor_base.h:293
unsigned index_t
type that will be used for index
Definition: tensor_base.h:123
save to saver: =
Definition: tensor_base.h:170
static MSHADOW_CONSTEXPR real_t kAlphaBLAS
helper constant to use BLAS, alpha
Definition: tensor_base.h:202
static MSHADOW_CONSTEXPR real_t kBetaBLAS
helper constant to use BLAS, beta
Definition: tensor_base.h:191
static MSHADOW_XINLINE real_t Map(real_t a, real_t b)
map a, b to result using defined operation
Definition: tensor_base.h:147
static MSHADOW_XINLINE void Reduce(volatile real_t &dst, volatile real_t src)
do reduction into dst
Definition: tensor_base.h:247
static MSHADOW_XINLINE real_t Map(real_t a, real_t b)
map a, b to result using defined operation
Definition: tensor_base.h:140
divide operator
Definition: tensor_base.h:152
static MSHADOW_XINLINE void Save(real_t &a, real_t b)
save b to a using save method
Definition: tensor_base.h:185
static MSHADOW_CONSTEXPR real_t kBetaBLAS
helper constant to use BLAS, beta
Definition: tensor_base.h:178
static MSHADOW_CONSTEXPR real_t kInitV
an intial value of reducer
Definition: tensor_base.h:270
op::right OPType
corresponding binary operator type
Definition: tensor_base.h:180
static MSHADOW_XINLINE void Reduce(volatile real_t &dst, volatile real_t src)
do reduction into dst
Definition: tensor_base.h:260
op::minus OPType
corresponding binary operator type
Definition: tensor_base.h:206
static MSHADOW_XINLINE real_t Map(real_t a, real_t b)
map a, b to result using defined operation
Definition: tensor_base.h:133
identity function that maps a real number to it self
Definition: tensor_base.h:234
op::mul OPType
corresponding binary operator type
Definition: tensor_base.h:215
float real_t
type that will be used for content
Definition: tensor_base.h:118
const unsigned kRandBufferSize
buffer size for each random number generator
Definition: tensor_base.h:112
get rhs
Definition: tensor_base.h:159
static MSHADOW_XINLINE void Save(real_t &a, real_t b)
save b to a using save method
Definition: tensor_base.h:220
minus to saver: -=
Definition: tensor_base.h:196
static MSHADOW_CONSTEXPR real_t kAlphaBLAS
helper constant to use BLAS, alpha
Definition: tensor_base.h:189
multiply to saver: *=
Definition: tensor_base.h:209
const float kPi
pi
Definition: tensor_base.h:114
static MSHADOW_XINLINE real_t Map(real_t a, real_t b)
map a, b to result using defined operation
Definition: tensor_base.h:154
op::plus OPType
corresponding binary operator type
Definition: tensor_base.h:193
static MSHADOW_XINLINE void Save(real_t &a, real_t b)
save b to a using save method
Definition: tensor_base.h:198
static MSHADOW_CONSTEXPR real_t kBetaBLAS
helper constant to use BLAS, beta
Definition: tensor_base.h:204
void Error(const char *msg)
send error message then exit
Definition: tensor_base.h:280
static MSHADOW_XINLINE void Save(real_t &a, real_t b)
save b to a using save method
Definition: tensor_base.h:172
divide to saver: /=
Definition: tensor_base.h:218
static MSHADOW_CONSTEXPR real_t kAlphaBLAS
helper constant to use BLAS, alpha
Definition: tensor_base.h:176
maximum reducer
Definition: tensor_base.h:258
static MSHADOW_XINLINE real_t PartialGrad(real_t redres, real_t redsrc)
calculate gradient of redres with respect to redsrc, redres: reduced result, redsrc: one of reduction...
Definition: tensor_base.h:251
plus operator
Definition: tensor_base.h:138
save to saver: +=
Definition: tensor_base.h:183
sum reducer
Definition: tensor_base.h:245
void Assert(bool exp, const char *msg)
assert a expression is true
Definition: tensor_base.h:289
mul operator
Definition: tensor_base.h:131
static MSHADOW_CONSTEXPR real_t kInitV
an intial value of reducer
Definition: tensor_base.h:255
static MSHADOW_XINLINE real_t PartialGrad(real_t redres, real_t redsrc)
calculate gradient of redres with respect to redsrc, redres: reduced result, redsrc: one of reduction...
Definition: tensor_base.h:265
static MSHADOW_XINLINE real_t Map(real_t a, real_t b)
map a, b to result using defined operation
Definition: tensor_base.h:161
minus operator
Definition: tensor_base.h:145
op::div OPType
corresponding binary operator type
Definition: tensor_base.h:224
static MSHADOW_XINLINE void Save(real_t &a, real_t b)
save b to a using save method
Definition: tensor_base.h:211