Last active
February 14, 2016 18:28
-
-
Save Viacheslav77/708482e897cccd661949 to your computer and use it in GitHub Desktop.
Реализовать многопоточное перемножение квадратных матриц. Сравнить скорость выполнения алгоритма с однопоточным решением.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MatrixThread; | |
| /*Реализовать многопоточное перемножение квадратных матриц. Сравнить скорость | |
| выполнения алгоритма с однопоточным решением.*/ | |
| public class MatrixThread extends Thread{ | |
| int [][] mA; | |
| int [][] mB; | |
| int [][] mRuz; | |
| int lineNumber; | |
| int numberOfThread; | |
| static int couterThread = 1; | |
| public MatrixThread (int [][] mA1, int [][] mB1, int [][] mRuz1, int lineNumber1, int numberOfThread1){ | |
| mA = mA1; | |
| mB=mB1; | |
| mRuz=mRuz1; | |
| lineNumber=lineNumber1; | |
| numberOfThread= numberOfThread1+1; | |
| } | |
| public void run (){ | |
| System.out.print(numberOfThread + " "); | |
| int [] buf =new int [mA.length]; | |
| for(int i= 0; i< mA.length; i++ ) | |
| for(int j= 0; j< mB.length; j++ ){ | |
| buf[i]+= mA[lineNumber][j]*mB[j][lineNumber]; | |
| } | |
| synchronized(mRuz){ | |
| System.arraycopy(buf, 0, mRuz[lineNumber], 0, mA.length); | |
| } | |
| } | |
| public int [] runOne (){ | |
| int [] buf =new int [mA.length]; | |
| for(int i= 0; i< mA.length; i++ ) | |
| for(int j= 0; j< mB.length; j++ ){ | |
| buf[i]+= mA[lineNumber][j]*mB[j][lineNumber]; | |
| } | |
| return buf; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MatrixThread; | |
| /*Реализовать многопоточное перемножение | |
| квадратных матриц. Сравнить скорость | |
| выполнения алгоритма с однопоточным | |
| решением.*/ | |
| import java.util.ArrayList; | |
| import java.util.Random; | |
| public class MyClass { | |
| public static void main(String[]args){ | |
| int numberOfThread = 10; | |
| int size = 500; | |
| int [][] mA = new int[size][size]; | |
| int [][] mB = new int[size][size]; | |
| int [][] mRuz= new int[size][size]; | |
| Random r = new Random(); | |
| for(int i = 0; i<size; i++) | |
| for(int j=0; j< size; j++){ | |
| mA[i][j]=r.nextInt(); | |
| mB[i][j]=r.nextInt(); | |
| } | |
| long time1=System.currentTimeMillis(); | |
| for(int i=0;i<size;i++) { | |
| MatrixThread t=new MatrixThread(mA,mB,mRuz,i,0); | |
| System.arraycopy(t.runOne(), 0, mRuz[i], 0, size); | |
| } | |
| long time2=System.currentTimeMillis(); | |
| System.out.println("Multiplication square matrices."); | |
| System.out.println("A single-threaded solution time is " + (time2-time1) + " ms" + "\n"); | |
| time1=System.currentTimeMillis(); | |
| int sizeThread = size/numberOfThread; | |
| int k = 0; | |
| System.out.println("Multi-threaded solution." + " Start Threads :\n"); | |
| for(int i=0;i<sizeThread;i++){ | |
| ArrayList<MatrixThread> mList=new ArrayList<>(); | |
| System.out.print("Begin " ); | |
| for(int j=0;j<numberOfThread;j++){ | |
| mList.add(new MatrixThread(mA,mB,mRuz,k++,j)); | |
| mList.get(j).start(); | |
| } | |
| for (MatrixThread t: mList){ | |
| try{ | |
| t.join(); | |
| }catch(InterruptedException ex){ | |
| ex.printStackTrace(); | |
| } | |
| } | |
| System.out.println(" End" ); | |
| } | |
| time2=System.currentTimeMillis(); | |
| System.out.println("\nMulti-threaded solution time is " + (time2-time1) + " ms"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Multiplication square matrices. | |
| A single-threaded solution time is 412 ms | |
| Multi-threaded solution. Start Threads : | |
| Begin 1 3 2 4 6 8 10 5 7 9 End | |
| Begin 1 3 2 7 4 5 9 6 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 9 7 10 8 6 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 7 4 9 6 10 8 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 10 8 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 2 1 4 3 6 5 8 7 10 9 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 2 7 5 3 6 9 8 1 10 4 End | |
| Begin 2 3 4 1 5 6 8 7 10 9 End | |
| Begin 1 2 9 7 5 4 6 3 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 4 5 6 7 8 9 10 End | |
| Begin 1 2 4 3 5 6 10 9 8 7 End | |
| Begin 1 3 2 5 7 4 9 6 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 5 7 2 9 4 6 8 10 End | |
| Begin 1 3 5 2 7 4 9 6 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 3 5 7 4 2 9 6 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 4 5 6 7 10 8 9 End | |
| Begin 2 1 4 3 6 5 8 7 10 9 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 10 8 6 4 2 9 7 5 End | |
| Begin 1 4 3 2 5 6 7 8 9 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 2 1 4 3 6 5 8 7 10 9 End | |
| Begin 2 1 4 3 6 5 8 7 10 9 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 3 5 4 7 6 9 8 10 End | |
| Begin 2 1 4 3 6 5 8 7 10 9 End | |
| Begin 1 2 3 4 5 6 8 7 10 9 End | |
| Begin 1 2 3 5 4 9 7 10 8 6 End | |
| Begin 1 3 2 5 4 7 6 9 8 10 End | |
| Begin 1 2 4 3 6 5 8 7 10 9 End | |
| Multi-threaded solution time is 354 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment