| 1 | /**
|
|---|
| 2 | * @file dgesv.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Apr 18 13:09:29 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Lapack solver for general matrices.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifndef DGESV_HPP_
|
|---|
| 11 | #define DGESV_HPP_
|
|---|
| 12 |
|
|---|
| 13 | #ifndef HAVE_LAPACK
|
|---|
| 14 | #error You need LAPACK in order to use MGDSYSV
|
|---|
| 15 | #endif
|
|---|
| 16 |
|
|---|
| 17 | #include <cassert>
|
|---|
| 18 | #include <cstddef>
|
|---|
| 19 |
|
|---|
| 20 | namespace VMG
|
|---|
| 21 | {
|
|---|
| 22 |
|
|---|
| 23 | extern "C" {
|
|---|
| 24 |
|
|---|
| 25 | void FC_FUNC(dgesv,DGESV) (const int* N, const int* NRHS, vmg_float* A,
|
|---|
| 26 | const int* LDA, int* IPIV, vmg_float* B,
|
|---|
| 27 | const int* LDB, int* INFO);
|
|---|
| 28 |
|
|---|
| 29 | void FC_FUNC(sgesv, SGESV) (const int* N, const int* NRHS, vmg_float* A,
|
|---|
| 30 | const int* LDA, int* IPIV, vmg_float* B,
|
|---|
| 31 | const int* LDB, int* INFO);
|
|---|
| 32 |
|
|---|
| 33 | } /* extern "C" */
|
|---|
| 34 |
|
|---|
| 35 | /* By default, assume fcs_float is double. */
|
|---|
| 36 | #if defined(FCS_FLOAT_IS_FLOAT)
|
|---|
| 37 | #define dgesv FC_FUNC(sgesv,SGESV)
|
|---|
| 38 | #else
|
|---|
| 39 | #define dgesv FC_FUNC(dgesv,DGESV)
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | template<class T>
|
|---|
| 43 | class DGESV : public T
|
|---|
| 44 | {
|
|---|
| 45 | public:
|
|---|
| 46 | DGESV() :
|
|---|
| 47 | T()
|
|---|
| 48 | {Init();}
|
|---|
| 49 |
|
|---|
| 50 | virtual ~DGESV();
|
|---|
| 51 |
|
|---|
| 52 | private:
|
|---|
| 53 | void Compute();
|
|---|
| 54 | void Init();
|
|---|
| 55 | void Realloc();
|
|---|
| 56 |
|
|---|
| 57 | int la_INFO, la_LDA, la_LDB, la_N, la_NRHS;
|
|---|
| 58 | int *la_IPIV;
|
|---|
| 59 | vmg_float *la_A, *la_B;
|
|---|
| 60 |
|
|---|
| 61 | int la_cur_size, la_max_size;
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | template<class T>
|
|---|
| 65 | void DGESV<T>::Compute()
|
|---|
| 66 | {
|
|---|
| 67 | // Adjust size of vectors
|
|---|
| 68 | this->Realloc();
|
|---|
| 69 |
|
|---|
| 70 | la_N = la_LDA = la_LDB = la_cur_size;
|
|---|
| 71 |
|
|---|
| 72 | // Rewrite matrix in column-major order
|
|---|
| 73 | for (int i=0; i<la_N; i++) {
|
|---|
| 74 | la_B[i] = this->Rhs(i);
|
|---|
| 75 | for (int j=0; j<la_N; j++)
|
|---|
| 76 | la_A[i + j*la_N] = this->Mat(i,j);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // Solve system of equations
|
|---|
| 80 | dgesv(&la_N, &la_NRHS, la_A, &la_LDA, la_IPIV, la_B, &la_LDB, &la_INFO);
|
|---|
| 81 |
|
|---|
| 82 | // Assert successful exit of solver routine
|
|---|
| 83 | assert(la_INFO == 0);
|
|---|
| 84 |
|
|---|
| 85 | // Write solution back
|
|---|
| 86 | for (int i=0; i<la_N; i++)
|
|---|
| 87 | this->Sol(i) = la_B[i];
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | template<class T>
|
|---|
| 91 | void DGESV<T>::Init()
|
|---|
| 92 | {
|
|---|
| 93 | la_cur_size = 0;
|
|---|
| 94 | la_max_size = 0;
|
|---|
| 95 | la_NRHS = 1;
|
|---|
| 96 |
|
|---|
| 97 | la_IPIV = NULL;
|
|---|
| 98 | la_A = NULL;
|
|---|
| 99 | la_B = NULL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | template<class T>
|
|---|
| 103 | void DGESV<T>::Realloc()
|
|---|
| 104 | {
|
|---|
| 105 | la_cur_size = this->Size();
|
|---|
| 106 |
|
|---|
| 107 | if (la_cur_size > la_max_size) {
|
|---|
| 108 |
|
|---|
| 109 | delete [] la_IPIV;
|
|---|
| 110 | delete [] la_A;
|
|---|
| 111 | delete [] la_B;
|
|---|
| 112 |
|
|---|
| 113 | la_IPIV = new int[la_cur_size];
|
|---|
| 114 | la_A = new vmg_float[la_cur_size*la_cur_size];
|
|---|
| 115 | la_B = new vmg_float[la_cur_size];
|
|---|
| 116 |
|
|---|
| 117 | la_max_size = la_cur_size;
|
|---|
| 118 |
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | template<class T>
|
|---|
| 123 | DGESV<T>::~DGESV()
|
|---|
| 124 | {
|
|---|
| 125 | delete [] la_IPIV;
|
|---|
| 126 | delete [] la_A;
|
|---|
| 127 | delete [] la_B;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | #endif /* DGESV_HPP_ */
|
|---|