MatrixOperations.hpp 21.3 KB
Newer Older
1 2
//! \file 
//
3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  MatrixTOperations.hpp
//  HappyHeart
//
//  Created by Sebastien Gilles on 30/10/15.
//  Copyright © 2015 Inria. All rights reserved.
//

#ifndef HAPPY_HEART_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_
# define HAPPY_HEART_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_

# include <memory>
# include <vector>

17
# include "Utilities/Containers/EnumClass.hpp"
18

19 20
# include "ThirdParty/IncludeWithoutWarning/Petsc/PetscSys.hpp"
# include "ThirdParty/IncludeWithoutWarning/Petsc/PetscMat.hpp"
21

22 23 24 25
# include "ThirdParty/Wrappers/Petsc/Matrix/NonZeroPattern.hpp"
# include "ThirdParty/Wrappers/Petsc/Vector/Vector.hpp"
# include "ThirdParty/Wrappers/Petsc/Matrix/Private/BaseMatrix.hpp"
# include "ThirdParty/Wrappers/Petsc/Exceptions/Petsc.hpp"
26 27 28 29 30 31


namespace HappyHeart
{
    
    
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    // ============================
    //! \cond IGNORE_BLOCK_IN_DOXYGEN
    // Forward declarations.
    // ============================
    
    
    
    // Matrix actually used in HappyHeart, that wraps a Wrappers::Petsc::Matrix.
    class GlobalMatrix;
    

    // ============================
    // End of forward declarations.
    //! \endcond IGNORE_BLOCK_IN_DOXYGEN
    // ============================


49 50 51 52 53 54
    namespace Wrappers
    {
        
        
        namespace Petsc
        {
55 56
            
            
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            /*!
             * \class doxygen_hide_petsc_do_reuse_matrix
             *
             * Whether matrix must be reused or initialized from scratch in functions such
             * as MatTransposeMatMult.
             * If yes, it must have been called once with no beforehand, and one has to be sure the pattern
             * of the matrices involved is still the same.
             */
            
            /*!
             * \class doxygen_hide_petsc_do_reuse_matrix_arg
             *
             * \param[in] do_reuse_matrix \copydetails doxygen_hide_petsc_do_reuse_matrix
             */
            
            
            /*!
             * \brief \copydoc doxygen_hide_petsc_do_reuse_matrix
             */
            enum class DoReuseMatrix { yes, no };
77
                   
78 79 80
            /*!
             * \brief Wrapper over MatAXPY, that performs Y = a * X + Y.
             *
81
             *
82 83
             * \tparam[in] NonZeroPatternT This value indicates the level of similarity between X and Y non zero patterns.
             * \warning Beware: if X and Y aren't following the same pattern, 'Same' won't yield what
84
             * you expect! In the case you're considering adding a part computed by a transfert matrix, you should
85
             * use 'Subset'. In HappyHeart, I advise you to call in debug mode AssertSameNumberingSubset()
86
             * (defined in Core) just before or after the function call; if the assert is raised it means you can't
87
             * use 'Same'. \todo This could be enforced by a proper overload (but it mix Utilities and Core
88
             * libraries...), but for the time being the separate assert will do.
89 90 91
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
92 93
            template
            <
94
                NonZeroPattern NonZeroPatternT,
95 96 97 98 99 100 101 102 103
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
             && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
104
            AXPY(PetscScalar a,
105
                 const MatrixT& X,
106
                 MatrixU& Y,
107
                 const char* invoking_file, int invoking_line);
108 109 110 111 112 113 114
            
            
            /*!
             * \brief Wrapper over MatMult, that performs v2 = matrix * v1.
             *
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
115
             * \copydoc doxygen_hide_do_update_ghost_arg
116 117 118 119
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Private::BaseMatrix, MatrixT>::value, void>
            MatMult(const MatrixT& matrix, const Vector& v1, Vector& v2,
120 121
                    const char* invoking_file, int invoking_line,
                    update_ghost do_update_ghost = update_ghost::yes);
122 123 124 125 126 127 128
            
            
            /*!
             * \brief Wrapper over MatMultAdd, that performs v3 = v2 + matrix * v1.
             *
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
129
             * \copydoc doxygen_hide_do_update_ghost_arg
130 131 132 133
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Private::BaseMatrix, MatrixT>::value, void>
            MatMultAdd(const MatrixT& matrix, const Vector& v1, const Vector& v2, Vector& v3,
134 135
                       const char* invoking_file, int invoking_line,
                       update_ghost do_update_ghost = update_ghost::yes);
136 137 138 139 140 141 142 143
            
            
            
            /*!
             * \brief Wrapper over MatMultTranspose, that performs v2 = transpose(matrix) * v1.
             *
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
144
             * \copydoc doxygen_hide_do_update_ghost_arg
145 146 147 148
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Private::BaseMatrix, MatrixT>::value, void>
            MatMultTranspose(const MatrixT& matrix, const Vector& v1, Vector& v2,
149 150
                             const char* invoking_file, int invoking_line,
                             update_ghost do_update_ghost = update_ghost::yes);
151 152 153 154 155 156 157
            
            
            /*!
             * \brief Wrapper over MatMultTransposeAdd, that performs v3 = v2 + transpose(matrix) * v1.
             *
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
158
             * \copydoc doxygen_hide_do_update_ghost_arg
159 160 161 162
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Private::BaseMatrix, MatrixT>::value, void>
            MatMultTransposeAdd(const MatrixT& matrix, const Vector& v1, const Vector& v2, Vector& v3,
163 164
                                const char* invoking_file, int invoking_line,
                                update_ghost do_update_ghost = update_ghost::yes);
165
            
166 167 168 169 170 171 172 173
            /*!
             * \class doxygen_hide_matmatmult_warning
             *
             * \attention In Petsc, matrix-matrix multiplication functions compute on the fly the
             * pattern for the resulting matrix. However, it is possible this pattern isn't the one expected;
             * in the richer HappyHeart interface you should call in debug mode \a AssertMatrixRespectPattern()
             * to make sure the resulting matrix respects the pattern defined for the \a GlobalMatrix pair 
             * of \a NumberingSubset.
174 175 176 177 178 179 180 181 182 183
             *
             * \internal <b><tt>[internal]</tt></b> If you know Petsc, you might see I didn't give access to
             * argument MatReuse, setting it each time to MAT_INITIAL_MATRIX and skipping entirely MAT_REUSE_MATRIX.
             * This is because at the time being MatMatMult operations are seldom in the code (only Poromechanics so
             * far) and using Symbolic/Numeric seems more elegant. Of course, reintroducing the argument is really easy;
             * feel free to do so if you need it (for instance for MatMatMatMult support: Symbolic/Numeric doesn't 
             * work for them and Petsc guys seemed unlikely to fix that in our exchanges).
             *
             * \internal <b><tt>[internal]</tt></b> \todo #684 Investigate to use the argument fill, which provides an 
             * estimation of the non zero of the resulting matrix. Currently PETSC_DEFAULT is used.
184 185 186
             */
            
            
187 188 189 190
            
