Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SimpleCSRMatrix.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#pragma once
8
11
15#include <alien/data/ISpace.h>
16#include <alien/kernels/simple_csr/CSRStructInfo.h>
17#include <alien/kernels/simple_csr/DistStructInfo.h>
18#include <alien/kernels/simple_csr/SendRecvOp.h>
19#include <alien/kernels/simple_csr/SimpleCSRBackEnd.h>
20#include <alien/kernels/simple_csr/SimpleCSRInternal.h>
21#include <alien/kernels/simple_csr/SimpleCSRPrecomp.h>
22
23#include <alien/utils/StdTimer.h>
24/*---------------------------------------------------------------------------*/
25
26namespace Alien::SimpleCSRInternal
27{
28
29template <typename ValueT>
31
32}
33
34/*---------------------------------------------------------------------------*/
35
36namespace Alien
37{
38
39/*---------------------------------------------------------------------------*/
40
41template <typename ValueT>
43{
44 public:
45 // clang-format off
46 static const bool on_host_only = true ;
47 typedef BackEnd::tag::simplecsr TagType ;
48 typedef ValueT ValueType;
49 typedef SimpleCSRInternal::CSRStructInfo CSRStructInfo;
50 typedef SimpleCSRInternal::CSRStructInfo ProfileType;
51 typedef SimpleCSRInternal::DistStructInfo DistStructInfo;
53 typedef typename ProfileType::IndexType IndexType ;
54 typedef Alien::StdTimer TimerType ;
55 typedef TimerType::Sentry SentryType ;
56 // clang-format on
57
58 public:
61 : IMatrixImpl(nullptr, AlgebraTraits<BackEnd::tag::simplecsr>::name())
62 , m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
63 , m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
64 {}
65
68 : IMatrixImpl(multi_impl, AlgebraTraits<BackEnd::tag::simplecsr>::name())
69 , m_matrix(multi_impl ? multi_impl->vblock() != nullptr : false)
70 , m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
71 , m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
72 {}
73
76 {
77#ifdef ALIEN_USE_PERF_TIMER
78 m_timer.printInfo("SimpleCSR-MATRIX");
79#endif
80 }
81
82 void setTraceMng(ITraceMng* trace_mng) { m_trace = trace_mng; }
83
84 public:
85 void free()
86 { /* TODO */
87 }
88 void freeData()
89 { /* TODO */
90 }
91 void clear() {}
92
93 void allocate()
94 {
95 if (block()) {
96 const Integer size = block()->size();
97 m_matrix.getValues().resize((getCSRProfile().getNnz() + 1) * size * size);
98 }
99 else if (vblock()) {
100 m_matrix.getValues().resize(getCSRProfile().getBlockNnz() + 1);
101 }
102 else {
103 m_matrix.getValues().resize(getCSRProfile().getNnz() + 1);
104 }
105 }
106
107 void scal(ValueType const* values)
108 {
109 m_matrix.scal(values) ;
110 }
111
112 CSRStructInfo& getCSRProfile() { return m_matrix.getCSRProfile(); }
113
114 const CSRStructInfo& getCSRProfile() const { return m_matrix.getCSRProfile(); }
115
116 const CSRStructInfo& getProfile() const { return m_matrix.getCSRProfile(); }
117
118 const DistStructInfo& getDistStructInfo() const { return m_matrix_dist_info; }
119
120 SimpleCSRInternal::CommProperty::ePolicyType getSendPolicy() const
121 {
122 return m_send_policy;
123 }
124
125 SimpleCSRInternal::CommProperty::ePolicyType getRecvPolicy() const
126 {
127 return m_recv_policy;
128 }
129
130 ValueType* getAddressData() { return m_matrix.getDataPtr(); }
131 ValueType* data() { return m_matrix.getDataPtr(); }
132
133 ValueType const* getAddressData() const { return m_matrix.getDataPtr(); }
134 ValueType const* data() const { return m_matrix.getDataPtr(); }
135
136 MatrixInternal* internal() { return &m_matrix; }
137
138 MatrixInternal const* internal() const { return &m_matrix; }
139
140 bool isParallel() const { return m_is_parallel; }
141
142 Integer getLocalSize() const { return m_local_size; }
143
144 Integer getLocalOffset() const { return m_local_offset; }
145
146 Integer getGlobalSize() const { return m_global_size; }
147
148 Integer getGhostSize() const { return m_ghost_size; }
149
150 Integer getAllocSize() const
151 {
152 auto total_size = m_local_size + m_ghost_size;
153 if (block())
154 return total_size * block()->size();
155 else if (vblock()) {
156 return m_matrix_dist_info.m_block_offsets[total_size];
157 }
158 else
159 return total_size;
160 }
161
162 Integer blockSize() const
163 {
164 if (block())
165 {
166 return block()->size();
167 }
168 else if (vblock()) {
169 return 1 ;
170 }
171 else {
172 return m_own_block_size ;
173 }
174 }
175
176 void setBlockSize(Integer block_size)
177 {
178 if(this->m_multi_impl)
179 const_cast<MultiMatrixImpl*>(this->m_multi_impl)->setBlockInfos(block_size) ;
180 else
181 m_own_block_size = block_size ;
182 }
183
184 const VectorDistribution& rowDistribution() const
185 {
186 if(m_local_row_distribution.get())
187 return *m_local_row_distribution ;
188 else
189 return this->distribution().rowDistribution();
190 }
191
192 IMessagePassingMng* getParallelMng()
193 {
194 return m_parallel_mng;
195 }
196
197 void sequentialStart()
198 {
199 m_local_offset = 0;
200 m_local_size = getCSRProfile().getNRows();
201 m_global_size = m_local_size;
202 m_myrank = 0;
203 m_nproc = 1;
204 m_is_parallel = false;
205 m_matrix_dist_info.m_local_row_size.resize(m_local_size);
206 auto& profile = internal()->getCSRProfile();
207 ConstArrayView<Integer> offset = profile.getRowOffset();
208 for (Integer i = 0; i < m_local_size; ++i)
209 m_matrix_dist_info.m_local_row_size[i] = offset[i + 1] - offset[i];
210 }
211
212 void parallelStart(ConstArrayView<Integer> offset, IMessagePassingMng* parallel_mng,
213 bool need_sort_ghost_col = false)
214 {
215 m_local_size = getCSRProfile().getNRows();
216 m_parallel_mng = parallel_mng;
217 // m_trace = parallel_mng->traceMng();
218 if (m_parallel_mng == NULL) {
219 m_local_offset = 0;
220 m_global_size = m_local_size;
221 m_myrank = 0;
222 m_nproc = 1;
223 m_is_parallel = false;
224 }
225 else {
226 m_myrank = m_parallel_mng->commRank();
227 m_nproc = m_parallel_mng->commSize();
228 m_local_offset = offset[m_myrank];
229 m_global_size = offset[m_nproc];
230 m_is_parallel = (m_nproc > 1);
231 }
232 if (m_is_parallel) {
233 if (need_sort_ghost_col)
234 sortGhostCols(offset);
235 if (block()) {
236 m_matrix_dist_info.compute(
237 m_nproc, offset, m_myrank, m_parallel_mng, getCSRProfile(), m_trace);
238 }
239 else if (vblock()) {
240 m_matrix_dist_info.compute(m_nproc, offset, m_myrank, m_parallel_mng,
241 getCSRProfile(), vblock(), distribution(), m_trace);
242 }
243 else {
244 m_matrix_dist_info.compute(
245 m_nproc, offset, m_myrank, m_parallel_mng, getCSRProfile(), m_trace);
246 }
247 m_ghost_size = m_matrix_dist_info.m_ghost_nrow;
248 }
249 }
250
251 void sortGhostCols(ConstArrayView<Integer> offset)
252 {
253 IsLocal isLocal(offset, m_myrank);
254 UniqueArray<ValueType>& values = m_matrix.getValues();
255 ProfileType& profile = m_matrix.getCSRProfile();
256 UniqueArray<Integer>& cols = profile.getCols();
257 ConstArrayView<Integer> kcol = profile.getRowOffset();
258 Integer next = 0;
259 UniqueArray<Integer> gcols;
260 UniqueArray<ValueType> gvalues;
261 for (Integer irow = 0; irow < m_local_size; ++irow) {
262 bool need_sort = false;
263 Integer first = next;
264 next = kcol[irow + 1];
265 Integer row_size = next - first;
266 for (Integer k = first; k < next; ++k) {
267 if (!isLocal(cols[k])) {
268 need_sort = true;
269 break;
270 }
271 }
272 if (need_sort) {
273 gvalues.resize(row_size);
274 gcols.resize(row_size);
275 Integer local_count = 0;
276 Integer ghost_count = 0;
277 for (Integer k = first; k < next; ++k) {
278 Integer col = cols[k];
279 if (isLocal(col)) {
280 cols[first + local_count] = col;
281 values[first + local_count] = values[k];
282 ++local_count;
283 }
284 else {
285 gcols[ghost_count] = col;
286 gvalues[ghost_count] = values[k];
287 ++ghost_count;
288 }
289 }
290 for (Integer k = 0; k < ghost_count; ++k) {
291 cols[first + local_count] = gcols[k];
292 values[first + local_count] = gvalues[k];
293 ++local_count;
294 }
295 }
296 }
297 }
298
299 /*
300 DistStructInfo m_matrix_dist_info;
301 */
302
303 virtual SimpleCSRMatrix* cloneTo(const MultiMatrixImpl* multi) const
304 {
305 SimpleCSRMatrix* matrix = new SimpleCSRMatrix(multi);
306 matrix->m_is_parallel = m_is_parallel;
307 matrix->m_local_size = m_local_size;
308 matrix->m_local_offset = m_local_offset;
309 matrix->m_global_size = m_global_size;
310 matrix->m_ghost_size = m_ghost_size;
311 matrix->m_send_policy = m_send_policy;
312 matrix->m_recv_policy = m_recv_policy;
313 matrix->m_nproc = m_nproc;
314 matrix->m_myrank = m_myrank;
315 matrix->m_parallel_mng = m_parallel_mng;
316 matrix->m_trace = m_trace;
317 matrix->setBlockSize(blockSize()) ;
318 matrix->m_matrix.copy(m_matrix);
319 matrix->m_matrix_dist_info.copy(m_matrix_dist_info);
320 return matrix;
321 }
322
323 void copy(SimpleCSRMatrix const& matrix)
324 {
325 m_is_parallel = matrix.m_is_parallel;
326 m_local_size = matrix.m_local_size;
327 m_local_offset = matrix.m_local_offset;
328 m_global_size = matrix.m_global_size;
329 m_ghost_size = matrix.m_ghost_size;
330 m_send_policy = matrix.m_send_policy;
331 m_recv_policy = matrix.m_recv_policy;
332 m_nproc = matrix.m_nproc;
333 m_myrank = matrix.m_myrank;
334 m_parallel_mng = matrix.m_parallel_mng;
335 m_trace = matrix.m_trace;
336 if(blockSize()==matrix.blockSize())
337 m_matrix.copy(matrix.m_matrix);
338 else
339 {
340 auto nb_blocks = matrix.getCSRProfile().getNnz() + 1 ;
341 m_matrix.copy(matrix.m_matrix,blockSize(),matrix.blockSize(),nb_blocks) ;
342 }
343 m_matrix_dist_info.copy(matrix.m_matrix_dist_info);
344 }
345
346 void copy(SimpleCSRMatrix const& matrix,
347 ConstArrayView<Integer> filter,
348 UniqueArray<Integer>& parent_lids)
349 {
350 m_parallel_mng = distribution().parallelMng();
351 if(m_parallel_mng)
352 {
353 m_nproc = m_parallel_mng->commSize();
354 m_myrank = m_parallel_mng->commRank();
355 }
356 else
357 {
358 m_nproc = 1;
359 m_myrank = 0;
360 }
361
362 m_send_policy = matrix.m_send_policy;
363 m_recv_policy = matrix.m_recv_policy;
364 m_trace = matrix.m_trace;
365
366 m_local_size = std::accumulate(filter.begin(), filter.end(), 0);
367 UniqueArray<Integer> new_offset(m_nproc + 1);
368 new_offset[0] = 0;
369 if(m_nproc>1)
370 {
371 m_is_parallel = true;
372 Arccore::MessagePassing::mpAllGather(m_parallel_mng,
373 ConstArrayView<Integer>(1,&m_local_size),
374 ArrayView<Integer>(m_nproc,&new_offset[1]));
375 for (Integer i = 1; i <= m_nproc; ++i)
376 new_offset[i] += new_offset[i - 1];
377 }
378 else
379 {
380 new_offset[1] = m_local_size;
381 m_is_parallel = false;
382 }
383
384 m_local_offset = new_offset[m_myrank];
385 m_global_size = new_offset[m_nproc];
386 m_local_row_distribution.reset(new VectorDistribution(m_global_size,m_local_size,m_parallel_mng));
387 if(m_is_parallel)
388 {
389 UniqueArray<Integer> all_filter(matrix.m_local_size + matrix.m_ghost_size);
390 std::copy(filter.begin(),filter.end(),all_filter.begin());
391 using SendRecvOp = SimpleCSRInternal::SendRecvOp<Integer> ;
392 auto op = SendRecvOp(all_filter.unguardedBasePointer(),
393 matrix.m_matrix_dist_info.m_send_info,
394 matrix.m_send_policy,
395 all_filter.unguardedBasePointer(),
396 matrix.m_matrix_dist_info.m_recv_info,
397 matrix.m_recv_policy,
398 m_parallel_mng,
399 nullptr);
400 op.start();
401 op.end();
402
403 m_ghost_size = std::accumulate(all_filter.begin()+matrix.m_local_size, all_filter.end(), 0);
404
405
406 parent_lids.resize(m_local_size+m_ghost_size) ;
407 UniqueArray<Integer> old2new_uid(matrix.m_local_size + matrix.m_ghost_size);
408 old2new_uid.fill(-1) ;
409 Integer irow = 0 ;
410 for (Integer i = 0; i < matrix.m_local_size; ++i)
411 {
412 if (all_filter[i] == 1)
413 {
414 parent_lids[irow] = i ;
415 old2new_uid[i] = m_local_offset + irow ;
416 ++irow;
417 }
418 }
419 assert(irow==m_local_size) ;
420 {
421 using SendRecvOp = SimpleCSRInternal::SendRecvOp<Integer> ;
422 auto op = SendRecvOp( old2new_uid.unguardedBasePointer(),
423 matrix.m_matrix_dist_info.m_send_info,
424 matrix.m_send_policy,
425 old2new_uid.unguardedBasePointer(),
426 matrix.m_matrix_dist_info.m_recv_info,
427 matrix.m_recv_policy,
428 m_parallel_mng,
429 nullptr);
430 op.start();
431 op.end();
432 }
433
434 for (Integer i = matrix.m_local_size; i < matrix.m_local_size + matrix.m_ghost_size; ++i)
435 {
436 if (all_filter[i] == 1)
437 {
438 parent_lids[irow] = i ;
439 ++irow;
440 }
441 }
442 assert(irow==m_local_size+m_ghost_size) ;
443
444 m_matrix.computeSubMatrix(parent_lids,
445 old2new_uid,
446 matrix.m_local_offset,
447 matrix.m_local_size,
448 matrix.m_matrix.m_values,
449 matrix.getCSRProfile().getRowOffset(),
450 matrix.m_matrix_dist_info.m_cols,
451 matrix.m_matrix_dist_info.m_local_row_size,
452 all_filter,
453 new_offset,
454 m_myrank);
455 }
456 else
457 {
458 parent_lids.resize(m_local_size) ;
459 UniqueArray<Integer> old2new_uid(matrix.m_local_size);
460 old2new_uid.fill(-1) ;
461 Integer irow = 0 ;
462 for (Integer i = 0; i < matrix.m_local_size; ++i)
463 {
464 if (filter[i] == 1)
465 {
466 parent_lids[irow] = i ;
467 old2new_uid[i] = irow ;
468 ++irow;
469 }
470 }
471 assert(irow==m_local_size) ;
472 m_matrix.computeSubMatrix(parent_lids,
473 old2new_uid,
474 matrix.m_local_size,
475 matrix.m_matrix.m_values,
476 matrix.getCSRProfile().getRowOffset(),
477 matrix.getCSRProfile().getCols(),
478 matrix.m_local_offset,
479 filter,
480 new_offset,
481 m_myrank);
482 }
483
484 m_matrix_dist_info.compute(m_nproc,new_offset,m_myrank,m_parallel_mng,*m_matrix.m_profile);
485 }
486
487 void copyProfile(SimpleCSRMatrix const& matrix)
488 {
489 m_is_parallel = matrix.m_is_parallel;
490 m_local_size = matrix.m_local_size;
491 m_local_offset = matrix.m_local_offset;
492 m_global_size = matrix.m_global_size;
493 m_ghost_size = matrix.m_ghost_size;
494 m_send_policy = matrix.m_send_policy;
495 m_recv_policy = matrix.m_recv_policy;
496 m_nproc = matrix.m_nproc;
497 m_myrank = matrix.m_myrank;
498 m_parallel_mng = matrix.m_parallel_mng;
499 m_trace = matrix.m_trace;
500 m_matrix.getCSRProfile().copy(matrix.m_matrix.getCSRProfile());
501 m_matrix_dist_info.copy(matrix.m_matrix_dist_info);
502 if (vblock()) {
503 auto& profile = m_matrix.getCSRProfile();
504 const VBlock* block_sizes = vblock();
505 auto& block_row_offset = profile.getBlockRowOffset();
506 auto& block_cols = profile.getBlockCols();
507 auto kcol = profile.kcol();
508 auto cols = profile.cols();
509 Integer offset = 0;
510 for (Integer irow = 0; irow < m_local_size; ++irow) {
511 block_row_offset[irow] = offset;
512 auto row_blk_size = block_sizes->size(m_local_offset + irow);
513 for (auto k = kcol[irow]; k < kcol[irow + 1]; ++k) {
514 block_cols[k] = offset;
515 auto jcol = cols[k];
516 auto col_blk_size = block_sizes->size(jcol);
517 offset += row_blk_size * col_blk_size;
518 }
519 }
520 block_row_offset[m_local_size] = offset;
521 block_cols[kcol[m_local_size]] = offset;
522
523 const Integer total_size = m_local_size + m_ghost_size;
524
525 m_matrix_dist_info.m_block_sizes.resize(total_size);
526 m_matrix_dist_info.m_block_offsets.resize(total_size + 1);
527
528 offset = 0;
529 for (Integer i = 0; i < m_local_size; ++i) {
530 auto blk_size = block_sizes->size(m_local_offset + i);
531 m_matrix_dist_info.m_block_sizes[i] = blk_size;
532 m_matrix_dist_info.m_block_offsets[i] = offset;
533 offset += blk_size;
534 }
535 for (Integer i = m_local_size; i < total_size; ++i) {
536 auto blk_size = block_sizes->size(m_matrix_dist_info.m_recv_info.m_uids[i - m_local_size]);
537 m_matrix_dist_info.m_block_sizes[i] = blk_size;
538 m_matrix_dist_info.m_block_offsets[i] = offset;
539 offset += blk_size;
540 }
541 m_matrix_dist_info.m_block_offsets[total_size] = offset;
542 }
543 }
544
545 void notifyChanges()
546 {
547 m_matrix.notifyChanges();
548 }
549
550 void endUpdate()
551 {
552 if (m_matrix.needUpdate()) {
553 m_matrix.endUpdate();
554 this->updateTimestamp();
555 }
556 }
557
558 private:
559 class IsLocal
560 {
561 public:
562 IsLocal(const ConstArrayView<Integer> offset, const Integer myrank)
563 : m_offset(offset)
564 , m_myrank(myrank)
565 {}
566 bool operator()(Arccore::Integer col) const
567 {
568 return (col >= m_offset[m_myrank]) && (col < m_offset[m_myrank + 1]);
569 }
570
571 private:
572 const ConstArrayView<Integer> m_offset;
573 const Integer m_myrank;
574 };
575
576 MatrixInternal m_matrix;
577 bool m_is_parallel = 0;
578 Integer m_local_size = 0;
579 Integer m_local_offset = 0;
580 Integer m_global_size = 0;
581 Integer m_ghost_size = 0;
582 DistStructInfo m_matrix_dist_info;
583 SimpleCSRInternal::CommProperty::ePolicyType m_send_policy;
584 SimpleCSRInternal::CommProperty::ePolicyType m_recv_policy;
585 IMessagePassingMng* m_parallel_mng = nullptr;
586 Integer m_own_block_size = 1;
587 std::unique_ptr<VectorDistribution> m_local_row_distribution;
588 Integer m_nproc = 1;
589 Integer m_myrank = 0;
590 ITraceMng* m_trace = nullptr;
591
592 friend class SimpleCSRInternal::SimpleCSRMatrixMultT<ValueType>;
593
594 private:
595 mutable TimerType m_timer;
596
597 public:
598 TimerType& timer() const
599 {
600 return m_timer;
601 }
602};
603
604/*---------------------------------------------------------------------------*/
605
606} // namespace Alien
607
608/*---------------------------------------------------------------------------*/
Block.h.
IMatrixImpl.h.
ISpace.h.
MultiMatrixImpl.h.
VBlockOffsets.h.
VBlock.h.
Arccore::Integer size() const
Get square block size.
Definition Block.cc:90
virtual const VBlock * vblock() const
Get block datas of the matrix.
virtual const MatrixDistribution & distribution() const
Get the distribution of the matrix.
const MultiMatrixImpl * m_multi_impl
Pointer on matrices implementation.
virtual const Block * block() const
Get block datas of the matrix.
IMatrixImpl(const MultiMatrixImpl *multi_impl, BackEndId backend="")
Constructor.
const VectorDistribution & rowDistribution() const
Get the row distribution.
Arccore::MessagePassing::IMessagePassingMng * parallelMng() const
Get the parallel manager.
Multi matrices representation container.
SimpleCSRMatrix(const MultiMatrixImpl *multi_impl)
void clear()
Wipe out internal data.
void updateTimestamp()
Met à jour le timestamp.
Definition Timestamp.cc:50
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17