Coverage Report

Created: 2021-09-30 21:42

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/ccv_nnc_tfb.h
Line
Count
Source
1
/**********************************************************
2
 * C-based/Cached/Core Computer Vision Library
3
 * Liu Liu, 2010-02-01
4
 **********************************************************/
5
6
/**********************************************************
7
 * CCV - Neural Network Collection
8
 **********************************************************/
9
10
/**
11
 * This header is included into main ccv.h
12
 * such that enables toll-free bridging between
13
 * ccv_nnc_tensor_t and ccv_dense_matrix_t
14
 * In effect, ccv_dense_matrix_t will be a specialized
15
 * version of ccv_nnc_tensor_t. We are taking some penalties
16
 * from this change though, namely, the size of ccv_dense_matrix_t
17
 * will be bigger now.
18
 */
19
20
#ifndef GUARD_ccv_nnc_tfb_h
21
#define GUARD_ccv_nnc_tfb_h
22
23
#include <unistd.h>
24
#include <stdint.h>
25
26
enum {
27
  CCV_TENSOR_FORMAT_NCHW = 0x01,
28
  CCV_TENSOR_FORMAT_NHWC = 0x02,
29
  CCV_TENSOR_FORMAT_CHWN = 0x04,
30
};
31
32
enum {
33
  CCV_TENSOR_CPU_MEMORY = 0x1,
34
  CCV_TENSOR_GPU_MEMORY = 0x2,
35
};
36
37
enum {
38
  CCV_COMPUTE_DEVICE_000 = 0x00000,
39
  CCV_COMPUTE_DEVICE_001 = 0x00100,
40
  CCV_COMPUTE_DEVICE_002 = 0x00200,
41
  CCV_COMPUTE_DEVICE_003 = 0x00300,
42
  CCV_COMPUTE_DEVICE_004 = 0x00400,
43
  CCV_COMPUTE_DEVICE_005 = 0x00500,
44
  CCV_COMPUTE_DEVICE_006 = 0x00600,
45
  CCV_COMPUTE_DEVICE_007 = 0x00700,
46
  CCV_COMPUTE_DEVICE_ANY = 0xfff00, // The optimal allocation will be found by the algorithm.
47
};
48
49
1.56M
#define CCV_TENSOR_GET_MEMORY(type) ((type) & 0x3)
50
465k
#define CCV_TENSOR_GET_DEVICE(type) ((
type154
) & 0xfff00)
51
109k
#define CCV_TENSOR_GET_DEVICE_ID(type) (CCV_TENSOR_GET_DEVICE(type) >> 8)
52
35.4k
#define CCV_TENSOR_SET_DEVICE_ID(type, device_id) (type) = (((type) & ~0xfff00) | (((device_id) & 0xfff) << 8))
53
54
enum {
55
  CCV_TENSOR_VIEW       = 0x01000000,
56
  CCV_TENSOR_MULTIVIEW  = 0x02000000,
57
  CCV_TENSOR_PINNED_MEM = 0x04000000, // tensor is pinned in CUDA. This matches CCV_PINNED_MEM.
58
};
59
60
typedef struct {
61
  short v;
62
} ccv_float16_t; // In this way, you cannot perform any operations on float16.
63
64
typedef union ccv_numeric_data_u {
65
  unsigned char* u8;
66
  int* i32;
67
  ccv_float16_t* f16;
68
  float* f32;
69
  int64_t* i64;
70
  uint64_t* u64;
71
  double* f64;
72
  void* ptr; // Raw pointer
73
} ccv_numeric_data_t;
74
75
11.0M
#define CCV_NNC_MAX_DIM_ALLOC (12)
76
5.38G
#define CCV_NNC_MAX_DIM (2)
77
78
typedef struct {
79
  int type;
80
  int format;
81
  int datatype;
82
  int dim[CCV_NNC_MAX_DIM_ALLOC];
83
} ccv_nnc_tensor_param_t;
84
85
typedef struct {
86
  int type;
87
  int refcount;
88
  ccv_numeric_data_t data;
89
  uintptr_t alias_ref;
90
  uint64_t data_size;
91
  uint64_t sig;
92
  ccv_nnc_tensor_param_t info;
93
} ccv_nnc_tensor_t;
94
95
typedef struct {
96
  int type;
97
  int refcount;
98
  ccv_numeric_data_t data;
99
  uintptr_t alias_ref;
100
  uint64_t data_size;
101
  uint64_t sig;
102
  ccv_nnc_tensor_param_t info;
103
  /* tensor view and tensor shares the same data structure besides the following. */
104
  off_t off;
105
  int inc[CCV_NNC_MAX_DIM_ALLOC]; /**< "increment" or, length */
106
} ccv_nnc_tensor_view_t;
107
108
1.57M
#define CCV_IS_TENSOR_VIEW(x) ((*(int*)(x)) & CCV_TENSOR_VIEW)
109
593k
#define CCV_IS_TENSOR_MULTIVIEW(x) ((*(int*)(
x464
)) & CCV_TENSOR_MULTIVIEW)
110
111
#if CCV_NNC_TENSOR_TFB
112
477
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (((x) & 0xFFF) > 0) // has channel components
113
typedef struct {
114
  int type;
115
  int refcount;
116
  ccv_numeric_data_t data;
117
  uintptr_t reserved0;
118
  uint64_t reserved1;
119
  uint64_t sig;
120
  // This is used for toll-free bridging between ccv_dense_matrix_t and ccv_nnc_tensor_t
121
  // Note that this is bigger than it is needed, we carefully structured this
122
  // such that bit is reused as much, but still some wasted spaces.
123
  union {
124
    struct {
125
      int resides;
126
      int format;
127
      int datatype;
128
      int rows;
129
      int cols;
130
      int channels;
131
      int reserved2; /* This reserved bit need to be zero'ed such that later dim is not cared. */
132
      int step;
133
      union {
134
        unsigned char u8;
135
        int i32;
136
        ccv_float16_t f16;
137
        float f32;
138
        int64_t i64;
139
        double f64;
140
        void* p;
141
      } tb;
142
    };
143
    ccv_nnc_tensor_param_t info;
144
  }; 
145
} ccv_dense_matrix_t;
146
#else
147
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (0)
148
typedef struct {
149
  int type;
150
  int refcount;
151
  uint64_t sig;
152
  int cols;
153
  int rows;
154
  int step;
155
  union {
156
    unsigned char u8;
157
    int i32;
158
    ccv_float16_t f16;
159
    float f32;
160
    int64_t i64;
161
    double f64;
162
    void* p;
163
  } tb;
164
  ccv_numeric_data_t data;
165
} ccv_dense_matrix_t;
166
#endif
167
168
#endif