
nom.bdezonia.zorbage.algorithm.ParallelTransform22 Maven / Gradle / Ivy
/*
* Zorbage: an algebraic data hierarchy for use in numeric processing.
*
* Copyright (c) 2016-2021 Barry DeZonia All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* Neither the name of the nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
package nom.bdezonia.zorbage.algorithm;
import nom.bdezonia.zorbage.procedure.Procedure22;
import nom.bdezonia.zorbage.algebra.Algebra;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.datasource.TrimmedDataSource;
/**
*
* @author Barry DeZonia
*
*/
public class ParallelTransform22 {
// do not instantiate
private ParallelTransform22() { }
/**
* Transform twenty one lists into a twenty second list using a function/procedure call at each point
* in the twenty one lists. Use a parallel algorithm for extra speed.
*
* @param alg
* @param proc
* @param a
* @param b
* @param c
* @param d
* @param e
* @param f
* @param g
* @param h
* @param i
* @param j
* @param k
* @param l
* @param m
* @param n
* @param o
* @param p
* @param q
* @param r
* @param s
* @param t
* @param u
* @param v
*/
public static , A>
void compute(AA alg, Procedure22 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource i, IndexedDataSource j, IndexedDataSource k, IndexedDataSource l, IndexedDataSource m, IndexedDataSource n, IndexedDataSource o, IndexedDataSource p, IndexedDataSource q, IndexedDataSource r, IndexedDataSource s, IndexedDataSource t, IndexedDataSource u, IndexedDataSource v)
{
compute(alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, alg, proc, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v);
}
/**
* Transform twenty one lists into a twenty second list using a function/procedure call at each point
* in the twenty one lists. Use a parallel algorithm for extra speed.
*
* @param algA
* @param algB
* @param algC
* @param algD
* @param algE
* @param algF
* @param algG
* @param algH
* @param algI
* @param algJ
* @param algK
* @param algL
* @param algM
* @param algN
* @param algO
* @param algP
* @param algQ
* @param algR
* @param algS
* @param algT
* @param algU
* @param algV
* @param proc
* @param a
* @param b
* @param c
* @param d
* @param e
* @param f
* @param g
* @param h
* @param ii
* @param j
* @param k
* @param l
* @param m
* @param n
* @param o
* @param p
* @param q
* @param rr
* @param s
* @param t
* @param u
* @param v
*/
public static , A, BB extends Algebra, B, CC extends Algebra, C, DD extends Algebra, D, EE extends Algebra, E, FF extends Algebra, F, GG extends Algebra, G, HH extends Algebra, H, II extends Algebra, I, JJ extends Algebra, J, KK extends Algebra, K, LL extends Algebra, L, MM extends Algebra, M, NN extends Algebra, N, OO extends Algebra, O, PP extends Algebra, P, QQ extends Algebra, Q, RR extends Algebra, R, SS extends Algebra, S, TT extends Algebra, T, UU extends Algebra, U, VV extends Algebra, V>
void compute(AA algA, BB algB, CC algC, DD algD, EE algE, FF algF, GG algG, HH algH, II algI, JJ algJ, KK algK, LL algL, MM algM, NN algN, OO algO, PP algP, QQ algQ, RR algR, SS algS, TT algT, UU algU, VV algV, Procedure22 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource ii, IndexedDataSource j, IndexedDataSource k, IndexedDataSource l, IndexedDataSource m, IndexedDataSource n, IndexedDataSource o, IndexedDataSource p, IndexedDataSource q, IndexedDataSource rr, IndexedDataSource s, IndexedDataSource t, IndexedDataSource u, IndexedDataSource v)
{
long aSize = a.size();
int numProcs = Runtime.getRuntime().availableProcessors();
if (aSize < numProcs) {
numProcs = (int) aSize;
}
final Thread[] threads = new Thread[numProcs];
long thOffset = 0;
long slice = aSize / numProcs;
for (int i = 0; i < numProcs; i++) {
long thSize;
if (i != numProcs-1) {
thSize = slice;
}
else {
thSize = aSize;
}
IndexedDataSource aTrimmed = new TrimmedDataSource<>(a, thOffset, thSize);
IndexedDataSource bTrimmed = new TrimmedDataSource<>(b, thOffset, thSize);
IndexedDataSource cTrimmed = new TrimmedDataSource<>(c, thOffset, thSize);
IndexedDataSource dTrimmed = new TrimmedDataSource<>(d, thOffset, thSize);
IndexedDataSource eTrimmed = new TrimmedDataSource<>(e, thOffset, thSize);
IndexedDataSource fTrimmed = new TrimmedDataSource<>(f, thOffset, thSize);
IndexedDataSource gTrimmed = new TrimmedDataSource<>(g, thOffset, thSize);
IndexedDataSource hTrimmed = new TrimmedDataSource<>(h, thOffset, thSize);
IndexedDataSource iTrimmed = new TrimmedDataSource<>(ii, thOffset, thSize);
IndexedDataSource jTrimmed = new TrimmedDataSource<>(j, thOffset, thSize);
IndexedDataSource kTrimmed = new TrimmedDataSource<>(k, thOffset, thSize);
IndexedDataSource lTrimmed = new TrimmedDataSource<>(l, thOffset, thSize);
IndexedDataSource mTrimmed = new TrimmedDataSource<>(m, thOffset, thSize);
IndexedDataSource nTrimmed = new TrimmedDataSource<>(n, thOffset, thSize);
IndexedDataSource oTrimmed = new TrimmedDataSource<>(o, thOffset, thSize);
IndexedDataSource pTrimmed = new TrimmedDataSource<>(p, thOffset, thSize);
IndexedDataSource qTrimmed = new TrimmedDataSource<>(q, thOffset, thSize);
IndexedDataSource rTrimmed = new TrimmedDataSource<>(rr, thOffset, thSize);
IndexedDataSource sTrimmed = new TrimmedDataSource<>(s, thOffset, thSize);
IndexedDataSource tTrimmed = new TrimmedDataSource<>(t, thOffset, thSize);
IndexedDataSource uTrimmed = new TrimmedDataSource<>(u, thOffset, thSize);
IndexedDataSource vTrimmed = new TrimmedDataSource<>(v, thOffset, thSize);
Runnable r = new Computer(algA, algB, algC, algD, algE, algF, algG, algH, algI, algJ, algK, algL, algM, algN, algO, algP, algQ, algR, algS, algT, algU, algV, proc, aTrimmed, bTrimmed, cTrimmed, dTrimmed, eTrimmed, fTrimmed, gTrimmed, hTrimmed, iTrimmed, jTrimmed, kTrimmed, lTrimmed, mTrimmed, nTrimmed, oTrimmed, pTrimmed, qTrimmed, rTrimmed, sTrimmed, tTrimmed, uTrimmed, vTrimmed);
threads[i] = new Thread(r);
thOffset += slice;
aSize -= slice;
}
for (int i = 0; i < numProcs; i++) {
threads[i].start();
}
for (int i = 0; i < numProcs; i++) {
try {
threads[i].join();
} catch(InterruptedException exc) {
throw new IllegalArgumentException("Thread execution error in ParallelTransform");
}
}
}
private static class Computer, A, BB extends Algebra, B, CC extends Algebra, C, DD extends Algebra, D, EE extends Algebra, E, FF extends Algebra, F, GG extends Algebra, G, HH extends Algebra, H, II extends Algebra, I, JJ extends Algebra, J, KK extends Algebra, K, LL extends Algebra, L, MM extends Algebra, M, NN extends Algebra, N, OO extends Algebra, O, PP extends Algebra, P, QQ extends Algebra, Q, RR extends Algebra, R, SS extends Algebra, S, TT extends Algebra, T, UU extends Algebra, U, VV extends Algebra, V>
implements Runnable
{
private final AA algebraA;
private final BB algebraB;
private final CC algebraC;
private final DD algebraD;
private final EE algebraE;
private final FF algebraF;
private final GG algebraG;
private final HH algebraH;
private final II algebraI;
private final JJ algebraJ;
private final KK algebraK;
private final LL algebraL;
private final MM algebraM;
private final NN algebraN;
private final OO algebraO;
private final PP algebraP;
private final QQ algebraQ;
private final RR algebraR;
private final SS algebraS;
private final TT algebraT;
private final UU algebraU;
private final VV algebraV;
private final IndexedDataSource listA;
private final IndexedDataSource listB;
private final IndexedDataSource listC;
private final IndexedDataSource listD;
private final IndexedDataSource listE;
private final IndexedDataSource listF;
private final IndexedDataSource listG;
private final IndexedDataSource listH;
private final IndexedDataSource listI;
private final IndexedDataSource listJ;
private final IndexedDataSource listK;
private final IndexedDataSource listL;
private final IndexedDataSource listM;
private final IndexedDataSource listN;
private final IndexedDataSource listO;
private final IndexedDataSource listP;
private final IndexedDataSource listQ;
private final IndexedDataSource listR;
private final IndexedDataSource listS;
private final IndexedDataSource listT;
private final IndexedDataSource listU;
private final IndexedDataSource listV;
private final Procedure22 proc;
Computer(AA algA, BB algB, CC algC, DD algD, EE algE, FF algF, GG algG, HH algH, II algI, JJ algJ, KK algK, LL algL, MM algM, NN algN, OO algO, PP algP, QQ algQ, RR algR, SS algS, TT algT, UU algU, VV algV, Procedure22 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource i, IndexedDataSource j, IndexedDataSource k, IndexedDataSource l, IndexedDataSource m, IndexedDataSource n, IndexedDataSource o, IndexedDataSource p, IndexedDataSource q, IndexedDataSource r, IndexedDataSource s, IndexedDataSource t, IndexedDataSource u, IndexedDataSource v) {
algebraA = algA;
algebraB = algB;
algebraC = algC;
algebraD = algD;
algebraE = algE;
algebraF = algF;
algebraG = algG;
algebraH = algH;
algebraI = algI;
algebraJ = algJ;
algebraK = algK;
algebraL = algL;
algebraM = algM;
algebraN = algN;
algebraO = algO;
algebraP = algP;
algebraQ = algQ;
algebraR = algR;
algebraS = algS;
algebraT = algT;
algebraU = algU;
algebraV = algV;
listA = a;
listB = b;
listC = c;
listD = d;
listE = e;
listF = f;
listG = g;
listH = h;
listI = i;
listJ = j;
listK = k;
listL = l;
listM = m;
listN = n;
listO = o;
listP = p;
listQ = q;
listR = r;
listS = s;
listT = t;
listU = u;
listV = v;
this.proc = proc;
}
public void run() {
Transform22.compute(algebraA, algebraB, algebraC, algebraD, algebraE, algebraF, algebraG, algebraH, algebraI, algebraJ, algebraK, algebraL, algebraM, algebraN, algebraO, algebraP, algebraQ, algebraR, algebraS, algebraT, algebraU, algebraV, proc, listA, listB, listC, listD, listE, listF, listG, listH, listI, listJ, listK, listL, listM, listN, listO, listP, listQ, listR, listS, listT, listU, listV);
}
}
}