From a4a314ec3be84d8b6d06bb337ccdd71dc3986be8 Mon Sep 17 00:00:00 2001 From: Alice Date: Wed, 1 Jul 2026 16:51:53 +0200 Subject: [PATCH] some benchmarks for this useless sparsemask --- benchmarks/CMakeLists.txt | 2 +- include/aare/utils/SparseMask.hpp | 16 +++++++++++++- src/utils/SparseMask.cpp | 36 ++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index ef378e4d..b579fca5 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -19,7 +19,7 @@ add_executable(benchmarks) target_sources( benchmarks PRIVATE ndarray_benchmark.cpp calculateeta_benchmark.cpp - reduce_benchmark.cpp) + reduce_benchmark.cpp sparsemask_benchmark.cpp) # Link Google Benchmark and other necessary libraries target_link_libraries(benchmarks PRIVATE benchmark::benchmark aare_core diff --git a/include/aare/utils/SparseMask.hpp b/include/aare/utils/SparseMask.hpp index acb1e4f7..a3dfa554 100644 --- a/include/aare/utils/SparseMask.hpp +++ b/include/aare/utils/SparseMask.hpp @@ -1,3 +1,4 @@ +#include "aare/NDArray.hpp" #include "aare/NDView.hpp" #include #include @@ -41,11 +42,24 @@ class SparseMask { /// @brief Get number of bad channels size_t num_bad_channels() const; + /** + * Convert the sparse mask to a dense 2D array representation. + * @return An NDArray representing the dense mask, where true + * indicates a bad channel + */ + NDArray convert_to_dense() const; + private: - /// @brief stoarge format of the sparse mask, either row major or column + /// @brief storage format of the sparse mask, either row major or column /// major STORAGEFORMAT storage_format_; + /// @brief number of rows in the dense mask + size_t rows_; + + /// @brief number of columns in the dense mask + size_t cols_; + /// @brief for column major stores row indices of non-zero elements, for row /// major stores column indices of non-zero elements std::vector innerindices_; diff --git a/src/utils/SparseMask.cpp b/src/utils/SparseMask.cpp index b49d1647..c5b1e616 100644 --- a/src/utils/SparseMask.cpp +++ b/src/utils/SparseMask.cpp @@ -4,17 +4,17 @@ namespace aare { SparseMask::SparseMask(const STORAGEFORMAT storage_format, const size_t rows, const size_t cols) - : storage_format_(storage_format) { + : storage_format_(storage_format), rows_(rows), cols_(cols) { if (storage_format_ == STORAGEFORMAT::ROWMAJOR) { - outerindices_.resize(rows + 1, 0); + outerindices_.resize(rows_ + 1, 0); } else if (storage_format_ == STORAGEFORMAT::COLUMNMAJOR) { - outerindices_.resize(cols + 1, 0); + outerindices_.resize(cols_ + 1, 0); } else { throw std::invalid_argument( "Invalid storage format: must be either ROWMAJOR or COLUMNMAJOR"); } - innerindices_.reserve(rows * cols); // Reserve maximum possible size + innerindices_.reserve(rows_ * cols_); // Reserve maximum possible size } void SparseMask::insert(const size_t row, const size_t col) { @@ -52,15 +52,31 @@ bool SparseMask::is_masked(const size_t row, const size_t col) const { } else { auto start = outerindices_[index_outer_indices]; auto end = outerindices_[index_outer_indices + 1]; - for (size_t i = start; i < end; ++i) { - if (innerindices_[i] == nonzero_index) { - return true; // Found a non-zero element at (row, col) - } - } - return false; // No non-zero element found at (row, col) + return std::binary_search(innerindices_.begin() + start, + innerindices_.begin() + end, nonzero_index); } } size_t SparseMask::num_bad_channels() const { return innerindices_.size(); } +NDArray SparseMask::convert_to_dense() const { + NDArray dense_mask{ + std::array{static_cast(rows_), + static_cast(cols_)}, + false}; + + for (size_t i = 0; i < outerindices_.size() - 1; ++i) { + size_t start = outerindices_[i]; + size_t end = outerindices_[i + 1]; + for (size_t j = start; j < end; ++j) { + if (storage_format_ == STORAGEFORMAT::ROWMAJOR) { + dense_mask(i, innerindices_[j]) = true; + } else { + dense_mask(innerindices_[j], i) = true; + } + } + } + return dense_mask; +} + } // namespace aare \ No newline at end of file