Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
matrix_asserts.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2023 W. Liu *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
47 #ifndef MUI_MATRIX_ASSERTS_H_
48 #define MUI_MATRIX_ASSERTS_H_
49 
50 #include <cassert>
51 
52 namespace mui {
53 namespace linalg {
54 
55 // **************************************************
56 // ********** Protected member functions ************
57 // **************************************************
58 
59 // Member function to assert the matrix vector sizes
60 template<typename ITYPE, typename VTYPE>
61 void sparse_matrix<ITYPE,VTYPE>::assert_valid_vector_size(const std::string &file_name_input, const std::string &function_name_input) const {
62 
63  std::string file_name;
64  std::string function_name;
65 
66  if (file_name_input.empty()) {
67  file_name.assign("matrix_asserts.h");
68  } else {
69  file_name = file_name_input;
70  }
71 
72  if (function_name_input.empty()) {
73  function_name.assign("assert_valid_vector_size()");
74  } else {
75  function_name = function_name_input;
76  }
77 
78  if (matrix_format_ == format::COO) {
79 
80  if (nnz_ < 0) {
81  std::cerr << "MUI Error [" << file_name << "]: The number of non-zeros (nnz_=" << nnz_ << ") should be non-negative integer in " << function_name << std::endl;
82  std::abort();
83  }
84 
85  if (rows_ < 0) {
86  std::cerr << "MUI Error [" << file_name << "]: The number of rows (rows_=" << rows_ << ") should be non-negative integer in " << function_name << std::endl;
87  std::abort();
88  }
89 
90  if (cols_ < 0) {
91  std::cerr << "MUI Error [" << file_name << "]: The number of columns (cols_=" << cols_ << ") should be non-negative integer in " << function_name << std::endl;
92  std::abort();
93  }
94 
95  if ((rows_*cols_) < nnz_) {
96  std::cerr << "MUI Warning [" << file_name << "]: Matrix size (" << (rows_*cols_) << ") smaller than the number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << ". Possible duplicated elements occur. " << std::endl;
97  }
98 
99  if (static_cast<ITYPE>(matrix_coo.values_.size()) != nnz_) {
100  std::cerr << "MUI Error [" << file_name << "]: COO values_ matrix size (" << matrix_coo.values_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
101  std::abort();
102  }
103 
104  if (static_cast<ITYPE>(matrix_coo.row_indices_.size()) != nnz_) {
105  std::cerr << "MUI Error [" << file_name << "]: COO row_indices_ matrix size (" << matrix_coo.row_indices_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
106  std::abort();
107  }
108 
109  if (static_cast<ITYPE>(matrix_coo.col_indices_.size()) != nnz_) {
110  std::cerr << "MUI Error [" << file_name << "]: COO col_indices_ matrix size (" << matrix_coo.col_indices_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
111  std::abort();
112  }
113 
114  if (!matrix_csr.values_.empty()) {
115  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR values_ matrix (" << matrix_csr.values_.size() << ") under COO matrix format in " << function_name << std::endl;
116  }
117 
118  if (!matrix_csr.row_ptrs_.empty()) {
119  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR row_ptrs_ matrix (" << matrix_csr.row_ptrs_.size() << ") under COO matrix format in " << function_name << std::endl;
120  }
121 
122  if (!matrix_csr.col_indices_.empty()) {
123  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR col_indices_ matrix (" << matrix_csr.col_indices_.size() << ") under COO matrix format in " << function_name << std::endl;
124  }
125 
126  if (!matrix_csc.values_.empty()) {
127  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC values_ matrix (" << matrix_csc.values_.size() << ") under COO matrix format in " << function_name << std::endl;
128  }
129 
130  if (!matrix_csc.row_indices_.empty()) {
131  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC row_ptrs_ matrix (" << matrix_csc.row_indices_.size() << ") under COO matrix format in " << function_name << std::endl;
132  }
133 
134  if (!matrix_csc.col_ptrs_.empty()) {
135  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC col_indices_ matrix (" << matrix_csc.col_ptrs_.size() << ") under COO matrix format in " << function_name << std::endl;
136  }
137 
138  } else if (matrix_format_ == format::CSR) {
139 
140  if ((rows_*cols_) < nnz_) {
141  std::cerr << "MUI Warning [" << file_name << "]: Matrix size (" << (rows_*cols_) << ") smaller than the number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << ". Possible duplicated elements occur. " << std::endl;
142  }
143 
144  if (!matrix_coo.values_.empty()) {
145  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO values_ matrix (" << matrix_coo.values_.size() << ") under CSR matrix format in " << function_name << std::endl;
146  }
147 
148  if (!matrix_coo.row_indices_.empty()) {
149  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO row_indices_ matrix (" << matrix_coo.row_indices_.size() << ") under CSR matrix format in " << function_name << std::endl;
150  }
151 
152  if (!matrix_coo.col_indices_.empty()) {
153  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO col_indices_ matrix (" << matrix_coo.col_indices_.size() << ") under CSR matrix format in " << function_name << std::endl;
154  }
155 
156  if (static_cast<ITYPE>(matrix_csr.values_.size()) != nnz_) {
157  std::cerr << "MUI Error [" << file_name << "]: CSR values_ matrix size (" << matrix_csr.values_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
158  std::abort();
159  }
160 
161  if (static_cast<ITYPE>(matrix_csr.row_ptrs_.size()) != (rows_+1)) {
162  std::cerr << "MUI Error [" << file_name << "]: CSR row_ptrs_ matrix size (" << matrix_csr.row_ptrs_.size() << ") does not equals to number of rows + 1 (rows_+1=" << (rows_+1) << ") in " << function_name << std::endl;
163  std::abort();
164  }
165 
166  if (static_cast<ITYPE>(matrix_csr.col_indices_.size()) != nnz_) {
167  std::cerr << "MUI Error [" << file_name << "]: CSR col_indices_ matrix size (" << matrix_csr.col_indices_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
168  std::abort();
169  }
170 
171  if (!matrix_csc.values_.empty()) {
172  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC values_ matrix (" << matrix_csc.values_.size() << ") under CSR matrix format in " << function_name << std::endl;
173  }
174 
175  if (!matrix_csc.row_indices_.empty()) {
176  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC row_ptrs_ matrix (" << matrix_csc.row_indices_.size() << ") under CSR matrix format in " << function_name << std::endl;
177  }
178 
179  if (!matrix_csc.col_ptrs_.empty()) {
180  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSC col_indices_ matrix (" << matrix_csc.col_ptrs_.size() << ") under CSR matrix format in " << function_name << std::endl;
181  }
182 
183  } else if (matrix_format_ == format::CSC) {
184 
185  if ((rows_*cols_) < nnz_) {
186  std::cerr << "MUI Warning [" << file_name << "]: Matrix size (" << (rows_*cols_) << ") smaller than the number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << ". Possible duplicated elements occur. " << std::endl;
187  }
188 
189  if (!matrix_coo.values_.empty()) {
190  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO values_ matrix (" << matrix_coo.values_.size() << ") under CSC matrix format in " << function_name << std::endl;
191  }
192 
193  if (!matrix_coo.row_indices_.empty()) {
194  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO row_indices_ matrix (" << matrix_coo.row_indices_.size() << ") under CSC matrix format in " << function_name << std::endl;
195  }
196 
197  if (!matrix_coo.col_indices_.empty()) {
198  std::cerr << "MUI Warning [" << file_name << "]: Non-empty COO col_indices_ matrix (" << matrix_coo.col_indices_.size() << ") under CSC matrix format in " << function_name << std::endl;
199  }
200 
201  if (!matrix_csr.values_.empty()) {
202  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR values_ matrix size (" << matrix_csr.values_.size() << ") under CSC matrix format in " << function_name << std::endl;
203  }
204 
205  if (!matrix_csr.row_ptrs_.empty()) {
206  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR row_ptrs_ matrix (" << matrix_csr.row_ptrs_.size() << ") under CSC matrix format in " << function_name << std::endl;
207  }
208 
209  if (!matrix_csr.col_indices_.empty()) {
210  std::cerr << "MUI Warning [" << file_name << "]: Non-empty CSR col_indices_ matrix (" << matrix_csr.col_indices_.size() << ") under CSC matrix format in " << function_name << std::endl;
211  }
212 
213  if (static_cast<ITYPE>(matrix_csc.values_.size()) != nnz_) {
214  std::cerr << "MUI Error [" << file_name << "]: CSC values_ matrix size (" << matrix_csc.values_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
215  std::abort();
216  }
217 
218  if (static_cast<ITYPE>(matrix_csc.row_indices_.size()) != nnz_) {
219  std::cerr << "MUI Error [" << file_name << "]: CSC row_indices_ matrix size (" << matrix_csc.row_indices_.size() << ") does not equals to number of non-zeros (nnz_=" << nnz_ << ") in " << function_name << std::endl;
220  std::abort();
221  }
222 
223  if (static_cast<ITYPE>(matrix_csc.col_ptrs_.size()) != (cols_+1)) {
224  std::cerr << "MUI Error [" << file_name << "]: CSC col_indices_ matrix size (" << matrix_csc.col_ptrs_.size() << ") does not equals to number of cols + 1 (cols_+1=" << (cols_+1) << ") in " << function_name << std::endl;
225  std::abort();
226  }
227 
228  } else {
229  std::cerr << "MUI Error [" << file_name << "]: unknown matrix format in " << function_name << std::endl;
230  std::abort();
231  }
232 
233 }
234 
235 // Member function to assert if the COO matrix is sorted and deduplicated
236 template<typename ITYPE, typename VTYPE>
237 void sparse_matrix<ITYPE,VTYPE>::assert_coo_sorted_unique(const std::string &file_name_input, const std::string &function_name_input) const {
238 
239  std::string file_name;
240  std::string function_name;
241 
242  if (file_name_input.empty())
243  file_name.assign("matrix_asserts.h");
244  else
245  file_name = file_name_input;
246  if (function_name_input.empty())
247  function_name.assign("assert_coo_sorted_unique()");
248  else
249  function_name = function_name_input;
250 
251  ITYPE numEntries = matrix_coo.values_.size();
252 
253  if (numEntries > 1) {
254  for (ITYPE i = 1; i < numEntries; ++i) {
255  // Compare the current entry with the previous one
256  if (matrix_coo.row_indices_[i] < matrix_coo.row_indices_[i - 1]) {
257  // Row index is not sorted
258  std::cerr << "MUI Error [" << file_name << "]: The COO type matrix is not sorted (sorted row index check failed) in " << function_name << std::endl;
259  std::abort();
260  } else if (matrix_coo.row_indices_[i] == matrix_coo.row_indices_[i - 1]) {
261  // Row index is the same, check column index
262  if (matrix_coo.col_indices_[i] < matrix_coo.col_indices_[i - 1]) {
263  // Column index is not sorted
264  std::cerr << "MUI Error [" << file_name << "]: The COO type matrix is not sorted (sorted column index check failed) in " << function_name << std::endl;
265  std::abort();
266  } else if (matrix_coo.col_indices_[i] == matrix_coo.col_indices_[i - 1]) {
267  // Column index has duplicate elements
268  std::cerr << "MUI Error [" << file_name << "]: The COO type matrix exists duplicated elements (unique column index check failed) in " << function_name << std::endl;
269  std::abort();
270  }
271  }
272  }
273  }
274 }
275 
276 // Member function to assert if the CSR matrix is sorted and deduplicated
277 template<typename ITYPE, typename VTYPE>
278 void sparse_matrix<ITYPE,VTYPE>::assert_csr_sorted_unique(const std::string &file_name_input, const std::string &function_name_input) const {
279 
280  std::string file_name;
281  std::string function_name;
282 
283  if (file_name_input.empty())
284  file_name.assign("matrix_asserts.h");
285  else
286  file_name = file_name_input;
287  if (function_name_input.empty())
288  function_name.assign("assert_csr_sorted_unique()");
289  else
290  function_name = function_name_input;
291 
292  ITYPE numEntries = matrix_csr.values_.size();
293 
294  if (numEntries > 1) {
295  for(ITYPE i = 0; i < rows_; ++i){
296  if (matrix_csr.row_ptrs_[i] > matrix_csr.row_ptrs_[i+1]) {
297  // Row pointers is not sorted
298  std::cerr << "MUI Error [" << file_name << "]: The CSR type matrix is not sorted (sorted row pointers check failed) in " << function_name << std::endl;
299  std::abort();
300  }
301  for(ITYPE j = matrix_csr.row_ptrs_[i] + 1; j < matrix_csr.row_ptrs_[i+1]; ++j){
302  if(matrix_csr.col_indices_[j-1] > matrix_csr.col_indices_[j]){
303  // Column indices is not sorted
304  std::cerr << "MUI Error [" << file_name << "]: The CSR type matrix is not sorted (sorted column index check failed) in " << function_name << std::endl;
305  std::abort();
306  } else if (matrix_csr.col_indices_[j-1] == matrix_csr.col_indices_[j]) {
307  // Column indices is not unique
308  std::cerr << "MUI Error [" << file_name << "]: The CSR type matrix is not unique (deduplicated column index check failed) in " << function_name << std::endl;
309  std::abort();
310  }
311  }
312  }
313  }
314 }
315 
316 // Member function to assert if the CSC matrix is sorted and deduplicated
317 template<typename ITYPE, typename VTYPE>
318 void sparse_matrix<ITYPE,VTYPE>::assert_csc_sorted_unique(const std::string &file_name_input, const std::string &function_name_input) const {
319 
320  std::string file_name;
321  std::string function_name;
322 
323  if (file_name_input.empty())
324  file_name.assign("matrix_asserts.h");
325  else
326  file_name = file_name_input;
327  if (function_name_input.empty())
328  function_name.assign("assert_csc_sorted_unique()");
329  else
330  function_name = function_name_input;
331 
332  ITYPE numEntries = matrix_csc.values_.size();
333 
334  if (numEntries > 1) {
335  for(ITYPE i = 0; i < cols_; ++i){
336  if (matrix_csc.col_ptrs_[i] > matrix_csc.col_ptrs_[i+1]) {
337  // Column pointers is not sorted
338  std::cerr << "MUI Error [" << file_name << "]: The CSC type matrix is not sorted (sorted column pointers check failed) in " << function_name << std::endl;
339  std::abort();
340  }
341  for(ITYPE j = matrix_csc.col_ptrs_[i] + 1; j < matrix_csc.col_ptrs_[i+1]; ++j){
342  if(matrix_csc.row_indices_[j-1] > matrix_csc.row_indices_[j]){
343  // Row indices is not sorted
344  std::cerr << "MUI Error [" << file_name << "]: The CSC type matrix is not sorted (sorted row index check failed) in " << function_name << std::endl;
345  std::abort();
346  } else if (matrix_csc.row_indices_[j-1] == matrix_csc.row_indices_[j]) {
347  // Row indices is not unique
348  std::cerr << "MUI Error [" << file_name << "]: The CSC type matrix is not unique (deduplicated row index check failed) in " << function_name << std::endl;
349  std::abort();
350  }
351  }
352  }
353  }
354 }
355 
356 } // linalg
357 } // mui
358 
359 #endif /* MUI_MATRIX_ASSERTS_H_ */
void assert_csc_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:318
void assert_valid_vector_size(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:61
void assert_csr_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:278
void assert_coo_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:237
Definition: comm.h:54