|
| 1 | +package applications.threading; |
| 2 | + |
| 3 | +import utils.IActor; |
| 4 | +import utils.Pair; |
| 5 | +import utils.PairBuilder; |
| 6 | + |
| 7 | +/** |
| 8 | + * Category: Threading |
| 9 | + * ID: Example2 |
| 10 | + * Description: Implement trapezoid rule in action |
| 11 | + * Taken From: Code from the book Modern Java in Action 2nd Edition |
| 12 | + * |
| 13 | + * Details: |
| 14 | + * |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +public class Example2 { |
| 19 | + |
| 20 | + public static IActor<Double, Double> f = (Double a)->{return a*a;}; |
| 21 | + |
| 22 | + class LocalIntegrator implements Runnable{ |
| 23 | + |
| 24 | + |
| 25 | + public LocalIntegrator(Pair<Integer, Integer> range, double start, double h){ |
| 26 | + |
| 27 | + this.range = range; |
| 28 | + this.start = start; |
| 29 | + this.h = h; |
| 30 | + this.localSum = 0.0; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void run() { |
| 35 | + |
| 36 | + for(int i= range.first; i< range.second; ++i){ |
| 37 | + localSum += Example2.f.evaluate(start + i*h); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public double getLocalSum(){ |
| 42 | + return localSum; |
| 43 | + } |
| 44 | + |
| 45 | + private Pair<Integer, Integer> range; |
| 46 | + private double start; |
| 47 | + private double h; |
| 48 | + private double localSum; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public static void main(String[] args) throws InterruptedException{ |
| 53 | + |
| 54 | + // start interval |
| 55 | + double a = 0.0; |
| 56 | + |
| 57 | + // end interval |
| 58 | + double b = 1.0; |
| 59 | + |
| 60 | + // how many intervals |
| 61 | + int n = 200; |
| 62 | + |
| 63 | + // the discretization size |
| 64 | + double h = (b-a)/n; |
| 65 | + |
| 66 | + System.out.println("Discretization size: " + h); |
| 67 | + |
| 68 | + // number of threads |
| 69 | + int numTreads = 4; |
| 70 | + |
| 71 | + int localWorkSize = n/numTreads; |
| 72 | + |
| 73 | + System.out.println("Local work size: " + localWorkSize); |
| 74 | + |
| 75 | + // calculate the intervals |
| 76 | + Pair<Integer, Integer> range1 = PairBuilder.makePair(1, localWorkSize); |
| 77 | + Pair<Integer, Integer> range2 = PairBuilder.makePair(localWorkSize, 2*localWorkSize); |
| 78 | + Pair<Integer, Integer> range3 = PairBuilder.makePair(2*localWorkSize, 3*localWorkSize); |
| 79 | + Pair<Integer, Integer> range4 = PairBuilder.makePair(3*localWorkSize, 4*localWorkSize); |
| 80 | + |
| 81 | + // create the workers |
| 82 | + |
| 83 | + Example2 example2 = new Example2(); |
| 84 | + |
| 85 | + LocalIntegrator integrator1 = example2.new LocalIntegrator(range1, a, h); |
| 86 | + LocalIntegrator integrator2 = example2.new LocalIntegrator(range2, a, h); |
| 87 | + LocalIntegrator integrator3 = example2.new LocalIntegrator(range3, a, h); |
| 88 | + LocalIntegrator integrator4 = example2.new LocalIntegrator(range4, a, h); |
| 89 | + |
| 90 | + Thread t1 = new Thread( integrator1 ); |
| 91 | + Thread t2 = new Thread(integrator2 ); |
| 92 | + Thread t3 = new Thread(integrator3 ); |
| 93 | + Thread t4 = new Thread(integrator4 ); |
| 94 | + |
| 95 | + t1.start(); |
| 96 | + t2.start(); |
| 97 | + t3.start(); |
| 98 | + t4.start(); |
| 99 | + |
| 100 | + double sum = Example2.f.evaluate(a) * 0.5; |
| 101 | + |
| 102 | + t1.join(); |
| 103 | + t2.join(); |
| 104 | + t3.join(); |
| 105 | + t4.join(); |
| 106 | + |
| 107 | + sum += integrator1.getLocalSum(); |
| 108 | + sum += integrator2.getLocalSum(); |
| 109 | + sum += integrator3.getLocalSum(); |
| 110 | + sum += integrator4.getLocalSum(); |
| 111 | + sum += Example2.f.evaluate(b) * 0.5; |
| 112 | + |
| 113 | + System.out.println("Integral calculated: "+sum*h); |
| 114 | + } |
| 115 | +} |
0 commit comments