Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sparse conversion tests (dense2sparse, sparse2dense) #873

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions test/00_sparse/Basic.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@

using namespace matx;

template <typename T> class SparseTest : public ::testing::Test { };
template <typename T> class BasicSparseTest : public ::testing::Test { };

template <typename TensorType> class SparseTestsAll : public SparseTest<TensorType> { };
template <typename T> class BasicSparseTestsAll : public BasicSparseTest<T> { };

// For all operations that run on host and device.
TYPED_TEST_SUITE(SparseTestsAll, MatXAllTypesAllExecs);
TYPED_TEST_SUITE(BasicSparseTestsAll, MatXAllTypesAllExecs);

// For all operations that run on device only.
TYPED_TEST_SUITE(SparseTestsAllCUDA, MatXAllTypesCUDAExec);

TYPED_TEST(SparseTestsAll, MakeZeroCOO) {
TYPED_TEST(BasicSparseTestsAll, MakeZeroCOO) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto A = experimental::make_zero_tensor_coo<TestType, index_t>({17, 33});
Expand All @@ -60,10 +56,12 @@ TYPED_TEST(SparseTestsAll, MakeZeroCOO) {
ASSERT_EQ(A.posSize(1), 0);
ASSERT_EQ(A.crdSize(0), 0);
ASSERT_EQ(A.crdSize(1), 0);
// Element getter.
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
MATX_EXIT_HANDLER();
}

TYPED_TEST(SparseTestsAll, MakeCOO) {
TYPED_TEST(BasicSparseTestsAll, MakeCOO) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto vals = make_tensor<TestType>({3});
Expand Down Expand Up @@ -91,7 +89,7 @@ TYPED_TEST(SparseTestsAll, MakeCOO) {
MATX_EXIT_HANDLER();
}

TYPED_TEST(SparseTestsAll, MakeZeroCSR) {
TYPED_TEST(BasicSparseTestsAll, MakeZeroCSR) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto A = experimental::make_zero_tensor_csr<TestType, index_t, index_t>({17, 33});
Expand All @@ -103,10 +101,12 @@ TYPED_TEST(SparseTestsAll, MakeZeroCSR) {
ASSERT_EQ(A.posSize(1), 18);
ASSERT_EQ(A.crdSize(0), 0);
ASSERT_EQ(A.crdSize(1), 0);
// Element getter.
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
MATX_EXIT_HANDLER();
}

TYPED_TEST(SparseTestsAll, MakeCSR) {
TYPED_TEST(BasicSparseTestsAll, MakeCSR) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto vals = make_tensor<TestType>({3});
Expand Down Expand Up @@ -134,7 +134,7 @@ TYPED_TEST(SparseTestsAll, MakeCSR) {
MATX_EXIT_HANDLER();
}

TYPED_TEST(SparseTestsAll, MakeZeroCSC) {
TYPED_TEST(BasicSparseTestsAll, MakeZeroCSC) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto A = experimental::make_zero_tensor_csc<TestType, index_t, index_t>({17, 33});
Expand All @@ -146,10 +146,12 @@ TYPED_TEST(SparseTestsAll, MakeZeroCSC) {
ASSERT_EQ(A.posSize(1), 34);
ASSERT_EQ(A.crdSize(0), 0);
ASSERT_EQ(A.crdSize(1), 0);
// Element getter.
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
MATX_EXIT_HANDLER();
}

TYPED_TEST(SparseTestsAll, MakeCSC) {
TYPED_TEST(BasicSparseTestsAll, MakeCSC) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
auto vals = make_tensor<TestType>({3});
Expand Down
200 changes: 200 additions & 0 deletions test/00_sparse/Convert.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
////////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (c) 2025, NVIDIA Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/////////////////////////////////////////////////////////////////////////////////

#include "assert.h"
#include "matx.h"
#include "test_types.h"
#include "utilities.h"
#include "gtest/gtest.h"

using namespace matx;

// Helper method
template <typename T> static auto makeD() {
const index_t m = 10;
const index_t n = 10;
tensor_t<T, 2> D = make_tensor<T>({m, n});
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
D(i, j) = static_cast<T>(0);
}
}
D(0, 1) = static_cast<T>(1);
D(4, 4) = static_cast<T>(2);
D(9, 1) = static_cast<T>(3);
D(9, 9) = static_cast<T>(4);
return D;
}

template <typename T> class ConvertSparseTest : public ::testing::Test { };

template <typename T> class ConvertSparseTestsAll : public ConvertSparseTest<T> { };

TYPED_TEST_SUITE(ConvertSparseTestsAll, MatXFloatNonComplexTypesCUDAExec);

TYPED_TEST(ConvertSparseTestsAll, ConvertCOO) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
using ExecType = cuda::std::tuple_element_t<1, TypeParam>;

ExecType exec{};

auto D = makeD<TestType>();
const auto m = D.Size(0);
const auto n = D.Size(1);

// Convert dense D to sparse S.
auto S = experimental::make_zero_tensor_coo<TestType, index_t>({m, n});
(S = dense2sparse(D)).run(exec);
ASSERT_EQ(S.Rank(), 2);
ASSERT_EQ(S.Size(0), m);
ASSERT_EQ(S.Size(1), n);
ASSERT_EQ(S.Nse(), 4);
ASSERT_EQ(S.posSize(0), 2);
ASSERT_EQ(S.posSize(1), 0);
ASSERT_EQ(S.crdSize(0), 4);
ASSERT_EQ(S.crdSize(1), 4);

// Getters are expensive, but fully functional!
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(S(i, j), D(i, j));
}
}

// Convert sparse S back to dense D.
auto O = make_tensor<TestType>({m, n});
(O = sparse2dense(S)).run(exec);

// Back to cheap random-access getters only.
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(O(i, j), D(i, j));
}
}

