Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package matmath;
  2.  
  3. import java.util.concurrent.*;
  4.  
  5. class MatMathImpl
  6. {
  7.  
  8. public void multiply(int[][] A, int[][]B, int[][]C)
  9. {
  10. if (A[0].length == B.length){
  11. CountDownLatch latch =
  12. new CountDownLatch(A.length*B[0].length);
  13. ExecutorService executor =
  14. Executors.newFixedThreadPool(A.length*B[0].length);
  15. for (int r=0; r<A.length; r++)
  16. for (int c=0; c<B[0].length; c++)
  17. { executor.execute(new
  18. RowColProdExecutable(A, B, C, r, c, A[r].length, latch));}
  19. executor.shutdown();
  20. try { latch.await(); }
  21. catch (InterruptedException e) {e.printStackTrace();}
  22. }
  23. else
  24. System.out.println("Illegal Multiply operation: matrices"+
  25. " cannot be multiplied");
  26. }
  27.  
  28. public void add(int[][] A, int[][] B, int[][] C)
  29. {
  30. if ((A.length == B.length) && (A[0].length == B[0].length)){
  31. CountDownLatch latch =
  32. new CountDownLatch(A.length*A[0].length);
  33. ExecutorService executor =
  34. Executors.newFixedThreadPool(A.length*A[0].length);
  35. for (int r=0; r<A.length; r++)
  36. for (int c=0; c<B[0].length; c++)
  37. { executor.execute(new RowColSumExecutable(A, B, C, r, c, latch)); }
  38. executor.shutdown();
  39. try { latch.await(); }
  40. catch (InterruptedException e) {e.printStackTrace();}
  41. }
  42. else
  43. System.out.println("Illegal Add operation: matrices"+
  44. " must have the same dimensions");
  45. }
  46.  
  47. public void print(int[][] A)
  48. {
  49. for (int r=0; r<A.length; r++)
  50. {
  51. for (int c=0; c<A[r].length; c++)
  52. System.out.print(A[r][c]+" ");
  53. System.out.print("\n");
  54. }
  55. }
  56. }
  57.  
  58. class RowColProdExecutable implements Runnable
  59. {
  60.  
  61. private int[][] first, second, result;
  62. private int row, col, size;
  63. private CountDownLatch latch;
  64.  
  65. RowColProdExecutable(int[][] first, int[][] second, int[][] result,
  66. int row, int col, int size, CountDownLatch latch)
  67. {
  68. this.first = first;
  69. this.second = second;
  70. this.result = result;
  71. this.row = row;
  72. this.col = col;
  73. this.size = size;
  74. this.latch = latch;
  75. }
  76.  
  77. public void run()
  78. {
  79. for (int k=0; k < size; k++)
  80. { result[row][col] += first[row][k]*second[k][col]; }
  81. latch.countDown();
  82. }
  83. }
  84.  
  85. class RowColSumExecutable implements Runnable
  86. {
  87.  
  88. private int[][] first, second, result;
  89. private int row, col;
  90. private CountDownLatch latch;
  91.  
  92. RowColSumExecutable(int[][] first, int[][] second, int[][] result,
  93. int row, int col, CountDownLatch latch)
  94. {
  95. this.first = first;
  96. this.second = second;
  97. this.result = result;
  98. this.row = row;
  99. this.col = col;
  100. this.latch = latch;
  101. }
  102.  
  103. public void run()
  104. {
  105. result[row][col] = first[row][col] + second[row][col];
  106. latch.countDown();
  107. }
  108. }
  109.  
  110. public class MatMathMain
  111. {
  112. public static void main(String[] args)
  113. {
  114. int[][] A,B,C,D,r,s,t;
  115. MatMath u = new MatMathImpl();
  116.  
  117. u.add(A,B,r);
  118. u.multiply(r,C,s);
  119. u.multiply(s,D,t);
  120.  
  121. int[][] a = {{1,4,3},{6,2,8},{1,3,2},{7,6,4}};
  122. int[][] b = {{4,1},{3,6},{2,4}};
  123. int[][] c = new int[4][2];
  124. u.multiply(a,b,c);
  125. int[][] d = {{1,2},{1,2},{1,2},{1,2}};
  126. int[][] e = new int[4][2];
  127. u.add(c,d,e);
  128. int[][] f = {{1,2},{2,1}};
  129. int[][] g = new int[4][2];
  130. u.multiply(e,f,g);
  131. u.print(g);
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement