All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nom.bdezonia.zorbage.algorithm.ParallelTransform9 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.Procedure9;
import nom.bdezonia.zorbage.algebra.Algebra;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.datasource.TrimmedDataSource;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class ParallelTransform9 {

	// do not instantiate
	
	private ParallelTransform9() { }
	
	/**
	 * Transform eight lists into a ninth list using a function/procedure call at each point
	 * in the eight 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
	 */
	public static , A>
		void compute(AA alg, Procedure9 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource i)
	{
		compute(alg, alg, alg, alg, alg, alg, alg, alg, alg, proc, a, b, c, d, e, f, g, h, i);	
	}
	
	/**
	 * Transform eight lists into a ninth list using a function/procedure call at each point
	 * in the eight 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 proc
	 * @param a
	 * @param b
	 * @param c
	 * @param d
	 * @param e
	 * @param f
	 * @param g
	 * @param h
	 * @param ii
	 */
	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>
		void compute(AA algA, BB algB, CC algC, DD algD, EE algE, FF algF, GG algG, HH algH, II algI, Procedure9 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource ii)
	{
		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);
			Runnable r = new Computer(algA, algB, algC, algD, algE, algF, algG, algH, algI, proc, aTrimmed, bTrimmed, cTrimmed, dTrimmed, eTrimmed, fTrimmed, gTrimmed, hTrimmed, iTrimmed);
			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>
		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 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 Procedure9 proc;
		
		Computer(AA algA, BB algB, CC algC, DD algD, EE algE, FF algF, GG algG, HH algH, II algI, Procedure9 proc, IndexedDataSource a, IndexedDataSource b, IndexedDataSource c, IndexedDataSource d, IndexedDataSource e, IndexedDataSource f, IndexedDataSource g, IndexedDataSource h, IndexedDataSource i) {
			algebraA = algA;
			algebraB = algB;
			algebraC = algC;
			algebraD = algD;
			algebraE = algE;
			algebraF = algF;
			algebraG = algG;
			algebraH = algH;
			algebraI = algI;
			listA = a;
			listB = b;
			listC = c;
			listD = d;
			listE = e;
			listF = f;
			listG = g;
			listH = h;
			listI = i;
			this.proc = proc;
		}
		
		public void run() {
			Transform9.compute(algebraA, algebraB, algebraC, algebraD, algebraE, algebraF, algebraG, algebraH, algebraI, proc, listA, listB, listC, listD, listE, listF, listG, listH, listI);
		}
	}
}