Authored by David Mayerich
1 parent 89676102

### integrated Rupali's changes - replaced OpenCV matrix math with LAPACK

Showing 1 changed file with 67 additions and 25 deletions
src/main.cu

 @@ -50,6 +50,43 @@ typedef struct { @@ -50,6 +50,43 @@ typedef struct { 50 }gnome; 50 }gnome; 51 gnome gnom; 51 gnome gnom; 52 52 53 +//computing matrix determinant using LU decomposition 54 +template 55 +T mtxdeterminant(T* M, size_t r, size_t c) { 56 + //----DETERMINANT using LU decomposition using LAPACK 57 + /* DGETRF computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges. 58 + The factorization has the form 59 + A = P * L * U 60 + where P is a permutation matrix, L is lower triangular with unit diagonal elements (lower trapezoidal if m > n), and U is upper triangular (upper trapezoidal if m < n).*/ 61 + int* ipiv = (int*)malloc(sizeof(int) * r); 62 + memset(ipiv, 0, r * sizeof(int)); 63 + LAPACKE_sgetrf(LAPACK_COL_MAJOR, (int)r, (int)c, M, (int)r, ipiv); 64 + 65 + //determinant of U 66 + T product = 1; 67 + for (size_t i = 0; i < r; i++) { 68 + for (size_t j = 0; j < r; j++) { 69 + if (i == j) { 70 + product = product * M[i * r + j]; 71 + } 72 + } 73 + } 74 + 75 + //determinant of P 76 + int j; 77 + double detp = 1.; 78 + for (j = 0; j < r; j++) { 79 + if (j + 1 != ipiv[j]) { 80 + // j+1 : following feedback of ead : ipiv is from Fortran, hence starts at 1. 81 + // hey ! This is a transpose ! 82 + detp = -detp; 83 + } 84 + } 85 + T detSw = product * detp * 1; //det(U)*det(P)*det(L) 86 + if (ipiv != NULL) std::free(ipiv); 87 + return detSw; 88 + 89 +} 53 90 54 void gpuComputeEignS( size_t g, size_t fea){ 91 void gpuComputeEignS( size_t g, size_t fea){ 55 //eigen value computation will return r = (nC-1) eigen vectors so new projected data will have dimension of r rather than f 92 //eigen value computation will return r = (nC-1) eigen vectors so new projected data will have dimension of r rather than f @@ -87,7 +124,7 @@ void gpuComputeEignS( size_t g, size_t fea){ @@ -87,7 +124,7 @@ void gpuComputeEignS( size_t g, size_t fea){ 87 int *IPIV = (int*) malloc(sizeof(int) * f); 124 int *IPIV = (int*) malloc(sizeof(int) * f); 88 //computing inverse of matrix Sw 125 //computing inverse of matrix Sw 89 memset(IPIV, 0, f * sizeof(int)); 126 memset(IPIV, 0, f * sizeof(int)); 90 - LAPACKE_sgetrf(LAPACK_COL_MAJOR, (int)f, (int)f, gSw_a, (int)f, IPIV); 127 + LAPACKE_sgetrf(LAPACK_COL_MAJOR, (int)f, (int)f, gSw_a, (int)f, IPIV); 91 // DGETRI computes the inverse of a matrix using the LU factorization computed by DGETRF. 128 // DGETRI computes the inverse of a matrix using the LU factorization computed by DGETRF. 92 LAPACKE_sgetri(LAPACK_COL_MAJOR, (int)f, gSw_a, (int)f, IPIV); 129 LAPACKE_sgetri(LAPACK_COL_MAJOR, (int)f, gSw_a, (int)f, IPIV); 93 130 @@ -146,23 +183,22 @@ void gpuComputeEignS( size_t g, size_t fea){ @@ -146,23 +183,22 @@ void gpuComputeEignS( size_t g, size_t fea){ 146 mtxMul(tempgSw, &gnom.lda[g * r * f], &gnom.Sw[g * f * f], r, f, f,f); 183 mtxMul(tempgSw, &gnom.lda[g * r * f], &gnom.Sw[g * f * f], r, f, f,f); 147 float* nSw = (float*)calloc(r * r, sizeof(float)); 184 float* nSw = (float*)calloc(r * r, sizeof(float)); 148 mtxMultranspose(nSw, tempgSw, &gnom.lda[g * r * f], r, f, r, f); 185 mtxMultranspose(nSw, tempgSw, &gnom.lda[g * r * f], r, f, r, f); 149 - if(debug){ 150 - std::cout<<"From Eigen function: projected Sb sn Sw"<