Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
MatrixProfilerT.h
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#pragma once
8
9#include <vector>
10
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15#include <alien/kernels/simple_csr/CSRStructInfo.h>
16#include <alien/kernels/simple_csr/SimpleCSRInternal.h>
17#include <alien/kernels/simple_csr/SimpleCSRMatrix.h>
18
19#include <alien/utils/ArrayUtils.h>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Alien
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Common
31{
32
33 /*---------------------------------------------------------------------------*/
34 /*---------------------------------------------------------------------------*/
35
37 template <typename ValueT, typename MatrixImplT>
39 : m_matrix(matrix)
40 {
41 m_matrix_impl = &m_matrix.impl()->template get<typename MatrixImplType::TagType>(false);
43 const ISpace& space = m_matrix_impl->rowSpace();
44
45 if (space != m_matrix_impl->colSpace())
46 m_square_matrix = false;
47
48 const MatrixDistribution& dist = m_matrix_impl->distribution();
49 m_parallel_mng = dist.parallelMng();
50
51 if (m_parallel_mng == nullptr) {
52 m_nproc = 1;
53 }
54 else {
55 m_nproc = m_parallel_mng->commSize();
56 }
57
58 m_local_size = dist.localRowSize();
59 m_global_size = dist.globalRowSize();
60 m_local_offset = dist.rowOffset();
61 if (!m_square_matrix) {
62 m_col_local_size = dist.localColSize();
63 m_col_global_size = dist.globalColSize();
64 m_col_local_offset = dist.colOffset();
65 }
66
67 m_def_matrix.resize(m_local_size);
68 }
69
70 /*---------------------------------------------------------------------------*/
71
72 template <typename ValueT, typename MatrixImplT>
73 MatrixProfilerT<ValueT,MatrixImplT>::~MatrixProfilerT()
74 {
75 if (!m_allocated) {
76 allocate();
77 }
78 }
79
80 /*---------------------------------------------------------------------------*/
81
82 template <typename ValueT, typename MatrixImplT>
83 void MatrixProfilerT<ValueT,MatrixImplT>::addMatrixEntry(Integer iIndex, Integer jIndex)
84 {
85 const Integer local_row = iIndex - m_local_offset;
86 // ALIEN_ASSERT((local_row >= 0 and local_row < m_local_size),("Cannot manage not
87 // prepared row"));
88
89 std::vector<Integer>& row_def = m_def_matrix[local_row];
90 const Integer row_def_size = static_cast<Integer>(row_def.size());
91 if (row_def_size == 0)
92 row_def.push_back(jIndex);
93 else {
95 jIndex, ConstArrayView<Integer>(row_def_size, &row_def[0]));
96 if (pos >= row_def_size)
97 row_def.push_back(jIndex);
98 else if (row_def[pos] != jIndex) {
99 row_def.insert(row_def.begin() + pos, jIndex);
100 }
101 }
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 template <typename ValueT, typename MatrixImplT>
107 void MatrixProfilerT<ValueT,MatrixImplT>::allocate()
108 {
109 if (m_allocated)
110 return;
111 _startTimer();
112 computeProfile();
113 m_matrix_impl->updateTimestamp();
114 m_allocated = true;
115 _stopTimer();
116 }
117
118 /*---------------------------------------------------------------------------*/
119
120 template <typename ValueT, typename MatrixImplT>
121 void MatrixProfilerT<ValueT,MatrixImplT>::computeProfile()
122 {
123 UniqueArray<Integer> m_offset;
124 m_offset.resize(m_nproc + 1);
125 if (m_parallel_mng) {
126 Arccore::MessagePassing::mpAllGather(m_parallel_mng,
127 ConstArrayView<Integer>(1, &m_local_offset), m_offset.subView(0, m_nproc));
128 }
129 m_offset[m_nproc] = m_global_size;
130
131 auto& profile = m_matrix_impl->getCSRProfile();
132 profile.init(m_local_size);
133
134 {
135 ArrayView<Integer> row_offsets = profile.getRowOffset();
136 Integer offset = 0;
137 for (Integer i = 0; i < m_local_size; ++i) {
138 row_offsets[i] = offset;
139 offset += static_cast<Integer>(m_def_matrix[i].size());
140 }
141 row_offsets[m_local_size] = offset;
142 }
143
144 profile.allocate();
145 {
146 ArrayView<Integer> cols = profile.getCols();
147
148 for (Integer i = 0, pos = 0; i < m_local_size; ++i) {
149 const VectorDefinition& vdef = m_def_matrix[i];
150 for (VectorDefinition::const_iterator iterJ = vdef.begin(); iterJ != vdef.end();
151 ++iterJ)
152 cols[pos++] = *iterJ;
153 }
154 }
155
156 if (m_matrix_impl->vblock()) {
157 const VBlock* block_sizes = m_matrix_impl->vblock();
158 auto& block_row_offset = profile.getBlockRowOffset();
159 auto& block_cols = profile.getBlockCols();
160 auto kcol = profile.kcol();
161 auto cols = profile.cols();
162 Integer offset = 0;
163 for (Integer irow = 0; irow < m_local_size; ++irow) {
164 block_row_offset[irow] = offset;
165 auto row_blk_size = block_sizes->size(m_local_offset + irow);
166 for (auto k = kcol[irow]; k < kcol[irow + 1]; ++k) {
167 block_cols[k] = offset;
168 auto jcol = cols[k];
169 auto col_blk_size = block_sizes->size(jcol);
170 offset += row_blk_size * col_blk_size;
171 }
172 }
173 block_row_offset[m_local_size] = offset;
174 block_cols[kcol[m_local_size]] = offset;
175 }
176
177 m_matrix_impl->allocate();
178 m_matrix_impl->internal()->setValues(0) ;
179 //ArrayView<ValueT> values = m_matrix_impl->internal()->getValues();
180 //values.fill(0);
181
182 if (m_nproc > 1)
183 m_matrix_impl->parallelStart(m_offset, m_parallel_mng, true);
184 else
185 m_matrix_impl->sequentialStart();
186
187 profile.getColOrdering() = SimpleCSRInternal::CSRStructInfo::eFull;
188
189 profile.setTimestamp(m_matrix_impl->timestamp() + 1);
190 }
191
192 /*---------------------------------------------------------------------------*/
193 /*---------------------------------------------------------------------------*/
194
195} // namespace Common
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200} // namespace Alien
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
MultiMatrixImpl.h.
Integer m_local_offset
Global matrix informations.
MatrixProfilerT(IMatrix &matrix)
Scalar matrix builder.
Interface for all matrices.
Definition IMatrix.h:51
Interface for algebraic space objects.
Definition ISpace.h:44
Computes a matrix distribution.
Arccore::MessagePassing::IMessagePassingMng * parallelMng() const
Get the parallel manager.
Integer dichotomicPositionScan(const T &x, ConstArrayView< T > v)
Definition ArrayUtils.h:183
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17