            /*!
             * \brief Wrapper over MatMatMult, that performs C = A * B.
             *
191 192
             * \copydetails doxygen_hide_matmatmult_warning
             *
193 194
             * If the operation is performed many times with each time the same non zero pattern for the matrices,
             * rather use MatMatMultSymbolic/MatMatMultNumeric to improve efficiency.
195
            
196 197 198
             * One can also reuse a pattern for several matrices, but so far I do not need that in HappyHeart so
             * I equally use the default setting.
             *
199 200
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
             *
201
             */
202 203 204 205 206 207 208 209 210 211 212
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
213
            MatMatMult(const MatrixT& matrix1,
214 215
                       const MatrixT& matrix2,
                       MatrixU& matrix3,
216 217
                       const char* invoking_file, int invoking_line,
                       DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
218 219
            
            
220 221
            /*!
             * \brief Wrapper over MatMatMultSymbolic, that computes the structure of C = A * B. 
222
             *             
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
             * Actual computation is done with a call to MatMatMultNumeric, which might be called many times.
             *
             * \param[in] matrix1 A in C = A * B.
             * \param[in] matrix2 B in C = A * B.
             * \param[out] matrix3 C in C = A * B. Its pattern is correctly allocated after the call.
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            MatMatMultSymbolic(const MatrixT& matrix1,
                               const MatrixT& matrix2,
                               MatrixU& matrix3,
                               const char* invoking_file, int invoking_line);
            
            
            /*!
             * \brief Wrapper over MatMatMultNumeric, that computes C = A * B in the case MatMatMultSymbolic
             * has been used to allocate structure of C.
             *
252 253
             * \copydetails doxygen_hide_matmatmult_warning
             *
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
             * \param[in] matrix1 A in C = A * B.
             * \param[in] matrix2 B in C = A * B.
             * \param[out] matrix3 C in C = A * B.
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            MatMatMultNumeric(const MatrixT& matrix1,
                              const MatrixT& matrix2,
                              MatrixU& matrix3,
                              const char* invoking_file, int invoking_line);
            
            
277 278 279
            /*!
             * \brief Wrapper over MatMatMatMult, that performs D = A * B * C.
             *
280
             *
281
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
282
             */
283 284 285 286 287 288 289 290 291 292 293
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
294
            MatMatMatMult(const MatrixT& matrix1,
295 296 297
                          const MatrixT& matrix2,
                          const MatrixT& matrix3,
                          MatrixU& matrix4,
298 299
                          const char* invoking_file, int invoking_line,
                          DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
300
            
301
                  
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
            /*!
             * \brief Creates a new matrix object that behaves like A'.
             *
             * The transpose A' is NOT actually formed! Rather the new matrix object performs the matrix-vector product 
             * by using the MatMultTranspose() on the original matrix.
             *
             * \param[in] A matrix to transpose.
             * \param[out] transpose The matrix that figuratively represents A'. This matrix must not have been 
             * allocated!
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
            template<class MatrixT>
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value,
                void
            >
            MatCreateTranspose(const MatrixT& A,
                               MatrixT& transpose,
                               const char* invoking_file, int invoking_line);
            
324
            
325
            /*!
326
             * \class doxygen_hide_mat_transpose_mat_mult
327
             *
328
             *  \copydetails doxygen_hide_matmatmult_warning
329 330 331 332 333 334
             * \param[in] matrix1 A in C = A^T * B.
             * \param[in] matrix2 B in C = A^T * B.
             * \param[out] matrix3 C in B in C = A^T * B. The matrix must be not allocated when this function is called.
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
335 336 337 338 339 340
            
            
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A^T * B.
             *
             * \copydetails doxygen_hide_mat_transpose_mat_mult
341
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
342
             */
343 344 345 346 347 348 349 350 351 352 353 354 355 356
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            MatTransposeMatMult(const MatrixT& matrix1,
                                const MatrixT& matrix2,
                                MatrixU& matrix3,
357 358
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
359 360
            
            
361
                     
362 363 364
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A * B^T.
             *
365
             * \copydetails doxygen_hide_matmatmult_warning
366
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
367
             *
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
             * \param[in] matrix1 A in C = A * B^T.
             * \param[in] matrix2 B in C = A * B^T.
             * \param[out] matrix3 C in B in C = A * B^T. The matrix must be not allocated when this function is called.
             * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
             * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            MatMatTransposeMult(const MatrixT& matrix1,
                                const MatrixT& matrix2,
                                MatrixU& matrix3,
388 389
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
390 391
            
            
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            /*!
             * \class doxygen_hide_mat_pt_a_p
             *
             * \warning Unfortunately a simple test with P = I leads to error  
             * \verbatim
             * [0]PETSC ERROR: Nonconforming object sizes
             * [0]PETSC ERROR: Expected fill=-2 must be >= 1.0
             * \endverbatim
             * The reason is that PETSC_DEFAULT may not be supported (I've asked Petsc developers); but even with 
             * hand-tailored fill it doesn't seem to work...
             * So unfortunately I have to advise instead MatMatMult followed by MatTransposeMatMult instead...
             *
             * \copydetails doxygen_hide_matmatmult_warning
             *
             * \param[in] A A in C = P^T * A * P.
             * \param[in] P P in C = P^T * A * P
             * \param[out] out C in C = P^T * A * P. The matrix must be not allocated when this function is called.
             * \copydetails doxygen_hide_invoking_file_and_line
             */
            
            /*!
             * \brief Performs the matrix product C = P^T * A * P
             *
             * \copydoc doxygen_hide_mat_pt_a_p
             *
             *  \attention If the operation is performed many times with each time the same non zero pattern for the matrices,
             * rather use PtAPSymbolic/PtAPNumeric to improve efficiency.
             *
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            PtAP(const MatrixT& A,
                 const MatrixT& P,
                 MatrixU& out,
435 436
                 const char* invoking_file, int invoking_line,
                 DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
            
            
            
            /*!
             * \brief Wrapper over PtAPSymbolic, that computes the structure of C = P^T * A * P.
             *
             * Actual computations must be performed through calls to PtAPNumeric.
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatPtAPSymbolic.html#MatPtAPSymbolic
             * for more details.
             *
             * \copydoc doxygen_hide_mat_pt_a_p
             *
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            PtAPSymbolic(const MatrixT& A,
                         const MatrixT& P,
                         MatrixU& out,
                         const char* invoking_file, int invoking_line);
            
            
            /*!
             * \brief Wrapper over PtAPNumeric, that computes C = P^T * A * P. in the case C has already been allocated.
             *
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatPtAPNumeric.html#MatPtAPNumeric
             * for more details.
             *
             * \copydoc doxygen_hide_mat_pt_a_p
             *
             */
            template
            <
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
                std::is_base_of<Private::BaseMatrix, MatrixT>::value
                && std::is_base_of<Private::BaseMatrix, MatrixU>::value,
                void
            >
            PtAPNumeric(const MatrixT& A,
                        const MatrixT& P,
                        MatrixU& out,
                        const char* invoking_file, int invoking_line);


            
494
            
495 496 497 498 499 500
        } //namespace Petsc
        
        
    } //namespace Wrappers
    
    
501 502 503 504 505 506 507 508 509 510
    
    /*! 
     * \brief Convenient alias to avoid repeating the namespaces in each call.
     *
     * It should be used anyway only as template arguments of functions within this namespaces, such as 
     * Wrappers::Petsc::AXPY.
     */
    using NonZeroPattern = Wrappers::Petsc::NonZeroPattern;
    
    
511 512 513
} // namespace HappyHeart


514
# include "ThirdParty/Wrappers/Petsc/Matrix/MatrixOperations.hxx"
515 516 517


#endif // HAPPY_HEART_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_