DSPLIB User Guide
DSPLIB_matMul.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * *
3  * module name :DSPLIB *
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 #include "DSPLIB_bufParams.h"
24 #include "DSPLIB_matMul_priv.h"
25 
27 {
28  int32_t privBufSize = sizeof(DSPLIB_matMul_PrivArgs);
29  return privBufSize;
30 }
31 
34  const DSPLIB_bufParams2D_t *bufParamsIn0,
35  const DSPLIB_bufParams2D_t *bufParamsIn1,
36  const DSPLIB_bufParams2D_t *bufParamsOut,
37  const DSPLIB_matMul_InitArgs *pKerInitArgs)
38 {
40 
41 #if DSPLIB_DEBUGPRINT
42  printf("Enter DSPLIB_matMul_init_checkParams\n");
43 #endif
44  if (handle == NULL) {
45  status = DSPLIB_ERR_NULL_POINTER;
46  }
47 
48  if (status == DSPLIB_SUCCESS) {
49  if (((bufParamsIn0->data_type != DSPLIB_FLOAT32) || (bufParamsIn1->data_type != DSPLIB_FLOAT32)) && ((bufParamsIn0->data_type != DSPLIB_FLOAT64) || (bufParamsIn1->data_type != DSPLIB_FLOAT64))) {
50  status = DSPLIB_ERR_INVALID_TYPE;
51  }
52  /* if ((bufParamsIn0->data_type != DSPLIB_INT16) && (bufParamsIn0->data_type != DSPLIB_INT32) && */
53  /* (bufParamsIn0->data_type != DSPLIB_INT8) && (bufParamsIn0->data_type != DSPLIB_FLOAT32)) { */
54  /* status = DSPLIB_ERR_INVALID_TYPE; */
55  /* } */
56  else if (bufParamsIn0->data_type != bufParamsOut->data_type ||
57  bufParamsIn1->data_type != bufParamsOut->data_type) {
58  status = DSPLIB_ERR_INVALID_TYPE;
59  }
60  else {
61  /* Nothing to do here */
62  }
63  }
64 
65  if (status == DSPLIB_SUCCESS) {
66 
67  if ((bufParamsIn0->dim_x != bufParamsIn1->dim_y) || // A is m x k, B is k x n, C is m x n
68  (bufParamsIn0->dim_y != bufParamsOut->dim_y) || (bufParamsIn1->dim_x != bufParamsOut->dim_x)) {
70  }
71  }
72 
73  return status;
74 }
75 
77  const void *restrict pIn0,
78  const void *restrict pIn1,
79  const void *restrict pOut)
80 {
81  DSPLIB_STATUS status;
82 
83 #if DSPLIB_DEBUGPRINT
84  printf("Enter DSPLIB_matMul_exec_checkParams\n");
85 #endif
86  if ((pIn0 == NULL) || (pIn1 == NULL) || (pOut == NULL)) {
87  status = DSPLIB_ERR_NULL_POINTER;
88  }
89  else {
90  status = DSPLIB_SUCCESS;
91  }
92 
93  return status;
94 }
95 
97  DSPLIB_bufParams2D_t *bufParamsIn0,
98  DSPLIB_bufParams2D_t *bufParamsIn1,
99  DSPLIB_bufParams2D_t *bufParamsOut,
100  const DSPLIB_matMul_InitArgs *pKerInitArgs)
101 {
102  DSPLIB_STATUS status = DSPLIB_SUCCESS;
103  DSPLIB_matMul_PrivArgs *pKerPrivArgs = (DSPLIB_matMul_PrivArgs *) handle;
104 
105  uint32_t M = bufParamsOut->dim_y;
106  uint32_t N = bufParamsOut->dim_x;
107  uint32_t K = bufParamsIn0->dim_x;
108 
109  pKerPrivArgs->M = M;
110  pKerPrivArgs->N = N;
111  pKerPrivArgs->K = K;
112 
113  uint32_t datatype = bufParamsIn0->data_type;
114  uint32_t strideIn0Elements = bufParamsIn0->stride_y / DSPLIB_sizeof(datatype);
115  uint32_t strideIn1Elements = bufParamsIn1->stride_y / DSPLIB_sizeof(datatype);
116  uint32_t strideOutElements = bufParamsOut->stride_y / DSPLIB_sizeof(datatype);
117 
118  pKerPrivArgs->strideIn0Elements = strideIn0Elements;
119  pKerPrivArgs->strideIn1Elements = strideIn1Elements;
120  pKerPrivArgs->strideOutElements = strideOutElements;
121 
122 #if DSPLIB_DEBUGPRINT
123  printf("DSPLIB_DEBUGPRINT Enter DSPLIB_matMul_init\n");
124 #endif
125 
126  if (pKerInitArgs->funcStyle == DSPLIB_FUNCTION_NATC) {
127  if (bufParamsIn0->data_type == DSPLIB_FLOAT32) {
128  pKerPrivArgs->execute = DSPLIB_matMul_exec_cn<float>;
129  }
130  else if (bufParamsIn0->data_type == DSPLIB_FLOAT64) {
131  pKerPrivArgs->execute = DSPLIB_matMul_exec_cn<double>;
132  }
133  else {
134  status = DSPLIB_ERR_INVALID_TYPE;
135 #if DSPLIB_DEBUGPRINT
136  printf("DSPLIB_DEBUGPRINT CP 2 status %d\n", status);
137 #endif
138  }
139  }
140  else {
141  if (bufParamsIn0->data_type == DSPLIB_FLOAT32) {
142 #if DSPLIB_DEBUGPRINT
143  printf("DSPLIB_DEBUGPRINT bufParamsIn->data_type == DSPLIB_FLOAT32\n");
144 #endif
145  pKerPrivArgs->execute = DSPLIB_matMul_exec_ci<float>;
146  status = DSPLIB_matMul_init_ci<float>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut, pKerInitArgs);
147  }
148  else if (bufParamsIn0->data_type == DSPLIB_FLOAT64) {
149 #if DSPLIB_DEBUGPRINT
150  printf("DSPLIB_DEBUGPRINT bufParamsIn->data_type == DSPLIB_FLOAT64\n");
151 #endif
152  pKerPrivArgs->execute = DSPLIB_matMul_exec_ci<double>;
153  status = DSPLIB_matMul_init_ci<double>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut, pKerInitArgs);
154  }
155  else {
156  status = DSPLIB_ERR_INVALID_TYPE;
157  }
158  }
159 #if DSPLIB_DEBUGPRINT
160  printf("DSPLIB_DEBUGPRINT CP 3 status %d\n", status);
161 #endif
162  return status;
163 }
164 
166 DSPLIB_matMul_exec(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
167 {
168  DSPLIB_STATUS status;
169 
170 #if DSPLIB_DEBUGPRINT
171  printf("DSPLIB_DEBUGPRINT Enter DSPLIB_matMul_exec\n");
172 #endif
173 
174  DSPLIB_matMul_PrivArgs *pKerPrivArgs = (DSPLIB_matMul_PrivArgs *) handle;
175 
176  status = pKerPrivArgs->execute(handle, pIn0, pIn1, pOut);
177 
178  return status;
179 }
template DSPLIB_STATUS DSPLIB_matMul_exec_ci< float >(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template DSPLIB_STATUS DSPLIB_matMul_init_ci< float >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn0, const DSPLIB_bufParams2D_t *bufParamsIn1, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_matMul_InitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_matMul_init_ci< double >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn0, const DSPLIB_bufParams2D_t *bufParamsIn1, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_matMul_InitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_matMul_exec_ci< double >(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template DSPLIB_STATUS DSPLIB_matMul_exec_cn< double >(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template DSPLIB_STATUS DSPLIB_matMul_exec_cn< float >(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_matMul.
static int32_t DSPLIB_sizeof(uint32_t type)
Inline function returns number of bytes per element given a type of DSPLIB_data_type_e.
DSPLIB_STATUS_NAME
The enumeration of all status codes.
Definition: DSPLIB_types.h:151
void * DSPLIB_kernelHandle
Handle type for DSPLIB operations.
Definition: DSPLIB_types.h:172
@ DSPLIB_FUNCTION_NATC
Definition: DSPLIB_types.h:176
@ DSPLIB_FLOAT32
@ DSPLIB_FLOAT64
@ DSPLIB_ERR_INVALID_DIMENSION
Definition: DSPLIB_types.h:156
@ DSPLIB_SUCCESS
Definition: DSPLIB_types.h:152
@ DSPLIB_ERR_NULL_POINTER
Definition: DSPLIB_types.h:157
@ DSPLIB_ERR_INVALID_TYPE
Definition: DSPLIB_types.h:155
DSPLIB_STATUS DSPLIB_matMul_exec_checkParams(DSPLIB_kernelHandle handle, const void *restrict pIn0, const void *restrict pIn1, const void *restrict pOut)
This function checks the validity of the parameters passed to DSPLIB_matMul_exec function....
DSPLIB_STATUS DSPLIB_matMul_exec(DSPLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
This function is the main kernel compute function.
DSPLIB_STATUS DSPLIB_matMul_init_checkParams(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn0, const DSPLIB_bufParams2D_t *bufParamsIn1, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_matMul_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to DSPLIB_matMul_init function....
int32_t DSPLIB_matMul_getHandleSize(DSPLIB_matMul_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
DSPLIB_STATUS DSPLIB_matMul_init(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsIn0, DSPLIB_bufParams2D_t *bufParamsIn1, DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_matMul_InitArgs *pKerInitArgs)
This function should be called before the DSPLIB_matMul_exec function is called. This function takes ...
A structure for a 2 dimensional buffer descriptor.
uint32_t data_type
Values are of type DSPLIB_data_type_e.
int32_t stride_y
Stride in Y dimension in bytes.
uint32_t dim_x
Width of buffer in X dimension in elements.
uint32_t dim_y
Height of buffer in Y dimension in elements.
Structure containing the parameters to initialize the kernel.
int8_t funcStyle
Variant of the function refer to DSPLIB_FUNCTION_STYLE
Structure that is reserved for internal use by the kernel.
pFxnDSPLIB_matMul_exec execute
Function pointer to point to the right execution variant between DSPLIB_matMul_exec_cn and DSPLIB_mat...