Question: Hello I need HEEEEEELP!!!!! The NetBeans project Sum , included with the files for this chapter, calculates the sum of an array of 1,000 randomly
Hello I need HEEEEEELP!!!!!
The NetBeans project Sum, included with the files for this chapter, calculates the sum of an array of 1,000 randomly generated integers. But there is a catch -- the code has a 10 millisecond delay built into the code that sums the array. Under normal circumstances, within a single thread or process, this would mean that the code takes a little over 10 seconds to run (10 milliseconds per operation times 1,000 operations, plus a small overhead).
The software is written for parallel processing using Java's fork/join framework. As the chapter reports, on my computer, with parallel processing, the code ran in about 1/3 of a second.
Your task is to run the software with modifications, or in different computing environments, and report your results. The goal is for you to explore factors that affect the efficiency of parallel computing. You could change the maximum number of processes allowed by the program, change the code to move from recursion to iteration for a different number of array elements, try the program with larger arrays, or make other changes to explore concurrent computing.
You should run the program several times, either in different environments, or with different values for the things you are changing, and report on your results. You should address such issues as:
What platform did you run the code on? How did the performance of on computer compare to another? How did the number of maximum processes or other values you changes affect the time it took the program to run. Try to focus on one change or one change at a time and report what you did and what conclusions you you draw from this experiment
Here is the Sum Project
package sum;
import java.util.concurrent.*;
import java.util.Scanner;
// class for managing ForkJoinPool settings
class Globals {
static int processes = 1; // set default number of processes to 1
static ForkJoinPool fjPool; // ForkJoinPool object variable
} // end class Globals
//*****************************************************************************
class Sum extends RecursiveTask
// set constant to switch to iterative sequential processes at n = 50
static final int SEQUENTIAL_THRESHOLD = 50;
int low; // low (left) end of dataset
int high; // high (right end of dataset
long[] array;
// Sum constructor lo and hi establish section of array for this Sum object
Sum(long[] arr, int lo, int hi) {
array = arr;
low = lo;
high = hi;
} // end Sum constructor
//****************************************************************
// the compute method is the hybrid summation algorithm
protected Long compute() {
// if below threshold, computer iterative sum
if (high - low < SEQUENTIAL_THRESHOLD) {
long sum = 0;
// place add a random value to the array and add it to the sum
for (int i = low; i < high; ++i) {
sum = sum + array[i];
// sleep for 10 milliseconds to delay operation
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} // end try catch
} //end for
return sum;
} // end if
// else perform recursion
else {
// find midpoint
int mid = low + (high - low) / 2;
// find sum of left half
Sum left = new Sum(array, low, mid);
// find sum of left half
Sum right = new Sum(array, mid, high);
//separate into different processes, then join results
left.fork();
long rightAns = right.compute();
long leftAns = left.join();
return leftAns + rightAns;
} // end else
} // end compute()
// the sumArray method ivokes processes from the pool of processes
static long sumArray(long[] array) {
return Globals.fjPool.invoke(new Sum(array, 0, array.length));
} // end sumArray()
//**********************************************************************************
/* The main method asks the user to set the maximum number of processes that will be
* allowed to run concurrently. It casn exceed the number of processors
* because of time-sharing mutitasking as well as parallel processing.
*/
public static void main(String[] args) {
// variable to hold the sum of the values in the array
long sum = 0;
Scanner kb = new Scanner(System.in);
System.out.println("Enter the maximum number of concurrent processes for this code:");
Globals.processes = kb.nextInt();
//set the maximum number of processes;
Globals.fjPool = new ForkJoinPool(Globals.processes);
// declare a long array and load it with random values
long[] myArray = new long[1000];
for (int i = 0; i < myArray.length; ++i)
myArray[i] = (long) (Math.random() * 100 + 1);
// get the start time in nanoseconds
long startTime = System.nanoTime();
// sum the array
sum = sumArray(myArray);
// get the end time in nanoseconds
long endTime = System.nanoTime();
// calculate elapsed time in nanoseconds
long duration = endTime - startTime;
// print the sum of the array
System.out.printf("the sum of the values in the array is: %-,12d%n", sum);
// print the elapsed time in seconds (nanaoseconds/ 1 billion)
System.out.printf("The algorithm took %12.8f seconds.%n", (double) duration / 1.0e+09);
} // end main
} // end class Sum
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
