DSPLIB User Guide
DSPLIB_qrd_inverse_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 withInvA |**
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  *
30  * INCLUDE
31  *
32  ******************************************************************************/
33 
35 #include <cstring>
36 
37 /*******************************************************************************
38  *
39  * INITIALIZATION
40  *
41  ******************************************************************************/
42 
43 template <typename dataType>
45  DSPLIB_bufParams2D_t *bufParamsQ,
46  DSPLIB_bufParams2D_t *bufParamsR,
47  DSPLIB_bufParams2D_t *bufParamsInvA,
48  DSPLIB_bufParams2D_t *bufParamsInvAFinal,
49  const DSPLIB_qrdInvInitArgs *pKerInitArgs)
50 {
51  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
53  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", status);
54  return status;
55 }
57  DSPLIB_bufParams2D_t *bufParamsQ,
58  DSPLIB_bufParams2D_t *bufParamsR,
59  DSPLIB_bufParams2D_t *bufParamsInvA,
60  DSPLIB_bufParams2D_t *bufParamsInvAFinal,
61  const DSPLIB_qrdInvInitArgs *pKerInitArgs);
63  DSPLIB_bufParams2D_t *bufParamsQ,
64  DSPLIB_bufParams2D_t *bufParamsR,
65  DSPLIB_bufParams2D_t *bufParamsInvA,
66  DSPLIB_bufParams2D_t *bufParamsInvAFinal,
67  const DSPLIB_qrdInvInitArgs *pKerInitArgs);
68 
69 /*******************************************************************************
70  *
71  * IMPLEMENTATION
72  *
73  ******************************************************************************/
74 template <typename dataType>
76  void *restrict pQ,
77  void *restrict pR,
78  void *restrict pInvA,
79  void *restrict pLocalInvAScracth,
80  void *restrict pScratch)
81 {
82  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
83 
85 
87  int32_t strideQ = pKerPrivArgs->strideQ;
88  int32_t strideR = pKerPrivArgs->strideR;
89  int32_t heightR = pKerPrivArgs->heightR;
90  int32_t widthR = pKerPrivArgs->widthR;
91  int32_t strideInvA = pKerPrivArgs->strideInvA;
92  int32_t dataSize = sizeof(dataType);
93 
94  /* Typecast void pointers to respective data type */
95  dataType *pLocalQ = (dataType *) pQ;
96  dataType *pLocalR = (dataType *) pR;
97  dataType *pLocalInvA = (dataType *) pInvA;
98 
99  int32_t colRstride = strideR / dataSize;
100  int32_t colQstride = strideQ / dataSize;
101  int32_t colInvAStride = strideInvA / dataSize;
102 
103  int32_t row;
104  int32_t col;
105  int32_t k;
106 
107  dataType sum;
108  dataType factor;
109 
110  DSPLIB_DEBUGPRINTFN(0, "pLocalQ: %p pLocalR: %p pLocalInvA: %p widthR: %d heightR: %d\n", pLocalQ, pLocalR,
111  pLocalInvA, widthR, heightR);
112 
113  /* ------------------------------------------------------------------- */
114  /* Write each column of 'pLocal' to a row of 'pLocalInvA'. */
115  /* ------------------------------------------------------------------- */
116 
117  /* set pLocalInvA matrix to identity */
118  for (row = 0; row < heightR; row++) {
119  for (col = 0; col < widthR; col++) {
120  if (row == col){
121  pLocalInvA[col + row * colInvAStride] = 1.0;
122  }
123  else {
124  pLocalInvA[col + row * colInvAStride] = 0.0;
125  }
126  }
127  }
128 
129  /* use Gauss Jordan algorithm to invert R whose result will be in pLocalInvA */
130  for (col = widthR - 1; col >= 1; col--) {
131  for (row = col - 1; row >= 0; row--) {
132  factor = pLocalR[col + row * colRstride] / pLocalR[col + col * colRstride];
133  for (k = 0; k < widthR; k++) {
134  pLocalInvA[k + row * colInvAStride] -= factor * pLocalInvA[k + col * colInvAStride];
135  pLocalR[k + row * colRstride] -= factor * pLocalR[k + col * colRstride];
136  }
137  }
138  }
139 
140  /* scale R and pLocalInvA to get identity matrix in R */
141  for (row = heightR - 1; row >= 0; row--) {
142  factor = pLocalR[row + row * colRstride];
143  for (col = 0; col < widthR; col++) {
144  pLocalInvA[col + row * colInvAStride] /= factor;
145  pLocalR[col + row * colRstride] /= factor;
146  }
147  }
148 
149  /* pLocalInvA = inv_R * Q' */
150  for (row = 0; row < heightR; row++) {
151  for (col = 0; col < widthR; col++) {
152  sum = 0.0;
153  for (k = 0; k < widthR; k++) {
154  sum += pLocalInvA[k + row * colInvAStride] * pLocalQ[k + col * colQstride];
155  }
156  pLocalR[col] = sum;
157  }
158  for (col = 0; col < widthR; col++) {
159  pLocalInvA[col + row * colInvAStride] = pLocalR[col];
160  }
161  }
162 
163  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", status);
164 
165  return (status);
166 }
167 
168 // explicit instantiation for the different data type versions
170  void *restrict pQ,
171  void *restrict pR,
172  void *restrict pInvA,
173  void *restrict pLocalInvAScracth,
174  void *restrict pScratch);
175 
177  void *restrict pQ,
178  void *restrict pR,
179  void *restrict pInvA,
180  void *restrict pLocalInvAScracth,
181  void *restrict pScratch);
182 /* ======================================================================== */
183 /* End of file: DSPLIB_qrd_inverse_cn.cpp */
184 /* ======================================================================== */
template DSPLIB_STATUS DSPLIB_qrd_inverse_exec_cn< float >(DSPLIB_kernelHandle handle, void *restrict pQ, void *restrict pR, void *restrict pInvA, void *restrict pLocalInvAScracth, void *restrict pScratch)
DSPLIB_STATUS DSPLIB_qrd_inverse_init_cn(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams2D_t *bufParamsInvA, DSPLIB_bufParams2D_t *bufParamsInvAFinal, const DSPLIB_qrdInvInitArgs *pKerInitArgs)
This function is the initialization function for the natural C implementation of the kernel....
template DSPLIB_STATUS DSPLIB_qrd_inverse_exec_cn< double >(DSPLIB_kernelHandle handle, void *restrict pQ, void *restrict pR, void *restrict pInvA, void *restrict pLocalInvAScracth, void *restrict pScratch)
DSPLIB_STATUS DSPLIB_qrd_inverse_exec_cn(DSPLIB_kernelHandle handle, void *restrict pQ, void *restrict pR, void *restrict pInvA, void *restrict pLocalInvAScracth, void *restrict pScratch)
This function is the main execution function for the natural C implementation of the kernel....
template DSPLIB_STATUS DSPLIB_qrd_inverse_init_cn< float >(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams2D_t *bufParamsInvA, DSPLIB_bufParams2D_t *bufParamsInvAFinal, const DSPLIB_qrdInvInitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_qrd_inverse_init_cn< double >(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams2D_t *bufParamsInvA, DSPLIB_bufParams2D_t *bufParamsInvAFinal, const DSPLIB_qrdInvInitArgs *pKerInitArgs)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_qrd_inverse.
#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.
uint32_t heightR
Height of input data matrix
int32_t strideR
Stride between rows of R output data matrix
uint32_t widthR
Size of input buffer for different batches DSPLIB_qrd_inverse_init that will be retrieved and used by...
int32_t strideInvA
Stride between rows of input data matrix
int32_t strideQ
Stride between rows of Q output data matrix