173 lines
12 KiB
HTML
173 lines
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Matrices.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.ST4 {color: #9876aa}
|
|
.ST5 {color: #ffc66d}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/MP2_CalebFontenot/src/main/java/com/calebfontenot/mp2_calebfontenot/Matrices.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.mp2_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.math.BigInteger;
|
|
<span class="literal">import</span> java.util.ArrayList;
|
|
<span class="literal">import</span> java.util.concurrent.ForkJoinPool;
|
|
<span class="literal">import</span> java.util.concurrent.RecursiveTask;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Matrices {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
|
Matrices.<span class="ST2">multiplyParallel</span>();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> ArrayList<ArrayList<BigInteger>> <span class="ST1">createRandomMatrix</span>(<span class="literal">int</span> rows, <span class="literal">int</span> columns) {
|
|
ArrayList<ArrayList<BigInteger>> matrix = <span class="literal">new</span> ArrayList<ArrayList<BigInteger>>(rows);
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < rows; ++i) {
|
|
ArrayList<BigInteger> row = <span class="literal">new</span> ArrayList<BigInteger>();
|
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < columns; ++j) {
|
|
row.add(<span class="literal">new</span> BigInteger(Integer.<span class="ST2">toString</span>(<span class="number">1</span> + (<span class="literal">int</span>) (Math.<span class="ST2">random</span>() * <span class="number">9</span>))));
|
|
}
|
|
matrix.add(row);
|
|
}
|
|
<span class="literal">return</span> matrix;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">multiplyParallel</span>() {
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> ArrayList<ArrayList<BigInteger>> A = new ArrayList<>();</span>
|
|
<span class="comment"> ArrayList<BigInteger> row1 = new ArrayList<>();</span>
|
|
<span class="comment"> row1.add(new BigInteger("6"));</span>
|
|
<span class="comment"> row1.add(new BigInteger("1"));</span>
|
|
<span class="comment"> ArrayList<BigInteger> row2 = new ArrayList<>();</span>
|
|
<span class="comment"> row2.add(new BigInteger("7"));</span>
|
|
<span class="comment"> row2.add(new BigInteger("2"));</span>
|
|
<span class="comment"> A.add(row1);</span>
|
|
<span class="comment"> A.add(row2);</span>
|
|
|
|
<span class="comment"> ArrayList<ArrayList<BigInteger>> B = new ArrayList<>();</span>
|
|
<span class="comment"> row1 = new ArrayList<BigInteger>();</span>
|
|
<span class="comment"> row1.add(new BigInteger("4"));</span>
|
|
<span class="comment"> row1.add(new BigInteger("1"));</span>
|
|
<span class="comment"> row1.add(new BigInteger("2"));</span>
|
|
<span class="comment"> row2 = new ArrayList<BigInteger>();</span>
|
|
<span class="comment"> row2.add(new BigInteger("1"));</span>
|
|
<span class="comment"> row2.add(new BigInteger("9"));</span>
|
|
<span class="comment"> row2.add(new BigInteger("5"));</span>
|
|
<span class="comment"> B.add(row1);</span>
|
|
<span class="comment"> B.add(row2);</span>
|
|
<span class="comment"> */</span>
|
|
ArrayList<ArrayList<BigInteger>> A = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">2</span>);
|
|
ArrayList<ArrayList<BigInteger>> B = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">3</span>);
|
|
|
|
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
|
|
= <span class="literal">new</span> Matrices.MatricesMultiplication(<span class="number">0</span>, A.size() - <span class="number">1</span>, A, B);
|
|
ForkJoinPool pool = <span class="literal">new</span> ForkJoinPool();
|
|
ArrayList<ArrayList<BigInteger>> mul = pool.invoke(rt);
|
|
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">MATRIX A</span><span class="string">"</span>);
|
|
<span class="ST2">printMatrix</span>(A);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="literal">\n</span><span class="string">MATRIX B</span><span class="string">"</span>);
|
|
<span class="ST2">printMatrix</span>(B);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="literal">\n</span><span class="string">MATRIX AxB</span><span class="string">"</span>);
|
|
<span class="ST2">printMatrix</span>(mul);
|
|
}
|
|
|
|
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMatrix</span>(ArrayList<ArrayList<BigInteger>> matrix) {
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < matrix.size(); ++i) {
|
|
System.<span class="ST3">out</span>.println(matrix.get(i));
|
|
}
|
|
}
|
|
|
|
<span class="literal">static</span> <span class="literal">class</span> <span class="ST2">MatricesMultiplication</span> <span class="literal">extends</span> RecursiveTask<ArrayList<ArrayList<BigInteger>>> {
|
|
|
|
ArrayList<ArrayList<BigInteger>> <span class="ST4">A</span>;
|
|
ArrayList<ArrayList<BigInteger>> <span class="ST4">B</span>;
|
|
ArrayList<ArrayList<BigInteger>> <span class="comment">AxB</span>;
|
|
<span class="literal">final</span> <span class="literal">int</span> <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span> = <span class="number">3</span>;<span class="comment">// threshold</span>
|
|
<span class="literal">int</span> <span class="ST4">startIndex</span>;
|
|
<span class="literal">int</span> <span class="ST4">endIndex</span>;
|
|
|
|
<span class="literal">public</span> MatricesMultiplication(<span class="literal">int</span> startIndex, <span class="literal">int</span> endIndex, ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B) {
|
|
<span class="literal">this</span>.<span class="ST4">startIndex</span> = startIndex;
|
|
<span class="literal">this</span>.<span class="ST4">endIndex</span> = endIndex;
|
|
<span class="literal">this</span>.<span class="ST4">A</span> = A;
|
|
<span class="literal">this</span>.<span class="ST4">B</span> = B;
|
|
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">protected</span> ArrayList<ArrayList<BigInteger>> <span class="ST5">compute</span>() {
|
|
<span class="comment">// Base case: if the number of rows in A is less than or equal to the threshold,</span>
|
|
<span class="comment">// perform matrix multiplication sequentially</span>
|
|
<span class="literal">if</span> (<span class="ST4">endIndex</span> - <span class="ST4">startIndex</span> + <span class="number">1</span> <= <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span>) {
|
|
ArrayList<ArrayList<BigInteger>> result = <span class="literal">new</span> ArrayList<>();
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST4">startIndex</span>; i <= <span class="ST4">endIndex</span>; i++) {
|
|
ArrayList<BigInteger> rowResult = <span class="literal">new</span> ArrayList<>();
|
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < <span class="ST4">B</span>.get(<span class="number">0</span>).size(); j++) {
|
|
BigInteger sum = BigInteger.<span class="ST3">ZERO</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">0</span>; k < <span class="ST4">A</span>.get(<span class="number">0</span>).size(); k++) {
|
|
sum = sum.add(<span class="ST4">A</span>.get(i).get(k).multiply(<span class="ST4">B</span>.get(k).get(j)));
|
|
}
|
|
rowResult.add(sum);
|
|
}
|
|
result.add(rowResult);
|
|
}
|
|
<span class="literal">return</span> result;
|
|
} <span class="literal">else</span> {
|
|
<span class="comment">// Split the task into smaller subtasks</span>
|
|
<span class="literal">int</span> middle = (<span class="ST4">startIndex</span> + <span class="ST4">endIndex</span>) / <span class="number">2</span>;
|
|
<span class="ST2">MatricesMultiplication</span> leftTask = <span class="literal">new</span> MatricesMultiplication(<span class="ST4">startIndex</span>, middle, <span class="ST4">A</span>, <span class="ST4">B</span>);
|
|
<span class="ST2">MatricesMultiplication</span> rightTask = <span class="literal">new</span> MatricesMultiplication(middle + <span class="number">1</span>, <span class="ST4">endIndex</span>, <span class="ST4">A</span>, <span class="ST4">B</span>);
|
|
|
|
<span class="comment">// Fork the subtasks</span>
|
|
leftTask.fork();
|
|
ArrayList<ArrayList<BigInteger>> rightResult = rightTask.compute();
|
|
|
|
<span class="comment">// Join the results of the subtasks</span>
|
|
ArrayList<ArrayList<BigInteger>> leftResult = leftTask.join();
|
|
|
|
<span class="comment">// Merge the results</span>
|
|
ArrayList<ArrayList<BigInteger>> result = <span class="literal">new</span> ArrayList<>();
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < leftResult.size(); i++) {
|
|
result.add(<span class="literal">new</span> ArrayList<>(leftResult.get(i)));
|
|
}
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < rightResult.size(); i++) {
|
|
result.add(<span class="literal">new</span> ArrayList<>(rightResult.get(i)));
|
|
}
|
|
|
|
<span class="literal">return</span> result;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|