-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conversion to a fixed sparsity pattern. (#147)
- Loading branch information
1 parent
e7d98ed
commit f972047
Showing
15 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef MATRIXCONVERSIONMODULE_ch | ||
#define MATRIXCONVERSIONMODULE_ch | ||
|
||
void SnapMatrixToSparsityPattern_wrp(int *ih_matA, const int *ih_matB); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "MatrixConversion.h" | ||
#include "PSMatrix.h" | ||
using namespace NTPoly; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
extern "C" { | ||
#include "MatrixConversion_c.h" | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
namespace NTPoly { | ||
////////////////////////////////////////////////////////////////////////////// | ||
void MatrixConversion::SnapMatrixToSparsityPattern(Matrix_ps& mata, | ||
const Matrix_ps& matb) { | ||
SnapMatrixToSparsityPattern_wrp(mata.ih_this, matb.ih_this); | ||
} | ||
} // namespace NTPoly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef MATRIXCONVERSION_h | ||
#define MATRIXCONVERSION_h | ||
|
||
#include "Wrapper.h" | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
namespace NTPoly { | ||
class Matrix_ps; | ||
class MatrixConversion { | ||
public: | ||
//! Some codes use a fixed sparsity pattern for a matrix instead of filtering | ||
//! small values. Using this routine, the matrix is filled to have the same | ||
//! pattern as the second matrix argument. Zeros of the sparsity pattern are | ||
//! left in, whereas values outside the sparsity are removed. This can | ||
//! faciliate conversion between formats. | ||
//! \param mata the matrix to modify. | ||
//! \param matb the matrix which defines the sparsity pattern. | ||
static void SnapMatrixToSparsityPattern(Matrix_ps& mata, | ||
const Matrix_ps& matb); | ||
}; | ||
} // namespace NTPoly | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!> This module contains helper routines for converting an NTPoly matrix | ||
!> to data structures used in other programs. | ||
MODULE MatrixConversionModule | ||
USE DataTypesModule, ONLY : NTREAL | ||
USE MatrixMapsModule, ONLY : MapMatrix_psr | ||
USE PSMatrixModule, ONLY : Matrix_ps, ConvertMatrixToReal, CopyMatrix, & | ||
& DestructMatrix | ||
USE PSMatrixAlgebraModule, ONLY : PairwiseMultiplyMatrix, ScaleMatrix, & | ||
& IncrementMatrix | ||
IMPLICIT NONE | ||
PRIVATE | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
PUBLIC :: SnapMatrixToSparsityPattern | ||
CONTAINS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!> Some codes use a fixed sparsity pattern for a matrix instead of filtering | ||
!> small values. Using this routine, the matrix is filled to have the same | ||
!> pattern as the second matrix argument. Zeros of the sparsity pattern are | ||
!> left in, whereas values outside the sparsity are removed. This can | ||
!> faciliate conversion between formats. | ||
SUBROUTINE SnapMatrixToSparsityPattern(mat, pattern) | ||
!> The matrix to modify. | ||
TYPE(Matrix_ps), INTENT(INOUT) :: mat | ||
!> The matrix which defines the sparsity pattern. | ||
TYPE(Matrix_ps), INTENT(IN) :: pattern | ||
!! Local Variables | ||
TYPE(Matrix_ps) :: filtered | ||
TYPE(Matrix_ps) :: pattern_1s | ||
TYPE(Matrix_ps) :: pattern_0s | ||
TYPE(Matrix_ps) :: pattern_real | ||
INTEGER :: II | ||
|
||
!! First we need to make sure that the sparsity pattern is all 1s. | ||
IF (pattern%is_complex) THEN | ||
CALL ConvertMatrixToReal(pattern, pattern_real) | ||
ELSE | ||
CALL CopyMatrix(pattern, pattern_real) | ||
END IF | ||
CALL MapMatrix_psr(pattern_real, pattern_1s, SetMatrixToOne) | ||
|
||
!! Then all zeros | ||
CALL CopyMatrix(pattern_1s, pattern_0s) | ||
CALL ScaleMatrix(pattern_0s, 0.0_NTREAL) | ||
|
||
!! Here we add in the zero values that were missing from the original | ||
!! matrix. The secret here is that if you use a negative threshold, we | ||
!! never filter a value. | ||
CALL IncrementMatrix(pattern_0s, mat, threshold_in=-1.0_NTREAL) | ||
|
||
!! Next, we zero out values outside of the sparsity pattern. | ||
CALL CopyMatrix(mat, filtered) | ||
CALL PairwiseMultiplyMatrix(pattern_1s, filtered, mat) | ||
|
||
!! Cleanup | ||
CALL DestructMatrix(pattern_1s) | ||
CALL DestructMatrix(pattern_0s) | ||
CALL DestructMatrix(pattern_real) | ||
CALL DestructMatrix(filtered) | ||
|
||
END SUBROUTINE SnapMatrixToSparsityPattern | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
FUNCTION SetMatrixToOne(row, column, val) RESULT(valid) | ||
INTEGER, INTENT(INOUT), OPTIONAL :: row | ||
INTEGER, INTENT(INOUT), OPTIONAL :: column | ||
REAL(NTREAL), INTENT(INOUT), OPTIONAL :: val | ||
LOGICAL :: valid | ||
|
||
val = 1.0_NTREAL | ||
valid = .TRUE. | ||
END FUNCTION SetMatrixToOne | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
END MODULE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!> This module contains helper routines for converting an NTPoly matrix | ||
!> to data structures used in other programs. | ||
MODULE MatrixConversionModule_wrp | ||
USE MatrixConversionModule | ||
USE PSMatrixModule_wrp, ONLY : Matrix_ps_wrp | ||
USE WrapperModule, ONLY : SIZE_wrp | ||
USE ISO_C_BINDING, ONLY : C_INT | ||
IMPLICIT NONE | ||
PRIVATE | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
PUBLIC :: SnapMatrixToSparsityPattern_wrp | ||
CONTAINS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!> Some codes use a fixed sparsity pattern for a matrix instead of filtering | ||
!> small values. Using this routine, the matrix is filled to have the same | ||
!> pattern as the second matrix argument. Zeros of the sparsity pattern are | ||
!> left in, whereas values outside the sparsity are removed. This can | ||
!> faciliate conversion between formats. | ||
SUBROUTINE SnapMatrixToSparsityPattern_wrp(ih_matA, ih_matB) & | ||
& BIND(C,NAME="SnapMatrixToSparsityPattern_wrp") | ||
INTEGER(KIND=C_INT), INTENT(INOUT) :: ih_matA(SIZE_wrp) | ||
INTEGER(KIND=C_INT), INTENT(IN) :: ih_matB(SIZE_wrp) | ||
TYPE(Matrix_ps_wrp) :: h_matA | ||
TYPE(Matrix_ps_wrp) :: h_matB | ||
|
||
h_matA = TRANSFER(ih_matA,h_matA) | ||
h_matB = TRANSFER(ih_matB,h_matB) | ||
CALL SnapMatrixToSparsityPattern(h_matA%data, h_matB%data) | ||
END SUBROUTINE SnapMatrixToSparsityPattern_wrp | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
END MODULE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters