|
|
|
@ -33,9 +33,15 @@ public class Matrices implements Matrix
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ArrayList<ArrayList<BigInteger>> subtractParallel(ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B)
|
|
|
|
|
{
|
|
|
|
|
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
|
|
|
|
|
= new Matrices.MatricesSubtraction(0, A.size() - 1, A, B);
|
|
|
|
|
ForkJoinPool pool = new ForkJoinPool();
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> result = pool.invoke(rt);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class MatricesAddition extends RecursiveTask<ArrayList<ArrayList<BigInteger>>>
|
|
|
|
@ -132,6 +138,18 @@ public class Matrices implements Matrix
|
|
|
|
|
return resultsOfMultiplications;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ArrayList<BigInteger> subtractLists(ArrayList<BigInteger> list1, ArrayList<BigInteger> list2)
|
|
|
|
|
{
|
|
|
|
|
ArrayList<BigInteger> resultsOfSubtraction = new ArrayList<BigInteger>();
|
|
|
|
|
for (int bi = 0; bi < list1.size();
|
|
|
|
|
++bi)
|
|
|
|
|
{
|
|
|
|
|
resultsOfSubtraction.add(list1.get(bi).subtract(list2.get(bi)));
|
|
|
|
|
}
|
|
|
|
|
return resultsOfSubtraction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ArrayList<ArrayList<BigInteger>> columnMajorOrderReversal(ArrayList<ArrayList<BigInteger>> b)
|
|
|
|
|
{
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> tranformed = new ArrayList<ArrayList<BigInteger>>();
|
|
|
|
@ -170,6 +188,14 @@ public class Matrices implements Matrix
|
|
|
|
|
return bd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static BigInteger subtract(ArrayList<BigInteger> list) {
|
|
|
|
|
BigInteger bd = BigInteger.ZERO;
|
|
|
|
|
for (int bi = 0; bi < list.size(); bi++) {
|
|
|
|
|
bd = bd.subtract(list.get(bi));
|
|
|
|
|
}
|
|
|
|
|
return bd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class MatricesMultiplication extends RecursiveTask<ArrayList<ArrayList<BigInteger>>>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
@ -262,4 +288,97 @@ public class Matrices implements Matrix
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class MatricesSubtraction 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 MatricesSubtraction(int startIndex, int endIndex,
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> A,
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> B)
|
|
|
|
|
{
|
|
|
|
|
this.startIndex = startIndex;//start at this row of the matrix
|
|
|
|
|
this.endIndex = endIndex;//end at this row of the matrix
|
|
|
|
|
this.A = A;
|
|
|
|
|
this.B = B;
|
|
|
|
|
AxB = new ArrayList<ArrayList<BigInteger>>();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* matrix
|
|
|
|
|
* 1, 2, 3
|
|
|
|
|
* 4, 5, 6
|
|
|
|
|
*
|
|
|
|
|
* will be transformed to
|
|
|
|
|
* 1, 4
|
|
|
|
|
* 2, 5
|
|
|
|
|
* 3, 6
|
|
|
|
|
*
|
|
|
|
|
* @param list
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected ArrayList<ArrayList<BigInteger>> compute()
|
|
|
|
|
{
|
|
|
|
|
//>>This is the addition of matrices in the IF.
|
|
|
|
|
//That is, HOW_MANY_ROWS_IN_PARALLEL from matrix A and HOW_MANY_ROWS_IN_PARALLEL from matrix B
|
|
|
|
|
if (this.endIndex - this.startIndex < HOW_MANY_ROWS_IN_PARALLEL)
|
|
|
|
|
{
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> resultC = new ArrayList<ArrayList<BigInteger>>();
|
|
|
|
|
ArrayList<ArrayList<BigInteger>> bTransformed = columnMajorOrderReversal(B);
|
|
|
|
|
|
|
|
|
|
for (int biA = this.startIndex;
|
|
|
|
|
biA <= this.endIndex;
|
|
|
|
|
++biA)
|
|
|
|
|
{
|
|
|
|
|
ArrayList<BigInteger> rowA = A.get(biA);
|
|
|
|
|
ArrayList<BigInteger> rowAxB = new ArrayList<BigInteger>();
|
|
|
|
|
ArrayList<BigInteger> rowCalculation = new ArrayList<BigInteger>();
|
|
|
|
|
for (int biB = 0;
|
|
|
|
|
biB < bTransformed.size();
|
|
|
|
|
++biB)
|
|
|
|
|
{
|
|
|
|
|
ArrayList<BigInteger> rowB = bTransformed.get(biB);
|
|
|
|
|
|
|
|
|
|
ArrayList<BigInteger> productsOfRow = subtractLists(rowA, rowB);
|
|
|
|
|
BigInteger sumOfDifference = add(productsOfRow);
|
|
|
|
|
rowCalculation.add(sumOfDifference);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
resultC.add(rowCalculation);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return resultC;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ //>> keep on FORKING the matrix until the
|
|
|
|
|
//side of the matric is equal or less to HOW_MANY_ROWS_IN_PARALLEL
|
|
|
|
|
|
|
|
|
|
int mid = (this.startIndex + this.endIndex) / 2;
|
|
|
|
|
RecursiveTask<ArrayList<ArrayList<BigInteger>>> firstHalf
|
|
|
|
|
= new MatricesMultiplication(this.startIndex, mid, A, B);
|
|
|
|
|
|
|
|
|
|
RecursiveTask<ArrayList<ArrayList<BigInteger>>> secondHalf
|
|
|
|
|
= new MatricesMultiplication(mid + 1, this.endIndex, A, B);
|
|
|
|
|
|
|
|
|
|
firstHalf.fork();//this line will invoke method compute
|
|
|
|
|
secondHalf.fork();///this line will invoke method compute
|
|
|
|
|
|
|
|
|
|
//>> join what the FORKs returned from the IFs
|
|
|
|
|
AxB.addAll(firstHalf.join());
|
|
|
|
|
AxB.addAll(secondHalf.join());
|
|
|
|
|
|
|
|
|
|
return AxB;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|