Docsity

MPI Programming: Parallel Matrix-Vector and Matrix-Matrix Multiplication - Prof. Susan Hay, Exams of Algorithms and Programming

Toggle sidebar
bg1
COSC 612 Programming with MPI Distributed 10/15/09
Due: 10/29/09 ?
You will implement the following using MPI. You can demo on a single node (e.g., your
laptop), with a variety of processes.
There are three parts to this program. Do them in order. These assignments are slightly
modified from Pacheco, Parallel Programming with MPI, Chapter 5. Use your answer to
#1 as help in implementing the subsequent parts.
“1. In order to avoid some of the problems that may occur with I/O on your system, you
should only input the scalar parameters to your programs. For example, if you are
multiplying a matrix by a vector, you should only input the order of the matrix. The
program should generate the matrix and vector (e.g., a matrix of all ones and a vectors
of all ones). Of course, you won’t be able to tell whether your program works unless
you print the results of your computations. So you should write two output functions:
a vector output function and a matrix output function for matrices distributed by
block rows*.”
2. Test the program that will compute a matrix-vector multiply. (Recall, you had to write
a threaded version in the “elementary programming exercises. Does the MPI version
have a significantly different approach?)
/* From Pacheco, Parallel Programming with MPI */
/* All arrays are allocated in calling program */
void Parallel_matrix_vector_prod (
LOCAL_MATRIX_T local_A /* in */,
int m /* in */,
int n /* in */,
float local_x[] /* in */,
float global_x[] /* in */,
float local_y[] /* out */,
int local_m /* in */,
int local_n /* in */ )
{
/* local_m = n/p, local_n = n/p */
int i, j;
MPI_Allgather(local_x, local_n, MPI_FLOAT, global_x,
local_m, MPI_FLOAT, MPI_COMM_WORLD);
for (i = 0; i < local_m; i++ ) {
local_y[i] = 0.0;
for (j = 0; j < n; j++ )
local_y[i] = local_y[i] +
local_A[i][j] * global_x[j];
pf2

Partial preview of the text

Download MPI Programming: Parallel Matrix-Vector and Matrix-Matrix Multiplication - Prof. Susan Hay and more Exams Algorithms and Programming in PDF only on Docsity!

COSC 612 Programming with MPI Distributed 10/15/ Due: 10/29/09? You will implement the following using MPI. You can demo on a single node (e.g., your laptop), with a variety of processes. There are three parts to this program. Do them in order. These assignments are slightly modified from Pacheco, Parallel Programming with MPI , Chapter 5. Use your answer to #1 as help in implementing the subsequent parts. “1. In order to avoid some of the problems that may occur with I/O on your system, you should only input the scalar parameters to your programs. For example, if you are multiplying a matrix by a vector, you should only input the order of the matrix. The program should generate the matrix and vector (e.g., a matrix of all ones and a vectors of all ones). Of course, you won’t be able to tell whether your program works unless you print the results of your computations. So you should write two output functions: a vector output function and a matrix output function for matrices distributed by block rows*.”

  1. Test the program that will compute a matrix-vector multiply. (Recall, you had to write a threaded version in the “elementary programming exercises. Does the MPI version have a significantly different approach?) /* From Pacheco, Parallel Programming with MPI / / All arrays are allocated in calling program / void Parallel_matrix_vector_prod ( LOCAL_MATRIX_T local_A / in /, int m / in /, int n / in /, float local_x[] / in /, float global_x[] / in /, float local_y[] / out /, int local_m / in /, int local_n / in / ) { / local_m = n/p, local_n = n/p */ int i, j; MPI_Allgather(local_x, local_n, MPI_FLOAT, global_x, local_m, MPI_FLOAT, MPI_COMM_WORLD); for (i = 0; i < local_m; i++ ) { local_y[i] = 0.0; for (j = 0; j < n; j++ ) local_y[i] = local_y[i] + local_A[i][j] * global_x[j];

} /* for i / } /Parallel_matrix_vector_prod */ “3. Recollect that a matrix-matrix product is formed by taking the dot product of each row of the left factor with each column of the right factor. More explicitly, suppose A = (aij) is an m X n matrix and B = (bij) is an n X r matrix. Then the product matrix, AB = C = (cij) is an m X r matrix, and cij is the dot product of the ith row of A with the jth column of B, or cij = ai0b0j + ai1b1j +... + ai,n- 1 bn-1, j. If A and B have been distributed by block rows, then we can use out parallel matrix vector product to compute a parallel matrix-matrix product as follows: for each column x of B { compute the parallel matrix-vector product Ax } Write a program that will compute a parallel matrix-matrix product using this algorithm.