import mpi.*; import static mpi.MPI.slice; /** * @author kmazouzi * * Matrix Multiply using Master/Worker paradigm */ public class MatrixPar { public static void main(String[] args) throws MPIException { int N = 400; int MASTER = 0; int FROM_MASTER = 1; int FROM_WORKER = 2; int numtasks, /* number of tasks in partition */ taskid, /* a task identifier */ numworkers, /* number of worker tasks */ source, /* task id of message source */ dest, /* task id of message destination */ averow, extra, /* used to determine rows sent to each worker */ i, j, k, /* misc */ count; int[] a; /* matrix A to be multiplied */ int[] b; /* matrix B to be multiplied */ int[] c; /* result matrix C */ int[] offset = new int[1]; int[] rows = new int[1]; /* rows of matrix A sent to each worker */ MPI.Init(args); if (args.length == 1) { N = Integer.parseInt(args[0]); } taskid = MPI.COMM_WORLD.getRank(); numtasks = MPI.COMM_WORLD.getSize(); numworkers = numtasks - 1; int mtype; /* *************** Master Task ****************** */ if (taskid == MASTER) { /** * Master initialize a,b,c WARNING : workers will initialize only * the needed parts */ a = new int[N * N]; /* matrix A to be multiplied */ b = new int[N * N]; /* matrix B to be multiplied */ c = new int[N * N]; /* result matrix C */ System.out.println("Perfoming Parellel Matrix Multiply, size = " + N + "x" + N + " . Using " + numworkers + " worker"); // Init matrix A,B for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { a[(i * N) + j] = 1; b[(i * N) + j] = 2; } } long start = System.currentTimeMillis(); // Send matrix data to worker tasks /** * We split A by rows and we send to each worker a part The whole * matrix B is sent to all workers * */ averow = N / numworkers; extra = N % numworkers; offset[0] = 0; mtype = FROM_MASTER; for (dest = 1; dest <= numworkers; dest++) { if (dest <= extra) { rows[0] = averow + 1; } else { rows[0] = averow; } System.out.println("Send offset : " + offset[0] + ", and number of rows : " + rows[0] + ", to the worker number : " + dest); MPI.COMM_WORLD.send(offset, 1, MPI.INT, dest, mtype); MPI.COMM_WORLD.send(rows, 1, MPI.INT, dest, mtype); count = rows[0] * N; MPI.COMM_WORLD.send(slice(a, (offset[0] * N)), count, MPI.INT, dest, mtype); count = N * N; MPI.COMM_WORLD.send(b, count, MPI.INT, dest, mtype); offset[0] = offset[0] + rows[0]; } // Wait for results from all worker tasks mtype = FROM_WORKER; for (i = 1; i <= numworkers; i++) { source = i; MPI.COMM_WORLD.recv(offset, 1, MPI.INT, source, mtype); MPI.COMM_WORLD.recv(rows, 1, MPI.INT, source, mtype); count = rows[0] * N; MPI.COMM_WORLD.recv(slice(c, offset[0] * N), count, MPI.INT, source, mtype); } long stop = System.currentTimeMillis(); System.out.println("Result of matrix c[0] = " + c[0] + ", c[" + (N * N - 2) + "] = " + c[N * N - 2]); System.out.println("Time Usage (ms) = " + (stop - start)); } /* * *************************** worker task The worker receive Matrix B, * and a part of A perform Matrix Multiply and send back result to * Master *********************************** */ if (taskid != MASTER) { mtype = FROM_MASTER; source = MASTER; MPI.COMM_WORLD.recv(offset, 1, MPI.INT, source, mtype); MPI.COMM_WORLD.recv(rows, 1, MPI.INT, source, mtype); count = rows[0] * N; a = new int[count]; /* initialize A and B */ c = new int[count]; MPI.COMM_WORLD.recv(a, count, MPI.INT, source, mtype); count = N * N; b = new int[count]; /* initialize B */ MPI.COMM_WORLD.recv(b, count, MPI.INT, source, mtype); for (i = 0; i < rows[0]; i++) { for (k = 0; k < N; k++) { c[(i * N) + k] = 0; for (j = 0; j < N; j++) { c[(i * N) + k] = c[(i * N) + k] + a[(i * N) + j] * b[(j * N) + k]; } } } // send result back to Master mtype = FROM_WORKER; MPI.COMM_WORLD.send(offset, 1, MPI.INT, MASTER, mtype); MPI.COMM_WORLD.send(rows, 1, MPI.INT, MASTER, mtype); MPI.COMM_WORLD.send(c, rows[0] * N, MPI.INT, MASTER, mtype); } MPI.Finalize(); } }