1 /*
  2  *   Copyright (c) 2011, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 /* Based on
 34  *
 35        SUBROUTINE DPOSV( UPLO, N, NRHS, A, LDA, B, LDB, INFO )
 36  *
 37  *  -- LAPACK driver routine (version 3.3.1) --
 38  *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
 39  *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
 40  *  -- April 2011                                                      --
 41  */
 42 
 43 #ifndef FLENS_LAPACK_GESV_POSV_TCC
 44 #define FLENS_LAPACK_GESV_POSV_TCC 1
 45 
 46 #include <algorithm>
 47 #include <flens/blas/blas.h>
 48 #include <flens/lapack/lapack.h>
 49 
 50 #include <flens/lapack/interface/include/f77lapack.h>
 51 
 52 namespace flens { namespace lapack {
 53 
 54 //== generic lapack implementation =============================================
 55 
 56 template <typename MA, typename MB>
 57 typename SyMatrix<MA>::IndexType
 58 posv_generic(SyMatrix<MA> &A, GeMatrix<MB> &B)
 59 {
 60     typedef typename GeMatrix<MB>::IndexType  IndexType;
 61 
 62     IndexType info = potrf(A);
 63 
 64     if (info==0) {
 65         potrs(A, B);
 66     }
 67     return info;
 68 }
 69 
 70 //== interface for native lapack ===============================================
 71 
 72 #ifdef CHECK_CXXLAPACK
 73 
 74 template <typename MA, typename MB>
 75 typename SyMatrix<MA>::IndexType
 76 posv_native(SyMatrix<MA> &A, GeMatrix<MB> &B)
 77 {
 78     typedef typename SyMatrix<MA>::ElementType  T;
 79 
 80     const char       UPLO = char(A.upLo());
 81     const INTEGER    N = A.dim();
 82     const INTEGER    NRHS = B.numCols();
 83     const INTEGER    LDA = A.leadingDimension();
 84     const INTEGER    LDB = B.leadingDimension();
 85     INTEGER          INFO;
 86 
 87     if (IsSame<T, DOUBLE>::value) {
 88         LAPACK_IMPL(dposv)(&UPLO, &N, &NRHS,
 89                            A.data(), &LDA,
 90                            B.data(), &LDB,
 91                            &INFO);
 92     } else {
 93         ASSERT(0);
 94     }
 95     ASSERT(INFO>=0);
 96 
 97     return INFO;
 98 }
 99 
100 #endif // CHECK_CXXLAPACK
101 
102 //== public interface ==========================================================
103 
104 template <typename MA, typename MB>
105 typename SyMatrix<MA>::IndexType
106 posv(SyMatrix<MA> &A, GeMatrix<MB> &B)
107 {
108     typedef typename SyMatrix<MA>::IndexType    IndexType;
109 //
110 //  Test the input parameters
111 //
112     ASSERT(A.firstRow()==1);
113     ASSERT(A.firstCol()==1);
114 
115     ASSERT(B.firstRow()==1);
116     ASSERT(B.firstCol()==1);
117 
118     ASSERT(B.numRows()==A.dim());
119 
120 #   ifdef CHECK_CXXLAPACK
121 //
122 //  Make copies of output arguments
123 //
124     typename SyMatrix<MA>::NoView  A_org = A;
125     typename GeMatrix<MB>::NoView  B_org = B;
126 #   endif
127 
128 //
129 //  Call implementation
130 //
131     const IndexType info = posv_generic(A, B);
132 
133 #   ifdef CHECK_CXXLAPACK
134 //
135 //  Compare results
136 //
137     typename SyMatrix<MA>::NoView  A_generic = A;
138     typename GeMatrix<MB>::NoView  B_generic = B;
139     A = A_org;
140     B = B_org;
141 
142     const IndexType _info = posv_native(A, B);
143 
144     bool failed = false;
145     if (! isIdentical(A_generic, A, "A_generic""A")) {
146         std::cerr << "A_org = " << A_org << std::endl;
147         std::cerr << "CXXLAPACK: A_generic = " << A_generic << std::endl;
148         std::cerr << "F77LAPACK: A = " << A << std::endl;
149         failed = true;
150     }
151 
152     if (! isIdentical(B_generic, B, "B_generic""B")) {
153         std::cerr << "B_org = " << B_org << std::endl;
154         std::cerr << "CXXLAPACK: B_generic = " << B_generic << std::endl;
155         std::cerr << "F77LAPACK: B = " << B << std::endl;
156         failed = true;
157     }
158 
159     if (! isIdentical(info, _info, " info""_info")) {
160         std::cerr << "CXXLAPACK:  info = " << info << std::endl;
161         std::cerr << "F77LAPACK: _info = " << _info << std::endl;
162         failed = true;
163     }
164 
165     if (failed) {
166         ASSERT(0);
167     }
168 
169 #   endif
170 
171     return info;
172 }
173 
174 //-- forwarding ----------------------------------------------------------------
175 template <typename MA, typename MB>
176 typename MA::IndexType
177 posv(MA &&A, MB &&B)
178 {
179     typedef typename MA::IndexType  IndexType;
180 
181     CHECKPOINT_ENTER;
182     const IndexType info =  posv(A, B);
183     CHECKPOINT_LEAVE;
184 
185     return info;
186 }
187 
188 } } // namespace lapack, flens
189 
190 #endif // FLENS_LAPACK_GESV_POSV_TCC