Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1.  
  2. public class Exercise3 {
  3.  
  4. public class Matrix{
  5.  
  6. }
  7.  
  8. //Method that multiplies two matrices
  9. public static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2){
  10. int[][] product = new int[matrix1.length][matrix2[0].length];
  11.  
  12. if (matrix1[0].length != matrix2.length) {
  13. throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
  14. }
  15.  
  16. for(int x = 0; x < matrix1.length; x++) {
  17. for(int y = 0; y < matrix2[0].length; y++) {
  18. for (int z = 0; z < matrix1[0].length; z++) {
  19. product[x][y] += matrix1[x][z] * matrix2[y][z];
  20. }
  21. }
  22. }
  23. return product;
  24. }
  25.  
  26. //Method that multiplies two matrices and Multithread support
  27. public static int[][] matrixMultiplicationMultithread(int[][] matrix1, int[][] matrix2, int numOfThreads){
  28. int[][] product = new int[matrix1.length][matrix2[0].length];
  29. if (matrix1[0].length != matrix2.length) {
  30. throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
  31. }
  32.  
  33. for(int x = 0; x < matrix1.length; x++) {
  34. for(int y = 0; y < matrix2[0].length; y++) {
  35. for (int z = 0; z < matrix1[0].length; z++) {
  36. product[x][y] += matrix1[x][z] * matrix2[y][z];
  37. }
  38. }
  39. }
  40. return product;
  41. }
  42.  
  43. //Method to print a matrix
  44. public static void printMatrix(int[][] matrix) {
  45. for (int i = 0; i < matrix.length; i++) {
  46. for (int j = 0; j < matrix[i].length; j++) {
  47. System.out.print(matrix[i][j] + " ");
  48. }
  49. System.out.println();
  50. }
  51. }
  52.  
  53. //Method that initializes a Matrix
  54. public static int[][] initializeMatrix(int[][] matrix, int numOfRows, int numOfColumns) {
  55. for(int j = 0; j < numOfRows; j++) {
  56. for(int k = 0; k < numOfColumns; k++) {
  57. matrix[j][k] = j+k;
  58. }
  59. }
  60. return matrix;
  61. }
  62.  
  63. public static void main(String[] args) {
  64. Thread thread = null;
  65. int numOfRows = 300; //Access rows with .length
  66. int numOfColumns = 300; //Access columns with [0].length
  67. int numOfThreads = 1;
  68.  
  69. int[][] matrix1 = new int [numOfRows][numOfColumns];
  70. int[][] matrix2 = new int [numOfRows][numOfColumns];
  71.  
  72. matrix1 = initializeMatrix(matrix1, numOfRows, numOfColumns);
  73. matrix2 = initializeMatrix(matrix2, numOfRows, numOfColumns);
  74.  
  75. int[][] product = new int [matrix1.length][matrix2[0].length];
  76.  
  77. final long startTime = System.currentTimeMillis();
  78. product = matrixMultiplication(matrix1, matrix2);
  79. final long endTime = System.currentTimeMillis();
  80. System.out.println("The main thread took " +(endTime-startTime)+"ms to complete the matrix multiplication");
  81.  
  82.  
  83. int rowsDividedByNumOfThread = matrix1.length / numOfThreads;
  84. int start = 0;
  85. int end = rowsDividedByNumOfThread-1;
  86. for(int i = 0; i < numOfThreads; i++) {
  87. thread = new Thread(new MatrixMultiplicationThread(matrix1, matrix2, start, end));
  88. thread.start();
  89.  
  90. start = end + 1;
  91. end = end + rowsDividedByNumOfThread +1;
  92.  
  93.  
  94. }
  95.  
  96.  
  97. }
  98. }
  99. ________________________________________________________________________________________________
  100.  
  101. public class MatrixMultiplicationThread extends Thread {
  102. public int[][] matrix1 = new int[0][0]; //null?
  103. public int[][] matrix2 = new int[0][0];
  104. public int start = 0;
  105. public int end = 0;
  106. public int[][] product = new int[0][0];
  107.  
  108. public MatrixMultiplicationThread (int[][] matrix1, int[][] matrix2, int start, int end) {
  109. this.matrix1 = matrix1;
  110. this.matrix2 = matrix2;
  111. this.start = start;
  112. this.end = end;
  113. }
  114.  
  115. public int[][] getProduct() {
  116. return product;
  117. }
  118.  
  119. public void setProduct(int[][] product) {
  120. this.product = product;
  121. }
  122.  
  123. @Override
  124. public void run() {
  125. int[][] product = new int[matrix1.length][matrix2[0].length];
  126.  
  127. if (matrix1[0].length != matrix2.length) {
  128. throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
  129. }
  130.  
  131. for(int x = start; x < matrix1.length && x < end; x++) {
  132. for(int y = start; y < matrix2[0].length && y < end; y++) {
  133. for (int z = start; z < matrix1[0].length && y < end; z++) {
  134. product[x][y] += matrix1[x][z] * matrix2[y][z];
  135. }
  136. }
  137. }
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement