|
| 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 | +// Helper method |
| 42 | +template <typename T> static auto makeD() { |
| 43 | + const index_t m = 10; |
| 44 | + const index_t n = 10; |
| 45 | + tensor_t<T, 2> D = make_tensor<T>({m, n}); |
| 46 | + for (index_t i = 0; i < m; i++) { |
| 47 | + for (index_t j = 0; j < n; j++) { |
| 48 | + D(i, j) = static_cast<T>(0); |
| 49 | + } |
| 50 | + } |
| 51 | + D(0, 1) = static_cast<T>(1); |
| 52 | + D(4, 4) = static_cast<T>(2); |
| 53 | + D(9, 1) = static_cast<T>(3); |
| 54 | + D(9, 9) = static_cast<T>(4); |
| 55 | + return D; |
| 56 | +} |
| 57 | + |
| 58 | +template <typename T> class ConvertSparseTest : public ::testing::Test { }; |
| 59 | + |
| 60 | +template <typename T> class ConvertSparseTestsAll : public ConvertSparseTest<T> { }; |
| 61 | + |
| 62 | +TYPED_TEST_SUITE(ConvertSparseTestsAll, MatXFloatNonComplexTypesCUDAExec); |
| 63 | + |
| 64 | +TYPED_TEST(ConvertSparseTestsAll, ConvertCOO) { |
| 65 | + MATX_ENTER_HANDLER(); |
| 66 | + using TestType = cuda::std::tuple_element_t<0, TypeParam>; |
| 67 | + using ExecType = cuda::std::tuple_element_t<1, TypeParam>; |
| 68 | + |
| 69 | + ExecType exec{}; |
| 70 | + |
| 71 | + auto D = makeD<TestType>(); |
| 72 | + const auto m = D.Size(0); |
| 73 | + const auto n = D.Size(1); |
| 74 | + |
| 75 | + // Convert dense D to sparse S. |
| 76 | + auto S = experimental::make_zero_tensor_coo<TestType, index_t>({m, n}); |
| 77 | + (S = dense2sparse(D)).run(exec); |
| 78 | + ASSERT_EQ(S.Rank(), 2); |
| 79 | + ASSERT_EQ(S.Size(0), m); |
| 80 | + ASSERT_EQ(S.Size(1), n); |
| 81 | + ASSERT_EQ(S.Nse(), 4); |
| 82 | + ASSERT_EQ(S.posSize(0), 2); |
| 83 | + ASSERT_EQ(S.posSize(1), 0); |
| 84 | + ASSERT_EQ(S.crdSize(0), 4); |
| 85 | + ASSERT_EQ(S.crdSize(1), 4); |
| 86 | + |
| 87 | + // Getters are expensive, but fully functional! |
| 88 | + exec.sync(); |
| 89 | + for (index_t i = 0; i < m; i++) { |
| 90 | + for (index_t j = 0; j < n; j++) { |
| 91 | + ASSERT_EQ(S(i, j), D(i, j)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Convert sparse S back to dense D. |
| 96 | + auto O = make_tensor<TestType>({m, n}); |
| 97 | + (O = sparse2dense(S)).run(exec); |
| 98 | + |
| 99 | + // Back to cheap random-access getters only. |
| 100 | + exec.sync(); |
| 101 | + for (index_t i = 0; i < m; i++) { |
| 102 | + for (index_t j = 0; j < n; j++) { |
| 103 | + ASSERT_EQ(O(i, j), D(i, j)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + MATX_EXIT_HANDLER(); |
| 108 | +} |
| 109 | + |
| 110 | +TYPED_TEST(ConvertSparseTestsAll, ConvertCSR) { |
| 111 | + MATX_ENTER_HANDLER(); |
| 112 | + using TestType = cuda::std::tuple_element_t<0, TypeParam>; |
| 113 | + using ExecType = cuda::std::tuple_element_t<1, TypeParam>; |
| 114 | + |
| 115 | + ExecType exec{}; |
| 116 | + |
| 117 | + auto D = makeD<TestType>(); |
| 118 | + const auto m = D.Size(0); |
| 119 | + const auto n = D.Size(1); |
| 120 | + |
| 121 | + // Convert dense D to sparse S. |
| 122 | + auto S = experimental::make_zero_tensor_csr<TestType, index_t, index_t>({m, n}); |
| 123 | + (S = dense2sparse(D)).run(exec); |
| 124 | + ASSERT_EQ(S.Rank(), 2); |
| 125 | + ASSERT_EQ(S.Size(0), m); |
| 126 | + ASSERT_EQ(S.Size(1), n); |
| 127 | + ASSERT_EQ(S.Nse(), 4); |
| 128 | + ASSERT_EQ(S.posSize(0), 0); |
| 129 | + ASSERT_EQ(S.posSize(1), m + 1); |
| 130 | + ASSERT_EQ(S.crdSize(0), 0); |
| 131 | + ASSERT_EQ(S.crdSize(1), 4); |
| 132 | + |
| 133 | + // Getters are expensive, but fully functional! |
| 134 | + exec.sync(); |
| 135 | + for (index_t i = 0; i < m; i++) { |
| 136 | + for (index_t j = 0; j < n; j++) { |
| 137 | + ASSERT_EQ(S(i, j), D(i, j)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Convert sparse S back to dense D. |
| 142 | + auto O = make_tensor<TestType>({m, n}); |
| 143 | + (O = sparse2dense(S)).run(exec); |
| 144 | + |
| 145 | + // Back to cheap random-access getters only. |
| 146 | + exec.sync(); |
| 147 | + for (index_t i = 0; i < m; i++) { |
| 148 | + for (index_t j = 0; j < n; j++) { |
| 149 | + ASSERT_EQ(O(i, j), D(i, j)); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + MATX_EXIT_HANDLER(); |
| 154 | +} |
| 155 | + |
| 156 | +TYPED_TEST(ConvertSparseTestsAll, ConvertCSC) { |
| 157 | + MATX_ENTER_HANDLER(); |
| 158 | + using TestType = cuda::std::tuple_element_t<0, TypeParam>; |
| 159 | + using ExecType = cuda::std::tuple_element_t<1, TypeParam>; |
| 160 | + |
| 161 | + ExecType exec{}; |
| 162 | + |
| 163 | + auto D = makeD<TestType>(); |
| 164 | + const auto m = D.Size(0); |
| 165 | + const auto n = D.Size(1); |
| 166 | + |
| 167 | + // Convert dense D to sparse S. |
| 168 | + auto S = experimental::make_zero_tensor_csc<TestType, index_t, index_t>({m, n}); |
| 169 | + (S = dense2sparse(D)).run(exec); |
| 170 | + ASSERT_EQ(S.Rank(), 2); |
| 171 | + ASSERT_EQ(S.Size(0), m); |
| 172 | + ASSERT_EQ(S.Size(1), n); |
| 173 | + ASSERT_EQ(S.Nse(), 4); |
| 174 | + ASSERT_EQ(S.posSize(0), 0); |
| 175 | + ASSERT_EQ(S.posSize(1), n + 1); |
| 176 | + ASSERT_EQ(S.crdSize(0), 0); |
| 177 | + ASSERT_EQ(S.crdSize(1), 4); |
| 178 | + |
| 179 | + // Getters are expensive, but fully functional! |
| 180 | + exec.sync(); |
| 181 | + for (index_t i = 0; i < m; i++) { |
| 182 | + for (index_t j = 0; j < n; j++) { |
| 183 | + ASSERT_EQ(S(i, j), D(i, j)); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Convert sparse S back to dense D. |
| 188 | + auto O = make_tensor<TestType>({m, n}); |
| 189 | + (O = sparse2dense(S)).run(exec); |
| 190 | + |
| 191 | + // Back to cheap random-access getters only. |
| 192 | + exec.sync(); |
| 193 | + for (index_t i = 0; i < m; i++) { |
| 194 | + for (index_t j = 0; j < n; j++) { |
| 195 | + ASSERT_EQ(O(i, j), D(i, j)); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + MATX_EXIT_HANDLER(); |
| 200 | +} |
0 commit comments