70 #include <Teuchos_Comm.hpp> 71 #include <Teuchos_DefaultComm.hpp> 72 #include <Teuchos_ArrayView.hpp> 79 using Teuchos::DefaultComm;
80 using Teuchos::ArrayView;
84 typedef Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t, znode_t>
tcrsMatrix_t;
85 typedef Tpetra::CrsGraph<zlno_t, zgno_t, znode_t>
tcrsGraph_t;
86 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
tmap_t;
103 const RCP<
const Comm<int> > &comm)
105 int rank = comm->getRank();
106 int nprocs = comm->getSize();
109 for (
int p=0; p < nprocs; p++){
111 std::cout <<
"Rank " << p << std::endl;
112 for (
zlno_t i=0; i < nrows; i++){
113 std::cout <<
" Vtx " << i <<
": ";
115 for (
zlno_t j=idx[i]; j < idx[i+1]; j++)
116 std::cout << *elid++ <<
" ";
118 for (
zlno_t j=idx[i]; j < idx[i+1]; j++)
119 std::cout << *egid++ <<
" ";
120 std::cout << std::endl;
130 template <
typename BaseAdapter,
typename Adapter,
typename MatrixOrGraph>
132 RCP<const MatrixOrGraph> &M,
133 RCP<
const Tpetra::CrsGraph<zlno_t, zgno_t> > &Mgraph,
134 const RCP<
const Comm<int> > &comm,
135 bool idsAreConsecutive,
136 int nVtxWeights,
int nEdgeWeights,
int nnzWgtIdx,
int coordDim,
137 bool consecutiveIdsRequested,
bool removeSelfEdges,
bool buildLocalGraph)
142 int rank = comm->getRank();
143 int nprocs = comm->getSize();
146 zlno_t nLocalRows = M->getNodeNumRows();
147 zlno_t nLocalNZ = M->getNodeNumEntries();
148 zgno_t nGlobalRows = M->getGlobalNumRows();
149 zgno_t nGlobalNZ = M->getGlobalNumEntries();
151 std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags;
152 if (consecutiveIdsRequested)
165 for (
int i=0; i < coordDim; i++){
167 for (
zlno_t j=0; j < nLocalRows; j++){
168 coords[i][j] = 100000*i + j;
173 if (nVtxWeights > 0){
174 rowWeights =
new zscalar_t * [nVtxWeights];
175 for (
int i=0; i < nVtxWeights; i++){
177 rowWeights[i] = NULL;
179 rowWeights[i] =
new zscalar_t [nLocalRows];
180 for (
zlno_t j=0; j < nLocalRows; j++){
181 rowWeights[i][j] = 200000*i + j;
187 if (nEdgeWeights > 0){
188 printf(
"TODO: STILL NEED TO TEST EDGE WEIGHTS!\n");
193 Adapter tmi(M, nVtxWeights);
195 for (
int i=0; i < nVtxWeights; i++){
197 tmi.setWeightIsDegree(i);
199 tmi.setWeights(rowWeights[i], 1, i);
207 gids =
new zgno_t[nLocalRows];
208 for (
zlno_t i = 0; i < nLocalRows; i++)
209 gids[i] = M->getRowMap()->getGlobalElement(i);
211 (coordDim > 1 ? coords[1] : NULL),
212 (coordDim > 2 ? coords[2] : NULL),
214 tmi.setCoordinateInput(via);
217 int numLocalDiags = M->getNodeNumDiags();
218 int numGlobalDiags = M->getGlobalNumDiags();
220 const RCP<const tmap_t> rowMap = M->getRowMap();
221 const RCP<const tmap_t> colMap = M->getColMap();
225 int *numNbors =
new int [nLocalRows];
226 int *numLocalNbors =
new int [nLocalRows];
227 bool *haveDiag =
new bool [nLocalRows];
228 zgno_t totalLocalNbors = 0;
230 for (
zlno_t i=0; i < nLocalRows; i++){
231 numLocalNbors[i] = 0;
233 ArrayView<const zlno_t> idx;
234 Mgraph->getLocalRowView(i, idx);
235 numNbors[i] = idx.size();
237 for (
zlno_t j=0; j < idx.size(); j++){
244 zgno_t gidVal = colMap->getGlobalElement(idx[j]);
245 if (rowMap->getLocalElement(gidVal) !=
246 Teuchos::OrdinalTraits<zlno_t>::invalid()) {
257 if (rank == 0) std::cout <<
" Creating GraphModel" << std::endl;
259 RCP<const BaseAdapter> baseTmi = rcp(dynamic_cast<BaseAdapter *>(&tmi),
false);
265 catch (std::exception &e){
266 std::cerr << rank <<
") " << e.what() << std::endl;
273 if (rank == 0) std::cout <<
" Checking counts" << std::endl;
277 if (buildLocalGraph) {
281 size_t num = (removeSelfEdges ? (totalLocalNbors - numLocalDiags)
293 size_t num = (removeSelfEdges ? (nLocalNZ-numLocalDiags) : nLocalNZ);
297 num = (removeSelfEdges ? (nGlobalNZ-numGlobalDiags) : nGlobalNZ);
312 if (rank == 0) std::cout <<
" Checking vertices" << std::endl;
313 ArrayView<const zgno_t> vertexGids;
314 ArrayView<input_t> crds;
315 ArrayView<input_t> wgts;
321 catch (std::exception &e){
322 std::cerr << rank <<
") Error " << e.what() << std::endl;
327 if (vertexGids.size() != nLocalRows)
fail = 1;
330 if (buildLocalGraph) {
332 for (
zlno_t i = 0; i < nLocalRows; i++) {
333 if (vertexGids[i] != i) {
341 if (idsAreConsecutive){
342 zgno_t minLocalGID = rowMap->getMinGlobalIndex();
343 for (
zlno_t i=0; i < nLocalRows; i++){
344 if (vertexGids[i] != minLocalGID + i) {
351 if (consecutiveIdsRequested) {
353 zgno_t tnLocalRows = nLocalRows;
354 scan(*comm, Teuchos::REDUCE_SUM, 1, &tnLocalRows, &myFirstRow);
355 myFirstRow -= nLocalRows;
356 for (
zlno_t i=0; i < nLocalRows; i++){
357 if (vertexGids[i] != myFirstRow+i){
358 std::cout << rank <<
" Row " << i <<
" of " << nLocalRows
359 <<
" myFirstRow+i " << myFirstRow+i
360 <<
" vertexGids " << vertexGids[i]
369 for (
zlno_t i=0; i < nLocalRows; i++, myGid += nprocs){
370 if (vertexGids[i] != myGid){
371 std::cout << rank <<
" Row " << i <<
" of " << nLocalRows
372 <<
" myGid " << myGid <<
" vertexGids " << vertexGids[i]
384 if ((crds.size() != coordDim) || (wgts.size() != nVtxWeights))
fail = 1;
387 for (
int i=0; !
fail && i < coordDim; i++){
388 for (
zlno_t j=0; j < nLocalRows; j++){
389 if (crds[i][j] != 100000*i + j){
397 for (
int i=0; !
fail && i < nVtxWeights; i++){
399 for (
zlno_t j=0; j < nLocalRows; j++){
400 zscalar_t val = (buildLocalGraph ? numLocalNbors[j] : numNbors[j]);
401 if (removeSelfEdges && haveDiag[j])
403 if (wgts[i][j] != val){
410 for (
zlno_t j=0; j < nLocalRows; j++){
411 if (wgts[i][j] != 200000*i + j){
422 if (rank == 0) std::cout <<
" Checking edges" << std::endl;
424 if (!buildLocalGraph) {
425 ArrayView<const zgno_t> edgeGids;
426 ArrayView<const zlno_t> offsets;
430 numEdges = model->
getEdgeList(edgeGids, offsets, wgts);
432 catch(std::exception &e){
433 std::cerr << rank <<
") Error " << e.what() << std::endl;
441 for (ArrayView<const zlno_t>::size_type i=0; i < offsets.size()-1; i++){
442 size_t edgeListSize = offsets[i+1] - offsets[i];
444 size_t val = numNbors[i];
445 if (removeSelfEdges && haveDiag[i])
447 if (edgeListSize != val){
456 std::cout <<
"Printing graph now " << nGlobalRows << std::endl;
457 printGraph(nLocalRows, vertexGids.getRawPtr(), NULL,
458 edgeGids.getRawPtr(), offsets.getRawPtr(), comm);
462 std::cout <<
" " << nGlobalRows <<
" total rows" << std::endl;
469 if (rank == 0) std::cout <<
" Checking local edges" << std::endl;
470 ArrayView<const zgno_t> localEdges;
471 ArrayView<const zlno_t> localOffsets;
472 size_t numLocalNeighbors=0;
475 numLocalNeighbors= model->
getEdgeList(localEdges, localOffsets, wgts);
477 catch(std::exception &e){
478 std::cout << rank <<
") Error " << e.what() << std::endl;
479 std::cerr << rank <<
") Error " << e.what() << std::endl;
484 "getLocalEdgeList localOffsets.size", 1)
487 for (
zlno_t i=0; i < nLocalRows; i++){
488 size_t edgeListSize = localOffsets[i+1] - localOffsets[i];
490 size_t val = numLocalNbors[i];
491 if (removeSelfEdges && haveDiag[i])
493 if (edgeListSize != val){
494 std::cout << rank <<
"vtx " << i <<
" of " << localOffsets.size()
495 <<
" Number of local edges in model " << edgeListSize
496 <<
" not equal to expected number of edges " << val
505 "getLocalEdgeList sum size", 1)
507 fail = ((removeSelfEdges ? size_t(totalLocalNbors-numLocalDiags)
508 : size_t(totalLocalNbors))
509 != numLocalNeighbors);
513 if (totalLocalNbors == 0){
515 std::cout <<
" Graph of local edges is empty" << std::endl;
518 printGraph(nLocalRows, vertexGids.getRawPtr(),
519 localEdges.getRawPtr(), NULL, localOffsets.getRawPtr(), comm);
524 if (rank == 0) std::cout <<
" Cleaning up" << std::endl;
529 delete [] numLocalNbors;
532 if (nVtxWeights > 0){
533 for (
int i=0; i < nVtxWeights; i++){
535 delete [] rowWeights[i];
537 delete [] rowWeights;
543 for (
int i=0; i < coordDim; i++){
551 if (rank==0) std::cout <<
" OK" << std::endl;
556 const RCP<
const Comm<int> > &comm,
557 int nVtxWeights,
int nnzWgtIdx,
int coordDim,
558 bool consecutiveIdsRequested,
bool removeSelfEdges,
bool buildLocalGraph)
560 int rank = comm->getRank();
563 cout << endl <<
"=======================" << endl;
564 if (fname.size() > 0)
565 cout << endl <<
"Test parameters: file name " << fname << endl;
567 cout << endl <<
"Test parameters: dimension ";
568 cout << xdim <<
"x" << ydim <<
"x" << zdim << endl;
571 cout <<
"Num Vertex Weights: " << nVtxWeights << endl;
573 cout <<
" Dimension " << nnzWgtIdx <<
" is number of neighbors" << endl;
575 cout <<
"Coordinate dim: " << coordDim << endl;
576 cout <<
"Request consecutive vertex gids: ";
577 cout << (consecutiveIdsRequested ?
"yes" :
"no") << endl;
578 cout <<
"Request to remove self edges: ";
579 cout << (removeSelfEdges ?
"yes" :
"no") << endl;
585 if (fname.size() > 0)
594 RCP<const tcrsMatrix_t> Mconsec = rcp_const_cast<
const tcrsMatrix_t>(M);
596 RCP<const Tpetra::CrsGraph<zlno_t, zgno_t> > graph = Mconsec->getCrsGraph();
602 std::cout <<
" TEST MatrixAdapter for graph having Consecutive IDs" 604 bool idsAreConsecutive =
true;
606 testAdapter<baseMAdapter_t,xMAdapter_t,tcrsMatrix_t>(Mconsec, graph, comm,
610 consecutiveIdsRequested,
615 std::cout <<
" TEST GraphAdapter for graph having Consecutive IDs" 617 testAdapter<baseGAdapter_t,xGAdapter_t,tcrsGraph_t>(graph, graph, comm,
621 consecutiveIdsRequested,
627 Array<zgno_t> myNewRows;
628 int nprocs = comm->getSize();
629 for (
size_t i=rank; i < Mconsec->getGlobalNumRows(); i+=nprocs)
630 myNewRows.push_back(i);
632 RCP<const tcrsMatrix_t> Mnonconsec = rcp_const_cast<
const tcrsMatrix_t>(
634 *Mconsec, myNewRows.size(), myNewRows.getRawPtr()));
636 graph = Mnonconsec->getCrsGraph();
642 std::cout <<
" TEST MatrixAdapter graph having Round-Robin IDs" 644 idsAreConsecutive =
false;
646 testAdapter<baseMAdapter_t,xMAdapter_t,tcrsMatrix_t>(Mnonconsec, graph, comm,
650 consecutiveIdsRequested,
655 std::cout <<
" TEST GraphAdapter graph having Round-Robin IDs" 657 testAdapter<baseGAdapter_t,xGAdapter_t,tcrsGraph_t>(graph, graph, comm,
661 consecutiveIdsRequested,
669 int main(
int argc,
char *argv[])
671 Teuchos::GlobalMPISession session(&argc, &argv);
672 Teuchos::RCP<const Teuchos::Comm<int> > comm =
673 Teuchos::DefaultComm<int>::getComm();
675 int rank = comm->getRank();
680 bool consecutiveIdsRequested =
false;
681 bool removeSelfEdges =
false;
682 bool buildLocalGraph =
false;
683 string fname(
"simple");
686 std::cout <<
"TESTING base case (global)" << std::endl;
688 nVtxWeights, nnzWgtIdx, coordDim,
689 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
692 std::cout <<
"TESTING with row weights (global)" << std::endl;
695 nVtxWeights, nnzWgtIdx, coordDim,
696 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
699 std::cout <<
"TESTING with weights = nnz (global)" << std::endl;
702 nVtxWeights, nnzWgtIdx, coordDim,
703 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
706 std::cout <<
"TESTING with multiple row weights and coords (global)" 711 nVtxWeights, nnzWgtIdx, coordDim,
712 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
715 std::cout <<
"TESTING with consecutiveIdsRequested (global)" << std::endl;
716 consecutiveIdsRequested =
true;
718 nVtxWeights, nnzWgtIdx, coordDim,
719 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
722 std::cout <<
"TESTING with consecutiveIdsRequested and removeSelfEdges " 723 <<
"(global)" << std::endl;
724 removeSelfEdges =
true;
726 nVtxWeights, nnzWgtIdx, coordDim,
727 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
730 std::cout <<
"TESTING with removeSelfEdges (global)" << std::endl;
731 consecutiveIdsRequested =
false;
733 nVtxWeights, nnzWgtIdx, coordDim,
734 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
737 std::cout <<
"TESTING base case (local)" << std::endl;
738 buildLocalGraph =
true;
739 consecutiveIdsRequested =
false;
740 removeSelfEdges =
false;
742 nVtxWeights, nnzWgtIdx, coordDim,
743 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
746 std::cout <<
"TESTING with row weights (local)" << std::endl;
749 nVtxWeights, nnzWgtIdx, coordDim,
750 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
753 std::cout <<
"TESTING with weights = nnz (local)" << std::endl;
756 nVtxWeights, nnzWgtIdx, coordDim,
757 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
760 std::cout <<
"TESTING with multiple row weights and coords (local)" 765 nVtxWeights, nnzWgtIdx, coordDim,
766 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
769 std::cout <<
"TESTING with consecutiveIdsRequested (local)" << std::endl;
770 consecutiveIdsRequested =
true;
772 nVtxWeights, nnzWgtIdx, coordDim,
773 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
776 std::cout <<
"TESTING with consecutiveIdsRequested and removeSelfEdges" 779 removeSelfEdges =
true;
781 nVtxWeights, nnzWgtIdx, coordDim,
782 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
785 std::cout <<
"TESTING with removeSelfEdges (local)" << std::endl;
786 consecutiveIdsRequested =
false;
788 nVtxWeights, nnzWgtIdx, coordDim,
789 consecutiveIdsRequested, removeSelfEdges, buildLocalGraph);
791 cout <<
"PASS" << endl;
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Zoltan2::GraphAdapter< tcrsGraph_t, simpleUser_t > baseGAdapter_t
int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
size_t getGlobalNumEdges() const
Returns the global number edges. For local graphs, the number of global edges is the number of local ...
void testAdapter(RCP< const MatrixOrGraph > &M, RCP< const Tpetra::CrsGraph< zlno_t, zgno_t > > &Mgraph, const RCP< const Comm< int > > &comm, bool idsAreConsecutive, int nVtxWeights, int nEdgeWeights, int nnzWgtIdx, int coordDim, bool consecutiveIdsRequested, bool removeSelfEdges, bool buildLocalGraph)
MatrixAdapter defines the adapter interface for matrices.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
GraphAdapter defines the interface for graph-based user data.
algorithm requires consecutive ids
size_t getLocalNumVertices() const
Returns the number vertices on this process.
A simple class that can be the User template argument for an InputAdapter.
size_t getLocalNumEdges() const
Returns the number of edges on this process. In global or subset graphs, includes off-process edges...
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
static RCP< User > doMigration(const User &from, size_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
Provides access for Zoltan2 to Xpetra::CrsGraph data.
size_t getGlobalNumVertices() const
Returns the global number vertices.
common code used by tests
size_t getEdgeList(ArrayView< const gno_t > &edgeIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process' edge (neighbor) global Ids, including off-process edges.
Zoltan2::XpetraCrsGraphAdapter< tcrsGraph_t, simpleUser_t > xGAdapter_t
Defines XpetraCrsGraphAdapter class.
void testGraphModel(string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim, const RCP< const Comm< int > > &comm, int nVtxWeights, int nnzWgtIdx, int coordDim, bool consecutiveIdsRequested, bool removeSelfEdges, bool buildLocalGraph)
algorithm requires no self edges
Defines the XpetraCrsMatrixAdapter class.
int getNumWeightsPerEdge() const
Returns the number (0 or greater) of weights per edge.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > simpleUser_t
int main(int argc, char *argv[])
Tpetra::CrsGraph< zlno_t, zgno_t, znode_t > tcrsGraph_t
Zoltan2::BasicVectorAdapter< simpleUser_t > simpleVAdapter_t
The StridedData class manages lists of weights or coordinates.
BasicVectorAdapter represents a vector (plus optional weights) supplied by the user as pointers to st...
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
const int SMALL_NUMBER_OF_ROWS
Test of GraphModel interface.
Tpetra::Map< zlno_t, zgno_t, znode_t > tmap_t
static const std::string fail
size_t getVertexList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process' vertex Ids and their weights.
void printGraph(zlno_t nrows, const zgno_t *v, const zgno_t *elid, const zgno_t *egid, const zlno_t *idx, const RCP< const Comm< int > > &comm)
GraphModel defines the interface required for graph models.
size_t getVertexCoords(ArrayView< input_t > &xyz) const
Sets pointers to this process' vertex coordinates, if available. Order of coordinate info matches tha...
Defines the GraphModel interface.
Zoltan2::XpetraCrsMatrixAdapter< tcrsMatrix_t, simpleUser_t > xMAdapter_t
Zoltan2::MatrixAdapter< tcrsMatrix_t, simpleUser_t > baseMAdapter_t
Defines the BasicVectorAdapter class.
model represents graph within only one rank
int getCoordinateDim() const
Returns the dimension (0 to 3) of vertex coordinates.
std::string testDataFilePath(".")