Difference between revisions of "Iterative Averaging"
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
=Code to Investigate= | =Code to Investigate= | ||
+ | ==Sequential Iterative Averaging== | ||
{{CodeToInvestigate|SequentialIterativeAverager|iterativelyAverage|iterativeaveraging.demo}} | {{CodeToInvestigate|SequentialIterativeAverager|iterativelyAverage|iterativeaveraging.demo}} | ||
{{Sequential|public void iterativelyAverage(List<Slice<double[]>> slices, double[] a, double[] b, int iterationCount)}} | {{Sequential|public void iterativelyAverage(List<Slice<double[]>> slices, double[] a, double[] b, int iterationCount)}} | ||
− | <nowiki> | + | <nowiki>for (int iteration = 0; iteration < iterationCount; iteration++) { |
− | + | double[] arrayPrev = ((iteration & 1) == 0) ? a : b; | |
− | + | double[] arrayNext = ((iteration & 1) == 0) ? b : a; | |
− | + | for (Slice<double[]> slice : slices) { | |
− | + | for (int index = slice.getMinInclusive(); index < slice.getMaxExclusive(); index++) { | |
− | + | arrayNext[index] = (arrayPrev[index - 1] + arrayPrev[index + 1]) * 0.5; | |
− | + | } | |
− | } | + | } |
− | }</nowiki> | + | }</nowiki> |
+ | |||
+ | ==Phased PageRank== | ||
+ | <nowiki>int iterationCount = 53; | ||
+ | Phaser phaser = new Phaser(); | ||
+ | phaser.bulkRegister(slices.size()); | ||
+ | forall(slices, (slice) -> { | ||
+ | for (int iteration = 0; iteration < iterationCount; iteration++) { | ||
+ | PageRank[] arrayPrev = ((iteration & 1) == 0) ? a : b; | ||
+ | PageRank[] arrayNext = ((iteration & 1) == 0) ? b : a; | ||
+ | for (int index = slice.getMinInclusive(); index < slice.getMaxExclusive(); index++) { | ||
+ | arrayNext[index] = calculateNextRank(index, arrayPrev); | ||
+ | } | ||
+ | phaser.arriveAndAwaitAdvance(); | ||
+ | } | ||
+ | phaser.arriveAndDeregister(); | ||
+ | });</nowiki> | ||
=Code to Implement= | =Code to Implement= |
Revision as of 18:04, 6 March 2018
Contents
Motivation
X10/Habanero like Phasers have been added to Java since JDK7. We will gain some experience with using Phasers in a parallel for loop context.
Background
Warning: Our use of the forall loop with Phasers does not accurately convey their finicky nature. More than other features, Phasers seem to require more care to get performance improvements. |
Code to Investigate
Sequential Iterative Averaging
class: | SequentialIterativeAverager.java | DEMO: |
methods: | iterativelyAverage | |
package: | iterativeaveraging.demo | |
source folder: | src//java |
method: public void iterativelyAverage(List<Slice<double[]>> slices, double[] a, double[] b, int iterationCount)
(sequential implementation only)
for (int iteration = 0; iteration < iterationCount; iteration++) { double[] arrayPrev = ((iteration & 1) == 0) ? a : b; double[] arrayNext = ((iteration & 1) == 0) ? b : a; for (Slice<double[]> slice : slices) { for (int index = slice.getMinInclusive(); index < slice.getMaxExclusive(); index++) { arrayNext[index] = (arrayPrev[index - 1] + arrayPrev[index + 1]) * 0.5; } } }
Phased PageRank
int iterationCount = 53; Phaser phaser = new Phaser(); phaser.bulkRegister(slices.size()); forall(slices, (slice) -> { for (int iteration = 0; iteration < iterationCount; iteration++) { PageRank[] arrayPrev = ((iteration & 1) == 0) ? a : b; PageRank[] arrayNext = ((iteration & 1) == 0) ? b : a; for (int index = slice.getMinInclusive(); index < slice.getMaxExclusive(); index++) { arrayNext[index] = calculateNextRank(index, arrayPrev); } phaser.arriveAndAwaitAdvance(); } phaser.arriveAndDeregister(); });
Code to Implement
class: | ForForallIterativeAverager.java | |
methods: | iterativelyAverage | |
package: | iterativeaveraging.studio | |
source folder: | student/src/main/java |
method: public void iterativelyAverage(List<Slice<double[]>> slices, double[] a, double[] b, int iterationCount)
(parallel implementation required)
class: | ForallForPhasedIterativeAverager.java | |
methods: | iterativelyAverage | |
package: | iterativeaveraging.studio | |
source folder: | student/src/main/java |
method: public void iterativelyAverage(List<Slice<double[]>> slices, double[] a, double[] b, int iterationCount)
(parallel implementation required)
Testing Your Solution
Correctness
class: | IterativeAveragingTestSuite.java | |
package: | iterativeaveraging.studio | |
source folder: | testing/src/test/java |
Performance
class: | IterativeAveragingTiming.java | |
package: | iterativeaveraging.studio | |
source folder: | src/main/java |