ASDV-Java/Semester 4/Assignments/MP2_CalebFontenot/Matricies.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&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST1">createRandomMatrix</span>(<span class="literal">int</span> rows, <span class="literal">int</span> columns) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; matrix = <span class="literal">new</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;(rows);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rows; ++i) {
ArrayList&lt;BigInteger&gt; row = <span class="literal">new</span> ArrayList&lt;BigInteger&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; 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&lt;ArrayList&lt;BigInteger&gt;&gt; A = new ArrayList&lt;&gt;();</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row1 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;6&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row2 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;7&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> A.add(row1);</span>
<span class="comment"> A.add(row2);</span>
<span class="comment"> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;4&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> row2 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;9&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;5&quot;));</span>
<span class="comment"> B.add(row1);</span>
<span class="comment"> B.add(row2);</span>
<span class="comment"> */</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">2</span>);
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">3</span>);
RecursiveTask&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; 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&lt;ArrayList&lt;BigInteger&gt;&gt; mul = pool.invoke(rt);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">MATRIX A</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(A);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX B</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(B);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX AxB</span><span class="string">&quot;</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&lt;ArrayList&lt;BigInteger&gt;&gt; matrix) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; 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&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">A</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">B</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <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&lt;ArrayList&lt;BigInteger&gt;&gt; A, ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; 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&lt;ArrayList&lt;BigInteger&gt;&gt; <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> &lt;= <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span>) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST4">startIndex</span>; i &lt;= <span class="ST4">endIndex</span>; i++) {
ArrayList&lt;BigInteger&gt; rowResult = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <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 &lt; <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&lt;ArrayList&lt;BigInteger&gt;&gt; rightResult = rightTask.compute();
<span class="comment">// Join the results of the subtasks</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; leftResult = leftTask.join();
<span class="comment">// Merge the results</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; leftResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(leftResult.get(i)));
}
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rightResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(rightResult.get(i)));
}
<span class="literal">return</span> result;
}
}
}
}
</pre></body>
</html>