Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
SimpleCSRMatrixMultT.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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#pragma once
9
10#include <arccore/collections/Array2.h>
11
12#include <alien/handlers/scalar/CSRModifierViewT.h>
13
14/*---------------------------------------------------------------------------*/
15
16namespace Alien::SimpleCSRInternal
17{
18
19/*---------------------------------------------------------------------------*/
20
21template <typename ValueT>
23: m_matrix_impl(matrix)
24{}
25
26/*---------------------------------------------------------------------------*/
27
28template <typename ValueT>
29void SimpleCSRMatrixMultT<ValueT>::synchronize(VectorType& x) const
30{
31 if (m_matrix_impl.block()) {
32 if (m_matrix_impl.m_is_parallel)
33 _synchronizeBlock(x);
34 }
35 else if (m_matrix_impl.vblock()) {
36 if (m_matrix_impl.m_is_parallel)
37 _synchronizeVariableBlock(x);
38 }
39 else {
40 if (m_matrix_impl.m_is_parallel)
41 _synchronize(x);
42 }
43}
44
45
46
47
48template <typename ValueT>
49void SimpleCSRMatrixMultT<ValueT>::mult(const VectorType& x, VectorType& y) const
50{
51 if (m_matrix_impl.block()) {
52 if (m_matrix_impl.m_is_parallel)
53 _parallelMultBlock(x, y);
54 else
55 _seqMultBlock(x, y);
56 }
57 else if (m_matrix_impl.vblock()) {
58 if (m_matrix_impl.m_is_parallel)
59 _parallelMultVariableBlock(x, y);
60 else
61 _seqMultVariableBlock(x, y);
62 }
63 else {
64 if (m_matrix_impl.m_is_parallel)
65 _parallelMult(x, y);
66 else
67 _seqMult(x, y);
68 }
69}
70
71template <typename ValueT>
72void SimpleCSRMatrixMultT<ValueT>::addLMult(Real alpha, const VectorType& x, VectorType& y) const
73{
74 if (m_matrix_impl.m_is_parallel)
75 lowerUpperSynchronize(x,const_cast<VectorType&>(x)) ;
76
77 _seqAddLMult(alpha, x, y);
78
79 if (m_matrix_impl.m_is_parallel)
80 _endDistAddLMult(alpha, x, y);
81}
82
83template <typename ValueT>
84void SimpleCSRMatrixMultT<ValueT>::addUMult(Real alpha, const VectorType& x, VectorType& y) const
85{
86 if (m_matrix_impl.m_is_parallel)
87 upperLowerSynchronize(x,const_cast<VectorType&>(x)) ;
88
89 _seqAddUMult(alpha, x, y);
90
91 if (m_matrix_impl.m_is_parallel)
92 _endDistAddUMult(alpha, x, y);
93}
94
95template <typename ValueT>
96void SimpleCSRMatrixMultT<ValueT>::mult(const UniqueArray<Real>& x, UniqueArray<Real>& y) const
97{
98 if (m_matrix_impl.m_is_parallel)
99 _parallelMult(x, y);
100 else
101 _seqMult(x, y);
102}
103
104
105template <typename ValueT>
106void SimpleCSRMatrixMultT<ValueT>::lowerUpperSynchronize(const VectorType& x_impl, VectorType& y_impl) const
107{
108 Real* x_ptr = const_cast<Real*>(x_impl.getDataPtr());
109 Real* y_ptr = y_impl.getDataPtr();
110
111 const Integer block_size = m_matrix_impl.blockSize();
112
113 SendRecvOp<Real> op(x_ptr,
114 m_matrix_impl.m_matrix_dist_info.m_send_info,
115 m_matrix_impl.m_send_policy,
116 y_ptr,
117 m_matrix_impl.m_matrix_dist_info.m_recv_info,
118 m_matrix_impl.m_recv_policy,
119 m_matrix_impl.m_parallel_mng,
120 m_matrix_impl.m_trace,
121 block_size);
122 op.lowerRecv();
123 op.upperSend();
124}
125
126
127
128template <typename ValueT>
129void SimpleCSRMatrixMultT<ValueT>::upperLowerSynchronize(const VectorType& x_impl, VectorType& y_impl) const
130{
131 Real* x_ptr = const_cast<Real*>(x_impl.getDataPtr());
132 Real* y_ptr = y_impl.getDataPtr();
133 const Integer block_size = m_matrix_impl.blockSize();
134
135 SendRecvOp<Real> op(x_ptr,
136 m_matrix_impl.m_matrix_dist_info.m_send_info,
137 m_matrix_impl.m_send_policy,
138 y_ptr,
139 m_matrix_impl.m_matrix_dist_info.m_recv_info,
140 m_matrix_impl.m_recv_policy,
141 m_matrix_impl.m_parallel_mng,
142 m_matrix_impl.m_trace,
143 block_size);
144 op.upperRecv();
145 op.lowerSend();
146}
147/*---------------------------------------------------------------------------*/
148
149template <typename ValueT>
150void SimpleCSRMatrixMultT<ValueT>::_synchronize(VectorType& x_impl) const
151{
152 Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size;
153 x_impl.resize(alloc_size);
154 Real* x_ptr = (Real*)x_impl.getDataPtr();
155 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
156 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
157 ConstArrayView<Integer> row_offset =
158 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
159 SendRecvOp<Real> op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info,
160 m_matrix_impl.m_send_policy, x_ptr, m_matrix_impl.m_matrix_dist_info.m_recv_info,
161 m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace);
162 op.start();
163 op.end();
164}
165
166template <typename ValueT>
167void SimpleCSRMatrixMultT<ValueT>::_parallelMult(
168const VectorType& x_impl, VectorType& y_impl) const
169{
170 Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size;
171 x_impl.resize(alloc_size);
172 Real* y_ptr = y_impl.getDataPtr();
173 Real* x_ptr = (Real*)x_impl.getDataPtr();
174 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
175 // ConstArrayView<Integer> cols2 =
176 // m_matrix_impl.m_matrix.getCSRProfile().getCols();
177 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
178 ConstArrayView<Integer> row_offset =
179 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
180 SendRecvOp<Real> op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info,
181 m_matrix_impl.m_send_policy, x_ptr, m_matrix_impl.m_matrix_dist_info.m_recv_info,
182 m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace);
183 op.start();
184 ConstArrayView<Integer> local_row_size =
185 m_matrix_impl.m_matrix_dist_info.m_local_row_size;
186 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
187 Integer off = row_offset[irow];
188 Integer off2 = off + local_row_size[irow];
189 Real tmpy = 0.;
190 for (Integer j = off; j < off2; ++j) {
191 tmpy += matrix[j] * x_ptr[cols[j]];
192 }
193 y_ptr[irow] = tmpy;
194 }
195 op.end();
196
197 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
198 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
199 for (Integer i = 0; i < interface_nrow; ++i) {
200 Integer irow = row_ids[i];
201 Integer off = row_offset[irow] + local_row_size[irow];
202 Integer off2 = row_offset[irow + 1];
203 Real tmpy = 0.;
204 for (Integer j = off; j < off2; ++j) {
205 tmpy += matrix[j] * x_ptr[cols[j]];
206 }
207 y_ptr[irow] += tmpy;
208 }
209}
210
211template <typename ValueT>
212void SimpleCSRMatrixMultT<ValueT>::_parallelMult(
213const UniqueArray<Real>& x_impl, UniqueArray<Real>& y_impl) const
214{
215 Real* y_ptr = dataPtr(y_impl);
216 Real* x_ptr = (Real*)dataPtr(x_impl);
217 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
218 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
219 ConstArrayView<Integer> row_offset =
220 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
221 SendRecvOp<Real> op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info,
222 m_matrix_impl.m_send_policy, x_ptr, m_matrix_impl.m_matrix_dist_info.m_recv_info,
223 m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace);
224 op.start();
225 ConstArrayView<Integer> local_row_size =
226 m_matrix_impl.m_matrix_dist_info.m_local_row_size;
227 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
228 Integer off = row_offset[irow];
229 Integer off2 = off + local_row_size[irow];
230 Real tmpy = 0.;
231 for (Integer j = off; j < off2; ++j) {
232 tmpy += matrix[j] * x_ptr[cols[j]];
233 }
234 y_ptr[irow] = tmpy;
235 }
236 op.end();
237
238 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
239 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
240 for (Integer i = 0; i < interface_nrow; ++i) {
241 Integer irow = row_ids[i];
242 Integer off = row_offset[irow] + local_row_size[irow];
243 Integer off2 = row_offset[irow + 1];
244 Real tmpy = 0.;
245 for (Integer j = off; j < off2; ++j) {
246 tmpy += matrix[j] * x_ptr[cols[j]];
247 }
248 y_ptr[irow] += tmpy;
249 }
250}
251/*---------------------------------------------------------------------------*/
252
253template <typename ValueT>
254void SimpleCSRMatrixMultT<ValueT>::_seqMult(const VectorType& x_impl, VectorType& y_impl) const
255{
256#ifdef ALIEN_USE_PERF_TIMER
257 typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-SPMV");
258#endif
259 Real* y_ptr = y_impl.getDataPtr();
260 Real* x_ptr = (Real*)x_impl.getDataPtr();
261 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
262 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
263 ConstArrayView<Integer> row_offset =
264 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
265 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
266 Real tmpy = 0.;
267 for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) {
268 tmpy += matrix[j] * x_ptr[cols[j]];
269 }
270 y_ptr[irow] = tmpy;
271 }
272}
273
274template <typename ValueT>
275void SimpleCSRMatrixMultT<ValueT>::_seqAddLMult(Real alpha, const VectorType& x_impl, VectorType& y_impl) const
276{
277#ifdef ALIEN_USE_PERF_TIMER
278 typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-AddLMult");
279#endif
280 Real* y_ptr = y_impl.getDataPtr();
281 Real* x_ptr = (Real*)x_impl.getDataPtr();
282 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
283 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
284 ConstArrayView<Integer> row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
285 auto diag_offset = m_matrix_impl.m_matrix.getCSRProfile().getUpperDiagOffset();
286 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
287 Real tmpy = y_ptr[irow];
288 for (Integer j = row_offset[irow]; j < diag_offset[irow]; ++j) {
289 tmpy += alpha * matrix[j] * x_ptr[cols[j]];
290 }
291 y_ptr[irow] = tmpy;
292 }
293}
294
295template <typename ValueT>
296void SimpleCSRMatrixMultT<ValueT>::_endDistAddLMult(Real alpha,
297 const VectorType& x_impl, VectorType& y_impl) const
298{
299 Real* y_ptr = y_impl.getDataPtr();
300 Real* x_ptr = (Real*)x_impl.getDataPtr();
301 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
302 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
303 ConstArrayView<Integer> row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
304 ConstArrayView<Integer> local_row_size = m_matrix_impl.m_matrix_dist_info.m_local_row_size;
305
306 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
307 Integer first_upper_ghost_index = m_matrix_impl.m_matrix_dist_info.m_first_upper_ghost_index;
308 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
309
310 for (Integer i = 0; i < interface_nrow; ++i) {
311 Integer irow = row_ids[i];
312 Integer off = row_offset[irow] + local_row_size[irow];
313 Integer off2 = row_offset[irow + 1];
314 Real tmpy = 0.;
315 for (Integer j = off; j < off2; ++j) {
316 if(cols[j] < first_upper_ghost_index)
317 tmpy += matrix[j] * x_ptr[cols[j]];
318 }
319 y_ptr[irow] += alpha * tmpy;
320 }
321}
322
323template <typename ValueT>
324void SimpleCSRMatrixMultT<ValueT>::_seqAddUMult(Real alpha, const VectorType& x_impl, VectorType& y_impl) const
325{
326#ifdef ALIEN_USE_PERF_TIMER
327 typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-AddUMult");
328#endif
329 Real* y_ptr = y_impl.getDataPtr();
330 Real* x_ptr = (Real*)x_impl.getDataPtr();
331 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
332 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
333 ConstArrayView<Integer> row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
334 auto diag_offset = m_matrix_impl.m_matrix.getCSRProfile().getUpperDiagOffset();
335 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
336 Real tmpy = y_ptr[irow];
337 for (Integer j = diag_offset[irow] + 1; j < row_offset[irow + 1]; ++j) {
338 tmpy += alpha * matrix[j] * x_ptr[cols[j]];
339 }
340 y_ptr[irow] = tmpy;
341 }
342}
343
344template <typename ValueT>
345void SimpleCSRMatrixMultT<ValueT>::_endDistAddUMult(Real alpha,
346 const VectorType& x_impl, VectorType& y_impl) const
347{
348 Real* y_ptr = y_impl.getDataPtr();
349 Real* x_ptr = (Real*)x_impl.getDataPtr();
350 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
351 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
352 ConstArrayView<Integer> row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
353 ConstArrayView<Integer> local_row_size = m_matrix_impl.m_matrix_dist_info.m_local_row_size;
354 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
355 Integer first_upper_ghost_index = m_matrix_impl.m_matrix_dist_info.m_first_upper_ghost_index;
356 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
357
358 for (Integer i = 0; i < interface_nrow; ++i) {
359 Integer irow = row_ids[i];
360 Integer off = row_offset[irow] + local_row_size[irow];
361 Integer off2 = row_offset[irow + 1];
362 Real tmpy = 0.;
363 for (Integer j = off; j < off2; ++j) {
364 if(cols[j] >= first_upper_ghost_index)
365 tmpy += matrix[j] * x_ptr[cols[j]];
366 }
367 y_ptr[irow] += alpha * tmpy;
368 }
369}
370
371template <typename ValueT>
372void SimpleCSRMatrixMultT<ValueT>::_seqMult(
373const UniqueArray<Real>& x_impl, UniqueArray<Real>& y_impl) const
374{
375 Real* y_ptr = dataPtr(y_impl);
376 Real* x_ptr = (Real*)dataPtr(x_impl);
377 ConstArrayView<Real> matrix = m_matrix_impl.m_matrix.getValues();
378 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
379 ConstArrayView<Integer> row_offset =
380 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
381 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
382 Real tmpy = 0.;
383 for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) {
384 tmpy += matrix[j] * x_ptr[cols[j]];
385 }
386 y_ptr[irow] = tmpy;
387 }
388}
389
390/*---------------------------------------------------------------------------*/
391
392template <typename ValueT>
393void SimpleCSRMatrixMultT<ValueT>::_synchronizeBlock(VectorType& x) const
394{
395 Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size;
396 const Integer block_size = m_matrix_impl.block()->size();
397 x.resize(alloc_size * block_size);
398 ConstArrayView<Real> x_ptr = x.fullValues();
399 Real const* matrix = m_matrix_impl.m_matrix.getDataPtr();
400 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
401 ConstArrayView<Integer> row_offset =
402 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
403 SimpleCSRInternal::SendRecvOp<Real> op(x.getDataPtr(),
404 m_matrix_impl.m_matrix_dist_info.m_send_info, m_matrix_impl.m_send_policy,
405 (Real*)x.getDataPtr(), m_matrix_impl.m_matrix_dist_info.m_recv_info,
406 m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace,
407 block_size);
408 op.start();
409 op.end();
410}
411
412
413
414
415
416template <typename ValueT>
417void SimpleCSRMatrixMultT<ValueT>::_parallelMultBlock(const VectorType& x, VectorType& y) const
418{
419 Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size;
420 const Integer block_size = m_matrix_impl.block()->size();
421 x.resize(alloc_size * block_size);
422 ArrayView<Real> _y = y.fullValues();
423 ConstArrayView<Real> x_ptr = x.fullValues();
424 Real const* matrix = m_matrix_impl.m_matrix.getDataPtr();
425 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
426 // ConstArrayView<Integer> cols2 =
427 // m_matrix_impl.m_matrix.getCSRProfile().getCols();
428 ConstArrayView<Integer> row_offset =
429 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
430 SimpleCSRInternal::SendRecvOp<Real> op(x.getDataPtr(),
431 m_matrix_impl.m_matrix_dist_info.m_send_info, m_matrix_impl.m_send_policy,
432 (Real*)x.getDataPtr(), m_matrix_impl.m_matrix_dist_info.m_recv_info,
433 m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace,
434 block_size);
435 op.start();
436 ConstArrayView<Integer> local_row_size =
437 m_matrix_impl.m_matrix_dist_info.m_local_row_size;
438 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
439 ArrayView<Real> y_ptr = _y.subView(irow * block_size, block_size);
440 Integer off = row_offset[irow];
441 Integer off2 = off + local_row_size[irow];
442 Real const* m = matrix + off * block_size * block_size;
443 for (Integer ieq = 0; ieq < block_size; ++ieq)
444 y_ptr[ieq] = 0.;
445 for (Integer jcol = off; jcol < off2; ++jcol) {
446 ConstArrayView<Real> ptr = x_ptr.subView(cols[jcol] * block_size, block_size);
447 for (Integer ieq = 0; ieq < block_size; ++ieq)
448 for (Integer iu = 0; iu < block_size; ++iu)
449 y_ptr[ieq] += m[iu + block_size * ieq] * ptr[iu];
450 m += block_size * block_size;
451 }
452 // y_ptr += block_size;
453 }
454 op.end();
455
456 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
457 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
458 ArrayView<Real> y_ptr = _y;
459 for (Integer i = 0; i < interface_nrow; ++i) {
460 Integer irow = row_ids[i];
461 ArrayView<Real> yptr = y_ptr.subView(irow * block_size, block_size);
462 Integer off = row_offset[irow] + local_row_size[irow];
463 Integer off2 = row_offset[irow + 1];
464 Real const* m = matrix + off * block_size * block_size;
465 for (Integer jcol = off; jcol < off2; ++jcol) {
466 ConstArrayView<Real> ptr = x_ptr.subView(cols[jcol] * block_size, block_size);
467 for (Integer ieq = 0; ieq < block_size; ++ieq)
468 for (Integer iu = 0; iu < block_size; ++iu)
469 yptr[ieq] += m[iu + block_size * ieq] * ptr[iu];
470 m += block_size * block_size;
471 }
472 }
473}
474
475/*---------------------------------------------------------------------------*/
476
477template <typename ValueT>
478void SimpleCSRMatrixMultT<ValueT>::_seqMultBlock(const VectorType& x, VectorType& y) const
479{
480 Real* y_ptr = y.getDataPtr();
481 Real const* x_ptr = x.getDataPtr();
482 Real const* matrix = m_matrix_impl.m_matrix.getDataPtr();
483 const Integer block_size = m_matrix_impl.block()->size();
484 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
485 ConstArrayView<Integer> row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
486 Real* yptr = y_ptr ;
487 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow)
488 {
489 Integer off = row_offset[irow];
490 Integer off2 = row_offset[irow + 1];
491 Real const* m = matrix + off * block_size * block_size;
492 for (Integer ieq = 0; ieq < block_size; ++ieq)
493 yptr[ieq] = 0.;
494 for (Integer jcol = off; jcol < off2; ++jcol)
495 {
496 Real const* ptr = x_ptr + cols[jcol] * block_size;
497 for (Integer ieq = 0; ieq < block_size; ++ieq)
498 for (Integer iu = 0; iu < block_size; ++iu)
499 yptr[ieq] += m[iu + block_size * ieq] * ptr[iu];
500 m += block_size * block_size;
501 }
502 yptr += block_size;
503 }
504
505 /*
506 for(int il=0;il<m_matrix_impl.m_local_size;++il)
507 {
508 std::cout<<"X["<<il<<"]:";
509 for(int i=0;i<block_size;++i)
510 std::cout<<x_ptr[il*block_size+i]<<",";
511 std::cout<<std::endl;
512 }
513 for(int il=0;il<m_matrix_impl.m_local_size;++il)
514 {
515 std::cout<<"Y["<<il<<"]:";
516 for(int i=0;i<block_size;++i)
517 std::cout<<y_ptr[il*block_size+i]<<",";
518 std::cout<<std::endl;
519 }
520 for(std::size_t irow=0;irow<m_matrix_impl.m_local_size;++irow)
521 {
522 for(int ieq=0;ieq<block_size;++ieq)
523 {
524 std::cout<<"LINE["<<irow<<","<<ieq<<"] : ";
525 ValueType value = 0. ;
526 Integer off = row_offset[irow];
527 Integer off2 = row_offset[irow + 1];
528 Real const* m = matrix + off * block_size * block_size;
529 for (Integer jcol = off; jcol < off2; ++jcol)
530 {
531 Real const* ptr = x_ptr + cols[jcol] * block_size;
532 for (Integer iu = 0; iu < block_size; ++iu)
533 value += m[iu + block_size * ieq] * ptr[iu];
534 m += block_size * block_size;
535 }
536 std::cout<<"\nY_CPU["<<irow<<","<<ieq<<"]:"<<value<<std::endl;
537 }
538 }*/
539}
540
541/*---------------------------------------------------------------------------*/
542template <typename ValueT>
543void SimpleCSRMatrixMultT<ValueT>::_synchronizeVariableBlock(VectorType& x_impl) const
544{
545 // alien_info([&] { cout()<<"_parallelMultVariableBlock";}) ;
546
547 ConstArrayView<Integer> block_sizes = m_matrix_impl.getDistStructInfo().m_block_sizes;
548 ConstArrayView<Integer> block_offsets = m_matrix_impl.getDistStructInfo().m_block_offsets;
549 {
550 const Integer last = block_offsets.size() - 1;
551 x_impl.resize(block_offsets[last]);
552 }
553
554 const ValueT* x_ptr = x_impl.getDataPtr();
555 const ValueT* matrix_ptr = m_matrix_impl.m_matrix.getDataPtr();
556
557 ConstArrayView<Integer> row_offset =
558 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
559 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
560 ConstArrayView<Integer> block_cols =
561 m_matrix_impl.m_matrix.getCSRProfile().getBlockCols();
562
563 SendRecvOp<Real> op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info,
564 m_matrix_impl.m_send_policy, (ValueT*)x_ptr,
565 m_matrix_impl.m_matrix_dist_info.m_recv_info, m_matrix_impl.m_recv_policy,
566 m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace, block_sizes, block_offsets);
567
568 op.start();
569 op.end();
570}
571
572
573
574
575
576template <typename ValueT>
577void SimpleCSRMatrixMultT<ValueT>::_parallelMultVariableBlock(
578const VectorType& x_impl, VectorType& y_impl) const
579{
580 // alien_info([&] { cout()<<"_parallelMultVariableBlock";}) ;
581
582 ArrayView<ValueT> y = y_impl.fullValues();
583
584 ConstArrayView<Integer> block_sizes = m_matrix_impl.getDistStructInfo().m_block_sizes;
585 ConstArrayView<Integer> block_offsets = m_matrix_impl.getDistStructInfo().m_block_offsets;
586 {
587 const Integer last = block_offsets.size() - 1;
588 x_impl.resize(block_offsets[last]);
589 }
590
591 const ValueT* x_ptr = x_impl.getDataPtr();
592 const ValueT* matrix_ptr = m_matrix_impl.m_matrix.getDataPtr();
593
594 ConstArrayView<Integer> row_offset =
595 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
596 ConstArrayView<Integer> cols = m_matrix_impl.getDistStructInfo().m_cols;
597 ConstArrayView<Integer> block_cols =
598 m_matrix_impl.m_matrix.getCSRProfile().getBlockCols();
599
600 SendRecvOp<Real> op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info,
601 m_matrix_impl.m_send_policy, (ValueT*)x_ptr,
602 m_matrix_impl.m_matrix_dist_info.m_recv_info, m_matrix_impl.m_recv_policy,
603 m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace, block_sizes, block_offsets);
604
605 op.start();
606
607 UniqueArray<ValueT> tmpy;
608 tmpy.reserve(m_matrix_impl.vblock()->maxBlockSize());
609
610 ConstArrayView<Integer> local_row_size =
611 m_matrix_impl.m_matrix_dist_info.m_local_row_size;
612 for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) {
613 Integer off = row_offset[irow];
614 Integer off2 = off + local_row_size[irow];
615 const Integer block_size_row = block_sizes[irow];
616 tmpy.resize(block_size_row);
617 tmpy.fill(ValueT());
618 for (Integer j = off; j < off2; ++j) {
619 const Integer col = cols[j];
620 const Integer block_size_col = block_sizes[col];
621 ConstArrayView<ValueT> x(block_size_col, x_ptr + block_offsets[col]);
622 ConstArray2View<ValueT> matrix(
623 matrix_ptr + block_cols[j], block_size_row, block_size_col);
624 for (Integer krow = 0; krow < block_size_row; ++krow) {
625 for (Integer kcol = 0; kcol < block_size_col; ++kcol) {
626 tmpy[krow] += matrix[krow][kcol] * x[kcol];
627 }
628 }
629 }
630 y.subView(block_offsets[irow], block_size_row).copy(tmpy);
631 }
632
633 op.end();
634
635 Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow;
636 ConstArrayView<Integer> row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows;
637 for (Integer i = 0; i < interface_nrow; ++i) {
638 Integer irow = row_ids[i];
639 Integer off = row_offset[irow] + local_row_size[irow];
640 Integer off2 = row_offset[irow + 1];
641 const Integer block_size_row = block_sizes[irow];
642 tmpy.resize(block_size_row);
643 tmpy.fill(ValueT());
644 for (Integer j = off; j < off2; ++j) {
645 const Integer col = cols[j];
646 const Integer block_size_col = block_sizes[col];
647 ConstArrayView<ValueT> x(block_size_col, x_ptr + block_offsets[col]);
648 ConstArray2View<ValueT> matrix(
649 matrix_ptr + block_cols[j], block_size_row, block_size_col);
650 for (Integer krow = 0; krow < block_size_row; ++krow) {
651 for (Integer kcol = 0; kcol < block_size_col; ++kcol) {
652 tmpy[krow] += matrix[krow][kcol] * x[kcol];
653 }
654 }
655 }
656 ArrayView<ValueT> y_view = y.subView(block_offsets[irow], block_size_row);
657 for (Integer k = 0; k < block_size_row; ++k)
658 y_view[k] += tmpy[k];
659 }
660}
661
662/*---------------------------------------------------------------------------*/
663
664template <typename ValueT>
665void SimpleCSRMatrixMultT<ValueT>::_seqMultVariableBlock(
666const VectorType& x_impl, VectorType& y_impl) const
667{
668 // alien_info([&] { cout()<<"_seqMultVariableBlock";}) ;
669
670 ArrayView<ValueT> y = y_impl.fullValues();
671
672 const ValueT* x_ptr = x_impl.getDataPtr();
673 const ValueT* matrix_ptr = m_matrix_impl.m_matrix.getDataPtr();
674
675 ConstArrayView<Integer> row_offset =
676 m_matrix_impl.m_matrix.getCSRProfile().getRowOffset();
677 ConstArrayView<Integer> cols = m_matrix_impl.m_matrix.getCSRProfile().getCols();
678 ConstArrayView<Integer> block_cols =
679 m_matrix_impl.m_matrix.getCSRProfile().getBlockCols();
680 const VBlock* block = m_matrix_impl.vblock();
681 const VBlockImpl& block_infos = x_impl.vblockImpl();
682
683 UniqueArray<ValueT> tmpy;
684 tmpy.reserve(block->maxBlockSize());
685 for (Integer irow = 0; irow < m_matrix_impl.m_local_size;
686 ++irow) // Attention, c'est local !!!!
687 {
688 const Integer block_size_row = block->size(irow);
689 tmpy.resize(block_size_row);
690 tmpy.fill(ValueT());
691 for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) {
692 const Integer col = cols[j];
693 const Integer block_size_col = block->size(col);
694 ConstArrayView<ValueT> x(block_size_col, x_ptr + block_infos.offset(col));
695 ConstArray2View<ValueT> matrix(
696 matrix_ptr + block_cols[j], block_size_row, block_size_col);
697 for (Integer krow = 0; krow < block_size_row; ++krow) {
698 for (Integer kcol = 0; kcol < block_size_col; ++kcol) {
699 tmpy[krow] += matrix[krow][kcol] * x[kcol];
700 }
701 }
702 }
703 y.subView(block_infos.offset(irow), block_size_row).copy(tmpy);
704 }
705}
706
707
708template <typename ValueT>
709void SimpleCSRMatrixMultT<ValueT>::multDiag(VectorType& y) const
710{
711 Real* y_ptr = y.getDataPtr();
712 CSRConstViewT<MatrixType> view(m_matrix_impl);
713 // clang-format off
714 auto nrows = view.nrows() ;
715 auto kcol = view.kcol() ;
716 auto dcol = view.dcol() ;
717 auto cols = view.cols() ;
718 auto values = view.data() ;
719 // clang-format on
720 for (std::size_t irow = 0; irow < nrows; ++irow)
721 y_ptr[irow] = y_ptr[irow] * values[dcol[irow]];
722}
723
724template <typename ValueT>
725void SimpleCSRMatrixMultT<ValueT>::multInvDiag(VectorType& y) const
726{
727 Real* y_ptr = y.getDataPtr();
728 CSRConstViewT<MatrixType> view(m_matrix_impl);
729 // clang-format off
730 auto nrows = view.nrows() ;
731 auto kcol = view.kcol() ;
732 auto dcol = view.dcol() ;
733 auto cols = view.cols() ;
734 auto values = view.data() ;
735 // clang-format on
736 for (std::size_t irow = 0; irow < nrows; ++irow)
737 y_ptr[irow] = y_ptr[irow] / values[dcol[irow]];
738}
739
740template <typename ValueT>
741void SimpleCSRMatrixMultT<ValueT>::computeDiag(VectorType& y) const
742{
743 Real* y_ptr = y.getDataPtr();
744
745 CSRConstViewT<MatrixType> view(m_matrix_impl);
746 // clang-format off
747 auto nrows = view.nrows() ;
748 auto kcol = view.kcol() ;
749 auto dcol = view.dcol() ;
750 auto cols = view.cols() ;
751 auto values = view.data() ;
752 // clang-format on
753 if(m_matrix_impl.blockSize()==1)
754 {
755 for (std::size_t irow = 0; irow < nrows; ++irow)
756 y_ptr[irow] = values[dcol[irow]];
757 }
758 else
759 {
760 Integer block_size = m_matrix_impl.blockSize();
761 Integer block2_size = block_size*block_size ;
762 for (std::size_t irow = 0; irow < nrows; ++irow)
763 {
764 for(Integer ieq=0;ieq<block_size;++ieq)
765 y_ptr[irow*block_size+ieq] = values[dcol[irow]*block2_size+ieq*block_size+ieq];
766 }
767 }
768}
769
770template <typename ValueT>
771void SimpleCSRMatrixMultT<ValueT>::computeInvDiag(VectorType& y) const
772{
773 Real* y_ptr = y.getDataPtr();
774
775 CSRConstViewT<MatrixType> view(m_matrix_impl);
776 // clang-format off
777 auto nrows = view.nrows() ;
778 auto kcol = view.kcol() ;
779 auto dcol = view.dcol() ;
780 auto cols = view.cols() ;
781 auto values = view.data() ;
782 // clang-format on
783 if(m_matrix_impl.blockSize()==1)
784 {
785 for (std::size_t irow = 0; irow < nrows; ++irow)
786 y_ptr[irow] = 1. / values[dcol[irow]];
787 }
788 else
789 {
790 Integer block_size = m_matrix_impl.blockSize();
791 Integer block2_size = block_size*block_size ;
792 for (std::size_t irow = 0; irow < nrows; ++irow)
793 {
794 for(Integer ieq=0;ieq<block_size;++ieq)
795 {
796 y_ptr[irow*block_size+ieq] = 1. / values[dcol[irow]*block2_size+ieq*block_size+ieq];
797 }
798 }
799 /*
800 auto kcol = view.kcol() ;
801 for(int il=0;il<nrows;++il)
802 {
803 std::cout<<"MAT["<<il<<","<<il<<"]:";
804 for(int k=kcol[il];k<kcol[il+1];++k)
805 {
806 std::cout<<"("<<cols[k]<<",";
807 for(int ieq=0;ieq<block_size;++ieq)
808 for(int ju=0;ju<block_size;++ju)
809 std::cout<<values[k*block2_size+ieq*block_size +ju]<<",";
810 std::cout<<")"<<std::endl;
811 }
812 }
813 for(int il=0;il<nrows;++il)
814 {
815 std::cout<<"DIAG["<<il<<"]:";
816 for(int i=0;i<block_size;++i)
817 std::cout<<y_ptr[il*block_size+i]<<",";
818 std::cout<<std::endl;
819 std::cout<<"MAT["<<il<<","<<il<<"]:";
820 auto mat_offset = kcol[il]*block2_size ;
821 for(int ieq=0;ieq<block_size;++ieq)
822 for(int ju=0;ju<block_size;++ju)
823 std::cout<<values[mat_offset+ieq*block_size +ju]<<",";
824 std::cout<<std::endl;
825 }*/
826 }
827}
828
829/*---------------------------------------------------------------------------*/
830
831} // namespace Alien::SimpleCSRInternal
832
833/*---------------------------------------------------------------------------*/
SimpleCSRMatrixMultT(const MatrixType &matrix)
Constructeur de la classe.
void mult(const VectorType &x, VectorType &y) const
Matrix vector product.