Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| open_mpi_with_java_binding [2016/02/23 19:57] – [Documentation] admin | open_mpi_with_java_binding [2022/12/12 13:49] (Version actuelle) – [Documentation] admin | ||
|---|---|---|---|
| Ligne 6: | Ligne 6: | ||
| ===== How do I build and run my Java MPI application? | ===== How do I build and run my Java MPI application? | ||
| + | |||
| + | <note warning> | ||
| + | EDIT dec 2022\\ | ||
| + | use '' | ||
| + | </ | ||
| + | |||
| Load Java MPI binding : | Load Java MPI binding : | ||
| Ligne 31: | Ligne 37: | ||
| - | <WRAP center round tip 60%> | + | <WRAP center round tip 80%> |
| For convenience, | For convenience, | ||
| As always, the java executable, all required libraries, and your application classes must be available on all nodes. | As always, the java executable, all required libraries, and your application classes must be available on all nodes. | ||
| Ligne 37: | Ligne 43: | ||
| </ | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | * solution1: mpirun <code bash> | ||
| + | * solution2: mpirun <code bash> | ||
| + | </ | ||
| Ligne 65: | Ligne 77: | ||
| </ | </ | ||
| - | ==== Built it==== | + | ==== Build it==== |
| <code bash> | <code bash> | ||
| Ligne 96: | Ligne 108: | ||
| mpirun -np $NSLOTS java -Xmx3G Hello | mpirun -np $NSLOTS java -Xmx3G Hello | ||
| </ | </ | ||
| - | Check the Matrix Multiply example [[here]] | + | |
| + | Check the Matrix Multiply example | ||
| + | |||
| + | <file Java MatrixPar.java> | ||
| + | import mpi.*; | ||
| + | import static mpi.MPI.slice; | ||
| + | |||
| + | /** | ||
| + | * @author kmazouzi | ||
| + | * | ||
| + | * Matrix Multiply using Master/ | ||
| + | */ | ||
| + | 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, | ||
| + | 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(" | ||
| + | + N + " | ||
| + | // 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(" | ||
| + | + ", and number of rows : " + rows[0] | ||
| + | + ", to the worker number : " + dest); | ||
| + | MPI.COMM_WORLD.send(offset, | ||
| + | MPI.COMM_WORLD.send(rows, | ||
| + | count = rows[0] * N; | ||
| + | MPI.COMM_WORLD.send(slice(a, | ||
| + | dest, mtype); | ||
| + | count = N * N; | ||
| + | MPI.COMM_WORLD.send(b, | ||
| + | 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, | ||
| + | MPI.COMM_WORLD.recv(rows, | ||
| + | count = rows[0] * N; | ||
| + | MPI.COMM_WORLD.recv(slice(c, | ||
| + | source, | ||
| + | } | ||
| + | long stop = System.currentTimeMillis(); | ||
| + | |||
| + | System.out.println(" | ||
| + | + (N * N - 2) + "] = " + c[N * N - 2]); | ||
| + | System.out.println(" | ||
| + | |||
| + | } | ||
| + | |||
| + | /* | ||
| + | * *************************** 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, | ||
| + | MPI.COMM_WORLD.recv(rows, | ||
| + | count = rows[0] * N; | ||
| + | a = new int[count]; /* initialize A and B */ | ||
| + | c = new int[count]; | ||
| + | MPI.COMM_WORLD.recv(a, | ||
| + | count = N * N; | ||
| + | b = new int[count]; /* initialize B */ | ||
| + | MPI.COMM_WORLD.recv(b, | ||
| + | |||
| + | 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, | ||
| + | MPI.COMM_WORLD.send(rows, | ||
| + | MPI.COMM_WORLD.send(c, | ||
| + | } | ||
| + | |||
| + | MPI.Finalize(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ====== Documentation ====== | ====== Documentation ====== | ||
| * Browse the API http:// | * Browse the API http:// | ||
| - | * http://www.hpjava.org/papers/MPJ-CPE/ | + | * http://blogs.cisco.com/performance/java-bindings-for-open-mpi |
| * http:// | * http:// | ||
| * http:// | * http:// | ||
| + | * https:// | ||
| + | |||