Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Exercise3 {
- public class Matrix{
- }
- //Method that multiplies two matrices
- public static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2){
- int[][] product = new int[matrix1.length][matrix2[0].length];
- if (matrix1[0].length != matrix2.length) {
- throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
- }
- for(int x = 0; x < matrix1.length; x++) {
- for(int y = 0; y < matrix2[0].length; y++) {
- for (int z = 0; z < matrix1[0].length; z++) {
- product[x][y] += matrix1[x][z] * matrix2[y][z];
- }
- }
- }
- return product;
- }
- //Method that multiplies two matrices and Multithread support
- public static int[][] matrixMultiplicationMultithread(int[][] matrix1, int[][] matrix2, int numOfThreads){
- int[][] product = new int[matrix1.length][matrix2[0].length];
- if (matrix1[0].length != matrix2.length) {
- throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
- }
- for(int x = 0; x < matrix1.length; x++) {
- for(int y = 0; y < matrix2[0].length; y++) {
- for (int z = 0; z < matrix1[0].length; z++) {
- product[x][y] += matrix1[x][z] * matrix2[y][z];
- }
- }
- }
- return product;
- }
- //Method to print a matrix
- public static void printMatrix(int[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- //Method that initializes a Matrix
- public static int[][] initializeMatrix(int[][] matrix, int numOfRows, int numOfColumns) {
- for(int j = 0; j < numOfRows; j++) {
- for(int k = 0; k < numOfColumns; k++) {
- matrix[j][k] = j+k;
- }
- }
- return matrix;
- }
- public static void main(String[] args) {
- Thread thread = null;
- int numOfRows = 300; //Access rows with .length
- int numOfColumns = 300; //Access columns with [0].length
- int numOfThreads = 1;
- int[][] matrix1 = new int [numOfRows][numOfColumns];
- int[][] matrix2 = new int [numOfRows][numOfColumns];
- matrix1 = initializeMatrix(matrix1, numOfRows, numOfColumns);
- matrix2 = initializeMatrix(matrix2, numOfRows, numOfColumns);
- int[][] product = new int [matrix1.length][matrix2[0].length];
- final long startTime = System.currentTimeMillis();
- product = matrixMultiplication(matrix1, matrix2);
- final long endTime = System.currentTimeMillis();
- System.out.println("The main thread took " +(endTime-startTime)+"ms to complete the matrix multiplication");
- int rowsDividedByNumOfThread = matrix1.length / numOfThreads;
- int start = 0;
- int end = rowsDividedByNumOfThread-1;
- for(int i = 0; i < numOfThreads; i++) {
- thread = new Thread(new MatrixMultiplicationThread(matrix1, matrix2, start, end));
- thread.start();
- start = end + 1;
- end = end + rowsDividedByNumOfThread +1;
- }
- }
- }
- ________________________________________________________________________________________________
- public class MatrixMultiplicationThread extends Thread {
- public int[][] matrix1 = new int[0][0]; //null?
- public int[][] matrix2 = new int[0][0];
- public int start = 0;
- public int end = 0;
- public int[][] product = new int[0][0];
- public MatrixMultiplicationThread (int[][] matrix1, int[][] matrix2, int start, int end) {
- this.matrix1 = matrix1;
- this.matrix2 = matrix2;
- this.start = start;
- this.end = end;
- }
- public int[][] getProduct() {
- return product;
- }
- public void setProduct(int[][] product) {
- this.product = product;
- }
- @Override
- public void run() {
- int[][] product = new int[matrix1.length][matrix2[0].length];
- if (matrix1[0].length != matrix2.length) {
- throw new IllegalArgumentException("Columns of matrix1 do not match rows of matrix2");
- }
- for(int x = start; x < matrix1.length && x < end; x++) {
- for(int y = start; y < matrix2[0].length && y < end; y++) {
- for (int z = start; z < matrix1[0].length && y < end; z++) {
- product[x][y] += matrix1[x][z] * matrix2[y][z];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement