Skip to content

Commit 34f2e84

Browse files
committed
Factor out CodingLoopBase, rename loops to make more sense.
1 parent 5d44260 commit 34f2e84

26 files changed

+500
-846
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* One specific ordering/nesting of the coding loops.
3+
*
4+
* Copyright 2015, Backblaze, Inc. All rights reserved.
5+
*/
6+
7+
package com.backblaze.erasure;
8+
9+
public class ByteInputOutputExpCodingLoop extends CodingLoopBase {
10+
11+
@Override
12+
public void codeSomeShards(
13+
byte[][] matrixRows,
14+
byte[][] inputs, int inputCount,
15+
byte[][] outputs, int outputCount,
16+
int offset, int byteCount) {
17+
18+
for (int iByte = offset; iByte < offset + byteCount; iByte++) {
19+
{
20+
final int iInput = 0;
21+
final byte[] inputShard = inputs[iInput];
22+
final byte inputByte = inputShard[iByte];
23+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
24+
final byte[] outputShard = outputs[iOutput];
25+
final byte[] matrixRow = matrixRows[iOutput];
26+
outputShard[iByte] = Galois.multiply(matrixRow[iInput], inputByte);
27+
}
28+
}
29+
30+
for (int iInput = 1; iInput < inputCount; iInput++) {
31+
final byte[] inputShard = inputs[iInput];
32+
final byte inputByte = inputShard[iByte];
33+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
34+
final byte[] outputShard = outputs[iOutput];
35+
final byte[] matrixRow = matrixRows[iOutput];
36+
outputShard[iByte] ^= Galois.multiply(matrixRow[iInput], inputByte);
37+
}
38+
}
39+
}
40+
}
41+
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* One specific ordering/nesting of the coding loops.
3+
*
4+
* Copyright 2015, Backblaze, Inc. All rights reserved.
5+
*/
6+
7+
package com.backblaze.erasure;
8+
9+
public class ByteInputOutputTableCodingLoop extends CodingLoopBase {
10+
11+
@Override
12+
public void codeSomeShards(
13+
byte[][] matrixRows,
14+
byte[][] inputs, int inputCount,
15+
byte[][] outputs, int outputCount,
16+
int offset, int byteCount) {
17+
18+
final byte[][] table = Galois.MULTIPLICATION_TABLE;
19+
20+
for (int iByte = offset; iByte < offset + byteCount; iByte++) {
21+
{
22+
final int iInput = 0;
23+
final byte[] inputShard = inputs[iInput];
24+
final byte inputByte = inputShard[iByte];
25+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
26+
final byte[] outputShard = outputs[iOutput];
27+
final byte[] matrixRow = matrixRows[iOutput];
28+
final byte[] multTableRow = table[matrixRow[iInput] & 0xFF];
29+
outputShard[iByte] = multTableRow[inputByte & 0xFF];
30+
}
31+
}
32+
for (int iInput = 1; iInput < inputCount; iInput++) {
33+
final byte[] inputShard = inputs[iInput];
34+
final byte inputByte = inputShard[iByte];
35+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
36+
final byte[] outputShard = outputs[iOutput];
37+
final byte[] matrixRow = matrixRows[iOutput];
38+
final byte[] multTableRow = table[matrixRow[iInput] & 0xFF];
39+
outputShard[iByte] ^= multTableRow[inputByte & 0xFF];
40+
}
41+
}
42+
}
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* One specific ordering/nesting of the coding loops.
3+
*
4+
* Copyright 2015, Backblaze, Inc. All rights reserved.
5+
*/
6+
7+
package com.backblaze.erasure;
8+
9+
public class ByteOutputInputExpCodingLoop extends CodingLoopBase {
10+
11+
@Override
12+
public void codeSomeShards(
13+
byte[][] matrixRows,
14+
byte[][] inputs, int inputCount,
15+
byte[][] outputs, int outputCount,
16+
int offset, int byteCount) {
17+
18+
for (int iByte = offset; iByte < offset + byteCount; iByte++) {
19+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
20+
byte [] matrixRow = matrixRows[iOutput];
21+
int value = 0;
22+
for (int iInput = 0; iInput < inputCount; iInput++) {
23+
value ^= Galois.multiply(matrixRow[iInput], inputs[iInput][iByte]);
24+
}
25+
outputs[iOutput][iByte] = (byte) value;
26+
}
27+
}
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* One specific ordering/nesting of the coding loops.
3+
*
4+
* Copyright 2015, Backblaze, Inc. All rights reserved.
5+
*/
6+
7+
package com.backblaze.erasure;
8+
9+
public class ByteOutputInputTableCodingLoop extends CodingLoopBase {
10+
11+
@Override
12+
public void codeSomeShards(
13+
byte[][] matrixRows,
14+
byte[][] inputs, int inputCount,
15+
byte[][] outputs, int outputCount,
16+
int offset, int byteCount) {
17+
18+
byte [] [] table = Galois.MULTIPLICATION_TABLE;
19+
for (int iByte = offset; iByte < offset + byteCount; iByte++) {
20+
for (int iOutput = 0; iOutput < outputCount; iOutput++) {
21+
byte [] matrixRow = matrixRows[iOutput];
22+
int value = 0;
23+
for (int iInput = 0; iInput < inputCount; iInput++) {
24+
value ^= table[matrixRow[iInput] & 0xFF][inputs[iInput][iByte] & 0xFF];
25+
}
26+
outputs[iOutput][iByte] = (byte) value;
27+
}
28+
}
29+
}
30+
}

src/main/java/com/backblaze/erasure/CodingLoop.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,42 @@ public interface CodingLoop {
1717
*
1818
* The naming of the three loops is (with number of loops in benchmark):
1919
*
20-
* "index" - Index of byte within shard. (200,000 bytes in each shard)
21-
*
22-
* "shard" - Which output shard is being computed. (3 parity shards)
20+
* "byte" - Index of byte within shard. (200,000 bytes in each shard)
2321
*
2422
* "input" - Which input shard is being read. (17 data shards)
2523
*
24+
* "output" - Which output shard is being computed. (3 parity shards)
25+
*
2626
* And the naming for multiplication method is:
2727
*
2828
* "table" - Use the multiplication table.
2929
*
3030
* "exp" - Use the logarithm/exponent table.
31+
*
32+
* The ReedSolomonBenchmark class compares the performance of the different
33+
* loops, which will depend on the specific processor you're running on.
34+
*
35+
* This is the inner loop. It needs to be fast. Be careful
36+
* if you change it.
37+
*
38+
* I have tried inlining Galois.multiply(), but it doesn't
39+
* make things any faster. The JIT compiler is known to inline
40+
* methods, so it's probably already doing so.
3141
*/
3242
CodingLoop[] ALL_CODING_LOOPS =
3343
new CodingLoop[] {
34-
new IndexInputShardExpCodingLoop(),
35-
new IndexInputShardTableCodingLoop(),
36-
new IndexShardInputExpCodingLoop(),
37-
new IndexShardInputTableCodingLoop(),
38-
new InputIndexShardExpCodingLoop(),
39-
new InputIndexShardTableCodingLoop(),
40-
new InputShardIndexExpCodingLoop(),
41-
new InputShardIndexTableCodingLoop(),
42-
new ShardIndexInputExpCodingLoop(),
43-
new ShardIndexInputTableCodingLoop(),
44-
new ShardInputIndexExpCodingLoop(),
45-
new ShardInputIndexTableCodingLoop(),
44+
new ByteInputOutputExpCodingLoop(),
45+
new ByteInputOutputTableCodingLoop(),
46+
new ByteOutputInputExpCodingLoop(),
47+
new ByteOutputInputTableCodingLoop(),
48+
new InputByteOutputExpCodingLoop(),
49+
new InputByteOutputTableCodingLoop(),
50+
new InputOutputByteExpCodingLoop(),
51+
new InputOutputByteTableCodingLoop(),
52+
new OutputByteInputExpCodingLoop(),
53+
new OutputByteInputTableCodingLoop(),
54+
new OutputInputByteExpCodingLoop(),
55+
new OutputInputByteTableCodingLoop(),
4656
};
4757

4858
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Common implementations for coding loops.
3+
*
4+
* Copyright 2015, Backblaze, Inc. All rights reserved.
5+
*/
6+
7+
package com.backblaze.erasure;
8+
9+
/**
10+
* Common implementations for coding loops.
11+
*
12+
* Many of the coding loops do not have custom checkSomeShards() methods.
13+
* The benchmark doesn't measure that method.
14+
*/
15+
public abstract class CodingLoopBase implements CodingLoop {
16+
17+
@Override
18+
public boolean checkSomeShards(
19+
byte[][] matrixRows,
20+
byte[][] inputs, int inputCount,
21+
byte[][] toCheck, int checkCount,
22+
int offset, int byteCount) {
23+
24+
// This is the loop structure for ByteOutputInput, which does not
25+
// require temorary buffers for checking.
26+
byte [] [] table = Galois.MULTIPLICATION_TABLE;
27+
for (int iByte = offset; iByte < offset + byteCount; iByte++) {
28+
for (int iOutput = 0; iOutput < checkCount; iOutput++) {
29+
byte [] matrixRow = matrixRows[iOutput];
30+
int value = 0;
31+
for (int iInput = 0; iInput < inputCount; iInput++) {
32+
value ^= table[matrixRow[iInput] & 0xFF][inputs[iInput][iByte] & 0xFF];
33+
}
34+
if (toCheck[iOutput][iByte] != (byte) value) {
35+
return false;
36+
}
37+
}
38+
}
39+
return true;
40+
}
41+
}

src/main/java/com/backblaze/erasure/IndexInputShardExpCodingLoop.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/main/java/com/backblaze/erasure/IndexInputShardTableCodingLoop.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)