Caffe2 - C++ API
A deep learning, cross platform ML framework
common.h
1 #ifndef THCUNN_COMMON_H
2 #define THCUNN_COMMON_H
3 
4 // CUDA: grid stride looping
5 #define CUDA_KERNEL_LOOP(i, n) \
6  for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); i += blockDim.x * gridDim.x)
7 
8 #define THCUNN_assertSameGPU(...) THAssertMsg(THCTensor_(checkGPU)(__VA_ARGS__), \
9  "Some of weight/gradient/input tensors are located on different GPUs. Please move them to a single one.")
10 
11 // Use 1024 threads per block, which requires cuda sm_2x or above
12 const int CUDA_NUM_THREADS = 1024;
13 
14 // CUDA: number of blocks for threads.
15 inline int GET_BLOCKS(const int N)
16 {
17  return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS;
18 }
19 
20 #define THCUNN_resizeAs_indices(STATE, I1, I2) \
21  if (!I1->sizes().equals(I2->sizes())) \
22  { \
23  THCudaLongTensor_resizeAs(STATE, I1, I2); \
24  }
25 
26 #define THCUNN_check_shape(STATE, I1, I2) \
27  if (I1 != NULL && I2 != NULL && !THCTensor_(isSameSizeAs)(STATE, I1, I2)) \
28  { \
29  THCDescBuff s1 = THCTensor_(sizeDesc)(STATE, I1); \
30  THCDescBuff s2 = THCTensor_(sizeDesc)(STATE, I2); \
31  THError(#I1 " and " #I2 " shapes do not match: " \
32  #I1 " %s, " #I2 " %s", s1.str, s2.str); \
33  }
34 
35 
36 #define THCUNN_check_shape_indices(STATE, I1, I2) \
37  if (!I1->sizes().equals(I2->sizes())) \
38  { \
39  THCDescBuff s1 = THCIndexTensor_(sizeDesc)(STATE, I1); \
40  THCDescBuff s2 = THCTensor_(sizeDesc)(STATE, I2); \
41  THError(#I1 " and " #I2 " shapes do not match: " \
42  #I1 " %s, " #I2 " %s", s1.str, s2.str); \
43  }
44 
45 #define THCUNN_check_nElement(STATE, I1, I2) \
46  if (I1 != NULL && I2 != NULL ) { \
47  ptrdiff_t n1 = THCTensor_(nElement)(STATE, I1); \
48  ptrdiff_t n2 = THCTensor_(nElement)(STATE, I2); \
49  if (n1 != n2) \
50  { \
51  THCDescBuff s1 = THCTensor_(sizeDesc)(state, I1); \
52  THCDescBuff s2 = THCTensor_(sizeDesc)(state, I2); \
53  THError(#I1 " and " #I2 " have different number of elements: " \
54  #I1 "%s has %ld elements, while " \
55  #I2 "%s has %ld elements", s1.str, n1, s2.str, n2); \
56  } \
57  }
58 
59 #define THCUNN_check_dim_size(STATE, T, DIM, DIM_SIZE, SIZE) \
60  if (THCTensor_(nDimensionLegacyNoScalars)(STATE, T) != DIM || \
61  THCTensor_(sizeLegacyNoScalars)(STATE, T, DIM_SIZE) != SIZE) { \
62  THCDescBuff s1 = THCTensor_(sizeDesc)(state, T); \
63  THError("Need " #T " of dimension %d and " #T ".size[%d] == %d" \
64  " but got " #T " to be of shape: %s", DIM, DIM_SIZE, SIZE, s1.str); \
65  }
66 
67 #define THCUNN_check_dim_size_indices(STATE, T, DIM, DIM_SIZE, SIZE) \
68  if (THCIndexTensor_(nDimensionLegacyNoScalars)(STATE, T) != DIM || \
69  THCIndexTensor_(sizeLegacyNoScalars)(STATE, T, DIM_SIZE) != SIZE) { \
70  THCDescBuff s1 = THCIndexTensor_(sizeDesc)(state, T); \
71  THError("Need " #T " of dimension %d and " #T ".size[%d] == %d" \
72  " but got " #T " to be of shape: %s", DIM, DIM_SIZE, SIZE, s1.str); \
73  }
74 
75 #define THCUNN_argCheck(STATE, COND, ARG, T, FORMAT) \
76  if (!(COND)) { \
77  THCDescBuff s1 = THCTensor_(sizeDesc)(state, T); \
78  THArgCheck(COND, ARG, FORMAT, s1.str); \
79  }
80 
81 #endif