org.jctools.queues.SpmcArrayQueue Java Examples

The following examples show how to use org.jctools.queues.SpmcArrayQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpmcArrayQueuePeekTest.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // Smaller capacity helps test
    MessagePassingQueue<Long> messageQueue = new SpmcArrayQueue<>(8);
    new Producer(messageQueue).start();
    new Consumer(messageQueue).start();
    new Peeker(messageQueue).start();

    try {
        Thread.sleep(10 * 1000);
    } finally {
        stop = true;
    }
}
 
Example #2
Source File: ThreadedSimpleNBody.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public ThreadedSimpleNBody() {
	int cores = Runtime.getRuntime().availableProcessors();
	maxCuboids = cores * 2;
	maxRunners = cores * 12;
	sharedQueue = new SpmcArrayQueue<NBodyCuboid.CuboidCoordinates>(maxCuboids);
	resultsQueue = new MpscArrayQueue<CuboidRunnerResults>(maxCuboids);
	runnerExecutor = Executors.newFixedThreadPool(maxRunners);
}
 
Example #3
Source File: CuboidRunner.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public CuboidRunner(double dmp, double timeDelta,
	SpmcArrayQueue<CuboidCoordinates> cuboidQueue,
	MpscArrayQueue<CuboidRunnerResults> resultsQueue,
	Collection<NBody3DBody> universe) {
	this.id = UUID.randomUUID();
	this.dmp = dmp;
	this.timeDelta = timeDelta;
	this.cuboidQueue = cuboidQueue;
	this.universe = universe;
	this.resultsQueue = resultsQueue;
}
 
Example #4
Source File: Simulation.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
private StepResults runWithTreahds(StepData stepData) throws InvalidNumberOfCubesException {
		if (maxCuboids == -1) {
			maxCuboids = Runtime.getRuntime().availableProcessors();
			maxRunners = maxCuboids * 2;
			sharedQueue = new SpmcArrayQueue<NBodyCuboid.CuboidCoordinates>(maxCuboids);
			resultsQueue = new MpscArrayQueue<CuboidRunnerResults>(maxCuboids);
			runnerExecutor = Executors.newFixedThreadPool(maxRunners);
		}
		prprDrtn = Duration.ZERO;
		calcAccelDrtn = Duration.ZERO;
		calcVelDrtn = Duration.ZERO;
		calcPosDrtn = Duration.ZERO;
		memSize = 0;
		// x in b=2^x, where b is the next power of 2 greater than a
		int numCuboids = (int) Math.pow(2, 32 - Integer.numberOfLeadingZeros(maxCuboids - 1));
		Collection<CuboidCoordinates> stepCuboids = new StockCuboids(stepData.getCoordiantes()).setupCuboids(numCuboids);
		List<NBody3DBody> newUniverse = new ArrayList<>(stepData.getUniverse().size());
		while (!stepCuboids.isEmpty()) {
			sharedQueue.addAll(stepCuboids);
			List<CuboidRunner> runners = new ArrayList<CuboidRunner>();
			for (int r = 0; r < numCuboids; r++) {
				CuboidRunner runner = new CuboidRunner(0.995, 0.001, sharedQueue, resultsQueue, stepData.getUniverse());
				runners.add(runner);
			}

			List<Future<UUID>> futures = null;
			try {
				futures = runnerExecutor.invokeAll(runners);
			} catch (InterruptedException e1) {
				System.err.println("ex " + e1.getMessage());
			} finally {
				if (futures != null) {
					resultsQueue.drain(r -> {
//						boolean found = checkIds(futures, r);
//						if (!found) {
//							throw new IllegalStateException("Finished thread did not queue results");
//						}
						try {
							prprDrtn = prprDrtn.plus(r.durations().prepareDrtn());
							calcAccelDrtn = calcAccelDrtn.plus(r.durations().calcAccelDrtn());
							calcVelDrtn = calcVelDrtn.plus(r.durations().calcVelDrtn());
							calcPosDrtn = calcPosDrtn.plus(r.durations().calcPosDrtn());
							// Only count if durations are OK
							stepCuboids.remove(r.coordiantes());
							newUniverse.addAll(r.bodies());
							memSize += r.memUsed();
						} catch (RequestedDurationNotFound e) {
							// Retry
							System.err.println("ex " + e.getMessage());
						}
					});
				}
			}
		}
		System.out.println("Simultaion multi-thread finished " + LocalDateTime.now());
		return new StepResults(
				stepData.getCoordiantes(),
				new StockCuboidSimulationDurations(
						prprDrtn.toNanos(),
						calcAccelDrtn.toNanos(),
						calcVelDrtn.toNanos(),
						calcPosDrtn.toNanos()),
				newUniverse,
				memSize);
	}