-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtestMatrixSparse.cpp
201 lines (170 loc) · 7.93 KB
/
testMatrixSparse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2017, Lawrence Livermore National Security, LLC.
// Produced at the Lawrence Livermore National Laboratory (LLNL).
// Written by Cosmin G. Petra, [email protected].
// LLNL-CODE-742473. All rights reserved.
//
// This file is part of HiOp. For details, see https://github.com/LLNL/hiop. HiOp
// is released under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause).
// Please also read “Additional BSD Notice” below.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// i. Redistributions of source code must retain the above copyright notice, this list
// of conditions and the disclaimer below.
// ii. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the disclaimer (as noted below) in the documentation and/or
// other materials provided with the distribution.
// iii. Neither the name of the LLNS/LLNL 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 LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY 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.
//
// Additional BSD Notice
// 1. This notice is required to be provided under our contract with the U.S. Department
// of Energy (DOE). This work was produced at Lawrence Livermore National Laboratory under
// Contract No. DE-AC52-07NA27344 with the DOE.
// 2. Neither the United States Government nor Lawrence Livermore National Security, LLC
// nor any of their employees, makes any warranty, express or implied, or assumes any
// liability or responsibility for the accuracy, completeness, or usefulness of any
// information, apparatus, product, or process disclosed, or represents that its use would
// not infringe privately-owned rights.
// 3. Also, reference herein to any specific commercial products, process, or services by
// trade name, trademark, manufacturer or otherwise does not necessarily constitute or
// imply its endorsement, recommendation, or favoring by the United States Government or
// Lawrence Livermore National Security, LLC. The views and opinions of authors expressed
// herein do not necessarily state or reflect those of the United States Government or
// Lawrence Livermore National Security, LLC, and shall not be used for advertising or
// product endorsement purposes.
/**
* @file testMatrixSparse.cpp
*
* @author Asher Mancinelli <[email protected]>, PNNL
* @author Slaven Peles <[email protected]>, PNNL
* @author Cameron Rutherford <[email protected]>, PNNL
* @author Jake K. Ryan <[email protected]>, PNNL
*
*/
#include <iostream>
#include <cassert>
#include <cstring>
#include <hiopOptions.hpp>
#include <hiopLinAlgFactory.hpp>
#include <hiopVector.hpp>
#include <hiopMatrixDense.hpp>
#include "LinAlg/matrixTestsSparseTriplet.hpp"
#ifdef HIOP_USE_RAJA
#include "LinAlg/matrixTestsRajaSparseTriplet.hpp"
#endif
template <typename T>
static int runTests(const char* mem_space);
int main(int argc, char** argv)
{
using namespace hiop::tests;
using hiop::tests::global_ordinal_type;
if(argc > 1)
std::cout << "Executable " << argv[0] << " doesn't take any input.";
hiop::hiopOptions options;
int fail = 0;
//
// Test HiOp Sparse Matrices
//
std::cout << "\nTesting HiOp default sparse matrix implementation:\n";
fail += runTests<MatrixTestsSparseTriplet>("default");
#ifdef HIOP_USE_RAJA
#ifdef HIOP_USE_GPU
std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n";
std::cout << " ... using device memory space:\n";
fail += runTests<MatrixTestsRajaSparseTriplet>("device");
std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n";
std::cout << " ... using unified virtual memory space:\n";
fail += runTests<MatrixTestsRajaSparseTriplet>("um");
#else
std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n";
std::cout << " ... unified host memory space:\n";
fail += runTests<MatrixTestsRajaSparseTriplet>("host");
#endif // GPU
#endif // RAJA
if(fail)
{
std::cout << "\n" << fail << " sparse matrix tests failed\n\n";
}
else
{
std::cout << "\nAll sparse matrix tests passed\n\n";
}
return fail;
}
/// Driver for all sparse matrix tests
template <typename T>
static int runTests(const char* mem_space)
{
using namespace hiop;
using hiop::tests::local_ordinal_type;
using hiop::tests::global_ordinal_type;
T test;
hiopOptions options;
options.SetStringValue("mem_space", mem_space);
LinearAlgebraFactory::set_mem_space(mem_space);
int fail = 0;
local_ordinal_type M_local = 5;
local_ordinal_type N_local = 10 * M_local;
// Establishing sparsity pattern and initializing Matrix
local_ordinal_type entries_per_row = 5;
local_ordinal_type nnz = M_local * entries_per_row;
// Sparse matrix is not distributed
global_ordinal_type M_global = M_local;
global_ordinal_type N_global = N_local;
hiopMatrixSparse* mxn_sparse = LinearAlgebraFactory::createMatrixSparse(M_local, N_local, nnz);
test.initializeMatrix(mxn_sparse, entries_per_row);
hiopVector* vec_m = LinearAlgebraFactory::createVector(M_global);
hiopVector* vec_n = LinearAlgebraFactory::createVector(N_global);
/// @see LinAlg/matrixTestsSparseTriplet.hpp for reasons why some tests are implemented/not implemented
fail += test.matrixNumRows(*mxn_sparse, M_global);
fail += test.matrixNumCols(*mxn_sparse, N_global);
fail += test.matrixSetToZero(*mxn_sparse);
fail += test.matrixSetToConstant(*mxn_sparse);
fail += test.matrixMaxAbsValue(*mxn_sparse);
fail += test.matrixIsFinite(*mxn_sparse);
fail += test.matrixTimesVec(*mxn_sparse, *vec_m, *vec_n);
fail += test.matrixTransTimesVec(*mxn_sparse, *vec_m, *vec_n);
// Need a dense matrix to store the output of the following tests
global_ordinal_type W_delta = M_global * 10;
hiopMatrixDense* W_dense = LinearAlgebraFactory::createMatrixDense(N_global + W_delta, N_global + W_delta);
local_ordinal_type test_offset = 4;
fail += test.matrixAddMDinvMtransToDiagBlockOfSymDeMatUTri(*mxn_sparse, *vec_n, *W_dense, test_offset);
// testing adding sparse matrix to the upper triangular area of a symmetric dense matrix
fail += test.transAddToSymDenseMatrixUpperTriangle(*W_dense, *mxn_sparse);
// Initialise another sparse Matrix
local_ordinal_type M2 = M_global * 2;
nnz = M2 * (entries_per_row);
hiopMatrixSparse* m2xn_sparse = LinearAlgebraFactory::createMatrixSparse(M2, N_global, nnz);
test.initializeMatrix(m2xn_sparse, entries_per_row);
// Set offsets where to insert sparse matrix
local_ordinal_type i_offset = 1;
local_ordinal_type j_offset = M2 + 1;
fail += test.matrixAddMDinvNtransToSymDeMatUTri(*mxn_sparse, *m2xn_sparse, *vec_n, *W_dense, i_offset, j_offset);
//
// Perform hipoMatrixSparseTriplet specific tests
//
//auto * test_triplet = dynamic_cast<tests::MatrixTestsSparseTriplet *>(&test);
if (dynamic_cast<tests::MatrixTestsSparseTriplet *>(&test))
{
fail += test.copyRowsBlockFrom(*mxn_sparse, *m2xn_sparse,0, 1, M_global-1, mxn_sparse->numberOfNonzeros()-entries_per_row);
}
delete mxn_sparse;
delete m2xn_sparse;
delete W_dense;
delete vec_m;
delete vec_n;
return fail;
}