template<typename Matrix>
void exchange_with_neighbors(Matrix& A,
/* ranks of the neighbors */
int previous, int next,
/* data type for an inner row, i.e. without the border */
MPI_Datatype rowtype) {
MPI_Request requests[4]; int request_index = 0;
MPI_Irecv(&A(0, 1), 1, rowtype, previous, 0,
MPI_COMM_WORLD, &requests[request_index++]);
MPI_Irecv(&A(A.numRows-1, 1), 1, rowtype, next, 0,
MPI_COMM_WORLD, &requests[request_index++]);
MPI_Isend(&A(1, 1), 1, rowtype, previous, 0,
MPI_COMM_WORLD, &requests[request_index++]);
MPI_Isend(&A(A.numRows-2, 1), 1, rowtype, next, 0,
MPI_COMM_WORLD, &requests[request_index++]);
for (auto& request: requests) {
MPI_Status status;
MPI_Wait(&request, &status);
}
}