| [48b662] | 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 |
|
|---|
| [ac6d04] | 50 | DGESV(int size) :
|
|---|
| 51 | T(size)
|
|---|
| 52 | {Init();}
|
|---|
| 53 |
|
|---|
| [48b662] | 54 | virtual ~DGESV();
|
|---|
| 55 |
|
|---|
| [ac6d04] | 56 | protected:
|
|---|
| [48b662] | 57 | void Compute();
|
|---|
| [ac6d04] | 58 |
|
|---|
| 59 | private:
|
|---|
| [48b662] | 60 | void Init();
|
|---|
| 61 | void Realloc();
|
|---|
| 62 |
|
|---|
| 63 | int la_INFO, la_LDA, la_LDB, la_N, la_NRHS;
|
|---|
| 64 | int *la_IPIV;
|
|---|
| 65 | vmg_float *la_A, *la_B;
|
|---|
| 66 |
|
|---|
| 67 | int la_cur_size, la_max_size;
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | template<class T>
|
|---|
| 71 | void DGESV<T>::Compute()
|
|---|
| 72 | {
|
|---|
| 73 | // Adjust size of vectors
|
|---|
| 74 | this->Realloc();
|
|---|
| 75 |
|
|---|
| 76 | la_N = la_LDA = la_LDB = la_cur_size;
|
|---|
| 77 |
|
|---|
| 78 | // Rewrite matrix in column-major order
|
|---|
| 79 | for (int i=0; i<la_N; i++) {
|
|---|
| 80 | la_B[i] = this->Rhs(i);
|
|---|
| 81 | for (int j=0; j<la_N; j++)
|
|---|
| 82 | la_A[i + j*la_N] = this->Mat(i,j);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // Solve system of equations
|
|---|
| 86 | dgesv(&la_N, &la_NRHS, la_A, &la_LDA, la_IPIV, la_B, &la_LDB, &la_INFO);
|
|---|
| 87 |
|
|---|
| 88 | // Assert successful exit of solver routine
|
|---|
| 89 | assert(la_INFO == 0);
|
|---|
| 90 |
|
|---|
| 91 | // Write solution back
|
|---|
| 92 | for (int i=0; i<la_N; i++)
|
|---|
| 93 | this->Sol(i) = la_B[i];
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | template<class T>
|
|---|
| 97 | void DGESV<T>::Init()
|
|---|
| 98 | {
|
|---|
| 99 | la_cur_size = 0;
|
|---|
| 100 | la_max_size = 0;
|
|---|
| 101 | la_NRHS = 1;
|
|---|
| 102 |
|
|---|
| 103 | la_IPIV = NULL;
|
|---|
| 104 | la_A = NULL;
|
|---|
| 105 | la_B = NULL;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | template<class T>
|
|---|
| 109 | void DGESV<T>::Realloc()
|
|---|
| 110 | {
|
|---|
| 111 | la_cur_size = this->Size();
|
|---|
| 112 |
|
|---|
| 113 | if (la_cur_size > la_max_size) {
|
|---|
| 114 |
|
|---|
| 115 | delete [] la_IPIV;
|
|---|
| 116 | delete [] la_A;
|
|---|
| 117 | delete [] la_B;
|
|---|
| 118 |
|
|---|
| 119 | la_IPIV = new int[la_cur_size];
|
|---|
| 120 | la_A = new vmg_float[la_cur_size*la_cur_size];
|
|---|
| 121 | la_B = new vmg_float[la_cur_size];
|
|---|
| 122 |
|
|---|
| 123 | la_max_size = la_cur_size;
|
|---|
| 124 |
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | template<class T>
|
|---|
| 129 | DGESV<T>::~DGESV()
|
|---|
| 130 | {
|
|---|
| 131 | delete [] la_IPIV;
|
|---|
| 132 | delete [] la_A;
|
|---|
| 133 | delete [] la_B;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | #endif /* DGESV_HPP_ */
|
|---|