MatrixOperations.hpp 18.3 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 14 15 16 17

#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>

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 30 31 32


namespace HappyHeart
{
    
    
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    // ============================
    //! \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
    // ============================


50 51 52 53 54 55
    namespace Wrappers
    {
        
        
        namespace Petsc
        {
56 57
            
            
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            /*!
             * \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 };
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 HappyHeart, 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
            /*!
             * \brief Wrapper over MatShift, that performs M = M + a I, where a is a PetscScalar and I is the identity matrix.
             *
116 117 118
             * \param[in] matrix See formula above.
             * \param[in] a See formula above.
             * \copydoc doxygen_hide_invoking_file_and_line
119 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
            
            /*!
             * \brief Wrapper over MatMult, that performs v2 = matrix * v1.
             *
128 129 130 131
             * \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.
132
             * \copydoc doxygen_hide_do_update_ghost_arg
133 134
             */
            template<class MatrixT>
135
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
136 137 138
            MatMult(const MatrixT& matrix,
                    const Vector& v1,
                    Vector& v2,
139 140
                    const char* invoking_file, int invoking_line,
                    update_ghost do_update_ghost = update_ghost::yes);
141 142 143 144 145
            
            
            /*!
             * \brief Wrapper over MatMultAdd, that performs v3 = v2 + matrix * v1.
             *
146 147 148 149 150
             * \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.
151
             * \copydoc doxygen_hide_do_update_ghost_arg
152 153
             */
            template<class MatrixT>
154
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
155 156 157 158
            MatMultAdd(const MatrixT& matrix,
                       const Vector& v1,
                       const Vector& v2,
                       Vector& v3,
159 160
                       const char* invoking_file, int invoking_line,
                       update_ghost do_update_ghost = update_ghost::yes);
161 162 163 164 165 166
            
            
            
            /*!
             * \brief Wrapper over MatMultTranspose, that performs v2 = transpose(matrix) * v1.
             *
167
             * \copydoc doxygen_hide_invoking_file_and_line
168
             * \copydoc doxygen_hide_do_update_ghost_arg
169 170 171
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
172 173
             */
            template<class MatrixT>
174
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
175 176 177
            MatMultTranspose(const MatrixT& matrix,
                             const Vector& v1,
                             Vector& v2,
178 179
                             const char* invoking_file, int invoking_line,
                             update_ghost do_update_ghost = update_ghost::yes);
180 181 182 183 184
            
            
            /*!
             * \brief Wrapper over MatMultTransposeAdd, that performs v3 = v2 + transpose(matrix) * v1.
             *
185
             * \copydoc doxygen_hide_invoking_file_and_line
186
             * \copydoc doxygen_hide_do_update_ghost_arg
187 188 189 190
             * \param[in] matrix See formula above.
             * \param[in] v1 See formula above.
             * \param[in] v2 See formula above.
             * \param[in] v3 See formula above.
191 192
             */
            template<class MatrixT>
193
            std::enable_if_t<std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value, void>
194 195 196 197
            MatMultTransposeAdd(const MatrixT& matrix,
                                const Vector& v1,
                                const Vector& v2,
                                Vector& v3,
198 199
                                const char* invoking_file, int invoking_line,
                                update_ghost do_update_ghost = update_ghost::yes);
200
            
201 202 203 204 205 206
            /*!
             * \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()
207
             * to make sure the resulting matrix respects the pattern defined for the \a GlobalMatrix pair
208
             * of \a NumberingSubset.
209 210 211 212 213
             *
             * \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;
214
             * feel free to do so if you need it (for instance for MatMatMatMult support: Symbolic/Numeric doesn't
215 216
             * work for them and Petsc guys seemed unlikely to fix that in our exchanges).
             *
217
             * \internal <b><tt>[internal]</tt></b> \todo #684 Investigate to use the argument fill, which provides an
218
             * estimation of the non zero of the resulting matrix. Currently PETSC_DEFAULT is used.
219 220 221
             */
            
            
222 223
            
