Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 0f667b5

Browse files
knutwalkerjexp
authored andcommitted
Use floats instead of scaled ints for PR score (#872)
* Update PR Benchmark * Use float instead of scaled int to keep track of page ranks * Combine PR scores in a single pass, same as the HugePR does * Fix LongIterators benchmark * Ignore daming factor in HugeEigenvector PR
1 parent e0e3750 commit 0f667b5

16 files changed

+117
-116
lines changed

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/ArticleRankComputeStep.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class ArticleRankComputeStep extends BaseComputeStep implements Relationsh
4141
}
4242

4343

44-
private int srcRankDelta;
44+
private float srcRankDelta;
4545

4646

4747
void singleIteration() {
@@ -53,15 +53,15 @@ void singleIteration() {
5353
if (delta > 0) {
5454
int degree = degrees.degree(nodeId, Direction.OUTGOING);
5555
if (degree > 0) {
56-
srcRankDelta = (int) (100_000 * (delta / (degree + averageDegree)));
56+
srcRankDelta = (float) (delta / (degree + averageDegree));
5757
rels.forEachRelationship(nodeId, Direction.OUTGOING, this);
5858
}
5959
}
6060
}
6161
}
6262

6363
public boolean accept(int sourceNodeId, int targetNodeId, long relationId) {
64-
if (srcRankDelta != 0) {
64+
if (srcRankDelta != 0f) {
6565
int idx = binaryLookup(targetNodeId, starts);
6666
nextScores[idx][targetNodeId - starts[idx]] += srcRankDelta;
6767
}

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/BaseComputeStep.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public abstract class BaseComputeStep implements ComputeStep {
4141

4242
double[] pageRank;
4343
double[] deltas;
44-
int[][] nextScores;
45-
private int[][] prevScores;
44+
float[][] nextScores;
45+
float[][] prevScores;
4646

4747
final int partitionSize;
4848
final int startNode;
@@ -65,7 +65,7 @@ public abstract class BaseComputeStep implements ComputeStep {
6565
state = S_INIT;
6666
}
6767

68-
public void setStarts(int starts[], int[] lengths) {
68+
public void setStarts(int[] starts, int[] lengths) {
6969
this.starts = starts;
7070
this.lengths = lengths;
7171
}
@@ -76,7 +76,7 @@ public void run() {
7676
singleIteration();
7777
state = S_SYNC;
7878
} else if (state == S_SYNC) {
79-
synchronizeScores(combineScores());
79+
combineScores();
8080
state = S_NORM;
8181
} else if(state == S_NORM) {
8282
normalizeDeltas();
@@ -90,16 +90,16 @@ public void run() {
9090
void normalizeDeltas() {}
9191

9292
private void initialize() {
93-
this.nextScores = new int[starts.length][];
94-
Arrays.setAll(nextScores, i -> new int[lengths[i]]);
93+
this.nextScores = new float[starts.length][];
94+
Arrays.setAll(nextScores, i -> new float[lengths[i]]);
9595

9696
double[] partitionRank = new double[partitionSize];
9797

9898
double initialValue = initialValue();
9999
if(sourceNodeIds.length == 0) {
100100
Arrays.fill(partitionRank, initialValue);
101101
} else {
102-
Arrays.fill(partitionRank,0);
102+
Arrays.fill(partitionRank, 0.0);
103103

104104
int[] partitionSourceNodeIds = IntStream.of(sourceNodeIds)
105105
.filter(sourceNodeId -> sourceNodeId >= startNode && sourceNodeId < endNode)
@@ -126,45 +126,34 @@ public void prepareNormalizeDeltas(double l2Norm, int iteration) {
126126
this.l2Norm = l2Norm;
127127
}
128128

129-
public void prepareNextIteration(int[][] prevScores) {
129+
public void prepareNextIteration(float[][] prevScores) {
130130
this.prevScores = prevScores;
131131
}
132132

133-
private int[] combineScores() {
133+
void combineScores() {
134134
assert prevScores != null;
135135
assert prevScores.length >= 1;
136-
int[][] prevScores = this.prevScores;
137-
138-
int length = prevScores.length;
139-
int[] allScores = prevScores[0];
140-
for (int i = 1; i < length; i++) {
141-
int[] scores = prevScores[i];
142-
for (int j = 0; j < scores.length; j++) {
143-
allScores[j] += scores[j];
144-
scores[j] = 0;
145-
}
146-
}
147-
148-
return allScores;
149-
}
150136

151-
void synchronizeScores(int[] allScores) {
152137
double dampingFactor = this.dampingFactor;
153138
double[] pageRank = this.pageRank;
139+
double[] deltas = this.deltas;
140+
float[][] prevScores = this.prevScores;
141+
int length = prevScores[0].length;
154142

155-
int length = allScores.length;
156143
for (int i = 0; i < length; i++) {
157-
int sum = allScores[i];
158-
159-
double delta = dampingFactor * (sum / 100_000.0);
144+
double sum = 0.0;
145+
for (float[] scores : prevScores) {
146+
sum += (double) scores[i];
147+
scores[i] = 0f;
148+
}
149+
double delta = dampingFactor * sum;
160150
pageRank[i] += delta;
161151
deltas[i] = delta;
162-
allScores[i] = 0;
163152
}
164153
}
165154

166155
@Override
167-
public int[][] nextScores() {
156+
public float[][] nextScores() {
168157
return nextScores;
169158
}
170159

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/ComputeStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
package org.neo4j.graphalgo.impl.pagerank;
2020

2121
public interface ComputeStep extends Runnable {
22-
int[][] nextScores();
22+
float[][] nextScores();
2323

2424
double[] pageRank();
2525

2626
int[] starts();
2727

2828
void setStarts(int[] startArray, int[] lengthArray);
2929

30-
void prepareNextIteration(int[][] score);
30+
void prepareNextIteration(float[][] score);
3131

3232
void prepareNormalizeDeltas(double l2Norm, int iteration);
3333

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/EigenvectorCentralityComputeStep.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import org.neo4j.graphalgo.api.RelationshipIterator;
2424
import org.neo4j.graphdb.Direction;
2525

26-
import java.util.Arrays;
27-
import java.util.stream.IntStream;
28-
2926
import static org.neo4j.graphalgo.core.utils.ArrayUtil.binaryLookup;
3027

3128
final class EigenvectorCentralityComputeStep extends BaseComputeStep implements RelationshipConsumer {
@@ -45,7 +42,7 @@ final class EigenvectorCentralityComputeStep extends BaseComputeStep implements
4542
this.initialValue = 1.0 / nodeCount;
4643
}
4744

48-
private int srcRankDelta;
45+
private float srcRankDelta;
4946

5047

5148
void singleIteration() {
@@ -54,10 +51,10 @@ void singleIteration() {
5451
RelationshipIterator rels = this.relationshipIterator;
5552
for (int nodeId = startNode; nodeId < endNode; ++nodeId) {
5653
double delta = deltas[nodeId - startNode];
57-
if (delta > 0) {
54+
if (delta > 0.0) {
5855
int degree = degrees.degree(nodeId, Direction.OUTGOING);
5956
if (degree > 0) {
60-
srcRankDelta = (int) (100_000 * delta);
57+
srcRankDelta = (float) delta;
6158
rels.forEachRelationship(nodeId, Direction.OUTGOING, this);
6259
}
6360
}
@@ -66,7 +63,7 @@ void singleIteration() {
6663

6764
@Override
6865
public boolean accept(int sourceNodeId, int targetNodeId, long relationId) {
69-
if (srcRankDelta != 0) {
66+
if (srcRankDelta != 0f) {
7067
int idx = binaryLookup(targetNodeId, starts);
7168
nextScores[idx][targetNodeId - starts[idx]] += srcRankDelta;
7269
}
@@ -79,19 +76,24 @@ protected double initialValue() {
7976
}
8077

8178
@Override
82-
void synchronizeScores(int[] allScores) {
79+
void combineScores() {
80+
assert prevScores != null;
81+
assert prevScores.length >= 1;
82+
8383
double[] pageRank = this.pageRank;
84+
double[] deltas = this.deltas;
85+
float[][] prevScores = this.prevScores;
86+
int length = prevScores[0].length;
8487

85-
int length = allScores.length;
8688
for (int i = 0; i < length; i++) {
87-
int sum = allScores[i];
88-
89-
double delta = sum / 100_000.0;
89+
double delta = 0.0;
90+
for (float[] scores : prevScores) {
91+
delta += (double) scores[i];
92+
scores[i] = 0f;
93+
}
9094
pageRank[i] += delta;
9195
deltas[i] = delta;
92-
allScores[i] = 0;
9396
}
94-
9597
}
9698

9799
@Override

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/HugeArticleRankComputeStep.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class HugeArticleRankComputeStep extends HugeBaseComputeStep implements Hu
4949
}
5050

5151

52-
private int srcRankDelta;
52+
private float srcRankDelta;
5353

5454

5555
void singleIteration() {
@@ -61,15 +61,15 @@ void singleIteration() {
6161
if (delta > 0) {
6262
int degree = degrees.degree(nodeId, Direction.OUTGOING);
6363
if (degree > 0) {
64-
srcRankDelta = (int) (100_000 * (delta / (degree + averageDegree)));
64+
srcRankDelta = (float) (delta / (degree + averageDegree));
6565
rels.forEachRelationship(nodeId, Direction.OUTGOING, this);
6666
}
6767
}
6868
}
6969
}
7070

7171
public boolean accept(long sourceNodeId, long targetNodeId) {
72-
if (srcRankDelta != 0) {
72+
if (srcRankDelta != 0f) {
7373
int idx = binaryLookup(targetNodeId, starts);
7474
nextScores[idx][(int) (targetNodeId - starts[idx])] += srcRankDelta;
7575
}

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/HugeBaseComputeStep.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020

2121
import org.neo4j.graphalgo.api.HugeDegrees;
2222
import org.neo4j.graphalgo.api.HugeRelationshipIterator;
23-
import org.neo4j.graphalgo.api.HugeRelationshipWeights;
2423
import org.neo4j.graphalgo.core.utils.paged.AllocationTracker;
25-
import org.neo4j.graphdb.Direction;
2624

2725
import java.util.Arrays;
2826
import java.util.stream.LongStream;
2927

30-
import static org.neo4j.graphalgo.core.utils.ArrayUtil.binaryLookup;
3128
import static org.neo4j.graphalgo.core.utils.paged.MemoryUsage.sizeOfDoubleArray;
32-
import static org.neo4j.graphalgo.core.utils.paged.MemoryUsage.sizeOfIntArray;
29+
import static org.neo4j.graphalgo.core.utils.paged.MemoryUsage.sizeOfFloatArray;
3330

3431
public abstract class HugeBaseComputeStep implements HugeComputeStep {
3532
private static final int S_INIT = 0;
@@ -51,8 +48,8 @@ public abstract class HugeBaseComputeStep implements HugeComputeStep {
5148

5249
double[] pageRank;
5350
double[] deltas;
54-
int[][] nextScores;
55-
int[][] prevScores;
51+
float[][] nextScores;
52+
float[][] prevScores;
5653

5754
final long startNode;
5855
final long endNode;
@@ -104,11 +101,11 @@ public void run() {
104101
void normalizeDeltas() {}
105102

106103
private void initialize() {
107-
this.nextScores = new int[starts.length][];
104+
this.nextScores = new float[starts.length][];
108105
Arrays.setAll(nextScores, i -> {
109106
int size = lengths[i];
110-
tracker.add(sizeOfIntArray(size));
111-
return new int[size];
107+
tracker.add(sizeOfFloatArray(size));
108+
return new float[size];
112109
});
113110

114111
tracker.add(sizeOfDoubleArray(partitionSize) << 1);
@@ -118,7 +115,7 @@ private void initialize() {
118115
if(sourceNodeIds.length == 0) {
119116
Arrays.fill(partitionRank, initialValue);
120117
} else {
121-
Arrays.fill(partitionRank,0);
118+
Arrays.fill(partitionRank,0.0);
122119

123120
long[] partitionSourceNodeIds = LongStream.of(sourceNodeIds)
124121
.filter(sourceNodeId -> sourceNodeId >= startNode && sourceNodeId < endNode)
@@ -144,32 +141,32 @@ public void prepareNormalizeDeltas(double l2Norm) {
144141
this.l2Norm = l2Norm;
145142
}
146143

147-
public void prepareNextIteration(int[][] prevScores) {
144+
public void prepareNextIteration(float[][] prevScores) {
148145
this.prevScores = prevScores;
149146
}
150147

151-
private void combineScores() {
148+
void combineScores() {
152149
assert prevScores != null;
153150
assert prevScores.length >= 1;
154151

155152
int scoreDim = prevScores.length;
156-
int[][] prevScores = this.prevScores;
153+
float[][] prevScores = this.prevScores;
157154

158155
int length = prevScores[0].length;
159156
for (int i = 0; i < length; i++) {
160-
int sum = 0;
157+
double sum = 0.0;
161158
for (int j = 0; j < scoreDim; j++) {
162-
int[] scores = prevScores[j];
163-
sum += scores[i];
164-
scores[i] = 0;
159+
float[] scores = prevScores[j];
160+
sum += (double) scores[i];
161+
scores[i] = 0f;
165162
}
166-
double delta = dampingFactor * (sum / 100_000.0);
163+
double delta = dampingFactor * sum;
167164
pageRank[i] += delta;
168165
deltas[i] = delta;
169166
}
170167
}
171168

172-
public int[][] nextScores() {
169+
public float[][] nextScores() {
173170
return nextScores;
174171
}
175172

algo/src/main/java/org/neo4j/graphalgo/impl/pagerank/HugeComputeStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public interface HugeComputeStep extends Runnable {
2323

2424
long[] starts();
2525

26-
void prepareNextIteration(int[][] score);
26+
void prepareNextIteration(float[][] score);
2727

28-
int[][] nextScores();
28+
float[][] nextScores();
2929

3030
void setStarts(long[] startArray, int[] lengthArray);
3131

0 commit comments

Comments
 (0)