Difference between revisions of "Iterative Averaging"
| Line 32: | Line 32: | ||
<math>PR(u) = \sum_{v \in B_u} \frac{PR(v)}{L(v)}</math> | <math>PR(u) = \sum_{v \in B_u} \frac{PR(v)}{L(v)}</math> | ||
| − | <nowiki>int | + | <nowiki>for (int iteration : new IntegerRange(0, 52)) { |
| − | Phaser phaser = new Phaser(); | + | forall(slices, (slice) -> { |
| + | 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); | ||
| + | } | ||
| + | }); | ||
| + | }</nowiki> | ||
| + | |||
| + | <nowiki>Phaser phaser = new Phaser(); | ||
phaser.bulkRegister(slices.size()); | phaser.bulkRegister(slices.size()); | ||
forall(slices, (slice) -> { | forall(slices, (slice) -> { | ||
| − | for (int iteration | + | for (int iteration : new IntegerRange(0, iterationCount)) { |
PageRank[] arrayPrev = ((iteration & 1) == 0) ? a : b; | PageRank[] arrayPrev = ((iteration & 1) == 0) ? a : b; | ||
PageRank[] arrayNext = ((iteration & 1) == 0) ? b : a; | PageRank[] arrayNext = ((iteration & 1) == 0) ? b : a; | ||
| Line 44: | Line 53: | ||
phaser.arriveAndAwaitAdvance(); | phaser.arriveAndAwaitAdvance(); | ||
} | } | ||
| − | //note: arriveAndDeregister not required for this application | + | // note: arriveAndDeregister not required for this application |
phaser.arriveAndDeregister(); | phaser.arriveAndDeregister(); | ||
| − | });</nowiki> | + | }); |
| + | </nowiki> | ||
=Code to Implement= | =Code to Implement= | ||
Revision as of 17:27, 7 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
- bulkRegister
- arriveAndAwaitAdvance
- arriveAndDeregister (note: not required for this studio, but good to know about.)
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
Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle PR(u) = \sum_{v \in B_u} \frac{PR(v)}{L(v)}}
for (int iteration : new IntegerRange(0, 52)) {
forall(slices, (slice) -> {
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 phaser = new Phaser();
phaser.bulkRegister(slices.size());
forall(slices, (slice) -> {
for (int iteration : new IntegerRange(0, iterationCount)) {
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();
}
// note: arriveAndDeregister not required for this application
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 |