MatrixOperations.hpp 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
///
////// \file
///
///
/// Created by Sebastien Gilles <sebastien.gilles@inria.fr> on the Fri, 30 Oct 2015 12:41:42 +0100
/// Copyright (c) Inria. All rights reserved.
///
/// \ingroup ThirdPartyGroup
/// \addtogroup ThirdPartyGroup
/// \{
11

12 13
#ifndef MOREFEM_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_
# define MOREFEM_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_
14 15 16 17

# include <memory>
# include <vector>

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

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

23 24
# include "ThirdParty/Wrappers/Petsc/Matrix/NonZeroPattern.hpp"
# include "ThirdParty/Wrappers/Petsc/Vector/Vector.hpp"
25
# include "ThirdParty/Wrappers/Petsc/Matrix/Internal/BaseMatrix.hpp"
26
# include "ThirdParty/Wrappers/Petsc/Exceptions/Petsc.hpp"
27 28


29
namespace MoReFEM
30
{
31 32


33 34 35 36
    // ============================
    //! \cond IGNORE_BLOCK_IN_DOXYGEN
    // Forward declarations.
    // ============================
37 38 39



40
    // Matrix actually used in MoReFEM, that wraps a Wrappers::Petsc::Matrix.
41
    class GlobalMatrix;
42

43 44 45 46 47 48 49

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


50 51
    namespace Wrappers
    {
52 53


54 55
        namespace Petsc
        {
56 57


58 59 60 61 62 63 64 65
            /*!
             * \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.
             */
66

67 68 69 70 71
            /*!
             * \class doxygen_hide_petsc_do_reuse_matrix_arg
             *
             * \param[in] do_reuse_matrix \copydetails doxygen_hide_petsc_do_reuse_matrix
             */
72 73


74 75 76 77
            /*!
             * \brief \copydoc doxygen_hide_petsc_do_reuse_matrix
             */
            enum class DoReuseMatrix { yes, no };
78

79 80 81
            /*!
             * \brief Wrapper over MatAXPY, that performs Y = a * X + Y.
             *
82
             *
83
             * \tparam NonZeroPatternT This value indicates the level of similarity between X and Y non zero patterns.
84
             * \warning Beware: if X and Y aren't following the same pattern, 'Same' won't yield what
85
             * you expect! In the case you're considering adding a part computed by a transfert matrix, you should
86
             * use 'Subset'. In MoReFEM, I advise you to call in debug mode AssertSameNumberingSubset()
87
             * (defined in Core) just before or after the function call; if the assert is raised it means you can't
88
             * use 'Same'. \todo This could be enforced by a proper overload (but it mix Utilities and Core
89
             * libraries...), but for the time being the separate assert will do.
90 91 92 93 94
             * \copydoc doxygen_hide_invoking_file_and_line
             *
             * \param[in] a See formula above.
             * \param[in] X See formula above.
             * \param[in] Y See formula above.
95
             */
96 97
            template
            <
98
                NonZeroPattern NonZeroPatternT,
99 100 101 102 103
                class MatrixT,
                class MatrixU
            >
            std::enable_if_t
            <
104 105
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
             && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
106 107
                void
            >
108
            AXPY(PetscScalar a,
109
                 const MatrixT& X,
110
                 MatrixU& Y,
111
                 const char* invoking_file, int invoking_line);
112 113


114 115 116
            /*!
             * \brief Wrapper over MatShift, that performs M = M + a I, where a is a PetscScalar and I is the identity matrix.
             *
117 118 119
             * \param[in] matrix See formula above.
             * \param[in] a See formula above.
             * \copydoc doxygen_hide_invoking_file_and_line
120 121 122 123
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
            MatShift(const PetscScalar a, MatrixT& matrix, const char* invoking_file, int invoking_line);
124 125


126 127 128
            /*!
             * \brief Wrapper over MatMult, that performs v2 = matrix * v1.
             *
129 130 131 132
             * \copydoc doxygen_hide_invoking_file_and_line
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
133
             * \copydoc doxygen_hide_do_update_ghost_arg
134 135
             */
            template<class MatrixT>
136
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
137 138 139
            MatMult(const MatrixT& matrix,
                    const Vector& v1,
                    Vector& v2,
140 141
                    const char* invoking_file, int invoking_line,
                    update_ghost do_update_ghost = update_ghost::yes);
142

143 144 145
            /*!
             * \brief Wrapper over MatTranspose, that performs in-place or out-of-place transpose of a matrix.
             *
146 147 148
             * \copydoc doxygen_hide_invoking_file_and_line
             * \param[in] matrix1 Matrix to transpose.
             * \param[in] matrix2 Matrix to contain the result of the transpose.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
             *
             */
            template
            <
            class MatrixT,
            class MatrixU
            >
            std::enable_if_t
            <
            std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
            && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
            void
            >
            MatTranspose(MatrixT& matrix1,
                         MatrixU& matrix2,
                         const char* invoking_file, int invoking_line);
165 166


167 168 169
            /*!
             * \brief Wrapper over MatMultAdd, that performs v3 = v2 + matrix * v1.
             *
170 171 172 173 174
             * \copydoc doxygen_hide_invoking_file_and_line
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
             * \param[in] v3 See formula above.
175
             * \copydoc doxygen_hide_do_update_ghost_arg
176 177
             */
            template<class MatrixT>
178
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
179 180 181 182
            MatMultAdd(const MatrixT& matrix,
                       const Vector& v1,
                       const Vector& v2,
                       Vector& v3,
183 184
                       const char* invoking_file, int invoking_line,
                       update_ghost do_update_ghost = update_ghost::yes);
185 186 187



188 189 190
            /*!
             * \brief Wrapper over MatMultTranspose, that performs v2 = transpose(matrix) * v1.
             *
191
             * \copydoc doxygen_hide_invoking_file_and_line
192
             * \copydoc doxygen_hide_do_update_ghost_arg
193 194 195
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
196 197
             */
            template<class MatrixT>
198
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
199 200 201
            MatMultTranspose(const MatrixT& matrix,
                             const Vector& v1,
                             Vector& v2,
202 203
                             const char* invoking_file, int invoking_line,
                             update_ghost do_update_ghost = update_ghost::yes);
204 205


206 207 208
            /*!
             * \brief Wrapper over MatMultTransposeAdd, that performs v3 = v2 + transpose(matrix) * v1.
             *
209
             * \copydoc doxygen_hide_invoking_file_and_line
210
             * \copydoc doxygen_hide_do_update_ghost_arg
211 212 213 214
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
             * \param[in] v3 See formula above.
215 216
             */
            template<class MatrixT>
217
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
218 219 220 221
            MatMultTransposeAdd(const MatrixT& matrix,
                                const Vector& v1,
                                const Vector& v2,
                                Vector& v3,
222 223
                                const char* invoking_file, int invoking_line,
                                update_ghost do_update_ghost = update_ghost::yes);
224

225 226 227 228 229
            /*!
             * \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;
230
             * in the richer MoReFEM interface you should call in debug mode \a AssertMatrixRespectPattern()
231
             * to make sure the resulting matrix respects the pattern defined for the \a GlobalMatrix pair
232
             * of \a NumberingSubset.
233 234 235 236 237
             *
             * \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;
238
             * feel free to do so if you need it (for instance for MatMatMatMult support: Symbolic/Numeric doesn't
239 240
             * work for them and Petsc guys seemed unlikely to fix that in our exchanges).
             *
241
             * \internal <b><tt>[internal]</tt></b> \todo #684 Investigate to use the argument fill, which provides an
242
             * estimation of the non zero of the resulting matrix. Currently PETSC_DEFAULT is used.
243
             */
244 245 246



247
            /*!
248
             * \brief Wrapper over MatMatMult, that performs m3 = m1 * m2.
249
             *
250 251
             * \copydetails doxygen_hide_matmatmult_warning
             *
252 253
             * 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.
254

255
             * One can also reuse a pattern for several matrices, but so far I do not need that in MoReFEM so
256
             * I equally use the default setting.
257 258 259
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[in] m3 See formula above.
260
             *
261
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
262
             * \copydoc doxygen_hide_invoking_file_and_line
263
             */
264 265
            template
            <
266
            class MatrixT,
267 268
            class MatrixU,
            class MatrixV
269 270 271
            >
            std::enable_if_t
            <
272
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
273 274
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value,
275 276
                void
            >
277
            MatMatMult(const MatrixT& m1,
278 279
                       const MatrixU& m2,
                       MatrixV& m3,
280 281
                       const char* invoking_file, int invoking_line,
                       DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
282 283


284
            /*!
285
             * \brief Wrapper over MatMatMatMult, that performs m4 = m1 * m2 * m3.
286
             *
287 288 289 290
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[in] m3 See formula above.
             * \param[in] m4 See formula above.
291
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
292
             * \copydoc doxygen_hide_invoking_file_and_line
293
             */
294 295
            template
            <
296
            class MatrixT,
297 298 299
            class MatrixU,
            class MatrixV,
            class MatrixW
300 301 302
            >
            std::enable_if_t
            <
303
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
304 305 306
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixW>::value,
307 308
                void
            >
309
            MatMatMatMult(const MatrixT& m1,
310 311 312
                          const MatrixU& m2,
                          const MatrixV& m3,
                          MatrixW& m4,
313 314
                          const char* invoking_file, int invoking_line,
                          DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
315

316 317 318
            /*!
             * \brief Creates a new matrix object that behaves like A'.
             *
319
             * The transpose A' is NOT actually formed! Rather the new matrix object performs the matrix-vector product
320 321 322
             * by using the MatMultTranspose() on the original matrix.
             *
             * \param[in] A matrix to transpose.
323
             * \param[out] transpose The matrix that figuratively represents A'. This matrix must not have been
324
             * allocated!
325
             * \copydoc doxygen_hide_invoking_file_and_line
326
             */
327 328 329 330 331
            template
            <
            class MatrixT,
            class MatrixU
            >
332 333
            std::enable_if_t
            <
334 335
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
336 337 338
                void
            >
            MatCreateTranspose(const MatrixT& A,
339
                               MatrixU& transpose,
340
                               const char* invoking_file, int invoking_line);
341 342


343
            /*!
344
             * \class doxygen_hide_mat_transpose_mat_mult
345
             *
346 347
             * Formula is:
             * \verbatim
348
             m3 = m1^T * m2.
349 350 351 352 353 354 355
             * \endverbatim
             *
             * \copydetails doxygen_hide_matmatmult_warning
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[out] m3 See formula above. The matrix must be not allocated when this function is called.
             * \copydoc doxygen_hide_invoking_file_and_line
356
             */
357 358


359
            /*!
360
             * \brief Performs Matrix-Matrix Multiplication m3 = m1^T * m2.
361 362
             *
             * \copydetails doxygen_hide_mat_transpose_mat_mult
363
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
364
             */
365 366
            template
            <
367 368 369
                class MatrixT,
                class MatrixU,
                class MatrixV
370 371 372
            >
            std::enable_if_t
            <
373
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
374
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value,
375 376
                void
            >
377
            MatTransposeMatMult(const MatrixT& m1,
378 379
                                const MatrixU& m2,
                                MatrixV& m3,
380 381
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
382 383 384



385 386 387
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A * B^T.
             *
388
             * \copydetails doxygen_hide_matmatmult_warning
389
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
390
             *
391 392 393
             * \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.
394
             * \copydoc doxygen_hide_invoking_file_and_line
395 396 397
             */
            template
            <
398
            class MatrixT,
399 400
            class MatrixU,
            class MatrixV
401 402 403
            >
            std::enable_if_t
            <
404
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
405 406
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value,
407 408 409
                void
            >
            MatMatTransposeMult(const MatrixT& matrix1,
410 411
                                const MatrixU& matrix2,
                                MatrixV& matrix3,
412 413
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
414 415


416 417 418
            /*!
             * \class doxygen_hide_mat_pt_a_p
             *
419
             * \warning Unfortunately a simple test with P = I leads to error
420
             * \verbatim
421 422 423
             [0;39m[0;49m[0]PETSC ERROR: Nonconforming object sizes
             [0]PETSC ERROR: Expected fill=-2 must be >= 1.0
             \endverbatim
424
             * The reason is that PETSC_DEFAULT may not be supported (I've asked Petsc developers); but even with
425 426 427 428 429 430 431 432 433
             * 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
434
             * \copydoc doxygen_hide_petsc_do_reuse_matrix_arg
435
             */
436

437 438 439 440 441 442 443 444 445
            /*!
             * \brief Performs the matrix product C = P^T * A * P
             *
             * \copydoc doxygen_hide_mat_pt_a_p
             *
             *
             */
            template
            <
446
            class MatrixT,
447 448
            class MatrixU,
            class MatrixV
449 450 451
            >
            std::enable_if_t
            <
452
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
453 454
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value,
455 456 457
                void
            >
            PtAP(const MatrixT& A,
458 459
                 const MatrixU& P,
                 MatrixV& out,
460 461
                 const char* invoking_file, int invoking_line,
                 DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
462 463 464



465 466 467 468 469 470
            /*!
             * \brief Wrapper over MatGetOrdering, gets a reordering for a matrix to reduce fill or to improve numerical stability of LU factorization.
             *
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/MatOrderings/MatGetOrdering.html#MatGetOrdering
             * for more details.
             *
471 472 473 474
             * \param[in] A Matrix to get the ordering.
             * \param[in] type Type of the ordering.
             * \param[in] rperm Row permutation for the ordering.
             * \param[in] cperm Column permutation for the ordering.
475
             * \copydetails doxygen_hide_invoking_file_and_line
476 477 478 479 480 481 482 483 484
             *
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
            GetOrdering(MatrixT& A,
                        MatOrderingType type,
                        IS *rperm,
                        IS *cperm,
                        const char* invoking_file, int invoking_line);
485 486


487
            /*!
488
             * \brief Wrapper over MatLUFactor, that performs in-place LU factorization of matrix.
489 490 491 492
             *
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatLUFactor.html#MatLUFactor
             * for more details.
             *
493 494 495 496 497
             * \param[in] A Matrix to factor.
             * \param[in] row Result of the GetOrdering.
             * \param[in] col Result of the GetOrdering.
             * \param[in] info Info of LUFactor.
             * \copydetails doxygen_hide_invoking_file_and_line
498 499 500 501 502 503 504 505
             *
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
            LUFactor(MatrixT& A,
                     IS row, IS col,
                     const MatFactorInfo *info,
                     const char* invoking_file, int invoking_line);
506 507


508 509 510 511 512 513
            /*!
             * \brief Wrapper over MatMatSolve, solves A X = B, given a factored matrix.
             *
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatMatSolve.html
             * for more details.
             *
514 515 516 517
             * \param[in] A See formula above.
             * \param[in] B See formula above.
             * \param[in] X See formula above.
             * \copydetails doxygen_hide_invoking_file_and_line
518 519 520 521
             *
             */
            template
            <
GILLES Sebastien's avatar
GILLES Sebastien committed
522 523 524
                class MatrixT,
                class MatrixU,
                class MatrixV
525 526 527
            >
            std::enable_if_t
            <
GILLES Sebastien's avatar
GILLES Sebastien committed
528 529 530 531
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixV>::value,
                void
532 533 534 535 536
            >
            MatMatSolve(const MatrixT& A,
                        const MatrixU& B,
                        MatrixV& X,
                        const char* invoking_file, int invoking_line);
537 538


539 540 541 542 543 544
            /*!
             * \brief Wrapper over MatCholeskyFactor, that performs in-place Cholesky factorization of a symmetric matrix.
             *
             * \see http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatCholeskyFactor.html
             * for more details.
             *
545
             * \attention Not used yet, but might be useful in midterm developments. If finally too unwieldy for our
546 547
             * purposes it will be removed.
             *
548 549 550 551 552 553 554 555 556 557 558 559
             * \param[in] mat Matrix to factor.
             * \param[in] perm Result of the GetOrdering, row and column permutations.
             * \param[in] info Info of CholeskyFactor.
             * \copydetails doxygen_hide_invoking_file_and_line
             *
             */
            template<class MatrixT>
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
            CholeskyFactor(MatrixT& mat,
                           IS perm,
                           const MatFactorInfo *info,
                           const char* invoking_file, int invoking_line);
560 561


562 563


564
        } //namespace Petsc
565 566


567
    } // namespace Wrappers
568 569 570 571



    /*!
572 573
     * \brief Convenient alias to avoid repeating the namespaces in each call.
     *
574
     * It should be used anyway only as template arguments of functions within this namespaces, such as
575 576 577
     * Wrappers::Petsc::AXPY.
     */
    using NonZeroPattern = Wrappers::Petsc::NonZeroPattern;
578 579


580
} // namespace MoReFEM
581 582


583 584 585
/// @} // addtogroup ThirdPartyGroup


586
# include "ThirdParty/Wrappers/Petsc/Matrix/MatrixOperations.hxx"
587 588


589
#endif // MOREFEM_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_