1+ /* *********************************************************************************/
2+ /* MIT License */
3+ /* */
4+ /* Copyright (c) 2020, 2021 JetBrains-Research */
5+ /* */
6+ /* Permission is hereby granted, free of charge, to any person obtaining a copy */
7+ /* of this software and associated documentation files (the "Software"), to deal */
8+ /* in the Software without restriction, including without limitation the rights */
9+ /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
10+ /* copies of the Software, and to permit persons to whom the Software is */
11+ /* furnished to do so, subject to the following conditions: */
12+ /* */
13+ /* The above copyright notice and this permission notice shall be included in all */
14+ /* copies or substantial portions of the Software. */
15+ /* */
16+ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
17+ /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
18+ /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
19+ /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
20+ /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
21+ /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
22+ /* SOFTWARE. */
23+ /* *********************************************************************************/
24+
25+ #include < testing/testing.hpp>
26+
27+ void testMatrixVectorMultiplyAdd (cuBool_Index m, cuBool_Index n, float density) {
28+ cuBool_Matrix a;
29+ cuBool_Vector v, r;
30+
31+ // Generate test data with specified density
32+ testing::Matrix ta = std::move (testing::Matrix::generateSparse (m, n, density));
33+ testing::Vector tv = std::move (testing::Vector::generateSparse (n, density));
34+
35+ // Allocate input matrices and resize to fill with input data
36+ ASSERT_EQ (cuBool_Matrix_New (&a, m, n), CUBOOL_STATUS_SUCCESS);
37+ ASSERT_EQ (cuBool_Vector_New (&v, n), CUBOOL_STATUS_SUCCESS);
38+ ASSERT_EQ (cuBool_Vector_New (&r, m), CUBOOL_STATUS_SUCCESS);
39+
40+ // Transfer input data into input matrices
41+ ASSERT_EQ (cuBool_Matrix_Build (a, ta.rowsIndex .data (), ta.colsIndex .data (), ta.nvals , CUBOOL_HINT_VALUES_SORTED & CUBOOL_HINT_NO_DUPLICATES), CUBOOL_STATUS_SUCCESS);
42+ ASSERT_EQ (cuBool_Vector_Build (v, tv.index .data (), tv.nvals , CUBOOL_HINT_VALUES_SORTED & CUBOOL_HINT_NO_DUPLICATES), CUBOOL_STATUS_SUCCESS);
43+
44+ // Evaluate naive r += a x b on the cpu to compare results
45+ testing::MatrixVectorMultiplyFunctor functor;
46+ testing::Vector tr = functor (ta, tv);
47+
48+ // Evaluate r += a x b
49+ ASSERT_EQ (cuBool_MxV (r, a, v, CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
50+
51+ // Compare results
52+ ASSERT_EQ (tr.areEqual (r), true );
53+
54+ // Deallocate matrices
55+ ASSERT_EQ (cuBool_Matrix_Free (a), CUBOOL_STATUS_SUCCESS);
56+ ASSERT_EQ (cuBool_Vector_Free (v), CUBOOL_STATUS_SUCCESS);
57+ ASSERT_EQ (cuBool_Vector_Free (r), CUBOOL_STATUS_SUCCESS);
58+ }
59+
60+ void testRun (cuBool_Index m, cuBool_Index n, cuBool_Hints setup) {
61+ // Setup library
62+ ASSERT_EQ (cuBool_Initialize (setup), CUBOOL_STATUS_SUCCESS);
63+
64+ for (size_t i = 0 ; i < 5 ; i++) {
65+ testMatrixVectorMultiplyAdd (m, n, 0 .1f + (0 .05f ) * ((float ) i));
66+ }
67+
68+ // Finalize library
69+ ASSERT_EQ (cuBool_Finalize (), CUBOOL_STATUS_SUCCESS);
70+ }
71+
72+ TEST (cuBool_Matrix, MultiplyMatrixVectorSmall) {
73+ cuBool_Index m = 60 , n = 80 ;
74+ testRun (m, n, CUBOOL_HINT_NO);
75+ }
76+
77+ TEST (cuBool_Matrix, MultiplyMatrixVectorMedium) {
78+ cuBool_Index m = 500 , n = 800 ;
79+ testRun (m, n, CUBOOL_HINT_NO);
80+ }
81+
82+ TEST (cuBool_Matrix, MultiplyMatrixVectorLarge) {
83+ cuBool_Index m = 1000 , n = 500 ;
84+ testRun (m, n, CUBOOL_HINT_NO);
85+ }
86+
87+ TEST (cuBool_Matrix, MultiplyMatrixVectorSmallFallback) {
88+ cuBool_Index m = 60 , n = 80 ;
89+ testRun (m, n, CUBOOL_HINT_CPU_BACKEND);
90+ }
91+
92+ TEST (cuBool_Matrix, MultiplyMatrixVectorMediumFallback) {
93+ cuBool_Index m = 500 , n = 800 ;
94+ testRun (m, n, CUBOOL_HINT_CPU_BACKEND);
95+ }
96+
97+ TEST (cuBool_Matrix, MultiplyMatrixVectorLargeFallback) {
98+ cuBool_Index m = 1000 , n = 500 ;
99+ testRun (m, n, CUBOOL_HINT_CPU_BACKEND);
100+ }
101+
102+ CUBOOL_GTEST_MAIN
0 commit comments