Zoltan2
Loading...
Searching...
No Matches
IdentifierModel.cpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// Zoltan2: A package of combinatorial algorithms for scientific computing
6// Copyright 2012 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Karen Devine (kddevin@sandia.gov)
39// Erik Boman (egboman@sandia.gov)
40// Siva Rajamanickam (srajama@sandia.gov)
41//
42// ***********************************************************************
43//
44// @HEADER
45//
46// Testing of IdentifierModel
47//
48// TODO test with BasicIdentifierAdapter with weights
49
54
55#include <set>
56#include <bitset>
57
58#include <Teuchos_Comm.hpp>
59#include <Teuchos_DefaultComm.hpp>
60#include <Teuchos_ArrayView.hpp>
61#include <Teuchos_OrdinalTraits.hpp>
62
63#include <Tpetra_CrsMatrix.hpp>
64
65using Teuchos::RCP;
66using Teuchos::rcp;
67using Teuchos::Comm;
68
69void testIdentifierModel(std::string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim,
70 const RCP<const Comm<int> > &comm)
71{
72 int rank = comm->getRank();
73 int fail = 0, gfail = 0;
74
75 std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags = 0;
76
77 RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment(comm));
78
80 // Use an Tpetra::CrsMatrix for the user data.
82 typedef Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t> tcrsMatrix_t;
83
84 UserInputForTests *uinput;
85 if (fname.size() > 0)
86 uinput = new UserInputForTests(testDataFilePath, fname, comm, true);
87 else
88 uinput = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true, true);
89
90 RCP<tcrsMatrix_t > M = uinput->getUITpetraCrsMatrix();
91 zlno_t nLocalIds = M->getLocalNumRows();
92 zgno_t nGlobalIds = M->getGlobalNumRows();
93
94 ArrayView<const zgno_t> idList = M->getRowMap()->getLocalElementList();
95 std::set<zgno_t> idSet(idList.begin(), idList.end());
96
98 // Create an IdentifierModel with this input
100
104
105 RCP<const adapter_t> ia = Teuchos::rcp(new adapter_t(M));
106
108 RCP<const base_adapter_t> base_ia =
109 Teuchos::rcp_dynamic_cast<const base_adapter_t>(ia);
110
111 try{
113 base_ia, env, comm, modelFlags);
114 }
115 catch (std::exception &e){
116 std::cerr << rank << ") " << e.what() << std::endl;
117 fail = 1;
118 }
119
120 gfail = globalFail(*comm, fail);
121
122 if (gfail)
123 printFailureCode(*comm, fail);
124
125 // Test the IdentifierModel interface
126
127 if (model->getLocalNumIdentifiers() != size_t(nLocalIds)) {
128 std::cerr << rank << ") getLocalNumIdentifiers "
129 << model->getLocalNumIdentifiers() << " "
130 << nLocalIds << std::endl;
131 fail = 2;
132 }
133
134 if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds)) {
135 std::cerr << rank << ") getGlobalNumIdentifiers "
136 << model->getGlobalNumIdentifiers() << " "
137 << nGlobalIds << std::endl;
138 fail = 3;
139 }
140
141 gfail = globalFail(*comm, fail);
142
143 if (gfail)
144 printFailureCode(*comm, fail);
145
146 ArrayView<const zgno_t> gids;
147 ArrayView<input_t> wgts;
148
149 model->getIdentifierList(gids, wgts);
150
151 if (!fail && gids.size() != nLocalIds) {
152 std::cerr << rank << ") getIdentifierList IDs "
153 << gids.size() << " "
154 << nLocalIds << std::endl;
155 fail = 5;
156 }
157
158 if (!fail && wgts.size() != 0) {
159 std::cerr << rank << ") getIdentifierList Weights "
160 << wgts.size() << " "
161 << 0 << std::endl;
162 fail = 6;
163 }
164
165 for (zlno_t i=0; !fail && i < nLocalIds; i++){
166 std::set<zgno_t>::iterator next = idSet.find(gids[i]);
167 if (next == idSet.end()) {
168 std::cerr << rank << ") getIdentifierList gid not found "
169 << gids[i] << std::endl;
170 fail = 7;
171 }
172 }
173
174 Kokkos::View<const zgno_t*, typename tcrsMatrix_t::device_type> gidsKokkos;
175 Kokkos::View<zscalar_t **, typename tcrsMatrix_t::device_type> wgtsKokkos;
176
177 model->getIdentifierListKokkos(gidsKokkos, wgtsKokkos);
178
179 auto gidsKokkosHost = Kokkos::create_mirror_view(gidsKokkos);
180 Kokkos::deep_copy(gidsKokkosHost, gidsKokkos);
181 auto wgtsKokkosHost = Kokkos::create_mirror_view(wgtsKokkos);
182 Kokkos::deep_copy(wgtsKokkosHost, wgtsKokkos);
183
184
185 if (!fail && gidsKokkosHost.extent(0) != static_cast<size_t>(nLocalIds)) {
186 std::cerr << rank << ") getIdentifierList IDs "
187 << gidsKokkosHost.extent(0) << " "
188 << nLocalIds << std::endl;
189 fail = 5;
190 }
191
192 if (!fail && wgtsKokkosHost.extent(1) != 0) {
193 std::cerr << rank << ") getIdentifierList Weights "
194 << wgtsKokkosHost.extent(1) << " "
195 << 0 << std::endl;
196 fail = 6;
197 }
198
199 for (zlno_t i=0; !fail && i < nLocalIds; i++){
200 std::set<zgno_t>::iterator next = idSet.find(gidsKokkosHost(i));
201 if (next == idSet.end()) {
202 std::cerr << rank << ") getIdentifierList gid not found "
203 << gidsKokkosHost(i) << std::endl;
204 fail = 7;
205 }
206 }
207
208
209 gfail = globalFail(*comm, fail);
210
211 if (gfail)
212 printFailureCode(*comm, fail);
213
214 delete model;
215 delete uinput;
216}
217
218int main(int narg, char *arg[])
219{
220 Tpetra::ScopeGuard tscope(&narg, &arg);
221 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
222
223 int rank = comm->getRank();
224
225 string fname("simple");
226
227
228 if (rank == 0){
229 std::cout << fname;
230 }
231 testIdentifierModel(fname, 0,0,0,comm);
232
233 if (rank==0) std::cout << "PASS" << std::endl;
234
235 return 0;
236}
int globalFail(const Comm< int > &comm, int fail)
void printFailureCode(const Comm< int > &comm, int fail)
void testIdentifierModel(std::string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim, const RCP< const Comm< int > > &comm)
Defines the BasicIdentifierAdapter class.
Defines the IdentifierModel interface.
common code used by tests
Tpetra::Map ::local_ordinal_type zlno_t
std::string testDataFilePath(".")
Tpetra::Map ::global_ordinal_type zgno_t
Defines the XpetraCrsMatrixAdapter class.
int main()
RCP< tcrsMatrix_t > getUITpetraCrsMatrix()
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
IdentifierModel defines the interface for all identifier models.
size_t getIdentifierListKokkos(Kokkos::View< const gno_t *, typename node_t::device_type > &Ids, Kokkos::View< scalar_t **, typename node_t::device_type > &wgts) const
global_size_t getGlobalNumIdentifiers() const
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
MatrixAdapter defines the adapter interface for matrices.
The StridedData class manages lists of weights or coordinates.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
static const std::string fail
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t