46 static const bool on_host_only = true ;
48 typedef ValueT ValueType;
53 typedef typename ProfileType::IndexType IndexType ;
62 , m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
63 , m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
69 , m_matrix(multi_impl ? multi_impl->
vblock() != nullptr : false)
70 , m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
71 , m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
77#ifdef ALIEN_USE_PERF_TIMER
78 m_timer.printInfo(
"SimpleCSR-MATRIX");
82 void setTraceMng(ITraceMng* trace_mng) { m_trace = trace_mng; }
97 m_matrix.getValues().resize((getCSRProfile().getNnz() + 1) * size * size);
100 m_matrix.getValues().resize(getCSRProfile().getBlockNnz() + 1);
103 m_matrix.getValues().resize(getCSRProfile().getNnz() + 1);
107 void scal(ValueType
const* values)
109 m_matrix.scal(values) ;
112 CSRStructInfo& getCSRProfile() {
return m_matrix.getCSRProfile(); }
114 const CSRStructInfo& getCSRProfile()
const {
return m_matrix.getCSRProfile(); }
116 const CSRStructInfo& getProfile()
const {
return m_matrix.getCSRProfile(); }
118 const DistStructInfo& getDistStructInfo()
const {
return m_matrix_dist_info; }
120 SimpleCSRInternal::CommProperty::ePolicyType getSendPolicy()
const
122 return m_send_policy;
125 SimpleCSRInternal::CommProperty::ePolicyType getRecvPolicy()
const
127 return m_recv_policy;
130 ValueType* getAddressData() {
return m_matrix.getDataPtr(); }
131 ValueType* data() {
return m_matrix.getDataPtr(); }
133 ValueType
const* getAddressData()
const {
return m_matrix.getDataPtr(); }
134 ValueType
const* data()
const {
return m_matrix.getDataPtr(); }
136 MatrixInternal* internal() {
return &m_matrix; }
138 MatrixInternal
const* internal()
const {
return &m_matrix; }
140 bool isParallel()
const {
return m_is_parallel; }
142 Integer getLocalSize()
const {
return m_local_size; }
144 Integer getLocalOffset()
const {
return m_local_offset; }
146 Integer getGlobalSize()
const {
return m_global_size; }
148 Integer getGhostSize()
const {
return m_ghost_size; }
150 Integer getAllocSize()
const
152 auto total_size = m_local_size + m_ghost_size;
156 return m_matrix_dist_info.m_block_offsets[total_size];
162 Integer blockSize()
const
172 return m_own_block_size ;
176 void setBlockSize(Integer block_size)
179 const_cast<MultiMatrixImpl*
>(this->
m_multi_impl)->setBlockInfos(block_size) ;
181 m_own_block_size = block_size ;
184 const VectorDistribution& rowDistribution()
const
186 if(m_local_row_distribution.get())
187 return *m_local_row_distribution ;
192 IMessagePassingMng* getParallelMng()
194 return m_parallel_mng;
197 void sequentialStart()
200 m_local_size = getCSRProfile().getNRows();
201 m_global_size = m_local_size;
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];
212 void parallelStart(ConstArrayView<Integer> offset, IMessagePassingMng* parallel_mng,
213 bool need_sort_ghost_col =
false)
215 m_local_size = getCSRProfile().getNRows();
216 m_parallel_mng = parallel_mng;
218 if (m_parallel_mng == NULL) {
220 m_global_size = m_local_size;
223 m_is_parallel =
false;
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);
233 if (need_sort_ghost_col)
234 sortGhostCols(offset);
236 m_matrix_dist_info.compute(
237 m_nproc, offset, m_myrank, m_parallel_mng, getCSRProfile(), m_trace);
240 m_matrix_dist_info.compute(m_nproc, offset, m_myrank, m_parallel_mng,
244 m_matrix_dist_info.compute(
245 m_nproc, offset, m_myrank, m_parallel_mng, getCSRProfile(), m_trace);
247 m_ghost_size = m_matrix_dist_info.m_ghost_nrow;
251 void sortGhostCols(ConstArrayView<Integer> offset)
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();
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])) {
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];
280 cols[first + local_count] = col;
281 values[first + local_count] = values[k];
285 gcols[ghost_count] = col;
286 gvalues[ghost_count] = values[k];
290 for (Integer k = 0; k < ghost_count; ++k) {
291 cols[first + local_count] = gcols[k];
292 values[first + local_count] = gvalues[k];
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);
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);
340 auto nb_blocks = matrix.getCSRProfile().getNnz() + 1 ;
341 m_matrix.copy(matrix.m_matrix,blockSize(),matrix.blockSize(),nb_blocks) ;
343 m_matrix_dist_info.copy(matrix.m_matrix_dist_info);
347 ConstArrayView<Integer> filter,
348 UniqueArray<Integer>& parent_lids)
353 m_nproc = m_parallel_mng->commSize();
354 m_myrank = m_parallel_mng->commRank();
362 m_send_policy = matrix.m_send_policy;
363 m_recv_policy = matrix.m_recv_policy;
364 m_trace = matrix.m_trace;
366 m_local_size = std::accumulate(filter.begin(), filter.end(), 0);
367 UniqueArray<Integer> new_offset(m_nproc + 1);
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];
380 new_offset[1] = m_local_size;
381 m_is_parallel =
false;
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));
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,
403 m_ghost_size = std::accumulate(all_filter.begin()+matrix.m_local_size, all_filter.end(), 0);
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) ;
410 for (Integer i = 0; i < matrix.m_local_size; ++i)
412 if (all_filter[i] == 1)
414 parent_lids[irow] = i ;
415 old2new_uid[i] = m_local_offset + irow ;
419 assert(irow==m_local_size) ;
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,
434 for (Integer i = matrix.m_local_size; i < matrix.m_local_size + matrix.m_ghost_size; ++i)
436 if (all_filter[i] == 1)
438 parent_lids[irow] = i ;
442 assert(irow==m_local_size+m_ghost_size) ;
444 m_matrix.computeSubMatrix(parent_lids,
446 matrix.m_local_offset,
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,
458 parent_lids.resize(m_local_size) ;
459 UniqueArray<Integer> old2new_uid(matrix.m_local_size);
460 old2new_uid.fill(-1) ;
462 for (Integer i = 0; i < matrix.m_local_size; ++i)
466 parent_lids[irow] = i ;
467 old2new_uid[i] = irow ;
471 assert(irow==m_local_size) ;
472 m_matrix.computeSubMatrix(parent_lids,
475 matrix.m_matrix.m_values,
476 matrix.getCSRProfile().getRowOffset(),
477 matrix.getCSRProfile().getCols(),
478 matrix.m_local_offset,
484 m_matrix_dist_info.compute(m_nproc,new_offset,m_myrank,m_parallel_mng,*m_matrix.m_profile);
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);
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();
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;
516 auto col_blk_size = block_sizes->size(jcol);
517 offset += row_blk_size * col_blk_size;
520 block_row_offset[m_local_size] = offset;
521 block_cols[kcol[m_local_size]] = offset;
523 const Integer total_size = m_local_size + m_ghost_size;
525 m_matrix_dist_info.m_block_sizes.resize(total_size);
526 m_matrix_dist_info.m_block_offsets.resize(total_size + 1);
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;
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;
541 m_matrix_dist_info.m_block_offsets[total_size] = offset;
547 m_matrix.notifyChanges();
552 if (m_matrix.needUpdate()) {
553 m_matrix.endUpdate();
562 IsLocal(
const ConstArrayView<Integer> offset,
const Integer myrank)
566 bool operator()(Arccore::Integer col)
const
568 return (col >= m_offset[m_myrank]) && (col < m_offset[m_myrank + 1]);
572 const ConstArrayView<Integer> m_offset;
573 const Integer m_myrank;
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;
589 Integer m_myrank = 0;
590 ITraceMng* m_trace =
nullptr;
592 friend class SimpleCSRInternal::SimpleCSRMatrixMultT<ValueType>;
595 mutable TimerType m_timer;
598 TimerType& timer()
const