解決するためにより簡単な問題が必要です。私は、1000のランダムなX値と1000のランダムなY値の並列和を解く必要があります。私はjavaの並列ForkJoinフレームワークを使用しています。単一計算方法の使用では、完全に異なるルートでのX値とY値の合計を計算することはできません。複数の使用法JavaelloのForkJoin Frameworkの計算方法
以上のことを計算する必要があります。X * Yの合計、つまりΣxiyi1つのスレッドが横断するX値は別のタスクを割り当て、Yはスレッドプールに別のタスクとして挿入されます。それで、XとYの両方の値を同時に、つまりX * Y ==>(X = 100、Y = 150)にどのように乗算することが可能ですか? 最初のスレッドがY.にXと第二上で動作している
コード:
パブリッククラスRegressionLineForkJoin {
//private static final ForkJoinPool forkJoinPool = new ForkJoinPool(2); // use only 2 processors
private static Random r = new Random();
private static final int NR_OF_VALUES = 1000; // X & Y VALUES
private static final int THRESHOLD = 100; // need to calculate Threshold value.
private static class RegressionLineForkJoinTask extends RecursiveTask<Integer>
{
private int m_Start;
private int m_End;
public RegressionLineForkJoinTask(int a_start,int a_end)
{
this.m_Start = a_start;
this.m_End = a_end;
}
public Integer compute()
{
Integer Sum = 0;
if(this.m_End - this.m_Start < THRESHOLD)
{
calculateSum(this.m_Start,this.m_End);
}
else
{
int split = (this.m_Start + this.m_End)/2;
RegressionLineForkJoinTask oRegressionLineTask_1 = new RegressionLineForkJoinTask(this.m_Start , split);
RegressionLineForkJoinTask oRegressionLineTask_2 = new RegressionLineForkJoinTask(split+1 , this.m_End);
// Invoke the tasks in parallel
invokeAll(oRegressionLineTask_1,oRegressionLineTask_2);
Sum += oRegressionLineTask_1.join();
Sum += oRegressionLineTask_2.join();
//Sum
}//end of else
return Sum;
}
public static void main(String[ ] args)
{
RegressionLineForkJoinTask oRegressionLineForkJoinTask_X = new RegressionLineForkJoinTask(0,NR_OF_VALUES);
RegressionLineForkJoinTask oRegressionLineForkJoinTask_Y = new RegressionLineForkJoinTask(0,NR_OF_VALUES);
Integer Sum_X_Values = forkJoinPool.invoke(oRegressionLineForkJoinTask_X);
Integer Sum_Y_Values = forkJoinPool.invoke(oRegressionLineForkJoinTask_Y);
System.out.println("in main after forkjoin.invoke()");
}
private static double nextRandomFunctionValue(int a_startInex,int a_endIndex)
{
double randomValue = 0.0;
randomValue = a_startInex + (a_endIndex - a_startInex) * r.nextDouble();
return randomValue;
}//end of nextRandomFunctionValue
private static double calculateSum(int a_startIndex, int a_endIndex)
{
double sumValue = 0.0;
double RandomeValue = 0.0;
for(int index = a_startIndex; index< a_endIndex; index++)
{
RandomeValue = nextRandomFunctionValue(a_startIndex,a_endIndex);
sumValue += RandomeValue;
}//end of for
return sumValue;
}
}
}