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

com.fathzer.games.perft.MultiThreadedPerfT Maven / Gradle / Ivy

The newest version!
package com.fathzer.games.perft;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.fathzer.games.MoveGenerator;
import com.fathzer.games.MoveGenerator.MoveConfidence;
import com.fathzer.games.util.UncheckedException;

/** A Perft test with each divide computed by a separate thread.
 * @see PerfTBuilder
 */
class MultiThreadedPerfT extends PerfT {
	final ExecutorService exec;
	
	MultiThreadedPerfT(ExecutorService exec, MoveGenerator board, int depth, boolean playLeaves, MoveConfidence moveType) {
		super(board, depth, playLeaves, moveType);
		this.exec = exec;
	}
	
	@Override
	void compute(List moves) {
		final List>> results = moves.stream().map(m -> exec.submit(getDivideTask(board, m, depth))).toList();
		addDivides(results);
	}
	
	private Callable> getDivideTask(MoveGenerator board, M move, int depth) {
		return () -> getRootPerfT(board.fork(), move, depth - 1);
	}
	
	void addDivides(List>> results) {
		try {
			for (Future> f : results) {
				final Divide divide = f.get();
				if (divide!=null) {
					result.add(divide);
				}
			}
		} catch (InterruptedException e) {
			result.setInterrupted(true);
			interrupt();
			Thread.currentThread().interrupt();
		} catch (ExecutionException e) {
			throw new UncheckedException(e.getCause());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy