13#include <alien/kernels/simple_csr/CSRStructInfo.h>
14#include <alien/kernels/simple_csr/SimpleCSRMatrix.h>
15#include <alien/move/handlers/scalar/DirectMatrixBuilder.h>
23using namespace Arccore;
29SubMatrix::Extract(
const IMatrix& matrix,
const ExtractionIndices& indices)
31 if (indices.rowRange() != -1 || indices.colRange() != -1)
32 return SubMatrix::extractRange(matrix, indices);
34 return SubMatrix::extractIndices(matrix, indices);
40SubMatrix::extractRange(
const IMatrix& matrix,
const ExtractionIndices& indices)
43 const SimpleCSRMatrix<Real>* matrix_impl =
44 &matrix.
impl()->
get<BackEnd::tag::simplecsr>();
46 const auto& dist = matrix_impl->distribution();
48 const Integer startingRow = indices.rowStart();
49 const Integer rowRange = indices.rowRange();
50 const Integer startingCol = indices.colStart() != -1 ? indices.colStart() : 0;
51 const Integer colRange =
52 indices.colRange() != -1 ? indices.colRange() : dist.globalRowSize();
54 auto* parallel_mng = dist.parallelMng();
56 const Integer offset = dist.rowOffset();
57 const Integer matrixStart = offset;
58 const Integer matrixEnd = dist.localRowSize() + offset;
60 const Integer subMatrixStart = startingRow;
61 const Integer subMatrixEnd = startingRow + rowRange;
63 const Integer subMatrixLocalSize = std::max(
64 std::min(subMatrixEnd, matrixEnd) - std::max(subMatrixStart, matrixStart), 0);
65 Space subMatrixRowSpace(rowRange);
66 Space subMatrixColSpace(colRange);
67 MatrixDistribution subMatrixDistribution(
68 rowRange, subMatrixLocalSize, rowRange, parallel_mng);
69 MatrixData subMatrix(subMatrixDistribution);
70 DirectMatrixBuilder builder(std::move(subMatrix), DirectMatrixOptions::eResetValues,
71 DirectMatrixOptions::eUnSymmetric);
72 const Integer nnzMatrix = matrix_impl->internal()->getValues().size();
73 const Integer nrowsMatrix = matrix_impl->distribution().localRowSize();
74 const Integer averageEntriesByRow = nnzMatrix / nrowsMatrix;
75 builder.reserve(averageEntriesByRow);
78 const SimpleCSRInternal::CSRStructInfo& matrixProfile = matrix_impl->getCSRProfile();
80 ConstArrayView<Integer> rowsOffset = matrixProfile.getRowOffset();
82 ConstArrayView<Integer> cols = matrixProfile.getCols();
84 ConstArrayView<Real> values = matrix_impl->internal()->getValues();
87 (startingRow >= 0 && startingRow < matrix_impl->distribution().globalRowSize()),
88 (
"Error, submatrix and matrix dimensions are incompatibles"));
89 ALIEN_ASSERT((startingRow + rowRange <= matrix_impl->distribution().globalRowSize()),
90 (
"Error, submatrix and matrix dimensions are incompatibles"));
92 for (Integer i = 0; i < nrowsMatrix; ++i) {
93 const Integer local_row = i;
94 const Integer global_row = i + offset;
95 if (global_row < startingRow || global_row >= startingRow + rowRange)
98 for (Integer j = rowsOffset[local_row]; j < rowsOffset[local_row + 1]; ++j) {
99 const Integer global_col = cols[j];
100 if (global_col < startingCol || global_col >= startingCol + colRange)
103 builder.setData(indices.toLocalRow(global_row), indices.toLocalCol(global_col),
109 return builder.release();
115SubMatrix::extractIndices(
const IMatrix& matrix ALIEN_UNUSED_PARAM,
116 const ExtractionIndices& indices ALIEN_UNUSED_PARAM)
Algebraic Matrix with internal multi-representation object.
const AlgebraTraits< tag >::matrix_type & get() const
Get a specific matrix implementation.
MultiMatrixImpl * impl()
Get the multimatrix implementation.