DSPLIB User Guide
DSPLIB_lud_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 #include "DSPLIB_lud_priv.h"
34 
35 /*********************************************************************
36  *
37  * INITIALIZATION
38  *
39  *********************************************************************/
40 
42  const DSPLIB_bufParams2D_t *bufParamsA,
43  const DSPLIB_bufParams2D_t *bufParamsL,
44  const DSPLIB_bufParams2D_t *bufParamsU,
45  const DSPLIB_bufParams2D_t *bufParamsP,
46  const DSPLIB_ludInitArgs * pKerInitArgs)
47 {
48  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
49 
50  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", DSPLIB_SUCCESS);
51  return DSPLIB_SUCCESS;
52 }
53 /**********************************************************************/
54 /* NATURAL C IMPLEMENTATION */
55 /**********************************************************************/
56 template <typename dataType>
57 int DSPLIB_lud_cn(int order,
58  dataType * A,
59  dataType * L,
60  dataType * U,
61  unsigned short *P,
62  const int32_t strideOrder,
63  const int32_t strideP)
64 {
65  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering nat C c7x implementation");
66  int row, col;
67  int min_row, max_row, k, temp;
68  dataType min, max, tmp;
69  max_row = 0;
70  min_row = 0;
71 
72  int32_t dataSize = sizeof(dataType);
73  int32_t dataSizeP = sizeof(unsigned short);
74 
75  int32_t orderStride = strideOrder / dataSize;
76  int32_t orderPStride = strideP / dataSizeP;
77 
78  /* ------------------------------------------------------------------- */
79  /* generate identify matrix */
80  /* ------------------------------------------------------------------- */
81  for (row = 0; row < order; row++) {
82  for (col = 0; col < order; col++) {
83  if (row == col) {
84  P[col + row * orderPStride] = 1;
85  }
86  else {
87  P[col + row * orderPStride] = 0;
88  }
89  }
90  }
91 
92  /* ------------------------------------------------------------------- */
93  /* LU decomposition */
94  /* ------------------------------------------------------------------- */
95  memcpy(U, A, sizeof(dataType) * order * orderStride);
96 
97  for (k = 0; k < order - 1; k++) {
98 
99  /* find min and max entries in column */
100  max = 0.0;
101  min = 3.4e+38;
102  for (row = k; row < order; row++) {
103  if (fabs(U[k + row * orderStride]) > max) {
104  max = fabs(U[k + row * orderStride]);
105  max_row = row;
106  }
107 
108  if (fabs(U[k + row * orderStride]) < min) {
109  min = fabs(U[k + row * orderStride]);
110  min_row = row;
111  }
112  }
113 
114  /* swap rows if necessary */
115  if (k != max_row) {
116  for (col = 0; col < order; col++) {
117  tmp = U[col + min_row * orderStride];
118  U[col + min_row * orderStride] = U[col + max_row * orderStride];
119  U[col + max_row * orderStride] = tmp;
120  temp = P[col + min_row * orderPStride];
121  P[col + min_row * orderPStride] = P[col + max_row * orderPStride];
122  P[col + max_row * orderPStride] = temp;
123  }
124  }
125 
126  /* generate U matrix */
127 
128  dataType mulFactor = 1.0 / U[k + k * orderStride];
129  for (row = k + 1; row < order; row++) {
130  U[k + row * orderStride] *= mulFactor;
131  // U[k + row * orderStride] /= U[k + k * orderStride];
132  }
133 
134  for (row = k + 1; row < order; row++) {
135  for (col = k + 1; col < order; col++) {
136  U[col + row * orderStride] -= U[k + row * orderStride] * U[col + k * orderStride];
137  }
138  }
139  }
140 
141  /* extract lower triangular entries from L into U and set L lower entries to zero */
142  for (row = 0; row < order; row++) {
143  for (col = 0; col < order; col++) {
144  if (row < col) {
145  L[col + row * orderStride] = 0;
146  }
147  else {
148  if (row == col) {
149  L[col + row * orderStride] = 1;
150  }
151  else {
152  L[col + row * orderStride] = U[col + row * orderStride];
153  U[col + row * orderStride] = 0;
154  }
155  }
156  }
157  }
158 
159  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Exiting nat C c7x implementation");
160  return 0;
161 }
162 
163 template int DSPLIB_lud_cn<float>(int order,
164  float * A,
165  float * L,
166  float * U,
167  unsigned short *P,
168  const int32_t strideOrder,
169  const int32_t strideP);
170 template int DSPLIB_lud_cn<double>(int order,
171  double * A,
172  double * L,
173  double * U,
174  unsigned short *P,
175  const int32_t strideOrder,
176  const int32_t strideP);
177 
178 template <typename dataType>
180  void *restrict pA,
181  void *restrict pL,
182  void *restrict pU,
183  void *restrict pP)
184 {
185  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering exec_cn function");
186 
187  DSPLIB_STATUS status = DSPLIB_SUCCESS;
188 
189  DSPLIB_lud_PrivArgs *pKerPrivArgs = (DSPLIB_lud_PrivArgs *) handle;
190  int32_t order = pKerPrivArgs->order;
191  int32_t strideOrder = pKerPrivArgs->strideOrder;
192  int32_t strideP = pKerPrivArgs->strideP;
193 
194  /* Typecast void pointers to respective data type */
195  dataType * pALocal = (dataType *) pA;
196  dataType * pLLocal = (dataType *) pL;
197  dataType * pULocal = (dataType *) pU;
198  unsigned short *pPLocal = (unsigned short *) pP;
199 
200  DSPLIB_DEBUGPRINTFN(0, "pALocal: %p pLLocal: %p pULocal: %p pPLocal: %p\n", pALocal, pLLocal, pULocal, pPLocal);
201 
202  DSPLIB_lud_cn<dataType>(order, pALocal, pLLocal, pULocal, pPLocal, strideOrder, strideP);
203 
204  DSPLIB_DEBUGPRINTFN(0, "Exiting exec_cn function with return status: %d\n", status);
205 
206  return (status);
207 }
208 
209 // explicit instantiation for the different data type versions
211  void *restrict pA,
212  void *restrict pL,
213  void *restrict pU,
214  void *restrict pP);
215 
217  void *restrict pA,
218  void *restrict pL,
219  void *restrict pU,
220  void *restrict pP);
221 /* ======================================================================== */
222 /* End of file: DSPLIB_lud_cn.cpp */
223 /* ======================================================================== */
int DSPLIB_lud_cn(int order, dataType *A, dataType *L, dataType *U, unsigned short *P, const int32_t strideOrder, const int32_t strideP)
template int DSPLIB_lud_cn< float >(int order, float *A, float *L, float *U, unsigned short *P, const int32_t strideOrder, const int32_t strideP)
template DSPLIB_STATUS DSPLIB_lud_exec_cn< float >(DSPLIB_kernelHandle handle, void *restrict pA, void *restrict pL, void *restrict pU, void *restrict pP)
DSPLIB_STATUS DSPLIB_lud_init_cn(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsA, const DSPLIB_bufParams2D_t *bufParamsL, const DSPLIB_bufParams2D_t *bufParamsU, const DSPLIB_bufParams2D_t *bufParamsP, const DSPLIB_ludInitArgs *pKerInitArgs)
This function is the initialization function for the natural C implementation of the kernel....
template DSPLIB_STATUS DSPLIB_lud_exec_cn< double >(DSPLIB_kernelHandle handle, void *restrict pA, void *restrict pL, void *restrict pU, void *restrict pP)
DSPLIB_STATUS DSPLIB_lud_exec_cn(DSPLIB_kernelHandle handle, void *restrict pA, void *restrict pL, void *restrict pU, void *restrict pP)
This function is the main execution function for the natural C implementation of the kernel....
template int DSPLIB_lud_cn< double >(int order, double *A, double *L, double *U, unsigned short *P, const int32_t strideOrder, const int32_t strideP)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_lud.
#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.
Definition: DSPLIB_lud.h:117
Structure that is reserved for internal use by the kernel.
int32_t order
Size of input buffer for different batches DSPLIB_lud_init that will be retrieved and used by DSPLIB_...
int32_t strideOrder
Stride between rows of input and output data matrix
int32_t strideP
Stride between rows of output data matrix P