-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft vector ordering policy for SparseMatrix
- Loading branch information
1 parent
2993a44
commit 99781b3
Showing
7 changed files
with
175 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (C) 2023 National Center for Atmospheric Research, | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#pragma once | ||
|
||
#include <cmath> | ||
|
||
namespace micm | ||
{ | ||
|
||
/// @brief Defines the ordering of SparseMatrix object data to encourage vectorization | ||
/// | ||
/// Data is stored with sets of blocks in the block diagonal matrix as the highest | ||
/// level structure, then by row, then by non-zero columns in each row, then by | ||
/// individual blocks in the set of blocks. | ||
/// | ||
/// The template argument is the number of blocks per set of blocks and should be | ||
/// approximately the size of the vector register. | ||
template<std::size_t L> | ||
class SparseMatrixVectorOrdering | ||
{ | ||
protected: | ||
static std::size_t VectorSize( | ||
std::size_t number_of_blocks, | ||
const std::vector<std::size_t>& row_ids, | ||
const std::vector<std::size_t>& row_start) | ||
{ | ||
return std::ceil((double)number_of_blocks / (double)L) * row_ids.size(); | ||
}; | ||
|
||
std::size_t VectorIndex( | ||
std::size_t number_of_blocks, | ||
const std::vector<std::size_t>& row_ids, | ||
const std::vector<std::size_t>& row_start, | ||
std::size_t block, | ||
std::size_t row, | ||
std::size_t column) const | ||
{ | ||
if (row >= row_start.size() - 1 || column >= row_start.size() - 1 || block >= number_of_blocks) | ||
throw std::invalid_argument("SparseMatrix element out of range"); | ||
auto begin = std::next(row_ids.begin(), row_start[row]); | ||
auto end = std::next(row_ids.begin(), row_start[row + 1]); | ||
auto elem = std::find(begin, end, column); | ||
if (elem == end) | ||
throw std::invalid_argument("SparseMatrix zero element access not allowed"); | ||
return std::size_t{ (elem - row_ids.begin()) * L + block % L + (block / L) * row_ids.size() }; | ||
}; | ||
}; | ||
} // namespace micm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <micm/util/sparse_matrix.hpp> | ||
|
||
template<class OrderingPolicy> | ||
micm::SparseMatrix<double, OrderingPolicy> testZeroMatrix() | ||
{ | ||
auto builder = micm::SparseMatrix<double, OrderingPolicy>::create(3); | ||
auto row_ids = builder.RowIdsVector(); | ||
auto row_starts = builder.RowStartVector(); | ||
|
||
EXPECT_EQ(builder.NumberOfElements(), 0); | ||
EXPECT_EQ(row_ids.size(), 0); | ||
EXPECT_EQ(row_starts.size(), 4); | ||
EXPECT_EQ(row_starts[0], 0); | ||
EXPECT_EQ(row_starts[1], 0); | ||
EXPECT_EQ(row_starts[2], 0); | ||
EXPECT_EQ(row_starts[3], 0); | ||
|
||
micm::SparseMatrix<double, OrderingPolicy> matrix{ builder }; | ||
|
||
EXPECT_EQ(matrix.FlatBlockSize(), 0); | ||
|
||
EXPECT_THROW( | ||
try { std::size_t elem = matrix.VectorIndex(0, 0); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix zero element access not allowed"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { std::size_t elem = matrix.VectorIndex(6, 0); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { std::size_t elem = matrix.VectorIndex(1, 3); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { std::size_t elem = matrix.VectorIndex(6, 3); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { bool isZero = matrix.IsZero(6, 0); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { bool isZero = matrix.IsZero(1, 3); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { bool isZero = matrix.IsZero(6, 3); } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { matrix[0][0][4] = 2.0; } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { matrix[1][0][0] = 2.0; } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { matrix[0][3][0] = 2.0; } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix element out of range"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
EXPECT_THROW( | ||
try { matrix[0][1][1] = 2.0; } catch (const std::invalid_argument& e) { | ||
EXPECT_STREQ(e.what(), "SparseMatrix zero element access not allowed"); | ||
throw; | ||
}, | ||
std::invalid_argument); | ||
return matrix; | ||
} |
85 changes: 3 additions & 82 deletions
85
test/unit/util/test_sparse_matrix.cpp → .../test_sparse_matrix_standard_ordering.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <micm/util/sparse_matrix.hpp> | ||
#include <micm/util/sparse_matrix_vector_ordering.hpp> | ||
#include "test_sparse_matrix_policy.hpp" | ||
|
||
TEST(SparseMatrix, ZeroMatrix) | ||
{ | ||
testZeroMatrix<micm::SparseMatrixVectorOrdering<2>>(); | ||
} |