Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. package parallel_algorithm;
  2.  
  3. import java.util.Scanner;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.Future;
  7. import java.util.concurrent.FutureTask;
  8.  
  9. public class ParallelMatrixMultiplication {
  10.  
  11.     private static final int SIZE = 6;
  12.     private static final int THRESHOLD = 64;
  13.     private static final int MAX_THREADS = Runtime.getRuntime()
  14.             .availableProcessors();
  15.  
  16.     private final ExecutorService executor = Executors
  17.             .newFixedThreadPool(MAX_THREADS);
  18.  
  19.     private float[][] a = new float[SIZE][SIZE];
  20.     private float[][] b = new float[SIZE][SIZE];
  21.     private float[][] c = new float[SIZE][SIZE];
  22.  
  23.     public void initialize() {
  24.         init(a, b, SIZE);
  25.     }
  26.  
  27.     public void execute() {
  28.         MatrixMultiplyTask mainTask = new MatrixMultiplyTask(a, 0, 0, b, 0, 0,
  29.                 c, 0, 0, SIZE);
  30.         Future future = executor.submit(mainTask);
  31.  
  32.         try {
  33.             future.get();
  34.         } catch (Exception e) {
  35.             System.out.println("Error: " + e.getMessage());
  36.         }
  37.     }
  38.  
  39.     public void printResult() {
  40.         //check(c, SIZE);
  41.        
  42.         for (int i = 0; i < SIZE; ++i) {
  43.             for (int j = 0; j < SIZE; ++j) {
  44.                 System.out.print(c[i][j] + " ");
  45.             }
  46.             System.out.println();
  47.         }
  48.            
  49. //      for (int i = 0; i < SIZE && i <= 10; i++) {
  50. //          for (int j = 0; j < SIZE && j <= 10; j++) {
  51. //              if (j == 10) {
  52. //                  System.out.print("...");
  53. //              } else {
  54. //                  System.out.print(c[i][j] + " ");
  55. //              }
  56. //          }
  57. //
  58. //          if (i == 10) {
  59. //              System.out.println();
  60. //              for (int k = 0; k < 10; k++)
  61. //                  System.out.print(" ... ");
  62. //          }
  63. //
  64. //          System.out.println();
  65. //      }
  66.  
  67.         System.out.println();
  68.     }
  69.  
  70.     // To simplify checking, fill with all 1's. Answer should be all n's.
  71.     static void init(float[][] a, float[][] b, int n) {
  72.         for (int i = 0; i < n; ++i) {
  73.             for (int j = 0; j < n; ++j) {
  74.                 a[i][j] = 2.0F;
  75.                 b[i][j] = 1.3F;
  76.             }
  77.         }
  78.     }
  79.  
  80.     static void check(float[][] c, int n) {
  81.         for (int i = 0; i < n; i++) {
  82.             for (int j = 0; j < n; j++) {
  83.                 if (c[i][j] != n) {
  84.                     throw new Error("Check Failed at [" + i + "][" + j + "]: "
  85.                             + c[i][j]);
  86.                     // System.out.println("Check Failed at [" + i + "][" + j +
  87.                     // "]: " + c[i][j]);
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93.     public class Seq implements Runnable {
  94.  
  95.         private final MatrixMultiplyTask a;
  96.         private final MatrixMultiplyTask b;
  97.  
  98.         public Seq(MatrixMultiplyTask a, MatrixMultiplyTask b) {
  99.             this.a = a;
  100.             this.b = b;
  101.         }
  102.  
  103.         public void run() {
  104.             a.run();
  105.             b.run();
  106.         }
  107.     }
  108.  
  109.     private class MatrixMultiplyTask implements Runnable {
  110.         private final float[][] A; // Matrix A
  111.         private final int aRow; // first row of current quadrant of A
  112.         private final int aCol; // first column of current quadrant of A
  113.  
  114.         private final float[][] B; // Similarly for B
  115.         private final int bRow;
  116.         private final int bCol;
  117.  
  118.         private final float[][] C; // Similarly for result matrix C
  119.         private final int cRow;
  120.         private final int cCol;
  121.  
  122.         private final int size;
  123.  
  124.         public MatrixMultiplyTask(float[][] A, int aRow, int aCol, float[][] B,
  125.                 int bRow, int bCol, float[][] C, int cRow, int cCol, int size) {
  126.  
  127.             this.A = A;
  128.             this.aRow = aRow;
  129.             this.aCol = aCol;
  130.             this.B = B;
  131.             this.bRow = bRow;
  132.             this.bCol = bCol;
  133.             this.C = C;
  134.             this.cRow = cRow;
  135.             this.cCol = cCol;
  136.             this.size = size;
  137.         }
  138.  
  139.         public void run() {
  140.  
  141.             // System.out.println("Thread: " +
  142.             // Thread.currentThread().getName());
  143.  
  144.             if (size <= THRESHOLD) {
  145.                 multiplyStride2();
  146.             } else {
  147.  
  148.                 int h = size / 2;
  149.  
  150.                 Seq seq1 = new Seq(new MatrixMultiplyTask(A, aRow, aCol, // A11
  151.                         B, bRow, bCol, // B11
  152.                         C, cRow, cCol, // C11
  153.                         h),
  154.  
  155.                 new MatrixMultiplyTask(A, aRow, aCol + h, // A12
  156.                         B, bRow + h, bCol, // B21
  157.                         C, cRow, cCol, // C11
  158.                         h));
  159.  
  160.                 Seq seq2 = new Seq(new MatrixMultiplyTask(A, aRow, aCol, // A11
  161.                         B, bRow, bCol + h, // B12
  162.                         C, cRow, cCol + h, // C12
  163.                         h),
  164.  
  165.                 new MatrixMultiplyTask(A, aRow, aCol + h, // A12
  166.                         B, bRow + h, bCol + h, // B22
  167.                         C, cRow, cCol + h, // C12
  168.                         h));
  169.  
  170.                 Seq seq3 = new Seq(new MatrixMultiplyTask(A, aRow + h, aCol, // A21
  171.                         B, bRow, bCol, // B11
  172.                         C, cRow + h, cCol, // C21
  173.                         h),
  174.  
  175.                 new MatrixMultiplyTask(A, aRow + h, aCol + h, // A22
  176.                         B, bRow + h, bCol, // B21
  177.                         C, cRow + h, cCol, // C21
  178.                         h));
  179.  
  180.                 Seq seq4 = new Seq(new MatrixMultiplyTask(A, aRow + h, aCol, // A21
  181.                         B, bRow, bCol + h, // B12
  182.                         C, cRow + h, cCol + h, // C22
  183.                         h),
  184.  
  185.                 new MatrixMultiplyTask(A, aRow + h, aCol + h, // A22
  186.                         B, bRow + h, bCol + h, // B22
  187.                         C, cRow + h, cCol + h, // C22
  188.                         h));
  189.  
  190.                 final FutureTask s1Task = new FutureTask(seq2, null);
  191.                 final FutureTask s2Task = new FutureTask(seq3, null);
  192.                 final FutureTask s3Task = new FutureTask(seq4, null);
  193.  
  194.                 executor.execute(s1Task);
  195.                 executor.execute(s2Task);
  196.                 executor.execute(s3Task);
  197.  
  198.                 seq1.run();
  199.                 s1Task.run();
  200.                 s2Task.run();
  201.                 s3Task.run();
  202.  
  203.                 try {
  204.                     s1Task.get();
  205.                     s2Task.get();
  206.                     s3Task.get();
  207.                 } catch (Exception e) {
  208.                     System.out.println("Error: " + e.getMessage());
  209.                     executor.shutdownNow();
  210.                 }
  211.             }
  212.         }
  213.  
  214.         public void multiplyStride2() {
  215.             for (int j = 0; j < size; j += 2) {
  216.                 for (int i = 0; i < size; i += 2) {
  217.  
  218.                     float[] a0 = A[aRow + i];
  219.                     float[] a1 = A[aRow + i + 1];
  220.  
  221.                     float s00 = 0.0F;
  222.                     float s01 = 0.0F;
  223.                     float s10 = 0.0F;
  224.                     float s11 = 0.0F;
  225.  
  226.                     for (int k = 0; k < size; k += 2) {
  227.  
  228.                         float[] b0 = B[bRow + k];
  229.  
  230.                         s00 += a0[aCol + k] * b0[bCol + j];
  231.                         s10 += a1[aCol + k] * b0[bCol + j];
  232.                         s01 += a0[aCol + k] * b0[bCol + j + 1];
  233.                         s11 += a1[aCol + k] * b0[bCol + j + 1];
  234.  
  235.                         float[] b1 = B[bRow + k + 1];
  236.  
  237.                         s00 += a0[aCol + k + 1] * b1[bCol + j];
  238.                         s10 += a1[aCol + k + 1] * b1[bCol + j];
  239.                         s01 += a0[aCol + k + 1] * b1[bCol + j + 1];
  240.                         s11 += a1[aCol + k + 1] * b1[bCol + j + 1];
  241.                     }
  242.  
  243.                     C[cRow + i][cCol + j] += s00;
  244.                     C[cRow + i][cCol + j + 1] += s01;
  245.                     C[cRow + i + 1][cCol + j] += s10;
  246.                     C[cRow + i + 1][cCol + j + 1] += s11;
  247.                 }
  248.             }
  249.         }
  250.     }
  251.  
  252.     public static void main(String[] args) {
  253.         // TODO Auto-generated method stub
  254.  
  255.         ParallelMatrixMultiplication test = new ParallelMatrixMultiplication();
  256.         test.initialize();
  257.         test.execute();
  258.         //test.printResult();
  259.        
  260.         Scanner sc = new Scanner(System.in);
  261.  
  262.         while (true) {
  263.             if (sc.next().equals("OK")) {
  264.                 test.printResult();
  265.             }
  266.         }
  267.     }
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement