67 #include <Teuchos_Comm.hpp> 68 #include <Teuchos_DefaultComm.hpp> 69 #include <Teuchos_ArrayView.hpp> 75 using Teuchos::DefaultComm;
76 using Teuchos::ArrayView;
78 typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t>
tcrsMatrix_t;
79 typedef Tpetra::Map<lno_t, gno_t, node_t>
tmap_t;
87 const int *owner,
const lno_t *idx,
const RCP<
const Comm<int> > &comm)
89 int rank = comm->getRank();
90 int nprocs = comm->getSize();
94 std::cout <<
"Global graph:" << std::endl;
96 std::cout <<
"Local graph:" << std::endl;
99 for (
int p=0; p < nprocs; p++){
101 std::cout <<
"Rank " << p << std::endl;
103 for (
lno_t i=0; i < nrows; i++){
104 std::cout <<
" Vtx " << *v++ <<
": ";
106 for (
lno_t j=idx[i]; j < idx[i+1]; j++)
107 std::cout << *elid++ <<
" (" << *owner++ <<
") ";
109 for (
lno_t j=idx[i]; j < idx[i+1]; j++)
110 std::cout << *egid++ <<
" (" << *owner++ <<
") ";
111 std::cout << std::endl;
116 for (
lno_t i=0; i < nrows; i++){
117 std::cout <<
" Vtx " << i <<
": ";
119 for (
lno_t j=idx[i]; j < idx[i+1]; j++)
120 std::cout << *elid++ <<
" ";
122 for (
lno_t j=idx[i]; j < idx[i+1]; j++)
123 std::cout << *egid++ <<
" ";
124 std::cout << std::endl;
135 const RCP<
const Comm<int> > &comm,
136 bool idsAreConsecutive,
137 int rowWeightDim,
int nnzDim,
int coordDim,
138 bool consecutiveIdsRequested,
bool removeSelfEdges)
141 int rank = comm->getRank();
142 int nprocs = comm->getSize();
145 lno_t nLocalRows = M->getNodeNumRows();
146 lno_t nLocalNZ = M->getNodeNumEntries();
147 gno_t nGlobalRows = M->getGlobalNumRows();
148 gno_t nGlobalNZ = M->getGlobalNumEntries();
155 std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags;
156 if (consecutiveIdsRequested)
166 for (
int i=0; i < coordDim; i++){
167 coords[i] =
new scalar_t [nLocalRows];
168 for (
lno_t j=0; j < nLocalRows; j++){
169 coords[i][j] = 100000*i + j;
174 if (rowWeightDim > 0){
175 rowWeights =
new scalar_t * [rowWeightDim];
176 for (
int i=0; i < rowWeightDim; i++){
178 rowWeights[i] = NULL;
180 rowWeights[i] =
new scalar_t [nLocalRows];
181 for (
lno_t j=0; j < nLocalRows; j++){
182 rowWeights[i][j] = 200000*i + j;
188 adapter_t tmi(M, coordDim, rowWeightDim);
190 for (
int i=0; i < rowWeightDim; i++){
192 tmi.setRowWeightIsNumberOfNonZeros(i);
194 tmi.setRowWeights(i, rowWeights[i], 1);
197 for (
int i=0; i < coordDim; i++){
198 tmi.setRowCoordinates(i, coords[i], 1);
201 int numLocalDiags = M->getNodeNumDiags();
202 int numGlobalDiags = M->getGlobalNumDiags();
204 const RCP<const tmap_t> rowMap = M->getRowMap();
205 const RCP<const tmap_t> colMap = M->getColMap();
210 int minLocalGID = rowMap->getMinGlobalIndex();
211 int maxLocalGID = rowMap->getMaxGlobalIndex();
213 int *numNbors =
new int [nLocalRows];
214 int *numLocalNbors =
new int [nLocalRows];
215 bool *haveDiag =
new bool [nLocalRows];
216 gno_t totalLocalNbors = 0;
218 for (
lno_t i=0; i < nLocalRows; i++){
219 numLocalNbors[i] = 0;
221 ArrayView<const lno_t> idx;
222 ArrayView<const scalar_t> val;
223 M->getLocalRowView(i, idx, val);
224 numNbors[i] = idx.size();
226 for (
lno_t j=0; j < idx.size(); j++){
231 gno_t gidVal = colMap->getGlobalElement(idx[j]);
232 if (gidVal >= minLocalGID && gidVal <= maxLocalGID){
243 const base_adapter_t *baseTmi = &tmi;
249 catch (std::exception &e){
250 std::cerr << rank <<
") " << e.what() << std::endl;
265 size_t num = (removeSelfEdges ? (nLocalNZ-numLocalDiags) : nLocalNZ);
271 num = totalLocalNbors;
272 if (!removeSelfEdges){
273 for (
lno_t i=0; i < nLocalRows; i++)
282 num = (removeSelfEdges ? (nGlobalNZ-numGlobalDiags) : nGlobalNZ);
300 ArrayView<const gno_t> vertexGids;
301 ArrayView<input_t> crds;
302 ArrayView<input_t> wgts;
307 catch (std::exception &e){
308 std::cerr << rank <<
") Error " << e.what() << std::endl;
313 if (vertexGids.size() != nLocalRows)
319 if (idsAreConsecutive){
320 for (
lno_t i=0; i < nLocalRows; i++){
321 if (vertexGids[i] != minLocalGID + i) {
329 for (
lno_t i=0; i < nLocalRows; i++, myGid += nprocs){
330 if (vertexGids[i] != myGid){
339 if ((crds.size() != coordDim) || (wgts.size() != rowWeightDim))
343 for (
int i=0; !fail && i < coordDim; i++){
344 for (
lno_t j=0; j < nLocalRows; j++){
345 if (crds[i][j] != 200000*i + j){
353 for (
int i=0; !fail && i < rowWeightDim; i++){
355 for (
lno_t j=0; j < nLocalRows; j++){
357 if (removeSelfEdges && haveDiag[j])
359 if (wgts[i][j] != val){
366 for (
lno_t j=0; j < nLocalRows; j++){
367 if (wgts[i][j] != 100000*i + j){
377 ArrayView<const gno_t> edgeGids;
378 ArrayView<const int> procIds;
379 ArrayView<const lno_t> offsets;
383 numEdges = model->
getEdgeList(edgeGids, procIds, offsets, wgts);
385 catch(std::exception &e){
386 std::cerr << rank <<
") Error " << e.what() << std::endl;
394 for (ArrayView<const lno_t>::size_type i=0; i < offsets.size()-1; i++){
395 size_t edgeListSize = offsets[i+1] - offsets[i];
397 size_t val = numNbors[i];
398 if (removeSelfEdges && haveDiag[i])
400 if (edgeListSize != val){
408 if (nGlobalRows < 200){
409 printGraph(nLocalRows, vertexGids.getRawPtr(), NULL,
410 edgeGids.getRawPtr(), procIds.getRawPtr(), offsets.getRawPtr(), comm);
414 std::cout <<
" " << nGlobalRows <<
" total rows" << std::endl;
419 ArrayView<const lno_t> localEdges;
420 ArrayView<const lno_t> localOffsets;
421 size_t numLocalNeighbors=0;
426 catch(std::exception &e){
427 std::cerr << rank <<
") Error " << e.what() << std::endl;
432 #ifdef THIS_CODE_DOESNT_FAIL 434 for (
size_t i=0; i < localOffsets.size()-1; i++){
435 size_t edgeListSize = localOffsets[i+1] - localOffsets[i];
437 size_t val = numLocalNbors[i];
438 if (removeSelfEdges && haveDiag[i])
440 if (edgeListSize != val){
449 if (
size_t(totalLocalNbors) != numLocalNeighbors)
454 if (nGlobalRows < 200){
455 if (totalLocalNbors == 0){
457 std::cout <<
" Graph of local edges is empty" << std::endl;
460 printGraph(nLocalRows, vertexGids.getRawPtr(),
461 localEdges.getRawPtr(), NULL, NULL, localOffsets.getRawPtr(), comm);
470 delete [] numLocalNbors;
473 if (rowWeightDim > 0){
474 for (
int i=0; i < rowWeightDim; i++){
476 delete [] rowWeights[i];
478 delete [] rowWeights;
482 for (
int i=0; i < coordDim; i++){
490 if (rank==0) std::cout <<
" OK" << std::endl;
494 const RCP<
const Comm<int> > &comm,
495 int rowWeightDim,
int nnzDim,
int coordDim,
496 bool consecutiveIdsRequested,
bool removeSelfEdges)
498 int rank = comm->getRank();
501 cout << endl <<
"=======================" << endl;
502 if (fname.size() > 0)
503 cout << endl <<
"Test parameters: file name " << fname << endl;
505 cout << endl <<
"Test parameters: dimension ";
506 cout << xdim <<
"x" << ydim <<
"x" << zdim << endl;
509 cout <<
"Vertex weight dim: " << rowWeightDim << endl;
511 cout <<
" Dimension " << nnzDim <<
" is number of neighbors" << endl;
513 cout <<
"Coordinate dim: " << coordDim << endl;
514 cout <<
"Request consecutive vertex gids: ";
515 cout << (consecutiveIdsRequested ?
"yes" :
"no") << endl;
516 cout <<
"Request to remove self edges: ";
517 cout << (removeSelfEdges ?
"yes" :
"no") << endl;
520 typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t>
tcrsMatrix_t;
525 if (fname.size() > 0)
534 RCP<const tcrsMatrix_t> Mconsec = rcp_const_cast<
const tcrsMatrix_t>(M);
536 RCP<const Tpetra::CrsGraph<lno_t, gno_t> > graph = Mconsec->getCrsGraph();
538 printTpetraGraph<lno_t, gno_t>(comm, *graph, cout, 100,
539 "Graph with consecutive IDs");
541 bool idsAreConsecutive =
true;
544 rowWeightDim, nnzDim, coordDim,
545 consecutiveIdsRequested, removeSelfEdges);
548 testGraphInput(Mconsec, comm, idsAreConsecutive,
549 rowWeightDim, nnzDim, coordDim,
550 consecutiveIdsRequested, removeSelfEdges);
555 #ifdef TODO_THESE_HAVE_BEEN_TESTED 556 Array<gno_t> myNewRows;
557 for (
size_t i=rank; i < Mconsec->getGlobalNumRows(); i+=nprocs)
558 myNewRows.push_back(i);
560 RCP<const tcrsMatrix_t> Mnonconsec =
562 Mconsec, myNewRows.size(), myNewRows.getRawPtr());
564 graph = Mnonconsec->getCrsGraph();
566 printTpetraGraph<lno_t, gno_t>(comm, *graph, cout, 100,
567 "Graph with non-consecutive IDs");
569 idsAreConsecutive =
false;
572 rowWeightDim, nnzDim, coordDim,
573 consecutiveIdsRequested, removeSelfEdges);
576 testGraphInput(Mnonconsec, comm, idsAreConsecutive,
577 rowWeightDim, nnzDim, coordDim,
578 consecutiveIdsRequested, removeSelfEdges);
586 int main(
int argc,
char *argv[])
588 Teuchos::GlobalMPISession session(&argc, &argv);
589 Teuchos::RCP<const Teuchos::Comm<int> > comm =
590 Teuchos::DefaultComm<int>::getComm();
592 int rank = comm->getRank();
597 bool consecutiveIdsRequested=
false, removeSelfEdges=
false;
598 string fname(
"simple");
601 rowWeightDim, nnzDim, coordDim,
602 consecutiveIdsRequested, removeSelfEdges);
604 #ifdef TODO_THESE_HAVE_BEEN_TESTED 608 rowWeightDim, nnzDim, coordDim,
609 consecutiveIdsRequested, removeSelfEdges);
614 rowWeightDim, nnzDim, coordDim,
615 consecutiveIdsRequested, removeSelfEdges);
621 rowWeightDim, nnzDim, coordDim,
622 consecutiveIdsRequested, removeSelfEdges);
624 consecutiveIdsRequested =
true;
627 rowWeightDim, nnzDim, coordDim,
628 consecutiveIdsRequested, removeSelfEdges);
630 removeSelfEdges =
true;
633 rowWeightDim, nnzDim, coordDim,
634 consecutiveIdsRequested, removeSelfEdges);
636 consecutiveIdsRequested =
false;
639 rowWeightDim, nnzDim, coordDim,
640 consecutiveIdsRequested, removeSelfEdges);
644 cout <<
"PASS" << endl;
int getEdgeWeightDim() const
Returns the dimension (0 or greater) of edge weights.
int getVertexWeightDim() const
Returns the dimension (0 or greater) of vertex weights.
size_t getLocalNumLocalEdges() const
Returns the number of local edges on this process. Does not include edges to off-process vertices...
Zoltan2::StridedData< lno_t, scalar_t > input_t
size_t getLocalNumGlobalEdges() const
Returns the number of global edges on this process. Includes remote edges.
algorithm requires consecutive ids
void printGraph(lno_t nrows, const gno_t *v, const lno_t *elid, const gno_t *egid, const int *owner, const lno_t *idx, const RCP< const Comm< int > > &comm)
void testMatrixInput(RCP< const Tpetra::CrsMatrix< scalar_t, lno_t, gno_t > > &M, const RCP< const Comm< int > > &comm, bool idsAreConsecutive, int rowWeightDim, int nnzDim, int coordDim, bool consecutiveIdsRequested, bool removeSelfEdges)
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
Tpetra::Map< lno_t, gno_t, node_t > tmap_t
common code used by tests
size_t getGlobalNumVertices() const
Returns the global number vertices.
Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > tcrsMatrix_t
size_t getEdgeList(ArrayView< const gno_t > &edgeIds, ArrayView< const int > &procIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process' edge (neighbor) global Ids, including off-process edges.
void testGraphModel(string fname, gno_t xdim, gno_t ydim, gno_t zdim, const RCP< const Comm< int > > &comm, int rowWeightDim, int nnzDim, int coordDim, bool consecutiveIdsRequested, bool removeSelfEdges)
int getCoordinateDim() const
Returns the dimension (0 to 3) of vertex coordinates.
int main(int argc, char *argv[])
size_t getGlobalNumEdges() const
Returns the global number edges.
The StridedData class manages lists of weights or coordinates.
static RCP< const User > doMigration(const RCP< const User > &from, lno_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...
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
size_t getLocalNumVertices() const
Returns the number vertices on this process.
size_t getLocalEdgeList(ArrayView< const lno_t > &edgeIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts)
Sets pointers to this process' local-only edge (neighbor) LNOs, using the same implied vertex LNOs re...
GraphModel defines the interface required for graph models.
algorithm requires no self edges
Defines the GraphModel interface.
size_t getVertexList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &xyz, ArrayView< input_t > &wgts) const
Sets pointers to this process' vertex Ids and their weights.
std::string testDataFilePath(".")