3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-26 22:33:48 +01:00
dft_tools/test/triqs/arrays/blas_lapack.cpp
Olivier Parcollet b534936589 Fix matrix * alias issue and adapt det_manip
- The previous version of the * operator for matrix was too clever.
It was giving a lazy object and then rewriting C = A *B into gemm (a,A,B,0,C).
The pb was in case of aliasing : when e.g. C = A, or is a part of A.
gemm is not correct that case, and as a result generic code like
a = a *b
may not be correct in matrix case, which is unacceptable.

- So we revert to a simple * operator for matrix
that does immediate computation.
Same thing for matrix* vector

- we also suppress a_x_ty class.

-> for M = a * b,
when M is a matrix, there is no overhead due to move assignment
-> however, when M is a view, there is an additionnal copy.

-Correctness comes first, hence the fix.
However, if one wants more speed and one can guarantee that
there is no aliasing possible, then one has to write a direct gemm call.

-> det_manip class was adapted, since in that case, we can show there
no alias, and we want the speed gain, so the * ops where replaced
by direct blas call (using the array blas interface).

-> also gemm, gemv, ger were overloaded in the case the return
matrix/vector (i.e. last parameter of the function) is not an lvalue,
but a temporary view created on the fly.
2013-09-11 18:49:22 +02:00

96 lines
2.6 KiB
C++

/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011 by O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "./common.hpp"
#include "./src/array.hpp"
#include "./src/vector.hpp"
#include "./src/matrix.hpp"
#include "./src/linalg/det_and_inverse.hpp"
#include "./src/blas_lapack/gemm.hpp"
#include "./src/blas_lapack/gemv.hpp"
#include "./src/blas_lapack/ger.hpp"
#include "./src/blas_lapack/axpy.hpp"
#include <iostream>
using namespace triqs::arrays;
int main(int argc, char **argv) {
typedef std::complex<double> T;
triqs::arrays::vector<T> V(5),V2(5);
for (int i =0; i<5; ++i) {V(i) = i; V2(i) = -1;}
std::cout<<"V = "<<V<<std::endl;
std::cout<<"V2 = "<<V2<<std::endl;
std::cout <<"starting blas test"<<std::endl;
blas::axpy(2.0,V,V2);
std::cout<<"V = "<<V<<std::endl;
std::cout<<"V2 = "<<V2<<std::endl;
triqs::arrays::vector <double> V3(2);
for (int i =0; i<2; ++i) {V3(i) = i+1;}
triqs::arrays::matrix<double> M1(2,2,FORTRAN_LAYOUT), M2(2,2,FORTRAN_LAYOUT), M3(2,2,FORTRAN_LAYOUT);
for (int i =0; i<2; ++i)
for (int j=0; j<2; ++j)
{ M1(i,j) = i+j; M2(i,j) = 1; M3(i,j)=0;}
// try to multiply
blas::gemm(1.0,M1, M2, 1.0, M3);
std::cout<<"M1 = "<<M1<<std::endl;
std::cout<<"M2 = "<<M2<<std::endl;
std::cout<<"M3 = "<<M3<<std::endl;
triqs::arrays::matrix<double> Mc1(2,2), Mc2(2,2), Mc3(2,2);
for (int i =0; i<2; ++i)
for (int j=0; j<2; ++j)
{ Mc1(i,j) = i+j; Mc2(i,j) = 1; Mc3(i,j)=0;}
// try to multiply
blas::gemm(1.0,Mc1, Mc2, 1.0, Mc3);
std::cout<<"Mc1 = "<<Mc1<<std::endl;
std::cout<<"Mc2 = "<<Mc2<<std::endl;
std::cout<<"Mc3 = "<<Mc3<<std::endl;
std::cout<<"V3 = "<<V3<<std::endl;
blas::ger(1.0,V3,V3,M2);
std::cout<<"M2 = "<<M2<<std::endl;
// try to invert
triqs::arrays::vector <int> ipiv(2);
lapack::getrf(M1, ipiv);
lapack::getri(M1, ipiv);
std::cout<<"inv M1 = "<<M1<<std::endl;
}