Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Apfloat arbitrary precision arithmetic library
* Copyright (C) 2002-2017 Mikko Tommila
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.apfloat.samples;
import java.io.PrintWriter;
import java.io.IOException;
import org.apfloat.Apfloat;
import org.apfloat.ApfloatContext;
import org.apfloat.ApfloatRuntimeException;
/**
* Calculates pi using multiple threads in parallel.
*
* Note that to get any performance gain from running many
* threads in parallel, the JVM must be executing native threads.
* If the JVM is running in green threads mode, there is no
* advantage of having multiple threads, as the JVM will in fact
* execute just one thread and divide its time to multiple
* simulated threads.
*
* @version 1.8.1
* @author Mikko Tommila
*/
public class PiParallel
extends Pi
{
/**
* Parallel version of the binary splitting algorithm.
* Uses multiple threads to calculate pi in parallel.
*/
protected static class ParallelBinarySplittingPiCalculator
extends BinarySplittingPiCalculator
{
/**
* Construct a parallel pi calculator with the specified precision and radix.
*
* @param series The binary splitting series to be used.
*/
public ParallelBinarySplittingPiCalculator(BinarySplittingSeries series)
throws ApfloatRuntimeException
{
super(series);
}
@Override
public void r(final long n1, final long n2, final ApfloatHolder T, final ApfloatHolder Q, final ApfloatHolder P, final BinarySplittingProgressIndicator progressIndicator)
throws ApfloatRuntimeException
{
checkAlive();
ApfloatContext ctx = ApfloatContext.getContext();
int numberOfProcessors = ctx.getNumberOfProcessors();
if (n1 == n2)
{
// Pathological case where available threads > terms needed
T.setApfloat(Apfloat.ZERO);
Q.setApfloat(Apfloat.ONE);
if (P != null) P.setApfloat(Apfloat.ONE);
}
else if (numberOfProcessors == 1)
{
// End of splitting work between threads
// calculate remaining terms on the current thread
super.r(n1, n2, T, Q, P, progressIndicator);
}
else
{
// Multiple threads available
final ApfloatHolder LT = new ApfloatHolder(),
LQ = new ApfloatHolder(),
LP = new ApfloatHolder();
if (split(n1, n2, numberOfProcessors))
{
// Split work in ratio of number of threads and execute in parallel
int numberOfProcessors1 = numberOfProcessors / 2,
numberOfProcessors2 = numberOfProcessors - numberOfProcessors1;
final long nMiddle = n1 + (n2 - n1) * numberOfProcessors1 / numberOfProcessors;
if (DEBUG) Pi.err.println("PiParallel.r(" + n1 + ", " + n2 + ") splitting " + numberOfProcessors + " threads to r(" + n1 + ", " + nMiddle + ") " + numberOfProcessors1 + " threads, r(" + nMiddle + ", " + n2 + ") " + numberOfProcessors2 + " threads");
// Call recursively this r() method to further split the term calculation
Operation