I forgor to push this, this code is like months old lol

master
Caleb Fontenot 2024-04-11 16:46:49 +07:00
parent ecce3951fb
commit 391dc19c74
5 changed files with 345 additions and 0 deletions

1
.gitignore vendored

@ -208,3 +208,4 @@
/Semester 4/Assignments/MapASDV_CalebFontenot/nbproject/private/
/Semester 4/Assignments/MapASDV_CalebFontenot/build/
/Semester 4/Assignments/Hashing_CalebFontenot/target/
/Semester 4/Assignments/MP2_CalebFontenot/target/

@ -0,0 +1,172 @@
<!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>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>MP2_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mp2_calebfontenot.MP2_CalebFontenot</exec.mainClass>
</properties>
</project>

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mp2_calebfontenot;
/**
*
* @author caleb
*/
public class MP2_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

@ -0,0 +1,142 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp2_calebfontenot;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
*
* @author caleb
*/
public class Matrices {
public static void main(String[] args) {
Matrices.multiplyParallel();
}
public static ArrayList<ArrayList<BigInteger>> createRandomMatrix(int rows, int columns) {
ArrayList<ArrayList<BigInteger>> matrix = new ArrayList<ArrayList<BigInteger>>(rows);
for (int i = 0; i < rows; ++i) {
ArrayList<BigInteger> row = new ArrayList<BigInteger>();
for (int j = 0; j < columns; ++j) {
row.add(new BigInteger(Integer.toString(1 + (int) (Math.random() * 9))));
}
matrix.add(row);
}
return matrix;
}
public static void multiplyParallel() {
/*
ArrayList<ArrayList<BigInteger>> A = new ArrayList<>();
ArrayList<BigInteger> row1 = new ArrayList<>();
row1.add(new BigInteger("6"));
row1.add(new BigInteger("1"));
ArrayList<BigInteger> row2 = new ArrayList<>();
row2.add(new BigInteger("7"));
row2.add(new BigInteger("2"));
A.add(row1);
A.add(row2);
ArrayList<ArrayList<BigInteger>> B = new ArrayList<>();
row1 = new ArrayList<BigInteger>();
row1.add(new BigInteger("4"));
row1.add(new BigInteger("1"));
row1.add(new BigInteger("2"));
row2 = new ArrayList<BigInteger>();
row2.add(new BigInteger("1"));
row2.add(new BigInteger("9"));
row2.add(new BigInteger("5"));
B.add(row1);
B.add(row2);
*/
ArrayList<ArrayList<BigInteger>> A = Matrices.createRandomMatrix(2, 2);
ArrayList<ArrayList<BigInteger>> B = Matrices.createRandomMatrix(2, 3);
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
= new Matrices.MatricesMultiplication(0, A.size() - 1, A, B);
ForkJoinPool pool = new ForkJoinPool();
ArrayList<ArrayList<BigInteger>> mul = pool.invoke(rt);
System.out.println("MATRIX A");
printMatrix(A);
System.out.println("\nMATRIX B");
printMatrix(B);
System.out.println("\nMATRIX AxB");
printMatrix(mul);
}
private static void printMatrix(ArrayList<ArrayList<BigInteger>> matrix) {
for (int i = 0; i < matrix.size(); ++i) {
System.out.println(matrix.get(i));
}
}
static class MatricesMultiplication extends RecursiveTask<ArrayList<ArrayList<BigInteger>>> {
ArrayList<ArrayList<BigInteger>> A;
ArrayList<ArrayList<BigInteger>> B;
ArrayList<ArrayList<BigInteger>> AxB;
final int HOW_MANY_ROWS_IN_PARALLEL = 3;// threshold
int startIndex;
int endIndex;
public MatricesMultiplication(int startIndex, int endIndex, ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.A = A;
this.B = B;
}
@Override
protected ArrayList<ArrayList<BigInteger>> compute() {
// Base case: if the number of rows in A is less than or equal to the threshold,
// perform matrix multiplication sequentially
if (endIndex - startIndex + 1 <= HOW_MANY_ROWS_IN_PARALLEL) {
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = startIndex; i <= endIndex; i++) {
ArrayList<BigInteger> rowResult = new ArrayList<>();
for (int j = 0; j < B.get(0).size(); j++) {
BigInteger sum = BigInteger.ZERO;
for (int k = 0; k < A.get(0).size(); k++) {
sum = sum.add(A.get(i).get(k).multiply(B.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
return result;
} else {
// Split the task into smaller subtasks
int middle = (startIndex + endIndex) / 2;
MatricesMultiplication leftTask = new MatricesMultiplication(startIndex, middle, A, B);
MatricesMultiplication rightTask = new MatricesMultiplication(middle + 1, endIndex, A, B);
// Fork the subtasks
leftTask.fork();
ArrayList<ArrayList<BigInteger>> rightResult = rightTask.compute();
// Join the results of the subtasks
ArrayList<ArrayList<BigInteger>> leftResult = leftTask.join();
// Merge the results
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = 0; i < leftResult.size(); i++) {
result.add(new ArrayList<>(leftResult.get(i)));
}
for (int i = 0; i < rightResult.size(); i++) {
result.add(new ArrayList<>(rightResult.get(i)));
}
return result;
}
}
}
}