├── .idea ├── .gitignore ├── modules.xml ├── misc.xml ├── libraries │ └── lib.xml └── uiDesigner.xml ├── out └── production │ └── Lab6 │ ├── Main.class │ ├── Lab6_2.class │ └── MatrixMultiplication.class ├── Lab6.iml └── src ├── MatrixMultiplication.java ├── Main.java └── Lab6_2.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /out/production/Lab6/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sanek192319/ParallelProgrammingLab_6/HEAD/out/production/Lab6/Main.class -------------------------------------------------------------------------------- /out/production/Lab6/Lab6_2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sanek192319/ParallelProgrammingLab_6/HEAD/out/production/Lab6/Lab6_2.class -------------------------------------------------------------------------------- /out/production/Lab6/MatrixMultiplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sanek192319/ParallelProgrammingLab_6/HEAD/out/production/Lab6/MatrixMultiplication.class -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/libraries/lib.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Lab6.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/MatrixMultiplication.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class MatrixMultiplication { 4 | public static void main(String[] args) { 5 | int rowsA = 1000; 6 | int colsA = 1000; 7 | int colsB = 1000; 8 | 9 | double a[][] = createMatrix(rowsA,colsA,1); 10 | double b[][] = createMatrix(rowsA,colsB,1); 11 | double c[][] = new double[rowsA][colsB]; 12 | 13 | 14 | var start = System.currentTimeMillis(); 15 | c = multiplyMatrices(a, b); 16 | var end = System.currentTimeMillis(); 17 | var dur = end - start; 18 | System.out.println("time: " + dur); 19 | //printMatrix(c); 20 | 21 | } 22 | 23 | 24 | public static double[][] multiplyMatrices( double[][] a,double[][] b){ 25 | int rowsA = a.length; 26 | int colsA = a[0].length; 27 | int colsB = b[0].length; 28 | 29 | double[][] c = new double[rowsA][colsB]; 30 | 31 | for (int i = 0; i < rowsA; i++) { 32 | for (int j = 0; j < colsB; j++) { 33 | for (int k = 0; k < colsA; k++) { 34 | c[i][j] += a[i][k] * b[k][j]; 35 | } 36 | } 37 | } 38 | 39 | return c; 40 | } 41 | public static double[][] createMatrix(int rows, int cols, int value) { 42 | double[][] matrix = new double[rows][cols]; 43 | for (int i = 0; i < rows; i++) { 44 | for (int j = 0; j < cols; j++) { 45 | matrix[i][j] = value; 46 | } 47 | } 48 | return matrix; 49 | } 50 | public static void printMatrix(double[][] matrix) { 51 | int rows = matrix.length; 52 | int cols = matrix[0].length; 53 | for (int i = 0; i < rows; i++) { 54 | for (int j = 0; j < cols; j++) { 55 | System.out.print(matrix[i][j] + " "); 56 | } 57 | System.out.println(); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | import mpi.MPI; 2 | 3 | public class Main { 4 | private static final int rowsA = 4; 5 | private static final int colsA = 4; 6 | private static final int colsB = 4; 7 | private static final int MASTER = 0; 8 | private static final int FROM_MASTER = 1; 9 | private static final int TO_MASTER = 2; 10 | public static void main(String[] args) { 11 | double[][] a = new double[rowsA][colsA]; 12 | double[][] b = new double[colsA][colsB]; 13 | double[][] c = new double[rowsA][colsB]; 14 | MPI.Init(args); 15 | int numTasks = MPI.COMM_WORLD.Size(); 16 | int rank = MPI.COMM_WORLD.Rank(); 17 | 18 | if (numTasks < 2) { 19 | System.out.println("Need at least two MPI tasks. Quitting...\n"); 20 | MPI.Finalize(); 21 | System.exit(1); 22 | 23 | } 24 | int numWorkers = numTasks - 1; 25 | int[] offset = {0}; 26 | int[] rows = {0}; 27 | if(rank == MASTER) { 28 | System.out.println("Started with " + numTasks + " tasks"); 29 | a = InputMautix(rowsA,colsA,1); 30 | 31 | b = InputMautix(colsA,colsB,1); 32 | 33 | 34 | int rowsPerThread = rowsA / numWorkers; 35 | int extra = rowsA % numWorkers; 36 | var start = System.currentTimeMillis(); 37 | for(int dest = 1; dest <= numWorkers; dest++) { 38 | rows[0] = (dest <= extra) ? rowsPerThread + 1 : rowsPerThread; 39 | System.out.println("Sending " + rows[0] + " rows to task " + dest + " offset="+offset[0]); 40 | 41 | MPI.COMM_WORLD.Send(offset, 0, 1, MPI.INT, dest, FROM_MASTER); 42 | MPI.COMM_WORLD.Send(rows, 0, 1, MPI.INT, dest, FROM_MASTER); 43 | MPI.COMM_WORLD.Send(a, offset[0], rows[0], MPI.OBJECT, dest, FROM_MASTER); 44 | MPI.COMM_WORLD.Send(b, 0, colsA, MPI.OBJECT, dest, FROM_MASTER); 45 | 46 | offset[0] = offset[0] + rows[0]; 47 | } 48 | System.out.println("Counclusion: "); 49 | for(int source = 1; source <= numWorkers; source++) { 50 | MPI.COMM_WORLD.Recv(offset, 0, 1, MPI.INT, source, TO_MASTER); 51 | MPI.COMM_WORLD.Recv(rows, 0, 1, MPI.INT, source, TO_MASTER); 52 | MPI.COMM_WORLD.Recv(c, offset[0], rows[0], MPI.OBJECT, source, TO_MASTER); 53 | } 54 | var end = System.currentTimeMillis(); 55 | var dur = end - start; 56 | System.out.println("End with time: " + dur + " ms"); 57 | for(int i = 0; i < rowsA; i++) { 58 | for (int j = 0; j < colsB; j++) { 59 | System.out.print(c[i][j] +" "); 60 | } 61 | System.out.print('\n'); 62 | } 63 | } else { 64 | MPI.COMM_WORLD.Recv(offset, 0, 1, MPI.INT, MASTER, FROM_MASTER); 65 | MPI.COMM_WORLD.Recv(rows, 0, 1, MPI.INT, MASTER, FROM_MASTER); 66 | MPI.COMM_WORLD.Recv(a, 0, rows[0], MPI.OBJECT, MASTER, FROM_MASTER); 67 | MPI.COMM_WORLD.Recv(b, 0, colsA, MPI.OBJECT, MASTER, FROM_MASTER); 68 | 69 | 70 | for (int k = 0; k < colsB; k++) { 71 | for (int i = 0; i < rows[0]; i++) { 72 | for (int j = 0; j < colsA; j++) { 73 | c[i][k] += a[i][j] * b[j][k]; 74 | } 75 | } 76 | } 77 | 78 | MPI.COMM_WORLD.Send(offset, 0, 1, MPI.INT, MASTER, TO_MASTER); 79 | MPI.COMM_WORLD.Send(rows, 0, 1, MPI.INT, MASTER, TO_MASTER); 80 | MPI.COMM_WORLD.Send(c, 0, rows[0], MPI.OBJECT, MASTER, TO_MASTER); 81 | } 82 | 83 | MPI.Finalize(); 84 | } 85 | public static double[][] InputMautix(int rows,int cols,double value) 86 | { 87 | double[][] result = new double[rows][cols]; 88 | for(int i = 0; i < rows; i++) { 89 | for (int j = 0; j < cols; j++) { 90 | result[i][j] = value; 91 | } 92 | } 93 | return result; 94 | } 95 | } -------------------------------------------------------------------------------- /src/Lab6_2.java: -------------------------------------------------------------------------------- 1 | import mpi.MPI; 2 | 3 | public class Lab6_2 { 4 | private static final int rowsA = 1000; 5 | private static final int colsA = 1000; 6 | private static final int colsB = 1000; 7 | private static final int MASTER = 0; 8 | public static void main(String[] args) { 9 | double[][] a = new double[rowsA][colsA]; 10 | double[][] b = new double[colsA][colsB]; 11 | double[][] c = new double[rowsA][colsB]; 12 | MPI.Init(args); 13 | int numTasks = MPI.COMM_WORLD.Size(); 14 | int rank = MPI.COMM_WORLD.Rank(); 15 | 16 | if (numTasks < 2) { 17 | System.out.println("Need at least two MPI tasks. Quitting...\n"); 18 | MPI.COMM_WORLD.Abort(1); 19 | } 20 | int numWorkers = numTasks - 1; 21 | int[] offset = {0}; 22 | int[] rows = {0}; 23 | if(rank == MASTER) { 24 | System.out.println("Started with " + numTasks + " tasks"); 25 | a = InputMautix(rowsA,colsA,1); 26 | 27 | b = InputMautix(colsA,colsB,1); 28 | 29 | int rowsPerThread = rowsA / numWorkers; 30 | int extra = rowsA % numWorkers; 31 | var startTime = System.currentTimeMillis(); 32 | 33 | for(int dest = 1; dest <= numWorkers; dest++) { 34 | rows[0] = (dest <= extra) ? rowsPerThread + 1 : rowsPerThread; 35 | System.out.println("Sending " + rows[0] + " rows to task " + dest + " offset="+offset[0]); 36 | 37 | MPI.COMM_WORLD.Isend(offset, 0, 1, MPI.INT, dest, 0); 38 | MPI.COMM_WORLD.Isend(rows, 0, 1, MPI.INT, dest, 1); 39 | MPI.COMM_WORLD.Isend(a, offset[0], rows[0], MPI.OBJECT, dest, 2); 40 | MPI.COMM_WORLD.Isend(b, 0, colsA, MPI.OBJECT, dest, 3); 41 | 42 | offset[0] = offset[0] + rows[0]; 43 | } 44 | System.out.println("Counclusion: "); 45 | for(int source = 1; source <= numWorkers; source++) { 46 | var offsetRequest = MPI.COMM_WORLD.Irecv(offset, 0, 1, MPI.INT, source, 4); 47 | var rowsRequest = MPI.COMM_WORLD.Irecv(rows, 0, 1, MPI.INT, source, 5); 48 | offsetRequest.Wait(); 49 | rowsRequest.Wait(); 50 | var matrixRequest = MPI.COMM_WORLD.Irecv(c, offset[0], rows[0], MPI.OBJECT, source, 6); 51 | matrixRequest.Wait(); 52 | } 53 | var endTime = System.currentTimeMillis(); 54 | 55 | // for(int i = 0; i < rowsA; i++) { 56 | // for (int j = 0; j < colsB; j++) { 57 | // System.out.print(c[i][j] +" "); 58 | // } 59 | // System.out.print('\n'); 60 | // } 61 | 62 | var dur = endTime-startTime; 63 | System.out.println("End with time: " + dur + " ms"); 64 | } else { 65 | var offsetRequest = MPI.COMM_WORLD.Irecv(offset, 0, 1, MPI.INT, MASTER, 0); 66 | var rowsRequest = MPI.COMM_WORLD.Irecv(rows, 0, 1, MPI.INT, MASTER, 1); 67 | offsetRequest.Wait(); 68 | rowsRequest.Wait(); 69 | MPI.COMM_WORLD.Isend(offset, 0, 1, MPI.INT, MASTER, 4); 70 | MPI.COMM_WORLD.Isend(rows, 0, 1, MPI.INT, MASTER, 5); 71 | 72 | var aRequest = MPI.COMM_WORLD.Irecv(a, 0, rows[0], MPI.OBJECT, MASTER, 2); 73 | var bRequest = MPI.COMM_WORLD.Irecv(b, 0, colsA, MPI.OBJECT, MASTER, 3); 74 | aRequest.Wait(); 75 | bRequest.Wait(); 76 | for (int k = 0; k < colsB; k++) { 77 | for (int i = 0; i < rows[0]; i++) { 78 | for (int j = 0; j < colsA; j++) { 79 | c[i][k] += a[i][j] * b[j][k]; 80 | } 81 | } 82 | } 83 | 84 | 85 | MPI.COMM_WORLD.Isend(c, 0, rows[0], MPI.OBJECT, MASTER, 6); 86 | } 87 | 88 | MPI.Finalize(); 89 | } 90 | public static double[][] InputMautix(int rows,int cols,double value) 91 | { 92 | double[][] result = new double[rows][cols]; 93 | for(int i = 0; i < rows; i++) { 94 | for (int j = 0; j < cols; j++) { 95 | result[i][j] = value; 96 | } 97 | } 98 | return result; 99 | } 100 | } -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------