Transmission of matrix rows was easy, because the rows are stored
contiguously in C. But how are we to transfer columns? In C, matrices
are said to be row major ordered.
That is, if the rows of
our small matrices are COLUMNS
long, a column will
correspond to a set of entries in a long array containing all
matrix elements, which occur every COLUMNS
entries in
that long array.
In order to transfer columns we have to be able to tell
MPI that it should skip every COLUMNS
elements in order
to get from one column entry to the next one.
The call to function MPI_Type_vector
conveys this
information:
MPI_Type_vector (ROWS, 1, COLUMNS, MPI_INT, &column_type); MPI_Type_commit (&column_type);This call tells MPI that
column_type
is made of
ROWS
chunks of data, whose length is 1 per chunk
and whose MPI type is MPI_INT
, and that that data
is scattered throughout an array with the stride of
COLUMNS
.
The next call to MPI_Type_commit
tells MPI to
optimise retrieval of data from and writing to
what we now call column_type
-- if it can.
The function MPI_Type_vector
is discussed on page
61, section
3.12.1,
``MPI: A Message-Passing Interface Standard''.
Now we can attempt shifting columns of data between processes:
MPI_Sendrecv ( &matrix[0][1], 1, column_type, left_neighbour, 7007, &matrix[0][COLUMNS - 1], 1, column_type, right_neighbour, 7007, cartesian_communicator, &status ); MPI_Sendrecv ( &matrix[0][COLUMNS - 2], 1, column_type, right_neighbour, 8008, &matrix[0][0], 1, column_type, left_neighbour, 8008, cartesian_communicator, &status );Observe that we send and receive only one item of
column_type
,
not ROWS
items. column_type
represents the whole
column.
The first call sends the second column of the worker matrix
to the left_neighbour
while receiving the second column
of the neighbour matrix from the right_neighbour
into
its last column at the same time.
The second call sends the second last column of the worker matrix
to the right_neighbour
while receiving the second last
column of the neighbour matrix from the left_neighbour
into its first column at the same time.
Once this operation has been completed, the master process again collects matrices from the workers and displays them on standard output.