Skip to content

Commit 252dc03

Browse files
authored
Add sparse factory method tests (#871)
* Add sparse factory method tests
1 parent 2529c66 commit 252dc03

File tree

3 files changed

+185
-2
lines changed

3 files changed

+185
-2
lines changed

include/matx/core/sparse_tensor.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ class sparse_tensor_t
144144
c[l] = coordinates_[l].data();
145145
p[l] = positions_[l].data();
146146
// All non-null data resides in same space.
147-
assert(!c[l] || GetPointerKind(c[l]) == GetPointerKind(v));
148-
assert(!p[l] || GetPointerKind(p[l]) == GetPointerKind(v));
147+
if (v) {
148+
assert(!c[l] || GetPointerKind(c[l]) == GetPointerKind(v));
149+
assert(!p[l] || GetPointerKind(p[l]) == GetPointerKind(v));
150+
}
149151
}
150152
this->SetSparseData(v, c, p);
151153
}

test/00_sparse/Basic.cu

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// BSD 3-Clause License
3+
//
4+
// Copyright (c) 2025, NVIDIA Corporation
5+
// All rights reserved.
6+
//
7+
// Redistribution and use in source and binary forms, with or without
8+
// modification, are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright notice,
11+
// this list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright notice,
14+
// this list of conditions and the following disclaimer in the documentation
15+
// and/or other materials provided with the distribution.
16+
//
17+
// 3. Neither the name of the copyright holder nor the names of its
18+
// contributors may be used to endorse or promote products derived from
19+
// this software without specific prior written permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
/////////////////////////////////////////////////////////////////////////////////
32+
33+
#include "assert.h"
34+
#include "matx.h"
35+
#include "test_types.h"
36+
#include "utilities.h"
37+
#include "gtest/gtest.h"
38+
39+
using namespace matx;
40+
41+
template <typename T> class SparseTest : public ::testing::Test { };
42+
43+
template <typename TensorType> class SparseTestsAll : public SparseTest<TensorType> { };
44+
45+
// For all operations that run on host and device.
46+
TYPED_TEST_SUITE(SparseTestsAll, MatXAllTypesAllExecs);
47+
48+
// For all operations that run on device only.
49+
TYPED_TEST_SUITE(SparseTestsAllCUDA, MatXAllTypesCUDAExec);
50+
51+
TYPED_TEST(SparseTestsAll, MakeZeroCOO) {
52+
MATX_ENTER_HANDLER();
53+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
54+
auto A = experimental::make_zero_tensor_coo<TestType, index_t>({17, 33});
55+
ASSERT_EQ(A.Rank(), 2);
56+
ASSERT_EQ(A.Size(0), 17);
57+
ASSERT_EQ(A.Size(1), 33);
58+
ASSERT_EQ(A.Nse(), 0);
59+
ASSERT_EQ(A.posSize(0), 2);
60+
ASSERT_EQ(A.posSize(1), 0);
61+
ASSERT_EQ(A.crdSize(0), 0);
62+
ASSERT_EQ(A.crdSize(1), 0);
63+
MATX_EXIT_HANDLER();
64+
}
65+
66+
TYPED_TEST(SparseTestsAll, MakeCOO) {
67+
MATX_ENTER_HANDLER();
68+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
69+
auto vals = make_tensor<TestType>({3});
70+
auto idxi = make_tensor<index_t>({3});
71+
auto idxj = make_tensor<index_t>({3});
72+
vals(0) = static_cast<TestType>(10);
73+
vals(1) = static_cast<TestType>(20);
74+
vals(2) = static_cast<TestType>(30);
75+
idxi(0) = 0; idxi(1) = 0; idxi(2) = 3;
76+
idxj(0) = 3; idxj(1) = 6; idxj(2) = 7;
77+
auto A = experimental::make_tensor_coo(vals, idxi, idxj, {4, 8});
78+
ASSERT_EQ(A.Rank(), 2);
79+
ASSERT_EQ(A.Size(0), 4);
80+
ASSERT_EQ(A.Size(1), 8);
81+
ASSERT_EQ(A.Nse(), 3);
82+
ASSERT_EQ(A.posSize(0), 2);
83+
ASSERT_EQ(A.posSize(1), 0);
84+
ASSERT_EQ(A.crdSize(0), 3);
85+
ASSERT_EQ(A.crdSize(1), 3);
86+
// Element getters.
87+
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
88+
ASSERT_EQ(A(0, 3), vals(0));
89+
ASSERT_EQ(A(0, 6), vals(1));
90+
ASSERT_EQ(A(3, 7), vals(2));
91+
MATX_EXIT_HANDLER();
92+
}
93+
94+
TYPED_TEST(SparseTestsAll, MakeZeroCSR) {
95+
MATX_ENTER_HANDLER();
96+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
97+
auto A = experimental::make_zero_tensor_csr<TestType, index_t, index_t>({17, 33});
98+
ASSERT_EQ(A.Rank(), 2);
99+
ASSERT_EQ(A.Size(0), 17);
100+
ASSERT_EQ(A.Size(1), 33);
101+
ASSERT_EQ(A.Nse(), 0);
102+
ASSERT_EQ(A.posSize(0), 0);
103+
ASSERT_EQ(A.posSize(1), 18);
104+
ASSERT_EQ(A.crdSize(0), 0);
105+
ASSERT_EQ(A.crdSize(1), 0);
106+
MATX_EXIT_HANDLER();
107+
}
108+
109+
TYPED_TEST(SparseTestsAll, MakeCSR) {
110+
MATX_ENTER_HANDLER();
111+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
112+
auto vals = make_tensor<TestType>({3});
113+
auto rowp = make_tensor<index_t>({5});
114+
auto col = make_tensor<index_t>({3});
115+
vals(0) = static_cast<TestType>(10);
116+
vals(1) = static_cast<TestType>(20);
117+
vals(2) = static_cast<TestType>(30);
118+
rowp(0) = 0; rowp(1) = 2; rowp(2) = 2; rowp(3) = 2; rowp(4) = 3;
119+
col(0) = 3; col(1) = 6; col(2) = 7;
120+
auto A = experimental::make_tensor_csr(vals, rowp, col, {4, 8});
121+
ASSERT_EQ(A.Rank(), 2);
122+
ASSERT_EQ(A.Size(0), 4);
123+
ASSERT_EQ(A.Size(1), 8);
124+
ASSERT_EQ(A.Nse(), 3);
125+
ASSERT_EQ(A.posSize(0), 0);
126+
ASSERT_EQ(A.posSize(1), 5);
127+
ASSERT_EQ(A.crdSize(0), 0);
128+
ASSERT_EQ(A.crdSize(1), 3);
129+
// Element getters.
130+
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
131+
ASSERT_EQ(A(0, 3), vals(0));
132+
ASSERT_EQ(A(0, 6), vals(1));
133+
ASSERT_EQ(A(3, 7), vals(2));
134+
MATX_EXIT_HANDLER();
135+
}
136+
137+
TYPED_TEST(SparseTestsAll, MakeZeroCSC) {
138+
MATX_ENTER_HANDLER();
139+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
140+
auto A = experimental::make_zero_tensor_csc<TestType, index_t, index_t>({17, 33});
141+
ASSERT_EQ(A.Rank(), 2);
142+
ASSERT_EQ(A.Size(0), 17);
143+
ASSERT_EQ(A.Size(1), 33);
144+
ASSERT_EQ(A.Nse(), 0);
145+
ASSERT_EQ(A.posSize(0), 0);
146+
ASSERT_EQ(A.posSize(1), 34);
147+
ASSERT_EQ(A.crdSize(0), 0);
148+
ASSERT_EQ(A.crdSize(1), 0);
149+
MATX_EXIT_HANDLER();
150+
}
151+
152+
TYPED_TEST(SparseTestsAll, MakeCSC) {
153+
MATX_ENTER_HANDLER();
154+
using TestType = cuda::std::tuple_element_t<0, TypeParam>;
155+
auto vals = make_tensor<TestType>({3});
156+
auto row = make_tensor<index_t>({3});
157+
auto colp = make_tensor<index_t>({9});
158+
vals(0) = static_cast<TestType>(10);
159+
vals(1) = static_cast<TestType>(20);
160+
vals(2) = static_cast<TestType>(30);
161+
colp(0) = 0; colp(1) = 0; colp(2) = 0;
162+
colp(3) = 0; colp(4) = 1; colp(5) = 1;
163+
colp(6) = 1; colp(7) = 2; colp(8) = 3;
164+
row(0) = 0; row(1) = 0; row(2) = 3;
165+
auto A = experimental::make_tensor_csc(vals, colp, row, {4, 8});
166+
ASSERT_EQ(A.Rank(), 2);
167+
ASSERT_EQ(A.Size(0), 4);
168+
ASSERT_EQ(A.Size(1), 8);
169+
ASSERT_EQ(A.Nse(), 3);
170+
ASSERT_EQ(A.posSize(0), 0);
171+
ASSERT_EQ(A.posSize(1), 9);
172+
ASSERT_EQ(A.crdSize(0), 0);
173+
ASSERT_EQ(A.crdSize(1), 3);
174+
// Element getters.
175+
ASSERT_EQ(A(0, 0), static_cast<TestType>(0)); // not found
176+
ASSERT_EQ(A(0, 3), vals(0));
177+
ASSERT_EQ(A(0, 6), vals(1));
178+
ASSERT_EQ(A(3, 7), vals(2));
179+
MATX_EXIT_HANDLER();
180+
}

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set (test_sources
3737
01_radar/MVDRBeamformer.cu
3838
01_radar/ambgfun.cu
3939
01_radar/dct.cu
40+
00_sparse/Basic.cu
4041
main.cu
4142
)
4243

0 commit comments

Comments
 (0)