MATX_EXIT_HANDLER();
}

TYPED_TEST(ConvertSparseTestsAll, ConvertCSR) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
using ExecType = cuda::std::tuple_element_t<1, TypeParam>;

ExecType exec{};

auto D = makeD<TestType>();
const auto m = D.Size(0);
const auto n = D.Size(1);

// Convert dense D to sparse S.
auto S = experimental::make_zero_tensor_csr<TestType, index_t, index_t>({m, n});
(S = dense2sparse(D)).run(exec);
ASSERT_EQ(S.Rank(), 2);
ASSERT_EQ(S.Size(0), m);
ASSERT_EQ(S.Size(1), n);
ASSERT_EQ(S.Nse(), 4);
ASSERT_EQ(S.posSize(0), 0);
ASSERT_EQ(S.posSize(1), m + 1);
ASSERT_EQ(S.crdSize(0), 0);
ASSERT_EQ(S.crdSize(1), 4);

// Getters are expensive, but fully functional!
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(S(i, j), D(i, j));
}
}

// Convert sparse S back to dense D.
auto O = make_tensor<TestType>({m, n});
(O = sparse2dense(S)).run(exec);

// Back to cheap random-access getters only.
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(O(i, j), D(i, j));
}
}

MATX_EXIT_HANDLER();
}

TYPED_TEST(ConvertSparseTestsAll, ConvertCSC) {
MATX_ENTER_HANDLER();
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
using ExecType = cuda::std::tuple_element_t<1, TypeParam>;

ExecType exec{};

auto D = makeD<TestType>();
const auto m = D.Size(0);
const auto n = D.Size(1);

// Convert dense D to sparse S.
auto S = experimental::make_zero_tensor_csc<TestType, index_t, index_t>({m, n});
(S = dense2sparse(D)).run(exec);
ASSERT_EQ(S.Rank(), 2);
ASSERT_EQ(S.Size(0), m);
ASSERT_EQ(S.Size(1), n);
ASSERT_EQ(S.Nse(), 4);
ASSERT_EQ(S.posSize(0), 0);
ASSERT_EQ(S.posSize(1), n + 1);
ASSERT_EQ(S.crdSize(0), 0);
ASSERT_EQ(S.crdSize(1), 4);

// Getters are expensive, but fully functional!
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(S(i, j), D(i, j));
}
}

// Convert sparse S back to dense D.
auto O = make_tensor<TestType>({m, n});
(O = sparse2dense(S)).run(exec);

// Back to cheap random-access getters only.
exec.sync();
for (index_t i = 0; i < m; i++) {
for (index_t j = 0; j < n; j++) {
ASSERT_EQ(O(i, j), D(i, j));
}
}

MATX_EXIT_HANDLER();
}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set (test_sources
01_radar/ambgfun.cu
01_radar/dct.cu
00_sparse/Basic.cu
00_sparse/Convert.cu
main.cu
)

Expand Down