DSPLIB User Guide
DSPLIB_lud_inv_cn.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
3 **| **** |**
4 **| **** |**
5 **| ******o*** |**
6 **| ********_///_**** |**
7 **| ***** /_//_/ **** |**
8 **| ** ** (__/ **** |**
9 **| ********* |**
10 **| **** |**
11 **| *** |**
12 **| |**
13 **| Copyright (c) 2016 Texas Instruments Incorporated |**
14 **| ALL RIGHTS RESERVED |**
15 **| |**
16 **| Permission to use, copy, modify, or distribute this software, |**
17 **| whether in part or in whole, for any purpose is forbidden without |**
18 **| a signed licensing agreement and NDA from Texas Instruments |**
19 **| Incorporated (TI). |**
20 **| |**
21 **| TI makes no representation or warranties with respect to the |**
22 **| performance of this computer program, and specifically disclaims |**
23 **| any responsibility for any damages, special or consequential, |**
24 **| connected with the use of this program. |**
25 **| |**
26 **+--------------------------------------------------------------------------+**
27 *******************************************************************************/
28 
29 #include "DSPLIB_lud_inv_priv.h"
30 
31 /*********************************************************************
32  *
33  * INITIALIZATION
34  *
35  *********************************************************************/
36 
38  const DSPLIB_bufParams2D_t * bufParamsP,
39  const DSPLIB_bufParams2D_t * bufParamsL,
40  const DSPLIB_bufParams2D_t * bufParamsU,
41  const DSPLIB_bufParams2D_t * bufParamsinvA,
42  const DSPLIB_lud_invInitArgs *pKerInitArgs)
43 {
44  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
45 
46  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", DSPLIB_SUCCESS);
47  return DSPLIB_SUCCESS;
48 }
49 
50 /**********************************************************************/
51 /* NATURAL C IMPLEMENTATION */
52 /**********************************************************************/
53 
54 template <typename dataType>
55 int DSPLIB_lud_inv_cn(const int order,
56  unsigned short *P,
57  dataType * L,
58  dataType * U,
59  dataType * invA,
60  const int32_t strideOrder,
61  const int32_t strideP)
62 {
63  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering nat C c7x implementation");
64 
65  int row, col, k;
66  dataType factor, sum;
67  dataType *inv_L, *inv_U, *inv_U_x_inv_L;
68 
69  int32_t dataSize = sizeof(dataType);
70  int32_t dataSizeP = sizeof(unsigned short);
71 
72  int32_t orderStride = strideOrder / dataSize;
73  int32_t orderPStride = strideP / dataSizeP;
74 
75  /* set inv_A matrix to identity */
76  inv_L = &invA[0];
77  for (row = 0; row < order; row++) {
78  for (col = 0; col < order; col++) {
79  if (row == col){
80  inv_L[col + row * orderStride] = 1.0;
81  }
82  else{
83  inv_L[col + row * orderStride] = 0.0;
84  }
85  }
86  }
87 
88  /* use Gauss Jordan algorithm to invert L whose result is in inv_L */
89  for (col = 0; col < order - 1; col++) {
90  for (row = col + 1; row < order; row++) {
91  dataType mulFact = 1 / L[col + col * orderStride];
92  ;
93  factor = L[col + row * orderStride] * mulFact;
94  // factor = L[col + row * orderStride] / L[col + col * orderStride];
95  for (k = 0; k < order; k++) {
96  inv_L[k + row * orderStride] -= factor * inv_L[k + col * orderStride];
97  L[k + row * orderStride] -= factor * L[k + col * orderStride];
98  }
99  }
100  }
101 
102  /* set inv_U matrix to identity */
103  inv_U = &L[0];
104  for (row = 0; row < order; row++) {
105  for (col = 0; col < order; col++) {
106  if (row == col){
107  inv_U[col + row * orderStride] = 1.0;
108  }
109  else{
110  inv_U[col + row * orderStride] = 0.0;
111  }
112  }
113  }
114 
115  /* use Gauss Jordan algorithm to invert U whose result is in L */
116  for (col = order - 1; col >= 1; col--) {
117  factor = U[col + col * orderStride];
118  dataType mulFact = 1 / factor;
119  for (row = col - 1; row >= 0; row--) {
120  factor = U[col + row * orderStride] * mulFact;
121  for (k = 0; k < order; k++) {
122  inv_U[k + row * orderStride] -= factor * inv_U[k + col * orderStride];
123  U[k + row * orderStride] -= factor * U[k + col * orderStride];
124  }
125  }
126  }
127 
128  /* scale U & L to get identity matrix in U */
129  for (row = order - 1; row >= 0; row--) {
130  factor = U[row + row * orderStride];
131  dataType mulFact = 1 / factor;
132  for (col = 0; col < order; col++) {
133  L[col + row * orderStride] *= mulFact;
134  U[col + row * orderStride] *= mulFact;
135  }
136  }
137 
138  /* compute inv_U_x_inv_L=inv(U)*inv(L) */
139  inv_U_x_inv_L = &L[0];
140  for (row = 0; row < order; row++) {
141  for (col = 0; col < order; col++) {
142  sum = 0;
143  for (k = 0; k < order; k++) {
144  sum += inv_U[k + row * orderStride] * inv_L[col + k * orderStride];
145  }
146  inv_U_x_inv_L[col + row * orderStride] = sum;
147  }
148  }
149  /* compute inv_A=inv(U)*inv(L)*P */
150  for (row = 0; row < order; row++) {
151  for (col = 0; col < order; col++) {
152  sum = 0;
153  for (k = 0; k < order; k++) {
154  sum += inv_U_x_inv_L[k + row * orderStride] * P[col + k * orderPStride];
155  }
156  invA[col + row * orderStride] = sum;
157  }
158  }
159 
160  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Exiting nat C c7x implementation");
161  return 0;
162 }
163 
164 template int DSPLIB_lud_inv_cn<float>(const int order,
165  unsigned short *P,
166  float * L,
167  float * U,
168  float * invA,
169  const int32_t strideOrder,
170  const int32_t strideP);
171 template int DSPLIB_lud_inv_cn<double>(const int order,
172  unsigned short *P,
173  double * L,
174  double * U,
175  double * invA,
176  const int32_t strideOrder,
177  const int32_t strideP);
178 
179 template <typename dataType>
181  void *restrict pP,
182  void *restrict pL,
183  void *restrict pU,
184  void *restrict pinvA,
185  void *restrict pStratch)
186 {
187  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering exec_cn function");
188 
189  DSPLIB_STATUS status = DSPLIB_SUCCESS;
190 
191  DSPLIB_lud_inv_PrivArgs *pKerPrivArgs = (DSPLIB_lud_inv_PrivArgs *) handle;
192 
193  int32_t order = pKerPrivArgs->order;
194  int32_t strideOrder = pKerPrivArgs->strideOrder;
195  int32_t strideP = pKerPrivArgs->strideP;
196 
197  /* Typecast void pointers to respective data type */
198  unsigned short *pPLocal = (unsigned short *) pP;
199  dataType * pLLocal = (dataType *) pL;
200  dataType * pULocal = (dataType *) pU;
201  dataType * pinvALocal = (dataType *) pinvA;
202 
203  DSPLIB_DEBUGPRINTFN(0, "pPLocal: %p pLLocal: %p pULocal: %p pinvALocal: %p order: %d\n", pPLocal, pLLocal, pULocal,
204  pinvALocal, order);
205 
206  DSPLIB_lud_inv_cn<dataType>(order, pPLocal, pLLocal, pULocal, pinvALocal, strideOrder, strideP);
207 
208  DSPLIB_DEBUGPRINTFN(0, "Exiting exec_cn function with return status: %d\n", status);
209 
210  return (status);
211 }
212 
214  void *restrict pP,
215  void *restrict pL,
216  void *restrict pU,
217  void *restrict pinv_A,
218  void *restrict pStratch);
219 
221  void *restrict pP,
222  void *restrict pL,
223  void *restrict pU,
224  void *restrict pinv_A,
225  void *restrict pStratch);
226 
227 /* ======================================================================== */
228 /* End of file: DSPLIB_lud_inv_cn.cpp */
229 /* ======================================================================== */
int DSPLIB_lud_inv_cn(const int order, unsigned short *P, dataType *L, dataType *U, dataType *invA, const int32_t strideOrder, const int32_t strideP)
template int DSPLIB_lud_inv_cn< double >(const int order, unsigned short *P, double *L, double *U, double *invA, const int32_t strideOrder, const int32_t strideP)
template DSPLIB_STATUS DSPLIB_lud_inv_exec_cn< float >(DSPLIB_kernelHandle handle, void *restrict pP, void *restrict pL, void *restrict pU, void *restrict pinv_A, void *restrict pStratch)
DSPLIB_STATUS DSPLIB_lud_inv_exec_cn(DSPLIB_kernelHandle handle, void *restrict pP, void *restrict pL, void *restrict pU, void *restrict pinvA, void *restrict pStratch)
This function is the main execution function for the natural C implementation of the kernel....
DSPLIB_STATUS DSPLIB_lud_inv_init_cn(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsP, const DSPLIB_bufParams2D_t *bufParamsL, const DSPLIB_bufParams2D_t *bufParamsU, const DSPLIB_bufParams2D_t *bufParamsinvA, const DSPLIB_lud_invInitArgs *pKerInitArgs)
This function is the initialization function for the natural C implementation of the kernel....
template int DSPLIB_lud_inv_cn< float >(const int order, unsigned short *P, float *L, float *U, float *invA, const int32_t strideOrder, const int32_t strideP)
template DSPLIB_STATUS DSPLIB_lud_inv_exec_cn< double >(DSPLIB_kernelHandle handle, void *restrict pP, void *restrict pL, void *restrict pU, void *restrict pinv_A, void *restrict pStratch)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_lud_inv.
#define DSPLIB_DEBUGPRINTFN(N, fmt,...)
Definition: DSPLIB_types.h:83
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_SUCCESS
Definition: DSPLIB_types.h:152
A structure for a 2 dimensional buffer descriptor.
Structure containing the parameters to initialize the kernel.
Structure that is reserved for internal use by the kernel.
int32_t strideOrder
Stride between rows of input and output data matrix
int32_t order
Size of input buffer for different batches DSPLIB_lud_inv_init that will be retrieved and used by DSP...
int32_t strideP
Stride between rows of output data matrix P