MMALIB User Guide
MMALIB_types.h
Go to the documentation of this file.
1 /******************************************************************************
2  * *
3  * module name :MMALIB *
4  * *
5  * module descripton :Matrix Multiply Accelerator Library module for C7x+MMA *
6  * *
7  * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/ *
8  * ALL RIGHTS RESERVED *
9  * *
10  ******************************************************************************/
11 
23 #ifndef COMMON_MMALIB_TYPES_H_
24 #define COMMON_MMALIB_TYPES_H_ 1
25 
26 /* This header is needed to be included in OpenCL programs which link
27  * against MMALIB, but OpenCL doesn't need the following headers */
28 #ifndef __OPENCL_VERSION__
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <inttypes.h>
32 #include <stddef.h> // for NULL
33 #include <stdio.h> // for printf
34 #endif
35 #include <c7x.h> // for streaming engine, streaming address gen.
36 
37 
38 #include "MMALIB_bufParams.h"
39 #include "TI_platforms.h"
40 
41 #if defined (__C7100__) || defined (__device__)
42 #include "c71/MMALIB_defines.h"
43 #else
44 #error invalid target
45 #endif
46 
47 #if defined (__C7100__)
48 #include "c7100/MMALIB_defines.h"
49 #include "c7100/MMALIB_types.h"
50 #endif
51 
52 
56 #if !defined(MMALIB_DEBUGPRINT)
57 #define MMALIB_DEBUGPRINT 0
58 #endif
59 #if MMALIB_DEBUGPRINT == 1
60 #include "c71/MMALIB_debug.h"
61 #endif
62 
63 #if (defined(_HOST_BUILD) && (MMALIB_COMPILER_VERSION < 1003999))
64 #define MMALIB_const
65 #else
66 #define MMALIB_const const
67 #endif
68 
69 /* ---------------------------------------------------------------- */
70 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The following two */
71 /* function-like macros do not have equivalent function */
72 /* implementations. */
73 /* ---------------------------------------------------------------- */
74 #define MMALIB_DEBUGPRINTFN(N, fmt, ...) \
75  do { \
76  if (MMALIB_DEBUGPRINT >= (N)) { \
77  fprintf(stdout, "MMALIB debug %s - %d: " fmt, __FUNCTION__, __LINE__, \
78  __VA_ARGS__); \
79  } \
80  } while (0)
81 
82 #define MMALIB_PRINTF(fmt,...) \
83  do { \
84  fprintf(stdout, fmt, __VA_ARGS__); \
85  } while (0)
86 
87 #if defined(MMALIB_MESSAGES)
88 // if enabled, display messages to the library user
89 // MMALIB_MESSAGES should only be defined when TARGET_BUILD=debug or CHECKPARAMS=1
90 #define MMALIB_MESG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
91 #else
92 #define MMALIB_MESG(fmt, ...)
93 #endif //#if defined(MMALIB_MESSAGES)
94 
95 /* Original implementation that had the benefit of always being compiled and thus
96  receiving error checking. However, the fprintf generates a MISRA-C violation.
97 #define MMALIB_DEBUGPRINTFN(N, fmt, ...) \
98  do { \
99  if (MMALIB_DEBUGPRINT >= (N)) { \
100  fprintf(stderr, "MMALIB debug %s - %d: " fmt, __FUNCTION__, __LINE__, \
101  __VA_ARGS__); \
102  } \
103  } while (0)
104 
105 #define MMALIB_PRINTF(fmt, ...) \
106  do { \
107  fprintf(stderr, fmt, __VA_ARGS__); \
108  } while (0)
109 #endif
110  */
111 
112 
113 #define MMALIB_SOFT_MMA_RESET 0
114 
115 // switch to enable or disable static inline for MMALIB functions defined in .c files (so not many functions)
116 #define MMALIB_STATIC_INLINE static inline
117 
118 
119 #ifdef __cplusplus
120 extern "C" {
121 #endif /* __cplusplus */
122 
130 /* @{ */
131 
132 /*******************************************************************************
133  *
134  * TYPEDEFS
135  *
136  ******************************************************************************/
137 
138 typedef double MMALIB_D64;
139 typedef float MMALIB_F32;
140 
142 typedef enum {
157  // MMALIB_CNN_convolve_row
169  // MMALIB_CNN_convolve_col_smallNo
183  // MMALIB_CNN_deconvolve_row
193  // MMALIB_CNN_fullyConnected
198  // MMALIB_CNN_pixelShuffle_row
211  // MMALIB_LINALG_matrixMatrixMultiply
215  // MMALIB_LINALG_matrixMatrixMultiplyAccumulate
219  // MMALIB_LINALG_matrixTranspose
223  // MMALIB_LINALG_pointwiseMatrixMatrixMultiply
228 
230 
231 typedef void *MMALIB_kernelHandle;
232 
234 typedef enum {
240 
243 typedef enum {
248 } MMALIB_buffer_mode_e; // formerly eMMABufferMode;
249 
250 /*******************************************************************************
251  *
252  * DEFINES
253  *
254  ******************************************************************************/
255 
256 #define MMALIB_PARAM_SIZE 128
257 #define MMALIB_BYTE_WIDTH 64
258 
259 /*******************************************************************************
260  *
261  * Macros
262  *
263  ******************************************************************************/
264 
265 #define MMALIB_ALIGN_SHIFT_64BYTES \
266  6
267 #define MMALIB_ALIGN_SHIFT_128BYTES \
268  7
269 #define MMALIB_ALIGN_SHIFT_256BYTES \
270  8
271 
272 #define MMALIB_ALIGN_64BYTES \
273  (1 << MMALIB_ALIGN_SHIFT_64BYTES)
274 #define MMALIB_ALIGN_128BYTES \
275  (1 << MMALIB_ALIGN_SHIFT_128BYTES)
276 #define MMALIB_ALIGN_256BYTES \
277  (1 << MMALIB_ALIGN_SHIFT_256BYTES)
278 
279 #define MMALIB_L2DATA_ALIGN_SHIFT \
280  MMALIB_ALIGN_SHIFT_64BYTES
281 
282 
284 #define MMALIB_L2DATA_ALIGNMENT (((uint32_t)1) << ((uint32_t)MMALIB_L2DATA_ALIGN_SHIFT))
285 
286 /* ---------------------------------------------------------------- */
287 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
288 /* being addressed */
289 /* ---------------------------------------------------------------- */
292 #define MMALIB_CALC_STRIDE(BYTES, ALIGN_SHIFT) \
293  (((((BYTES)-1) >> (ALIGN_SHIFT)) + 1) << (ALIGN_SHIFT))
294 
296 #define MMALIB_MMA_SIZE_8_BIT_SHIFT \
297  6
298 #define MMALIB_MMA_SIZE_16_BIT_SHIFT \
299  5
300 #define MMALIB_MMA_SIZE_32_BIT_SHIFT \
301  4
302 
303 
304 //#define MMALIB_MMA_SIZE_1_BYTE 1
305 #define MMALIB_MMA_SIZE_8_BIT \
306  ((uint32_t)(((uint32_t)1) \
307  << MMALIB_MMA_SIZE_8_BIT_SHIFT))
308 #define MMALIB_MMA_SIZE_16_BIT \
310  ((uint32_t)(((uint32_t)1) \
311  << MMALIB_MMA_SIZE_16_BIT_SHIFT))
312 #define MMALIB_MMA_SIZE_32_BIT \
314  ((int32_t)(((uint32_t)1) \
315  << MMALIB_MMA_SIZE_32_BIT_SHIFT))
316 
318 
320 /******************************************************************************
321  *
322  * Do not document these in the User Guide
323  ******************************************************************************/
324 
328 /* @{ */
329 
330 
331 /* ---------------------------------------------------------------- */
332 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
333 /* being addressed */
334 /* ---------------------------------------------------------------- */
335 // remove asm comments for Loki testing as they may add cycles
336  #if defined(_HOST_BUILD)
337  #define MMALIB_asm(string) ;
338  #else
339  #define MMALIB_asm(string); asm(string);
340  #endif // _HOST_BUILD
341 
342 /* ---------------------------------------------------------------- */
343 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The advisory is not */
344 /* being addressed so as not to lose portability across different */
345 /* platforms. */
346 /* ---------------------------------------------------------------- */
347 // cl7x unroll attributes not recognized by gcc/gpp compiler and generate
348 // warnings. Remove them with macro
349 #if defined(_HOST_BUILD)
350 #define str(x) #x
351 #define xstr(x) str(x)
352 #ifdef WIN32
353 #define __attribute__()
354 #define MMALIB_UNROLL(count)
355 #else
356 #define MMALIB_UNROLL(count) _Pragma(str(UNROLL(count)))
357 #endif
358 #else
359 #define MMALIB_UNROLL(COUNT) [[TI::unroll(COUNT)]]
360 #endif
361 
362 #if defined(_HOST_BUILD)
363 #ifdef WIN32
364 #define __attribute__()
365 #define MMALIB_MUST_ITERATE(initial, max, multiple)
366 #else
367 #define MMALIB_MUST_ITERATE(initial, max, multiple) _Pragma(str(MUST_ITERATE(initial, max, multiple)))
368 #endif
369 #else
370 #define MMALIB_MUST_ITERATE(INTIAL, MAX, MULTIPLE) [[TI::must_iterate(INTIAL, MAX, MULTIPLE)]]
371 #endif
372 
373 #ifndef MMALIB_NUMBER_RANDOM_DIM_TESTS
374 #define MMALIB_NUMBER_RANDOM_DIM_TESTS 25
375 #endif
376 
377 #define MMALIB_PERFORMANCE_TEST_PATTERN RANDOM_SIGNED
378 
379 #define MMALIB_TEST_OUTPUT_HEAP 0
380 #if defined(_HOST_BUILD)
381  // Valgrind works better when output is in the heap (it can't track statically allocated memory), so
382  // in host emulation mode, place test outputs in the heap rather than statically allocated MSMC
383  #define MMALIB_TEST_OUTPUT_MSMC MMALIB_TEST_OUTPUT_HEAP
384 #else
385  #define MMALIB_TEST_OUTPUT_MSMC 1
386 #endif
387 
388 #define STRIDE_OPT_4CYCLE 1
389 
390 /*******************************************************************************
391  *
392  * MACROS
393  *
394  ******************************************************************************/
395 
396 /* ---------------------------------------------------------------- */
397 /* MISRAC Rule 4.9(DEFINE.FUNC) Deviation: The following */
398 /* function-like macros are intended to be used across different */
399 /* data types. */
400 /* ---------------------------------------------------------------- */
401 #define MMALIB_min(x, y) \
402  (((x) < (y)) ? (x) : (y))
403 #define MMALIB_max(x, y) \
404  (((x) < (y)) ? (y) : (x))
405 #define MMALIB_ceilingDiv(x, y) \
406  (((x) + (y)-1) / \
407  (y))
410 /******************************************************************************
411  *
412  * COMMON MMA CONIFGURATIONS
413  *
414  ******************************************************************************/
415 
416  extern const uchar64 MMALIB_zeroVect_uchar64;
417  extern const ushort32 MMALIB_zeroVect_ushort32;
418  extern const uchar64 MMALIB_vperm_data_0_63;
419 
420  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i32s_i32s_o32s;
421 
422  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i16s_i16s_o16s;
423  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i16s_i16s_o16u;
424  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i16u_i16s_o16s;
425  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i16u_i16s_o16u;
426 
427  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i8s_i8s_o8s;
428  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i8s_i8s_o8u;
429  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i8u_i8s_o8s;
430  extern const MMALIB_MMA_CONFIG_REG configRegisterStruct_i8u_i8s_o8u;
431 
432  extern const __HWA_OFFSET_REG offsetRegStruct_zeros;
433  extern const __HWA_OFFSET_REG offsetRegStruct_diagonal_32bit;
434  extern const __HWA_OFFSET_REG offsetRegStruct_diagonal_16bit;
435  extern const __HWA_OFFSET_REG offsetRegStruct_diagonal_8bit;
436 
437 
441 #ifdef __cplusplus
442 }
443 #endif /* __cplusplus */
444 #endif /* COMMON_MMALIB_TYPES_H_ */
445 
446 /* ======================================================================== */
447 /* End of file: MMALIB_types.h */
448 /* ======================================================================== */
MMALIB_STATUS_NAME MMALIB_STATUS
Return value for MMALIB functions.
Definition: MMALIB_types.h:229
float MMALIB_F32
Single precision floating point.
Definition: MMALIB_types.h:139
MMALIB_STATUS_NAME
The enumeration of all status codes.
Definition: MMALIB_types.h:142
File to hold common structure, enums, macros and functions for MMALIB.
#define MMALIB_MMA_CONFIG_REG
MMALIB_buffer_mode_e
Buffer structure for input feature maps and coefficients of MMALIB_CNN_convolve_row_ixX_ixX_oxX.
Definition: MMALIB_types.h:243
Circular buffer for coefficients.
Definition: MMALIB_types.h:247
double MMALIB_D64
Double precision floating point.
Definition: MMALIB_types.h:138
MMALIB_FUNCTION_STYLE
Enumeration for the style of function implementation.
Definition: MMALIB_types.h:234
File to hold buffer parameter related info for MMALIB.
Linear buffer.
Definition: MMALIB_types.h:244
void * MMALIB_kernelHandle
Handle type for MMALIB operations.
Definition: MMALIB_types.h:231