MatrixOperations.hpp 18.1 KB
Newer Older
1 2
//! \file 
//
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//
//  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>

# include "ThirdParty/IncludeWithoutWarning/Petsc/PetscSys.hpp"
# include "ThirdParty/IncludeWithoutWarning/Petsc/PetscMat.hpp"

# include "ThirdParty/Wrappers/Petsc/Vector/Vector.hpp"
# include "ThirdParty/Wrappers/Petsc/Matrix/Private/BaseMatrix.hpp"
# include "ThirdParty/Wrappers/Petsc/Exceptions/Petsc.hpp"


namespace HappyHeart
{
    
    
    namespace Wrappers
    {
        
        
        namespace Petsc
        {
35 36
            
            
37 38 39 40 41 42 43
            /*!
             * \brief Wrapper over MatAXPY, that performs Y = a * X + Y.
             *
             * \param[in] structure either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN.
             * \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__.
             */
44 45 46 47 48 49 50 51 52 53 54
            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
            >
55 56
            AXPY(PetscScalar a,
                 const MatrixT& X,
57
                 MatrixU& Y,
58 59 60 61 62 63 64 65 66 67 68 69 70
                 const char* invoking_file, int invoking_line,
                 const MatStructure& structure = SAME_NONZERO_PATTERN);
            
            
            /*!
             * \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__.
             */
            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,
71
                    const char* invoking_file, int invoking_line);
72 73 74 75 76 77 78 79 80 81 82
            
            
            /*!
             * \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__.
             */
            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,
83
                       const char* invoking_file, int invoking_line);
84 85 86 87 88 89 90 91 92 93 94 95
            
            
            
            /*!
             * \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__.
             */
            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,
96
                             const char* invoking_file, int invoking_line);
97 98 99 100 101 102 103 104 105 106 107
            
            
            /*!
             * \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__.
             */
            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,
108
                                const char* invoking_file, int invoking_line);
109
            
110 111 112 113 114 115 116 117 118 119 120
            /*!
             * \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.
             */
            
            
121 122 123 124
            
            /*!
             * \brief Wrapper over MatMatMult, that performs C = A * B.
             *
125 126
             * \copydetails doxygen_hide_matmatmult_warning
             *
127 128 129
             * 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.
             *
130 131 132 133 134 135 136 137
             * \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.
             * 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.
             *
             * \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__.
             */
138 139 140 141 142 143 144 145 146 147 148
            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
            >
149
            MatMatMult(const MatrixT& matrix1,
150 151 152
                       const MatrixT& matrix2,
                       MatrixU& matrix3,
                       const char* invoking_file, int invoking_line);
153 154
            
            
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            /*!
             * \brief Wrapper over MatMatMultSymbolic, that computes the structure of C = A * B. 
             *
             * 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.
             *
187 188
             * \copydetails doxygen_hide_matmatmult_warning
             *
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
             * \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);
            
            
212 213 214
            /*!
             * \brief Wrapper over MatMatMatMult, that performs D = A * B * C.
             *
215 216 217
             * If the operation is performed many times with each time the same non zero pattern for the matrices,
             * rather use MatMatMatMultSymbolic/MatMatMatMultNumeric to improve efficiency.
             *
218 219 220 221 222 223 224 225
             * \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.
             * 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.
             *
             * \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__.
             */
226 227 228 229 230 231 232 233 234 235 236
            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
            >
237
            MatMatMatMult(const MatrixT& matrix1,
238 239 240 241
                          const MatrixT& matrix2,
                          const MatrixT& matrix3,
                          MatrixU& matrix4,
                          const char* invoking_file, int invoking_line);
242 243
            
            
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            /*!
             * \brief Wrapper over MatMatMultSymbolic, that performs D = A * B * C.
             *
             * Actual computation is done with a call to NatMatMatMultNumeric, which might be called many times.
             *
             * \param[in] matrix1 A in D = A * B * C.
             * \param[in] matrix2 B in D = A * B * C.
             * \param[in] matrix3 C in D = A * B * C.
             * \param[out] matrix4 D in D = A * B * C. Its pattern is correctly allocated after the call.

             * \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.
             * 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.
             *
             * \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__.
261 262
             *
             * \attention Currently not working due to lack of implementation in Petsc; a ticket has been issued.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
             */
            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
            >
            MatMatMatMultSymbolic(const MatrixT& matrix1,
                                  const MatrixT& matrix2,
                                  const MatrixT& matrix3,
                                  MatrixU& matrix4,
                                  const char* invoking_file, int invoking_line);
            
            
            /*!
             * \brief Wrapper over MatMatMatMultNumeric, that performs D = A * B * C in the case MatMatMatMultSymbolic
             * has been used to allocate structure of D.
             *
286 287
             * \copydetails doxygen_hide_matmatmult_warning
             *
288 289 290 291 292 293 294 295 296 297 298 299
             * \param[in] matrix1 A in D = A * B * C.
             * \param[in] matrix2 B in D = A * B * C.
             * \param[in] matrix3 C in D = A * B * C.
             * \param[out] matrix4 D in D = A * B * C.
             *
             * \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.
             * 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.
             *
             * \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__.
300 301
             *
             * \attention Currently not working due to lack of implementation in Petsc; a ticket has been issued.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
             */
            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
            >
            MatMatMatMultNumeric(const MatrixT& matrix1,
                                 const MatrixT& matrix2,
                                 const MatrixT& matrix3,
                                 MatrixU& matrix4,
                                 const char* invoking_file, int invoking_line);
            
            
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
            /*!
             * \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);
            
343
            
344 345 346
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A^T * B.
             *
347 348
             * \copydetails doxygen_hide_matmatmult_warning
             *
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
             * \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.
             *
             * \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__.
             */
            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,
                                const char* invoking_file, int invoking_line);
            
            
            /*!
             * \brief Performs Matrix-Matrix Multiplication C = A * B^T.
             *
378 379
             * \copydetails doxygen_hide_matmatmult_warning
             *
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
             * \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.
             *
             * \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,
                                const char* invoking_file, int invoking_line);
            
            
            
407 408 409 410 411 412 413 414 415 416 417 418 419
        } //namespace Petsc
        
        
    } //namespace Wrappers
    
    
} // namespace HappyHeart


# include "ThirdParty/Wrappers/Petsc/Matrix/MatrixOperations.hxx"


#endif // HAPPY_HEART_x_THIRD_PARTY_x_WRAPPERS_x_PETSC_x_MATRIX_x_MATRIX_OPERATIONS_HPP_