Coverage Report

Created: 2022-07-27 23:53

/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
#define CCV_TENSOR_GET_MEMORY(type) ((type) & 0x3)
50
#define CCV_TENSOR_GET_DEVICE(type) ((type) & 0xfff00)
51
#define CCV_TENSOR_GET_DEVICE_ID(type) (CCV_TENSOR_GET_DEVICE(type) >> 8)
52
239
#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
68
#define CCV_NNC_MAX_DIM_ALLOC (12)
76
#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
  int contiguous; // If we have other flags later, we can make this a generic flag.
105
  off_t off;
106
  int inc[CCV_NNC_MAX_DIM_ALLOC]; /**< "increment" or, length */
107
} ccv_nnc_tensor_view_t;
108
109
#define CCV_IS_TENSOR_VIEW(x) ((*(int*)(x)) & CCV_TENSOR_VIEW)
110
#define CCV_IS_TENSOR_MULTIVIEW(x) ((*(int*)(x)) & CCV_TENSOR_MULTIVIEW)
111
#define CCV_IS_TENSOR_CONTIGUOUS(x) (!CCV_IS_TENSOR_VIEW(x) || (((ccv_nnc_tensor_view_t*)x)->contiguous == 1))
112
113
#if CCV_NNC_TENSOR_TFB
114
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (((x) & 0xFFF) > 0) // has channel components
115
typedef struct {
116
  int type;
117
  int refcount;
118
  ccv_numeric_data_t data;
119
  uintptr_t reserved0;
120
  uint64_t reserved1;
121
  uint64_t sig;
122
  // This is used for toll-free bridging between ccv_dense_matrix_t and ccv_nnc_tensor_t
123
  // Note that this is bigger than it is needed, we carefully structured this
124
  // such that bit is reused as much, but still some wasted spaces.
125
  union {
126
    struct {
127
      int resides;
128
      int format;
129
      int datatype;
130
      int rows;
131
      int cols;
132
      int channels;
133
      int reserved2; /* This reserved bit need to be zero'ed such that later dim is not cared. */
134
      int step;
135
      union {
136
        unsigned char u8;
137
        int i32;
138
        ccv_float16_t f16;
139
        float f32;
140
        int64_t i64;
141
        double f64;
142
        void* p;
143
      } tb;
144
    };
145
    ccv_nnc_tensor_param_t info;
146
  }; 
147
} ccv_dense_matrix_t;
148
#else
149
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (0)
150
typedef struct {
151
  int type;
152
  int refcount;
153
  uint64_t sig;
154
  int cols;
155
  int rows;
156
  int step;
157
  union {
158
    unsigned char u8;
159
    int i32;
160
    ccv_float16_t f16;
161
    float f32;
162
    int64_t i64;
163
    double f64;
164
    void* p;
165
  } tb;
166
  ccv_numeric_data_t data;
167
} ccv_dense_matrix_t;
168
#endif
169
170
#endif