├── .idea ├── .gitignore ├── libraries │ └── lib.xml ├── misc.xml └── modules.xml ├── Lab7.iml ├── out └── production │ └── Lab7 │ └── Main.class └── src └── Main.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/libraries/lib.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Lab7.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /out/production/Lab7/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sanek192319/ParallelProgrammingLab_7/d590ed5e0682f8c3c5c44dd18fcad54f3f7295a2/out/production/Lab7/Main.class -------------------------------------------------------------------------------- /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 | 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 | long start = 0; 21 | if(rank == MASTER) { 22 | a = InputMautix(rowsA,colsA,1); 23 | 24 | b = InputMautix(colsA,colsB,1); 25 | start = System.currentTimeMillis(); 26 | } 27 | int rowsPerThread = rowsA / numTasks; 28 | int extra = rowsA % numTasks; 29 | 30 | int[] rowsCounts = new int[numTasks]; 31 | int[] offsetCounts = new int[numTasks]; 32 | for(int i = 0; i < numTasks; i++) { 33 | rowsCounts[i] = i < extra ? rowsPerThread + 1 : rowsPerThread; 34 | offsetCounts[i] = i == MASTER ? 0 : offsetCounts[i-1] + rowsCounts[i-1]; 35 | } 36 | 37 | int rowsForThisTask = rowsCounts[rank]; 38 | double[][] aRows = new double[rowsForThisTask][colsA]; 39 | MPI.COMM_WORLD.Scatterv( 40 | a, 0, rowsCounts, offsetCounts,MPI.OBJECT, 41 | aRows, 0, rowsForThisTask, MPI.OBJECT, 42 | MASTER 43 | ); 44 | MPI.COMM_WORLD.Bcast(b, 0, colsA, MPI.OBJECT, MASTER); 45 | 46 | double[][] cRows = new double[rowsForThisTask][colsB]; 47 | for (int k = 0; k < colsB; k++) { 48 | for (int i = 0; i < rowsForThisTask; i++) { 49 | for (int j = 0; j < colsA; j++) { 50 | cRows[i][k] += aRows[i][j] * b[j][k]; 51 | } 52 | } 53 | } 54 | 55 | MPI.COMM_WORLD.Allgatherv( 56 | cRows, 0, rowsForThisTask, MPI.OBJECT, 57 | c, 0, rowsCounts, offsetCounts, MPI.OBJECT 58 | ); 59 | 60 | if (rank == MASTER) { 61 | var endTime = System.currentTimeMillis(); 62 | var dur = endTime - start; 63 | 64 | for(int i = 0; i < rowsA; i++) { 65 | for (int j = 0; j < colsB; j++) { 66 | System.out.print(c[i][j] +" "); 67 | } 68 | System.out.print('\n'); 69 | } 70 | System.out.println("End with time: " + dur + " ms"); 71 | } 72 | MPI.Finalize(); 73 | } 74 | public static double[][] InputMautix(int rows,int cols,double value) 75 | { 76 | double[][] result = new double[rows][cols]; 77 | for(int i = 0; i < rows; i++) { 78 | for (int j = 0; j < cols; j++) { 79 | result[i][j] = value; 80 | } 81 | } 82 | return result; 83 | } 84 | } --------------------------------------------------------------------------------