📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / documents / Programmierparadigmen / scripts / java / matrix-multiplication.java
132937 viewsLicense: OTHER
import java.io.BufferedReader;1import java.io.FileReader;2import java.io.IOException;3import java.util.LinkedList;4import java.util.List;5import java.util.ArrayList;6import java.util.concurrent.Callable;7import java.util.concurrent.ExecutionException;8import java.util.concurrent.ExecutorService;9import java.util.concurrent.Executors;10import java.util.concurrent.Future;1112public class Shell {13static List<ArrayList<ArrayList<Integer>>> read(String filename) {14ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();15ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();1617String thisLine;1819try {20BufferedReader br = new BufferedReader(new FileReader(filename));21// Begin reading A22while ((thisLine = br.readLine()) != null) {23if (thisLine.trim().equals("")) {24break;25} else {26ArrayList<Integer> line = new ArrayList<Integer>();27String[] lineArray = thisLine.split("\t");28for (String number : lineArray) {29line.add(Integer.parseInt(number));30}31A.add(line);32}33}3435// Begin reading B36while ((thisLine = br.readLine()) != null) {37ArrayList<Integer> line = new ArrayList<Integer>();38String[] lineArray = thisLine.split("\t");39for (String number : lineArray) {40line.add(Integer.parseInt(number));41}42B.add(line);43}44br.close();45} catch (IOException e) {46System.err.println("Error: " + e);47}4849List<ArrayList<ArrayList<Integer>>> res = new LinkedList<ArrayList<ArrayList<Integer>>>();50res.add(A);51res.add(B);52return res;53}5455static void printMatrix(int[][] matrix) {56for (int[] line : matrix) {57int i = 0;58StringBuilder sb = new StringBuilder(matrix.length);59for (int number : line) {60if (i != 0) {61sb.append("\t");62} else {63i++;64}65sb.append(number);66}67System.out.println(sb.toString());68}69}7071public static int[][] parallelMult(ArrayList<ArrayList<Integer>> A,72ArrayList<ArrayList<Integer>> B, int threadNumber) {73int[][] C = new int[A.size()][B.get(0).size()];74ExecutorService executor = Executors.newFixedThreadPool(threadNumber);75List<Future<int[][]>> list = new ArrayList<Future<int[][]>>();7677int part = A.size() / threadNumber;78if (part < 1) {79part = 1;80}81for (int i = 0; i < A.size(); i += part) {82System.err.println(i);83Callable<int[][]> worker = new LineMultiplier(A, B, i, i+part);84Future<int[][]> submit = executor.submit(worker);85list.add(submit);86}8788// now retrieve the result89int start = 0;90int CF[][];91for (Future<int[][]> future : list) {92try {93CF = future.get();94for (int i=start; i < start+part; i += 1) {95C[i] = CF[i];96}97} catch (InterruptedException e) {98e.printStackTrace();99} catch (ExecutionException e) {100e.printStackTrace();101}102start+=part;103}104executor.shutdown();105106return C;107}108109public static void main(String[] args) {110String filename;111int cores = Runtime.getRuntime().availableProcessors();112System.err.println("Number of cores:\t" + cores);113114int threads;115if (args.length < 3) {116filename = "3.in";117threads = cores;118} else {119filename = args[1];120threads = Integer.parseInt(args[2]);121}122123List<ArrayList<ArrayList<Integer>>> matrices = read(filename);124ArrayList<ArrayList<Integer>> A = matrices.get(0);125ArrayList<ArrayList<Integer>> B = matrices.get(1);126int[][] C = parallelMult(A, B, threads);127printMatrix(C);128}129}130131132