Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
Extraction.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8
9#include "Extraction.h"
10
11#include <algorithm>
12
13#include <alien/kernels/simple_csr/CSRStructInfo.h>
14#include <alien/kernels/simple_csr/SimpleCSRMatrix.h>
15#include <alien/move/handlers/scalar/DirectMatrixBuilder.h>
16
17/*---------------------------------------------------------------------------*/
18/*---------------------------------------------------------------------------*/
19
20namespace Alien::Move
21{
22
23using namespace Arccore;
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
29SubMatrix::Extract(const IMatrix& matrix, const ExtractionIndices& indices)
30{
31 if (indices.rowRange() != -1 || indices.colRange() != -1)
32 return SubMatrix::extractRange(matrix, indices);
33 else
34 return SubMatrix::extractIndices(matrix, indices);
35}
36
37/*---------------------------------------------------------------------------*/
38
40SubMatrix::extractRange(const IMatrix& matrix, const ExtractionIndices& indices)
41{
42
43 const SimpleCSRMatrix<Real>* matrix_impl =
44 &matrix.impl()->get<BackEnd::tag::simplecsr>();
45
46 const auto& dist = matrix_impl->distribution();
47
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();
53
54 auto* parallel_mng = dist.parallelMng();
55
56 const Integer offset = dist.rowOffset();
57 const Integer matrixStart = offset;
58 const Integer matrixEnd = dist.localRowSize() + offset;
59
60 const Integer subMatrixStart = startingRow;
61 const Integer subMatrixEnd = startingRow + rowRange;
62
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);
76 builder.allocate();
77
78 const SimpleCSRInternal::CSRStructInfo& matrixProfile = matrix_impl->getCSRProfile();
79 // UniqueArray<Integer>& rowsOffset = matrixProfile.getRowOffset();
80 ConstArrayView<Integer> rowsOffset = matrixProfile.getRowOffset();
81 // UniqueArray<Integer>& cols = matrixProfile.getCols();
82 ConstArrayView<Integer> cols = matrixProfile.getCols();
83 // UniqueArray<Real>& values = matrix_impl->internal()->getValues();
84 ConstArrayView<Real> values = matrix_impl->internal()->getValues();
85
86 ALIEN_ASSERT(
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"));
91
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)
96 continue;
97
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)
101 continue;
102
103 builder.setData(indices.toLocalRow(global_row), indices.toLocalCol(global_col),
104 values[global_col]);
105 }
106 }
107
108 builder.finalize();
109 return builder.release();
110}
111
112/*---------------------------------------------------------------------------*/
113
115SubMatrix::extractIndices(const IMatrix& matrix ALIEN_UNUSED_PARAM,
116 const ExtractionIndices& indices ALIEN_UNUSED_PARAM)
117{
118 return MatrixData();
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124} // namespace Alien::Move
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
Algebraic Matrix with internal multi-representation object.
Definition MatrixData.h:40
const AlgebraTraits< tag >::matrix_type & get() const
Get a specific matrix implementation.
MultiMatrixImpl * impl()
Get the multimatrix implementation.