HP-MPI User's Guide (11th Edition)
Example applications
cart.C
Appendix A246
data = lrank;
MPI_Cart_coords(comm, lrank, NDIMS, coords);
}
}
// A destructor
Node::~Node(void)
{
if (comm != MPI_COMM_NULL) {
MPI_Comm_free(&comm);
}
}
// Shift function
void Node::shift(Direction dir)
{
if (comm == MPI_COMM_NULL) { return; }
int direction, disp, src, dest;
if (dir == NORTH) {
direction = 0; disp = -1;
} else if (dir == SOUTH) {
direction = 0; disp = 1;
} else if (dir == EAST) {
direction = 1; disp = 1;
} else {
direction = 1; disp = -1;
}
MPI_Cart_shift(comm, direction, disp, &src, &dest);
MPI_Status stat;
MPI_Sendrecv_replace(&data, 1, MPI_INT, dest, 0, src, 0, comm,
&stat);
}
// Synchronize and print the data being held
void Node::print(void)
{
if (comm != MPI_COMM_NULL) {
MPI_Barrier(comm);
if (lrank == 0) { puts(""); } // line feed
MPI_Barrier(comm);
printf("(%d, %d) holds %d\n", coords[0], coords[1], data);
}
}
// Print object's profile
void Node::profile(void)
{
// Non-member does nothing
if (comm == MPI_COMM_NULL) { return; }
// Print "Dimensions" at first
if (lrank == 0) {
printf("Dimensions: (%d, %d)\n", dims[0], dims[1]);
}