Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tpetra: Clean up CrsGraph::Swap testing #4089

Merged
merged 5 commits into from Dec 20, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 71 additions & 66 deletions packages/tpetra/core/test/CrsGraph/CrsGraph_UnitTests_Swap.cpp
Expand Up @@ -125,55 +125,58 @@ 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<class LO, class GO, class Node, class comm_t>
template<class LO, class GO, class Node, class comm_type>
william76 marked this conversation as resolved.
Show resolved Hide resolved
Teuchos::RCP<Tpetra::CrsGraph<LO, GO, Node>>
generate_crsgraph(Teuchos::RCP<comm_t> comm,
generate_crsgraph(Teuchos::RCP<comm_type> comm,
const std::vector<std::pair<GO, GO>>& gbl_edges,
const std::vector<std::pair<GO, size_t>>& gbl_row_owners,
const size_t gbl_num_columns,
const bool do_fillComplete=true)
{
using Teuchos::Comm;

typedef Tpetra::CrsGraph<LO, GO, Node> graph_t;
typedef Tpetra::Map<LO, GO, Node> map_t;
typedef Tpetra::CrsGraph<LO, GO, Node> graph_type;
william76 marked this conversation as resolved.
Show resolved Hide resolved
typedef Tpetra::Map<LO, GO, Node> map_type;

typedef typename std::map<GO, size_t> map_rows_t; // row_id, num_entries
typedef typename std::map<GO, size_t> map_rows_type; // row_id, num_entries


typedef typename std::vector<GO> vec_go_t;
typedef typename std::map<GO, vec_go_t> map_row_to_cols_t;
typedef typename std::vector<GO> vec_go_type;
typedef typename std::map<GO, vec_go_type> map_row_to_cols_type;

const bool verbose = Tpetra::Details::Behavior::verbose();

const size_t comm_rank = comm->getRank();
william76 marked this conversation as resolved.
Show resolved Hide resolved

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference (not for this PR), when printing debugging messages nondeterministically over multiple MPI processes to cout or cerr, good practice is to assemble each line (including the endline character) from each process as a string. This tends to make messages from different processes less mixed up. For example:

std::ostringstream os;
os << "(Proc " << myRank << ") gbl_num_rows: " << gbl_rows.size () << std::endl;
std::cout << os.str ();

Copy link
Contributor

@mhoemmen mhoemmen Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, it would make sense to turn that into a macro:

#define TPETRA_PRINTLN_LOCAL( out, myRank, stuff ) \
  do { \
    std::ostringstream tpl_out; \
    tpl_out << "(Proc " << myRank << ") " << stuff << std::endl; \
    out << tpl_out.str (); \
  } while (false)

Alas, C++ lacks the equivalent of Common Lisp's GENSYM, so I can't guarantee that tpl_out doesn't shadow any other names in scope. This should still work, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the #define TPETRA_PRINTLN_LOCAL a thing that already exists, or are you suggesting creating this macro in the file to use? Either way is doable, but if there's already a macro or function defined that does this then I'm happy to use it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@william76 I'm just suggesting this as a later thing to think about, after this PR is done :-D Thanks though!

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
Expand All @@ -187,9 +190,8 @@ generate_crsgraph(Teuchos::RCP<comm_t> 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<GO> global_ids;
Expand All @@ -200,19 +202,19 @@ generate_crsgraph(Teuchos::RCP<comm_t> 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<const map_t> row_map(new map_t(gbl_num_rows, global_ids.data(), lcl_num_rows, 0, comm));
RCP<const map_type> row_map(new map_type(gbl_num_rows, global_ids.data(), lcl_num_rows, 0, comm));

Teuchos::ArrayRCP<size_t> num_ent_per_row(lcl_num_rows);
size_t idx = 0;
Expand All @@ -226,15 +228,17 @@ generate_crsgraph(Teuchos::RCP<comm_t> comm,
}
}

#if 0
std::cout << "p=" << comm_rank << " | lcl_num_rows = " << lcl_num_rows << std::endl;
for(int i=0; i<lcl_num_rows; i++)
if(verbose)
{
std::cout << "p=" << comm_rank << " | " << "num_ent_per_row[" << i << "] = " << num_ent_per_row[i] << std::endl;
std::cout << "p=" << comm_rank << " | lcl_num_rows = " << lcl_num_rows << std::endl;
for(int i=0; i<lcl_num_rows; i++)
{
std::cout << "p=" << comm_rank << " | " << "num_ent_per_row[" << i << "] = "
<< num_ent_per_row[i] << std::endl;
}
}
#endif

RCP<graph_t> output_graph(new graph_t(row_map, num_ent_per_row, Tpetra::StaticProfile));
RCP<graph_type> output_graph(new graph_type(row_map, num_ent_per_row, Tpetra::StaticProfile));

for(auto& r: gbl_rows)
{
Expand All @@ -245,28 +249,29 @@ generate_crsgraph(Teuchos::RCP<comm_t> comm,
std::vector<GO> gbl_inds;
for(auto& v: r.second)
{
// std::cout << "p=" << comm_rank << " | " << "gbl_inds.push_back(" << v << ")" << std::endl;
gbl_inds.push_back(v);
}

#if 0
std::cout << "p=" << comm_rank << " | " << "gbl_inds size = " << r.second.size() << std::endl;
for(size_t i=0; i<gbl_inds.size(); i++)
if(verbose)
{
std::cout << "p=" << comm_rank << " | " << "gbl_inds[" << i << "] = " << gbl_inds[i] << std::endl;
std::cout << "p=" << comm_rank << " | " << "gbl_inds size = " << r.second.size() << std::endl;
for(size_t i=0; i<gbl_inds.size(); i++)
{
std::cout << "p=" << comm_rank << " | " << "gbl_inds[" << i << "] = "
<< gbl_inds[i] << std::endl;
}
std::cout << "p=" << comm_rank << " | " << "insertGlobalIndices(" << irow << ", "
<< r.second.size() << ", gbl_inds.data())" << std::endl;
}
#endif

// std::cout << "p=" << comm_rank << " | " << "insertGlobalIndices(" << irow << ", " << r.second.size() << ",
// gbl_inds.data())" << std::endl;
output_graph->insertGlobalIndices(irow, r.second.size(), gbl_inds.data());
}
}

RCP<const map_t> range_map = row_map;
RCP<const map_type> range_map = row_map;

const GO index_base = 0;
RCP<const map_t> domain_map(new map_t(gbl_num_columns, index_base, comm));
RCP<const map_type> domain_map(new map_type(gbl_num_columns, index_base, comm));

if(do_fillComplete)
{
Expand All @@ -288,14 +293,14 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(CrsGraph, Swap, LO, GO, Node)
using Teuchos::outArg;
using Teuchos::RCP;

typedef Teuchos::Comm<int> comm_t;
typedef Tpetra::CrsGraph<LO, GO, Node> graph_t;
typedef Teuchos::Comm<int> comm_type;
typedef Tpetra::CrsGraph<LO, GO, Node> graph_type;

typedef typename std::pair<GO, GO> pair_edge_t;
typedef typename std::pair<GO, size_t> pair_owner_t;
typedef typename std::pair<GO, GO> pair_edge_type;
typedef typename std::pair<GO, size_t> pair_owner_type;

typedef typename std::vector<pair_edge_t> vec_edges_t;
typedef typename std::vector<pair_owner_t> vec_owners_t;
typedef typename std::vector<pair_edge_type> vec_edges_type;
typedef typename std::vector<pair_owner_type> vec_owners_type;

bool verbose = Tpetra::Details::Behavior::verbose();

Expand All @@ -318,10 +323,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)
{
Expand All @@ -330,27 +335,27 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(CrsGraph, Swap, LO, GO, Node)
}

out << ">>> create graph_a" << std::endl;
RCP<graph_t> graph_a = generate_crsgraph<LO, GO, Node, comm_t>(comm, vec_edges, vec_owners, 12);
RCP<graph_type> graph_a = generate_crsgraph<LO, GO, Node, comm_type>(comm, vec_edges, vec_owners, 12);
//graph_a->describe(out, Teuchos::VERB_DEFAULT);

out << ">>> create graph_b" << std::endl;
RCP<graph_t> graph_b = generate_crsgraph<LO, GO, Node, comm_t>(comm, vec_edges, vec_owners, 12);
RCP<graph_type> graph_b = generate_crsgraph<LO, GO, Node, comm_type>(comm, vec_edges, vec_owners, 12);
//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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these typedefs btw :-D

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_t> graph_c = generate_crsgraph<LO, GO, Node, comm_t>(comm, vec_edges, vec_owners, 12);
RCP<graph_type> graph_c = generate_crsgraph<LO, GO, Node, comm_type>(comm, vec_edges, vec_owners, 12);
//graph_c->describe(out, Teuchos::VERB_DEFAULT);

out << ">>> create graph_d" << std::endl;
RCP<graph_t> graph_d = generate_crsgraph<LO, GO, Node, comm_t>(comm, vec_edges, vec_owners, 12);
RCP<graph_type> graph_d = generate_crsgraph<LO, GO, Node, comm_type>(comm, vec_edges, vec_owners, 12);
//graph_d->describe(out, Teuchos::VERB_DEFAULT);

TEST_EQUALITY(graph_a->isIdenticalTo(*graph_b), true); // graph_a and graph_b should be the same
Expand Down