lapack_int LAPACKE_sgetrf_work( int matrix_layout, lapack_int m, lapack_int n, float* a, lapack_int lda, lapack_int* ipiv ) { lapack_int info = 0; if( matrix_layout == LAPACK_COL_MAJOR ) { /* Call LAPACK function and adjust info */ LAPACK_sgetrf( &m, &n, a, &lda, ipiv, &info ); if( info < 0 ) { info = info - 1; } } else if( matrix_layout == LAPACK_ROW_MAJOR ) { lapack_int lda_t = MAX(1,m); float* a_t = NULL; /* Check leading dimension(s) */ if( lda < n ) { info = -5; LAPACKE_xerbla( "LAPACKE_sgetrf_work", info ); return info; } /* Allocate memory for temporary array(s) */ a_t = (float*)LAPACKE_malloc( sizeof(float) * lda_t * MAX(1,n) ); if( a_t == NULL ) { info = LAPACK_TRANSPOSE_MEMORY_ERROR; goto exit_level_0; } /* Transpose input matrices */ LAPACKE_sge_trans( matrix_layout, m, n, a, lda, a_t, lda_t ); /* Call LAPACK function and adjust info */ LAPACK_sgetrf( &m, &n, a_t, &lda_t, ipiv, &info ); if( info < 0 ) { info = info - 1; } /* Transpose output matrices */ LAPACKE_sge_trans( LAPACK_COL_MAJOR, m, n, a_t, lda_t, a, lda ); /* Release memory and exit */ LAPACKE_free( a_t ); exit_level_0: if( info == LAPACK_TRANSPOSE_MEMORY_ERROR ) { LAPACKE_xerbla( "LAPACKE_sgetrf_work", info ); } } else { info = -1; LAPACKE_xerbla( "LAPACKE_sgetrf_work", info ); } return info; }
int main (int argc,char* argv[]) { // Some needed ints. n=size of matrix, info = error checker from ssyev lapack_int n=5,info; /* Declare matrix of values! */ float atemp[n*n] = { 1.96f, -6.49f, -0.47f, -7.20f, -0.65f, 0.00f, 3.80f, -6.39f, 1.50f, -6.34f, 0.00f, 0.00f, 4.17f, -1.51f, 2.67f, 0.00f, 0.00f, 0.00f, 5.70f, 1.80f, 0.00f, 0.00f, 0.00f, 0.00f, -7.10f }; /* Copy to a vector! */ std::vector<float> a(n*n); /* Declare vector to hold eigenvalues! */ std::vector<float> w(n); /* Move data to the std::vector a*/ std::memcpy(&a[0],atemp,n*n*sizeof(float)); /* Make a working vector! */ std::vector<float> c(n*n); std::memcpy(&c[0],&a[0],n*n*sizeof(float)); /* -------------------- Eigenvalues with lapack ----------------- */ /* Example of how to use std::vector with ssyev */ info = LAPACKE_ssyev( LAPACK_ROW_MAJOR,'V','U',n,&c[0],n,&w[0] ); /* Error checking */ if (info > 0) { std::cout << "Diagonalization failed! Error code: " << info << std::endl; exit(EXIT_FAILURE); } /* -------------------------------------------------------------- */ /* Print the eigenvalues */ std::cout << "Eigenvalues: " << std::endl; for (int j=0;j<n;++j) { std::cout << w[j] << " "; } std::cout << std::endl; /* Print the eigenvectors */ std::cout << "Eigenvectors: " << std::endl; for (int i=0;i<n;++i) { for (int j=0;j<n;++j) { std::cout << c[i*n+j] << " "; } std::cout << std::endl; } std::memcpy(&c[0],&a[0],n*n*sizeof(float)); /* -------------------- Matrix inverse with Lapack ----------------- */ // Seg faults on my machine, some problem with pthreads, but you get the point lapack_int ipiv[n]; LAPACK_sgetrf(&n,&n,&c[0],&n,ipiv,&info); // Error checking if (info > 0) { std::cout << "Inversion failed! Error code: " << info << std::endl; exit(EXIT_FAILURE); } info = LAPACKE_sgetri( n,n,&c[0],n,ipiv ); // Error checking if (info > 0) { std::cout << "Inversion failed! Error code: " << info << std::endl; exit(EXIT_FAILURE); } /* ----------------------------------------------------------------- */ /* -------------------- Matrix Mult with cBlas ----------------- */ std::vector<float> b(n*n); cblas_sgemm(CblasRowMajor,CblasTrans,CblasNoTrans,n,n,1,1.0f,&a[0],n,&c[0],n,0.0f,&b[0],n); //cblas_sgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,n,n,1,1.0f,&c[0],n,&a[0],n,0.0f,&b[0],n); /* -------------------------------------------------------------- */ /* Print the eigenvectors */ std::cout << "Matrx Mult Result: " << std::endl; for (int i=0;i<n;++i) { for (int j=0;j<n;++j) { std::cout << b[i*n+j] << " "; } std::cout << std::endl; } return 0; }