项目作者: michalgorul

项目描述 :
Simple matrix multiplication tester which compares execution times of some popular implementations of this issue
高级语言: Java
项目地址: git://github.com/michalgorul/matrix-multiplication.git
创建时间: 2021-08-17T08:06:06Z
项目社区:https://github.com/michalgorul/matrix-multiplication

开源协议:

下载


Matrix Multiplication Tester

The goal of this project was to become familiar with the concept of multithreading in java. It was my first encounter with this issue. This application multiplies a matrix of user-specified dimensions using several popular libraries and my own implementation of this problem, and then compares execution times of this multiplication. It is possible to select a specific number of simultaneously running threads to find the most efficient number.

GUI design

image

Sample ActionListener class

public class StartListener implements ActionListener {

  1. @Override
  2. public void actionPerformed(ActionEvent e) {
  3. try {
  4. if (mainPage.getColumnsFirst() != mainPage.getRowsSecond()) {
  5. mainPage.showMatricesSizeError();
  6. } else if (mainPage.getMinValue() >= mainPage.getMaxValue()) {
  7. mainPage.showValuesError();
  8. } else {
  9. matricesMultiplication = new MatricesMultiplication(
  10. mainPage.getRowsFirst(),
  11. mainPage.getColumnsFirst(),
  12. mainPage.getMinValue(),
  13. mainPage.getMaxValue(),
  14. mainPage.getRowsSecond(),
  15. mainPage.getColumnsSecond());
  16. if (mainPage.getRowsFirst() > 500 && mainPage.getColumnsFirst() > 500
  17. && mainPage.getRowsSecond() > 500 && mainPage.getColumnsSecond() > 500){
  18. mainPage.showLongerTimeInfo();
  19. setTimesMap(matricesMultiplication);
  20. setTimesTextFields();
  21. mainPage.showDoneInfo();
  22. }
  23. else{
  24. setTimesMap(matricesMultiplication);
  25. setTimesTextFields();
  26. }
  27. }
  28. } catch (Exception ex) {
  29. ex.printStackTrace();
  30. }
  31. }
  32. }

Class responsible for matrix multiplication using multiple threads

  1. public class ParallelThreads {
  2. public static double[][] multiply(double[][] matrix1, double[][] matrix2, int numberThreads) {
  3. double[][] result = new double[matrix1.length][matrix2[0].length];
  4. List threads = new ArrayList<>();
  5. int rows = matrix1.length;
  6. if(rows > numberThreads){
  7. for (int i = 0; i < rows; i++) {
  8. MultiplyRow task = new MultiplyRow(result, matrix1, matrix2, i);
  9. Thread thread = new Thread(task);
  10. thread.start();
  11. threads.add(thread);
  12. if (threads.size() % numberThreads == 0) {
  13. waitForThreads(threads);
  14. }
  15. }
  16. }
  17. else if(rows < numberThreads){
  18. for (int i = 0; i < rows; i++) {
  19. MultiplyRow task = new MultiplyRow(result, matrix1, matrix2, i);
  20. Thread thread = new Thread(task);
  21. thread.start();
  22. threads.add(thread);
  23. waitForThreads(threads);
  24. }
  25. }
  26. return result;
  27. }
  28. private static void waitForThreads(List<Thread> threads) {
  29. for (Thread thread : threads) {
  30. try {
  31. thread.join();
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. threads.clear();
  37. }
  38. }