Java 程序的并行化增加了执行时间
我正在上莱斯大学开设的并行编程课程。在赋值中,我们应该递归地计算数组元素的倒数和。为了并行化这一点,我们使用了一个fork/join池,递归地计算出所需的左、右子数组之和,并将它们合并回来。但我得到的执行时间比我按顺序执行的时间要长。以下是eclipse中的控制台输出- 连续时间:0.136,值:7.485471 并联时间:6.674,数值:7.485471 守则如下:Java 程序的并行化增加了执行时间,java,parallel-processing,Java,Parallel Processing,我正在上莱斯大学开设的并行编程课程。在赋值中,我们应该递归地计算数组元素的倒数和。为了并行化这一点,我们使用了一个fork/join池,递归地计算出所需的左、右子数组之和,并将它们合并回来。但我得到的执行时间比我按顺序执行的时间要长。以下是eclipse中的控制台输出- 连续时间:0.136,值:7.485471 并联时间:6.674,数值:7.485471 守则如下: package edu.coursera.parallel; import java.util.Random; import
package edu.coursera.parallel;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* Class wrapping methods for implementing reciprocal array sum in parallel.
*/
public final class ReciprocalArraySum
{
/**
* Default constructor.
*/
private ReciprocalArraySum()
{
}
/**
* Sequentially compute the sum of the reciprocal values for a given array.
*
* @param input Input array
* @return The sum of the reciprocals of the array input
*/
protected static double seqArraySum(final double[] input)
{
double sum = 0;
// Compute sum of reciprocals of array elements
for (int i = 0; i < input.length; i++)
{
sum += (1 / input[i]);
}
return sum;
}
/**
* Computes the size of each chunk, given the number of chunks to create
* across a given number of elements.
*
* @param nChunks The number of chunks to create
* @param nElements The number of elements to chunk across
* @return The default chunk size
*/
private static int getChunkSize(final int nChunks, final int nElements)
{
// Integer ceil
return (nElements + nChunks - 1) / nChunks;
}
/**
* Computes the inclusive element index that the provided chunk starts at,
* given there are a certain number of chunks.
*
* @param chunk The chunk to compute the start of
* @param nChunks The number of chunks created
* @param nElements The number of elements to chunk across
* @return The inclusive index that this chunk starts at in the set of
* nElements
*/
private static int getChunkStartInclusive(final int chunk, final int nChunks, final int nElements)
{
final int chunkSize = getChunkSize(nChunks, nElements);
return chunk * chunkSize;
}
/**
* Computes the exclusive element index that the provided chunk ends at,
* given there are a certain number of chunks.
*
* @param chunk The chunk to compute the end of
* @param nChunks The number of chunks created
* @param nElements The number of elements to chunk across
* @return The exclusive end index for this chunk
*/
private static int getChunkEndExclusive(final int chunk, final int nChunks, final int nElements)
{
final int chunkSize = getChunkSize(nChunks, nElements);
final int end = (chunk + 1) * chunkSize;
if (end > nElements)
{
return nElements;
}
else
{
return end;
}
}
/**
* This class stub can be filled in to implement the body of each task
* created to perform reciprocal array sum in parallel.
*/
private static class ReciprocalArraySumTask extends RecursiveAction
{
/**
* Starting index for traversal done by this task.
*/
private final int startIndexInclusive;
/**
* Ending index for traversal done by this task.
*/
private final int endIndexExclusive;
/**
* Input array to reciprocal sum.
*/
private final double[] input;
/**
* Intermediate value produced by this task.
*/
private double value;
/**
* Constructor.
* @param setStartIndexInclusive Set the starting index to begin
* parallel traversal at.
* @param setEndIndexExclusive Set ending index for parallel traversal.
* @param setInput Input values
*/
ReciprocalArraySumTask(final int setStartIndexInclusive, final int setEndIndexExclusive, final double[] setInput)
{
this.startIndexInclusive = setStartIndexInclusive;
this.endIndexExclusive = setEndIndexExclusive;
this.input = setInput;
}
/**
* Getter for the value produced by this task.
* @return Value produced by this task
*/
public double getValue()
{
return value;
}
@Override
protected void compute()
{
// TODO
if((endIndexExclusive - startIndexInclusive) <= (input.length/5))
{
for(int i=startIndexInclusive;i<endIndexExclusive;i++)
{
value+=(1/input[i]);
}
}
else
{
int mid = ((startIndexInclusive+endIndexExclusive)/2);
ReciprocalArraySumTask l = new ReciprocalArraySumTask(startIndexInclusive, mid, input);
ReciprocalArraySumTask r = new ReciprocalArraySumTask(mid, endIndexExclusive, input);
l.fork();
r.fork();
l.join();
r.join();
value = l.getValue()+r.getValue();
}
}
}
/**
* TODO: Modify this method to compute the same reciprocal sum as
* seqArraySum, but use two tasks running in parallel under the Java Fork
* Join framework. You may assume that the length of the input array is
* evenly divisible by 2.
*
* @param input Input array
* @return The sum of the reciprocals of the array input
*/
protected static double parArraySum(final double[] input)
{
assert input.length % 2 == 0;
double sum = 0.0;
// Compute sum of reciprocals of array elements
/*for(int i=0;i<input.length;i++)
{
sum+=(1/input[i]);
}*/
ReciprocalArraySumTask t = new ReciprocalArraySumTask(0,input.length,input);
t.compute();
sum = t.getValue();
return sum;
}
/**
* TODO: Extend the work you did to implement parArraySum to use a set
* number of tasks to compute the reciprocal array sum. You may find the
* above utilities getChunkStartInclusive and getChunkEndExclusive helpful
* in computing the range of element indices that belong to each chunk.
*
* @param input Input array
* @param numTasks The number of tasks to create
* @return The sum of the reciprocals of the array input
*/
protected static double parManyTaskArraySum(final double[] input, final int numTasks)
{
double sum = 0;
// Compute sum of reciprocals of array elements
for (int i = 0; i < input.length; i++)
{
sum += 1 / input[i];
}
return sum;
}
public static void main(String[] args)
{
ReciprocalArraySum r = new ReciprocalArraySum();
double[] imp = new double[1000];
Random rGen = new Random();
for(int i=0;i<1000;i++)
{
imp[i] = (i+1);
}
long startTimeSeq = System.nanoTime();
double value = r.seqArraySum(imp);
long endTimeSeq = System.nanoTime();
System.out.printf("Time for sequential: %4.3f , Value: %f\n",(endTimeSeq-startTimeSeq)/1e6,value);
long startTimePar = System.nanoTime();
value = r.parArraySum(imp);
long endTimePar = System.nanoTime();
System.out.printf("Time for parallel: %4.3f , Value: %f",(endTimePar-startTimePar)/1e6,value);
}
}
您正在为每个fork使用join。甚至使用Java8也可以加入。尝试使用这个类。这是一种分散-聚集的形式,不会停滞。它取代了流中使用的join。您还没有用足够的迭代来预热JIT。您应该考虑使用JMH进行微基准标记,而不是自己进行。此外,您的应用程序使用了多少处理器内核?除了检查之外,您可以首先看到它在遇到更大问题时的表现。1000个项目不足以抵消分叉/连接的开销。@MalteHartwig分配提供了一个JUnit测试环境,并使用200万个项目对其进行测试。它的性能仍然较差。分配提供的测试环境可能运行在只分配了一个处理器核心的虚拟机上。由于这是一个纯粹的计算绑定任务,所以在单处理器内核上并行计算不会给您带来任何好处。这就是为什么我早些时候问您使用了多少内核。@DodgyCodeException是的,我使用的是4个内核和语句System.setProperty…,4对执行时间没有任何影响。