From 7dbb295d118af45b432b1bd5d534728224555cc6 Mon Sep 17 00:00:00 2001 From: Aditya Kashi Date: Thu, 1 Apr 2021 10:49:00 +0200 Subject: [PATCH] reduced nesting of loops as demanded by Sonarcloud --- core/matrix/fbcsr.cpp | 68 +++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/core/matrix/fbcsr.cpp b/core/matrix/fbcsr.cpp index 9a26c674694..0ea4d3e8b2b 100644 --- a/core/matrix/fbcsr.cpp +++ b/core/matrix/fbcsr.cpp @@ -336,43 +336,47 @@ void Fbcsr::read(const mat_data &data) blocks.size() * bs * bs, bs); tmp->row_ptrs_.get_data()[0] = 0; - if (data.nonzeros.size() > 0) { - index_type cur_brow = 0; - index_type cur_bnz = 0; - index_type cur_bcol = blocks.begin()->first.block_column; - const index_type num_brows = detail::get_num_blocks(bs, data.size[0]); - - acc::range> values( - std::array{blocks.size(), static_cast(bs), - static_cast(bs)}, - tmp->values_.get_data()); - - for (auto it = blocks.begin(); it != blocks.end(); it++) { - GKO_ENSURE_IN_BOUNDS(cur_brow, num_brows); - - tmp->col_idxs_.get_data()[cur_bnz] = it->first.block_column; - for (int ibr = 0; ibr < bs; ibr++) { - for (int jbr = 0; jbr < bs; jbr++) { - values(cur_bnz, ibr, jbr) = it->second(ibr, jbr); - } - } - if (it->first.block_row > cur_brow) { - tmp->row_ptrs_.get_data()[++cur_brow] = cur_bnz; - } else { - assert(cur_brow == it->first.block_row); - assert(cur_bcol <= it->first.block_column); - } - cur_bcol = it->first.block_column; - cur_bnz++; - } + if (data.nonzeros.size() == 0) { + tmp->move_to(this); + return; + } + + index_type cur_brow = 0; + index_type cur_bnz = 0; + index_type cur_bcol = blocks.begin()->first.block_column; + const index_type num_brows = detail::get_num_blocks(bs, data.size[0]); - tmp->row_ptrs_.get_data()[++cur_brow] = - static_cast(blocks.size()); + acc::range> values( + std::array{blocks.size(), static_cast(bs), + static_cast(bs)}, + tmp->values_.get_data()); - assert(cur_brow == tmp->get_size()[0] / bs); + for (auto it = blocks.begin(); it != blocks.end(); it++) { + GKO_ENSURE_IN_BOUNDS(cur_brow, num_brows); + + tmp->col_idxs_.get_data()[cur_bnz] = it->first.block_column; + for (int ibr = 0; ibr < bs; ibr++) { + for (int jbr = 0; jbr < bs; jbr++) { + values(cur_bnz, ibr, jbr) = it->second(ibr, jbr); + } + } + if (it->first.block_row > cur_brow) { + tmp->row_ptrs_.get_data()[++cur_brow] = cur_bnz; + } else { + assert(cur_brow == it->first.block_row); + assert(cur_bcol <= it->first.block_column); + } + + cur_bcol = it->first.block_column; + cur_bnz++; } + tmp->row_ptrs_.get_data()[++cur_brow] = + static_cast(blocks.size()); + + assert(cur_brow == tmp->get_size()[0] / bs); + tmp->move_to(this); }