            /*!
224
             * \brief Wrapper over MatMatMult, that performs m3 = m1 * m2.
225
             *
226 227
             * \copydetails doxygen_hide_matmatmult_warning
             *
228 229
             * 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.
230
             
231 232
             * 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.
233 234 235
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[in] m3 See formula above.
236
             *
237
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
238
             * \copydoc doxygen_hide_invoking_file_and_line
239
             */
240 241
            template
            <
242 243
            class MatrixT,
            class MatrixU
244 245 246
            >
            std::enable_if_t
            <
247 248
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
249 250
                void
            >
251 252 253
            MatMatMult(const MatrixT& m1,
                       const MatrixT& m2,
                       MatrixU& m3,
254 255
                       const char* invoking_file, int invoking_line,
                       DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
256
            
257
                      
258
            /*!
259
             * \brief Wrapper over MatMatMatMult, that performs m4 = m1 * m2 * m3.
260
             *
261 262 263 264
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[in] m3 See formula above.
             * \param[in] m4 See formula above.
265
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
266
             * \copydoc doxygen_hide_invoking_file_and_line
267
             */
268 269
            template
            <
270 271
            class MatrixT,
            class MatrixU
272 273 274
            >
            std::enable_if_t
            <
275 276
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
277 278
                void
            >
279 280 281 282
            MatMatMatMult(const MatrixT& m1,
                          const MatrixT& m2,
                          const MatrixT& m3,
                          MatrixU& m4,
283 284
                          const char* invoking_file, int invoking_line,
                          DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
285
            
286 287 288
            /*!
             * \brief Creates a new matrix object that behaves like A'.
             *
289
             * The transpose A' is NOT actually formed! Rather the new matrix object performs the matrix-vector product
290 291 292
             * by using the MatMultTranspose() on the original matrix.
             *
             * \param[in] A matrix to transpose.
293
             * \param[out] transpose The matrix that figuratively represents A'. This matrix must not have been
294
             * allocated!
295
             * \copydoc doxygen_hide_invoking_file_and_line
296 297 298 299
             */
            template<class MatrixT>
            std::enable_if_t
            <
300
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value,
301 302 303 304 305 306
                void
            >
            MatCreateTranspose(const MatrixT& A,
                               MatrixT& transpose,
                               const char* invoking_file, int invoking_line);
            
307
            
308
            /*!
309
             * \class doxygen_hide_mat_transpose_mat_mult
310
             *
311 312 313 314 315 316 317 318 319 320
             * Formula is:
             * \verbatim
             * m3 = m1^T * m2.
             * \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
321
             */
322 323 324
            
            
            /*!
325
             * \brief Performs Matrix-Matrix Multiplication m3 = m1^T * m2.
326 327
             *
             * \copydetails doxygen_hide_mat_transpose_mat_mult
328
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
329 330 331
             * \param[in] m1 See formula above.
             * \param[in] m2 See formula above.
             * \param[in] m3 See formula above.
332
             */
333 334
            template
            <
335 336
            class MatrixT,
            class MatrixU
337 338 339
            >
            std::enable_if_t
            <
340 341
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
342 343
                void
            >
344 345 346
            MatTransposeMatMult(const MatrixT& m1,
                                const MatrixT& m2,
                                MatrixU& m3,
347 348
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
349 350
            
            
351
            
352 353 354
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A * B^T.
             *
355
             * \copydetails doxygen_hide_matmatmult_warning
356
             * \copydetails doxygen_hide_petsc_do_reuse_matrix_arg
357
             *
358 359 360
             * \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.
361
             * \copydoc doxygen_hide_invoking_file_and_line
362 363 364
             */
            template
            <
365 366
            class MatrixT,
            class MatrixU
367 368 369
            >
            std::enable_if_t
            <
370 371
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
372 373 374 375 376
                void
            >
            MatMatTransposeMult(const MatrixT& matrix1,
                                const MatrixT& matrix2,
                                MatrixU& matrix3,
377 378
                                const char* invoking_file, int invoking_line,
                                DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
379 380
            
            
381 382 383
            /*!
             * \class doxygen_hide_mat_pt_a_p
             *
384
             * \warning Unfortunately a simple test with P = I leads to error
385 386 387 388
             * \verbatim
             * [0]PETSC ERROR: Nonconforming object sizes
             * [0]PETSC ERROR: Expected fill=-2 must be >= 1.0
             * \endverbatim
389
             * The reason is that PETSC_DEFAULT may not be supported (I've asked Petsc developers); but even with
390 391 392 393 394 395 396 397 398
             * 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
399
             * \copydoc doxygen_hide_petsc_do_reuse_matrix_arg
400 401 402 403 404 405 406 407 408 409 410
             */
            
            /*!
             * \brief Performs the matrix product C = P^T * A * P
             *
             * \copydoc doxygen_hide_mat_pt_a_p
             *
             *
             */
            template
            <
411 412
            class MatrixT,
            class MatrixU
413 414 415
            >
            std::enable_if_t
            <
416 417
                std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixT>::value
                && std::is_base_of<Internal::Wrappers::Petsc::BaseMatrix, MatrixU>::value,
418 419 420 421 422
                void
            >
            PtAP(const MatrixT& A,
                 const MatrixT& P,
                 MatrixU& out,
423 424
                 const char* invoking_file, int invoking_line,
                 DoReuseMatrix do_reuse_matrix = DoReuseMatrix::no);
425 426
            
            
427
        } // namespace Petsc
428 429
        
        
430
    } // namespace Wrappers
431 432
    
    
433 434 435 436 437 438 439 440 441 442
    
    /*! 
     * \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;
    
    
443 444 445
} // namespace HappyHeart


446 447 448
/// @} // addtogroup ThirdPartyGroup


449
# include "ThirdParty/Wrappers/Petsc/Matrix/MatrixOperations.hxx"
450 451 452


#endif // HAPPY_HEART_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_