|
| 1 | +/*! |
| 2 | + * \file CSquareMatrixCM.cpp |
| 3 | + * \brief Implementation of dense matrix helper class in Column Major order (see hpp). |
| 4 | + * \author Edwin van der Weide, Pedro Gomes. |
| 5 | + * \version 7.0.8 "Blackbird" |
| 6 | + * |
| 7 | + * SU2 Project Website: https://su2code.github.io |
| 8 | + * |
| 9 | + * The SU2 Project is maintained by the SU2 Foundation |
| 10 | + * (http://su2foundation.org) |
| 11 | + * |
| 12 | + * Copyright 2012-2020, SU2 Contributors (cf. AUTHORS.md) |
| 13 | + * |
| 14 | + * SU2 is free software; you can redistribute it and/or |
| 15 | + * modify it under the terms of the GNU Lesser General Public |
| 16 | + * License as published by the Free Software Foundation; either |
| 17 | + * version 2.1 of the License, or (at your option) any later version. |
| 18 | + * |
| 19 | + * SU2 is distributed in the hope that it will be useful, |
| 20 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | + * Lesser General Public License for more details. |
| 23 | + * |
| 24 | + * You should have received a copy of the GNU Lesser General Public |
| 25 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "../../include/toolboxes/CSquareMatrixCM.hpp" |
| 29 | +#include "../../include/mpi_structure.hpp" |
| 30 | +#include "../../include/blas_structure.hpp" |
| 31 | + |
| 32 | +using namespace std; |
| 33 | + |
| 34 | +#if defined(HAVE_MKL) |
| 35 | +#include "mkl.h" |
| 36 | +#ifndef HAVE_LAPACK |
| 37 | +#define HAVE_LAPACK |
| 38 | +#endif |
| 39 | +#elif defined(HAVE_LAPACK) |
| 40 | +/*--- Lapack / Blas routines used in CSquareMatrixCM. ---*/ |
| 41 | +extern "C" void dgetrf_(const int*, const int*, passivedouble*, const int*, |
| 42 | + int*, int*); |
| 43 | +extern "C" void dgetri_(const int*, passivedouble*, const int*, int*, |
| 44 | + passivedouble*, const int*, int*); |
| 45 | +extern "C" void dgemm_(char*, char*, const int*, const int*, const int*, |
| 46 | + const passivedouble*, const passivedouble*, |
| 47 | + const int *, const passivedouble*, const int*, |
| 48 | + const passivedouble*, passivedouble*, const int*); |
| 49 | +#define DGEMM dgemm_ |
| 50 | +#endif |
| 51 | + |
| 52 | +void CSquareMatrixCM::Transpose() { |
| 53 | + |
| 54 | + for(int j=1; j<Size(); ++j) |
| 55 | + for(int i=0; i<j; ++i) |
| 56 | + swap(mat(i,j), mat(j,i)); |
| 57 | +} |
| 58 | + |
| 59 | +void CSquareMatrixCM::Invert() { |
| 60 | + |
| 61 | +#ifdef HAVE_LAPACK |
| 62 | + |
| 63 | + /*--- Computation of the inverse using the Lapack routines. ---*/ |
| 64 | + int sz = Size(); |
| 65 | + int info; |
| 66 | + vector<int> ipiv(sz); |
| 67 | + vector<passivedouble> work(sz); |
| 68 | + |
| 69 | + dgetrf_(&sz, &sz, mat.data(), &sz, ipiv.data(), &info); |
| 70 | + if(info != 0) SU2_MPI::Error(string("Matrix is singular"), CURRENT_FUNCTION); |
| 71 | + |
| 72 | + dgetri_(&sz, mat.data(), &sz, ipiv.data(), work.data(), &sz, &info); |
| 73 | + if(info != 0) SU2_MPI::Error(string("Matrix inversion failed"), CURRENT_FUNCTION); |
| 74 | + |
| 75 | +#else |
| 76 | + CBlasStructure::inverse(Size(), mat); |
| 77 | +#endif |
| 78 | +} |
| 79 | + |
| 80 | +void CSquareMatrixCM::MatMatMult(const char side, |
| 81 | + const ColMajorMatrix<passivedouble> &mat_in, |
| 82 | + ColMajorMatrix<passivedouble> &mat_out) const { |
| 83 | + |
| 84 | + /*--- Check the type of multiplication to be carried out. ---*/ |
| 85 | + if (side == 'L' || side == 'l') { |
| 86 | + |
| 87 | + /*--- Left side: mat_out = this * mat_in. Set some sizes |
| 88 | + and allocate the memory for mat_out. ---*/ |
| 89 | + const int M = Size(), N = mat_in.cols(); |
| 90 | + assert(M == mat_in.rows()); |
| 91 | + |
| 92 | + mat_out.resize(M,N); |
| 93 | + |
| 94 | +#ifdef HAVE_LAPACK |
| 95 | + |
| 96 | + /*--- The Lapack/blas function dgemm is used to carry out |
| 97 | + the matrix matrix multiplication. ---*/ |
| 98 | + passivedouble alpha = 1.0, beta = 0.0; |
| 99 | + char trans = 'N'; |
| 100 | + |
| 101 | + DGEMM(&trans, &trans, &M, &N, &M, &alpha, mat.data(), &M, |
| 102 | + mat_in.data(), &M, &beta, mat_out.data(), &M); |
| 103 | +#else |
| 104 | + /*--- Naive product. ---*/ |
| 105 | + for (int i = 0; i < M; ++i) { |
| 106 | + for (int j = 0; j < N; ++j) { |
| 107 | + mat_out(i,j) = 0.0; |
| 108 | + for (int k = 0; k < M; ++k) |
| 109 | + mat_out(i,j) += mat(i,k) * mat_in(k,j); |
| 110 | + } |
| 111 | + } |
| 112 | +#endif |
| 113 | + |
| 114 | + } |
| 115 | + else { |
| 116 | + |
| 117 | + /*--- Right_side: mat_out = mat_in * this. Set some sizes |
| 118 | + and allocate the memory for mat_out. ---*/ |
| 119 | + const int M = mat_in.rows(), N = Size(); |
| 120 | + assert(N == mat_in.cols()); |
| 121 | + |
| 122 | + mat_out.resize(M,N); |
| 123 | + |
| 124 | +#ifdef HAVE_LAPACK |
| 125 | + |
| 126 | + /*--- The Lapack/blas function dgemm is used to carry out |
| 127 | + the matrix matrix multiplication. ---*/ |
| 128 | + passivedouble alpha = 1.0, beta = 0.0; |
| 129 | + char trans = 'N'; |
| 130 | + |
| 131 | + DGEMM(&trans, &trans, &M, &N, &N, &alpha, mat_in.data(), &M, |
| 132 | + mat.data(), &N, &beta, mat_out.data(), &M); |
| 133 | +#else |
| 134 | + /*--- Naive product. ---*/ |
| 135 | + for (int i = 0; i < M; ++i) { |
| 136 | + for (int j = 0; j < N; ++j) { |
| 137 | + mat_out(i,j) = 0.0; |
| 138 | + for (int k = 0; k < N; ++k) |
| 139 | + mat_out(i,j) += mat_in(i,k) * mat(k,j); |
| 140 | + } |
| 141 | + } |
| 142 | +#endif |
| 143 | + } |
| 144 | +} |
0 commit comments