Skip to content
Snippets Groups Projects
Commit 2c69fa28 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Parallelized quantizeIntoIndices for VQ compression.

parent 71790ce8
No related branches found
No related tags found
No related merge requests found
......@@ -143,7 +143,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
assert (quantizer != null);
Log("Compression plane...");
final int[] indices = quantizer.quantizeIntoIndices(planeVectors);
final int[] indices = quantizer.quantizeIntoIndices(planeVectors, options.getWorkerCount());
try (OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048)) {
outBitStream.write(indices);
......
......@@ -31,13 +31,47 @@ public class VectorQuantizer {
}
public int[] quantizeIntoIndices(final int[][] dataVectors) {
return quantizeIntoIndices(dataVectors, 1);
}
public int[] quantizeIntoIndices(final int[][] dataVectors, final int maxWorkerCount) {
assert (dataVectors.length > 0 && dataVectors[0].length % vectorSize == 0) : "Wrong vector size";
int[] indices = new int[dataVectors.length];
// Speedup
for (int vectorIndex = 0; vectorIndex < dataVectors.length; vectorIndex++) {
indices[vectorIndex] = findClosestCodebookEntryIndex(dataVectors[vectorIndex],
VectorDistanceMetric.Euclidean);
if (maxWorkerCount == 1) {
for (int vectorIndex = 0; vectorIndex < dataVectors.length; vectorIndex++) {
indices[vectorIndex] = findClosestCodebookEntryIndex(dataVectors[vectorIndex],
VectorDistanceMetric.Euclidean);
}
} else {
// Cap the worker count on 8
final int workerCount = Math.min(maxWorkerCount, 8);
Thread[] workers = new Thread[workerCount];
final int workSize = dataVectors.length / workerCount;
for (int wId = 0; wId < workerCount; wId++) {
final int fromIndex = wId * workSize;
final int toIndex = (wId == workerCount - 1) ? dataVectors.length : (workSize + (wId * workSize));
workers[wId] = new Thread(() -> {
for (int vectorIndex = fromIndex; vectorIndex < toIndex; vectorIndex++) {
indices[vectorIndex] = findClosestCodebookEntryIndex(dataVectors[vectorIndex],
VectorDistanceMetric.Euclidean);
}
});
workers[wId].start();
}
try {
for (int wId = 0; wId < workerCount; wId++) {
workers[wId].join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return indices;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment