DSPLIB User Guide
DSPLIB_qrd_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  *
30  * INCLUDES
31  *
32  ******************************************************************************/
33 
34 #include "DSPLIB_qrd_priv.h"
35 #include <cstring>
36 
37 /*******************************************************************************
38  *
39  * INITIALIZATION
40  *
41  ******************************************************************************/
42 
43 template <typename dataType>
45  DSPLIB_bufParams2D_t *bufParamsA,
46  DSPLIB_bufParams2D_t *bufParamsQ,
47  DSPLIB_bufParams2D_t *bufParamsR,
48  DSPLIB_bufParams1D_t *bufParamsU,
49  const DSPLIB_qrdInitArgs *pKerInitArgs)
50 {
51 
53  return status;
54 }
56  DSPLIB_bufParams2D_t *bufParamsA,
57  DSPLIB_bufParams2D_t *bufParamsQ,
58  DSPLIB_bufParams2D_t *bufParamsR,
59  DSPLIB_bufParams1D_t *bufParamsU,
60  const DSPLIB_qrdInitArgs *pKerInitArgs);
62  DSPLIB_bufParams2D_t *bufParamsA,
63  DSPLIB_bufParams2D_t *bufParamsQ,
64  DSPLIB_bufParams2D_t *bufParamsR,
65  DSPLIB_bufParams1D_t *bufParamsU,
66  const DSPLIB_qrdInitArgs *pKerInitArgs);
67 
68 /*******************************************************************************
69  *
70  * IMPLEMENTATION
71  *
72  ******************************************************************************/
73 
74 template <typename dataType>
76  const void *restrict pA,
77  const void *restrict pQ,
78  const void *restrict pR,
79  const void *restrict pU,
80  const void *restrict pScratch)
81 {
82  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
83 
85 
86  DSPLIB_qrd_PrivArgs *pKerPrivArgs = (DSPLIB_qrd_PrivArgs *) handle;
87  int32_t nRows = pKerPrivArgs->heightA;
88  int32_t nCols = pKerPrivArgs->widthA;
89  int32_t strideA = pKerPrivArgs->strideA;
90  int32_t strideQ = pKerPrivArgs->strideQ;
91  int32_t strideR = pKerPrivArgs->strideR;
92  int32_t dataSize = sizeof(dataType);
93 
94  int32_t colStrideQ = strideQ / dataSize;
95  int32_t colStrideR = strideR / dataSize;
96  int32_t colStrideA = strideA / dataSize;
97 
98  dataType alpha;
99  dataType scale;
100  dataType sum;
101  dataType norm_sqr;
102 
103  int32_t row;
104  int32_t col;
105  int32_t i;
106  int32_t k;
107  int32_t loopCount;
108 
109  /* Typecast void pointers to respective data type */
110  dataType *pLocalA = (dataType *) pA;
111  dataType *pLocalQ = (dataType *) pQ;
112  dataType *pLocalR = (dataType *) pR;
113  dataType *pLocalU = (dataType *) pU;
114 
115  DSPLIB_DEBUGPRINTFN(0, "pALocal: %p pLocalQ: %p pLocalR: %p pLocalU: %p nCols: %d nRows: %d\n", pLocalA, pLocalQ,
116  pLocalR, pLocalU, nCols, nRows);
117 
118  /* ------------------------------------------------------------------- */
119  /* Write each column of 'pALocal' to a row of 'pLocal'. */
120  /* ------------------------------------------------------------------- */
121 
122  memcpy(pLocalR, pLocalA, sizeof(dataType) * nRows * colStrideA);
123  /* ------------------------------------------------------------------- */
124  /* generate identify matrix and copy A to R */
125  /* ------------------------------------------------------------------- */
126  memset(pLocalQ, 0.0, sizeof(dataType) * nRows * colStrideQ);
127  for (row = 0; row < nRows; row++) {
128  pLocalQ[row + row * colStrideQ] = 1.0;
129  }
130 
131  if (nRows <= nCols) {
132  loopCount = nRows - 2;
133  }
134  else {
135  loopCount = nCols - 1;
136  }
137 
138  for (col = 0; col <= loopCount; col++) {
139  sum = 0;
140  for (row = col; row < nRows; row++) {
141  sum += pLocalR[col + (row * colStrideR)] * pLocalR[col + (row * colStrideR)];
142  }
143  if (sum != 0) {
144  alpha = sqrt(sum);
145  if (pLocalR[col + (col * colStrideR)] >= 0) {
146  alpha = -alpha;
147  }
148  pLocalU[col] = pLocalR[col + (col * colStrideR)] + alpha;
149  pLocalR[col + (col * colStrideR)] = -alpha;
150  norm_sqr = pLocalU[col] * pLocalU[col];
151  for (row = col + 1; row < nRows; row++) {
152  pLocalU[row] = pLocalR[col + (row * colStrideR)];
153  pLocalR[col + (row * colStrideR)] = 0;
154  norm_sqr += pLocalU[row] * pLocalU[row];
155  }
156  if (alpha * pLocalU[col] != 0.00) {
157  scale = 1 / (alpha * pLocalU[col]);
158  /* R=Q1*R */
159  for (i = col + 1; i < nCols; i++) {
160  sum = 0;
161  for (k = col; k < nRows; k++) {
162  sum += pLocalU[k] * pLocalR[i + (k * colStrideR)];
163  }
164  sum *= scale;
165  for (k = col; k < nRows; k++) {
166  pLocalR[i + (k * colStrideR)] -= pLocalU[k] * sum;
167  }
168  }
169  /* Q=A*Q1 */
170  for (i = 0; i < nRows; i++) {
171  sum = 0;
172  for (k = col; k < nRows; k++) {
173  sum += pLocalU[k] * pLocalQ[k + (i * colStrideQ)];
174  }
175  sum *= scale;
176  for (k = col; k < nRows; k++) {
177  pLocalQ[k + i * colStrideQ] -= pLocalU[k] * sum;
178  }
179  }
180  } /* if (norm_sqr!=0) */
181  } /* if (sum!=0) */
182  } /* for (col=0;col<=loopCount;col++) */
183 
184  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", status);
185 
186  return (status);
187 }
188 
189 // explicnt instantiation for the different data type versions
191  const void *restrict pA,
192  const void *restrict pQ,
193  const void *restrict pR,
194  const void *restrict pU,
195  const void *restrict pScratch);
196 
198  const void *restrict pA,
199  const void *restrict pQ,
200  const void *restrict pR,
201  const void *restrict pU,
202  const void *restrict pScratch);
203 /* ======================================================================== */
204 /* End of file: DSPLIB_qrd_cn.cpp */
205 /* ======================================================================== */
DSPLIB_STATUS DSPLIB_qrd_init_cn(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsA, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams1D_t *bufParamsU, const DSPLIB_qrdInitArgs *pKerInitArgs)
This function is the initialization function for the natural C implementation of the kernel....
template DSPLIB_STATUS DSPLIB_qrd_exec_cn< double >(DSPLIB_kernelHandle handle, const void *restrict pA, const void *restrict pQ, const void *restrict pR, const void *restrict pU, const void *restrict pScratch)
template DSPLIB_STATUS DSPLIB_qrd_init_cn< double >(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsA, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams1D_t *bufParamsU, const DSPLIB_qrdInitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_qrd_exec_cn< float >(DSPLIB_kernelHandle handle, const void *restrict pA, const void *restrict pQ, const void *restrict pR, const void *restrict pU, const void *restrict pScratch)
template DSPLIB_STATUS DSPLIB_qrd_init_cn< float >(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsA, DSPLIB_bufParams2D_t *bufParamsQ, DSPLIB_bufParams2D_t *bufParamsR, DSPLIB_bufParams1D_t *bufParamsU, const DSPLIB_qrdInitArgs *pKerInitArgs)
DSPLIB_STATUS DSPLIB_qrd_exec_cn(DSPLIB_kernelHandle handle, const void *restrict pA, const void *restrict pQ, const void *restrict pR, const void *restrict pU, const void *restrict pScratch)
This function is the main execution function for the natural C implementation of the kernel....
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_qrd.
#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 1 dimensional buffer descriptor.
A structure for a 2 dimensional buffer descriptor.
Structure containing the parameters to initialize the kernel.
Definition: DSPLIB_qrd.h:112
Structure that is reserved for internal use by the kernel.
int32_t strideR
Stride between rows of R output data matrix
uint32_t heightA
Height of input data matrix
uint32_t widthA
Size of input buffer for different batches DSPLIB_qrd_init that will be retrieved and used by DSPLIB_...
int32_t strideQ
Stride between rows of Q output data matrix
int32_t strideA
Stride between rows of input data matrix