Thursday, 2 March 2017

Chapter 30 Exercise 12, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

30.12 (Parallel array initializer) Implement the following method using the Fork/
Join Framework to assign random values to the list.
public static void parallelAssignValues(double[] list)
Write a test program that creates a list with 9,000,000 elements and invokes
parallelAssignValues to assign random values to the list. Also
implement a sequential algorithm and compare the execution time of the
two. Note that if you use Math.random() , your parallel code execu-
tion time will be worse than the sequential code execution time because
Math.random() is synchronized and cannot be executed in parallel. To
fix this problem, create a Random object for assigning random values to a
small list.


import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class Exercise12 {

 public static void main(String[] args) {
  double[] list1 = new double[40000000];
  double[] list2 = new double[list1.length];
  long time = System.currentTimeMillis();
  parallelAssignValues(list1);
  System.out.println((System.currentTimeMillis() - time) + " msec - parallelAssignValues()");
  time = System.currentTimeMillis();
  sequentialAssignValues(list2);
  System.out.println((System.currentTimeMillis() - time) + " msec - sequentialAssignValues()");

 }

 public static void parallelAssignValues(double[] list) {
     RecursiveAction mainTask = new ParallelAssignValues(0, list.length, list);
     ForkJoinPool pool = new ForkJoinPool();
     pool.invoke(mainTask);
 }

 static class ParallelAssignValues extends RecursiveAction {
  private static final long serialVersionUID = 1L;
  private double[] list;
  private int THRESHOLD = 1000000;
  private int start;
  private int fin;
  
  public ParallelAssignValues(int start, int fin, double[] list) {
   this.list = list;
   this.start = start;
   this.fin = fin;
  }
  
  @Override
  protected void compute() {
   if ((fin - start) < THRESHOLD) {
    Random random = new Random();
    for (int i = start; i < fin; i++) {
     list[i] = random.nextDouble();
    }
   } else {
    int middle = (start + fin) / 2;
    invokeAll(new ParallelAssignValues(start, middle, list), new ParallelAssignValues(middle, fin, list));
   }
  }  
 }
 
 public static void sequentialAssignValues(double[] list) {
  Random random = new Random();
  for (int i = 0; i < list.length; i++) {
   list[i] = random.nextDouble();
  }
 }
}

No comments :

Post a Comment