diff --git a/packages/tpetra/core/test/CrsGraph/CrsGraph_UnitTests_Swap.cpp b/packages/tpetra/core/test/CrsGraph/CrsGraph_UnitTests_Swap.cpp index 9bef4be744a7..6e019b2174f8 100644 --- a/packages/tpetra/core/test/CrsGraph/CrsGraph_UnitTests_Swap.cpp +++ b/packages/tpetra/core/test/CrsGraph/CrsGraph_UnitTests_Swap.cpp @@ -114,8 +114,6 @@ TEUCHOS_STATIC_SETUP() " this option is ignored and a serial comm is always used."); clp.setOption("error-tol-slack", &errorTolSlack, "Slack off of machine epsilon used to check test results"); } -// todo: update options so that this test won't run in serial (it's not set up for that currently) - @@ -125,55 +123,55 @@ TEUCHOS_STATIC_SETUP() // gbl_num_columns: Max # of columns in the matrix-representation of the graph. // This should be >= the highest value of v from all edges (u,v) in edges. // Note: u and v are 0-indexed, so if the highest v is 11, then this should be 12. -template -Teuchos::RCP> -generate_crsgraph(Teuchos::RCP comm, - const std::vector>& gbl_edges, - const std::vector>& gbl_row_owners, - const size_t gbl_num_columns, - const bool do_fillComplete=true) +template +Teuchos::RCP > +generate_crsgraph(Teuchos::RCP >& comm, + const std::vector >& gbl_edges, + const std::vector >& gbl_row_owners, + const size_t gbl_num_columns, + const bool do_fillComplete=true) { using Teuchos::Comm; - typedef Tpetra::CrsGraph graph_t; - typedef Tpetra::Map map_t; - - typedef typename std::map map_rows_t; // row_id, num_entries + using graph_type = Tpetra::CrsGraph; // Tpetra CrsGraph type + using map_type = Tpetra::Map; // Tpetra Map type + using map_rows_type = std::map; // map rows to pid's + using vec_go_type = std::vector; // vector of GlobalOrdinals + using map_row_to_cols_type = std::map; // Map rows to columns + const bool verbose = Tpetra::Details::Behavior::verbose(); - typedef typename std::vector vec_go_t; - typedef typename std::map map_row_to_cols_t; + const int comm_rank = comm->getRank(); - const size_t comm_rank = comm->getRank(); - - map_row_to_cols_t gbl_rows; + map_row_to_cols_type gbl_rows; for(auto& e: gbl_edges) { if(gbl_rows.find(e.first) == gbl_rows.end()) { - gbl_rows[ e.first ] = vec_go_t(); + gbl_rows[ e.first ] = vec_go_type(); } gbl_rows[ e.first ].push_back(e.second); } - map_rows_t gbl_row2pid; + map_rows_type gbl_row2pid; for(auto& p: gbl_row_owners) { gbl_row2pid.insert(p); } // Print out some debugging information on what's in the graph - #if 0 - std::cout << "p=0 | gbl_num_rows: " << gbl_rows.size() << std::endl; - for(auto& p: gbl_rows) + if(verbose) { - std::cout << "p=0 | gbl_row : " << p.first << " (" << p.second.size() << ") "; - for(auto& j: p.second) + std::cout << "p=0 | gbl_num_rows: " << gbl_rows.size() << std::endl; + for(auto& p: gbl_rows) { - std::cout << j << " "; + std::cout << "p=0 | gbl_row : " << p.first << " (" << p.second.size() << ") "; + for(auto& j: p.second) + { + std::cout << j << " "; + } + std::cout << std::endl; } - std::cout << std::endl; + for(auto& p: gbl_row2pid) + std::cout << "p=0 | gbl_row2pid : " << p.first << " => " << p.second << std::endl; } - for(auto& p: gbl_row2pid) - std::cout << "p=0 | gbl_row2pid : " << p.first << " => " << p.second << std::endl; - #endif GO gbl_num_rows = gbl_rows.size(); // the number of global rows LO lcl_num_rows = 0; // this will be updated later @@ -187,9 +185,8 @@ generate_crsgraph(Teuchos::RCP comm, } } - #if 0 - std::cout << "p=" << comm_rank << " | " << "lcl_num_rows = " << lcl_num_rows << std::endl; - #endif + if(verbose) + std::cout << "p=" << comm_rank << " | " << "lcl_num_rows = " << lcl_num_rows << std::endl; // Set up global ids std::vector global_ids; @@ -200,73 +197,76 @@ generate_crsgraph(Teuchos::RCP comm, global_ids.push_back(r.first); } } - #if 0 - for(size_t i = 0; i < global_ids.size(); i++) + + if(verbose) { - std::cout << "p=" << comm_rank << " | " << "global_ids[" << i << "] = " << global_ids[i] << std::endl; + for(size_t i = 0; i < global_ids.size(); i++) + { + std::cout << "p=" << comm_rank << " | " << "global_ids[" << i << "] = " << global_ids[i] << std::endl; + } + std::cout << "p=" << comm_rank << " | " << "row_map = map_type(" << gbl_num_rows << ", " + << "global_ids.data(), " << lcl_num_rows << ", 0, comm)" << std::endl; } - #endif // Create the Row Map - #if 0 - std::cout << "p=" << comm_rank << " | " << "row_map = map_t(" << gbl_num_rows << ", " << "global_ids.data(), " - << lcl_num_rows << ", 0, comm)" << std::endl; - #endif - RCP row_map(new map_t(gbl_num_rows, global_ids.data(), lcl_num_rows, 0, comm)); + RCP row_map(new map_type(gbl_num_rows, global_ids.data(), lcl_num_rows, 0, comm)); Teuchos::ArrayRCP num_ent_per_row(lcl_num_rows); size_t idx = 0; for(auto& r: gbl_rows) { const GO irow = r.first; - const size_t row_pid = gbl_row2pid.find(irow)->second; - if(row_pid == comm_rank) + const int row_pid = gbl_row2pid.find(irow)->second; + if(comm_rank == row_pid) { num_ent_per_row[ idx++ ] = r.second.size(); } } - #if 0 - std::cout << "p=" << comm_rank << " | lcl_num_rows = " << lcl_num_rows << std::endl; - for(int i=0; iinsertGlobalIndices(irow, r.second.size(), gbl_inds.data()); } } - RCP range_map = row_map; + RCP range_map = row_map; const GO index_base = 0; - RCP domain_map(new map_t(gbl_num_columns, index_base, comm)); + RCP domain_map(new map_type(gbl_num_columns, index_base, comm)); if(do_fillComplete) { @@ -288,18 +288,17 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(CrsGraph, Swap, LO, GO, Node) using Teuchos::outArg; using Teuchos::RCP; - typedef Teuchos::Comm comm_t; - typedef Tpetra::CrsGraph graph_t; + using graph_type = Tpetra::CrsGraph; // Tpetra CrsGraph type + using pair_edge_type = std::pair; // Edge typs, (u,v) using GlobalOrdinal type + using pair_owner_type = std::pair; // For row owners, pairs are (rowid, comm rank) - typedef typename std::pair pair_edge_t; - typedef typename std::pair pair_owner_t; - - typedef typename std::vector vec_edges_t; - typedef typename std::vector vec_owners_t; + using vec_edges_type = std::vector; // For vectors of edges + using vec_owners_type = std::vector; // For vectors of owners bool verbose = Tpetra::Details::Behavior::verbose(); auto initialComm = getDefaultComm(); + TEUCHOS_TEST_FOR_EXCEPTION(initialComm->getSize() < 2, std::runtime_error, "This test requires at least two processors."); // Set up a communicator that has exactly two processors in it for the actual test. @@ -318,10 +317,10 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(CrsGraph, Swap, LO, GO, Node) success = true; - vec_edges_t vec_edges = {pair_edge_t(0, 0), pair_edge_t(0, 11), pair_edge_t(1, 3), pair_edge_t(1, 4), - pair_edge_t(3, 2), pair_edge_t(7, 5), pair_edge_t(7, 7), pair_edge_t(10, 6)}; - vec_owners_t vec_owners = {pair_owner_t(0, 0), pair_owner_t(1, 0), pair_owner_t(3, 0), pair_owner_t(7, 1), - pair_owner_t(10, 1)}; + vec_edges_type vec_edges = {pair_edge_type(0, 0), pair_edge_type(0, 11), pair_edge_type(1, 3), pair_edge_type(1, 4), + pair_edge_type(3, 2), pair_edge_type(7, 5), pair_edge_type(7, 7), pair_edge_type(10, 6)}; + vec_owners_type vec_owners = {pair_owner_type(0, 0), pair_owner_type(1, 0), pair_owner_type(3, 0), pair_owner_type(7, 1), + pair_owner_type(10, 1)}; if(verbose) { @@ -330,39 +329,40 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(CrsGraph, Swap, LO, GO, Node) } out << ">>> create graph_a" << std::endl; - RCP graph_a = generate_crsgraph(comm, vec_edges, vec_owners, 12); - //graph_a->describe(out, Teuchos::VERB_DEFAULT); + RCP graph_a = generate_crsgraph(comm, vec_edges, vec_owners, 12); + if(verbose) graph_a->describe(out, Teuchos::VERB_DEFAULT); out << ">>> create graph_b" << std::endl; - RCP graph_b = generate_crsgraph(comm, vec_edges, vec_owners, 12); - //graph_b->describe(out, Teuchos::VERB_DEFAULT); + RCP graph_b = generate_crsgraph(comm, vec_edges, vec_owners, 12); + if(verbose) graph_b->describe(out, Teuchos::VERB_DEFAULT); vec_edges.clear(); vec_owners.clear(); - vec_edges = {pair_edge_t(0, 0), pair_edge_t(0, 11), pair_edge_t(1, 7), pair_edge_t(1, 8), - pair_edge_t(3, 1), pair_edge_t(7, 5), pair_edge_t(10, 4) }; - vec_owners = {pair_owner_t(0, 0), pair_owner_t(1, 0), pair_owner_t(3, 1), pair_owner_t(7, 1), - pair_owner_t(10, 1)}; + vec_edges = {pair_edge_type(0, 0), pair_edge_type(0, 11), pair_edge_type(1, 7), pair_edge_type(1, 8), + pair_edge_type(3, 1), pair_edge_type(7, 5), pair_edge_type(10, 4) }; + vec_owners = {pair_owner_type(0, 0), pair_owner_type(1, 0), pair_owner_type(3, 1), pair_owner_type(7, 1), + pair_owner_type(10, 1)}; out << ">>> create graph_c" << std::endl; - RCP graph_c = generate_crsgraph(comm, vec_edges, vec_owners, 12); - //graph_c->describe(out, Teuchos::VERB_DEFAULT); + RCP graph_c = generate_crsgraph(comm, vec_edges, vec_owners, 12); + if(verbose) graph_c->describe(out, Teuchos::VERB_DEFAULT); out << ">>> create graph_d" << std::endl; - RCP graph_d = generate_crsgraph(comm, vec_edges, vec_owners, 12); - //graph_d->describe(out, Teuchos::VERB_DEFAULT); + RCP graph_d = generate_crsgraph(comm, vec_edges, vec_owners, 12); + if(verbose) graph_d->describe(out, Teuchos::VERB_DEFAULT); + // Verify the initial identical-to state of the graphs. TEST_EQUALITY(graph_a->isIdenticalTo(*graph_b), true); // graph_a and graph_b should be the same TEST_EQUALITY(graph_c->isIdenticalTo(*graph_d), true); // graph_c and graph_d should be the same TEST_EQUALITY(graph_a->isIdenticalTo(*graph_c), false); // graph_a and graph_c should be different TEST_EQUALITY(graph_b->isIdenticalTo(*graph_d), false); // graph_b and graph_d should be different - // Swap graph b and c out << ">>> swap graph_b and graph_c" << std::endl; graph_c->swap(*graph_b); + // Verify that the graphs did get swapped. TEST_EQUALITY(graph_a->isIdenticalTo(*graph_b), false); // graph_a and graph_b should be different TEST_EQUALITY(graph_c->isIdenticalTo(*graph_d), false); // graph_c and graph_d should be different TEST_EQUALITY(graph_a->isIdenticalTo(*graph_c), true); // graph_a and graph_c should be the same