Skip to content

Commit

Permalink
Optimization for singlethread
Browse files Browse the repository at this point in the history
  • Loading branch information
Baunsgaard committed Aug 17, 2023
1 parent 014c8de commit b8445b9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
25 changes: 13 additions & 12 deletions src/main/java/org/apache/sysds/runtime/compress/utils/ACount.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public final DblArray key() {
}

@Override
public ACount<DblArray> get(DblArray key) {
public final ACount<DblArray> get(DblArray key) {
DArrCounts e = this;
boolean eq = e.key.equals(key);
while(e.next != null && !eq) {
Expand All @@ -98,22 +98,23 @@ public ACount<DblArray> get(DblArray key) {
}

@Override
public DArrCounts inc(DblArray key, int c, int id) {
public final DArrCounts inc(final DblArray key, final int c, final int id) {
// once this method jit compile it becomse 2x faster.
DArrCounts e = this;
boolean eq = e.key.equals(key);
while(e.next != null && !eq) {
e = e.next;
eq = e.key.equals(key);
}

if(eq) {
if(e.key.equals(key)) {
e.count += c;
return e;
}
else { // e.next is null;
e.next = new DArrCounts(key, id, c);
return e.next;
while(e.next != null) {
e = e.next;
if(e.key.equals(key)) {
e.count += c;
return e;
}
}

e.next = new DArrCounts(key, id, c);
return e.next;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public ACountHashMap() {
size = 0;
}

public ACountHashMap(int arrSize) {
if(arrSize < shortCutSize)
data = create(1);
else {
arrSize = (int)(arrSize * (1.0 / LOAD_FACTOR));
arrSize += arrSize % 2;
data = create(arrSize);
}
size = 0;
}

public int size() {
return size;
}
Expand Down Expand Up @@ -100,7 +111,7 @@ public int getOrDefault(T key, int def) {
return (e == null) ? def : e.count;
}

public ACount<T>[] extractValues() {
public final ACount<T>[] extractValues() {
final ACount<T>[] ret = create(size);
int i = 0;
for(ACount<T> e : data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public DblArray(DblArray that) {
this(Arrays.copyOf(that._arr, that._arr.length), that.hashCode());
}


public double[] getData() {
return _arr;
}
Expand All @@ -55,18 +54,36 @@ public boolean isEmpty() {
}

@Override
public int hashCode() {
public final int hashCode() {
if(_hash != 0)
return _hash;
int h = Arrays.hashCode(_arr);
_hash = hashCode(_arr);
return _hash;
}

private final int hashCode(final double[] arr) {
int h = 1;
for(double element : _arr) {
long bits = Double.doubleToLongBits(element);
h = 857 * h + (int) (bits ^ (bits >>> 32));
}
h ^= (h >>> 20) ^ (h >>> 12);
h = h ^ (h >>> 7) ^ (h >>> 4);
_hash = h;
return _hash;
return h;
}

public boolean equals(DblArray that) {
return this.hashCode() == that.hashCode() && Arrays.equals(this._arr, that._arr);
public final boolean equals(DblArray that) {
if(hashCode() == that.hashCode()) {

final double[] t = _arr;
final double[] o = that._arr;
for(int i = 0; i < t.length; i++) {
if(t[i] != o[i])
return false;
}
return true;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@

import org.apache.sysds.runtime.compress.utils.ACount.DArrCounts;

public class DblArrayCountHashMap extends ACountHashMap<DblArray> {
public final class DblArrayCountHashMap extends ACountHashMap<DblArray> {

public DblArrayCountHashMap() {
super();
}

public DblArrayCountHashMap(int init_capacity) {
super();
super(init_capacity);
}

protected ACount<DblArray>[] create(int size) {
protected final DArrCounts[] create(int size) {
return new DArrCounts[size];
}

protected int hash(DblArray key) {
return Math.abs(key.hashCode());
}

protected ACount<DblArray> create(DblArray key, int id) {
protected final DArrCounts create(DblArray key, int id) {
return new DArrCounts(key, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@

import org.apache.sysds.runtime.compress.utils.ACount.DCounts;

public class DoubleCountHashMap extends ACountHashMap<Double> {
public final class DoubleCountHashMap extends ACountHashMap<Double> {

public DoubleCountHashMap() {
super();
}

public DoubleCountHashMap(int init_capacity) {
super();
super(init_capacity);
}

protected ACount<Double>[] create(int size) {
protected DCounts[] create(int size) {
return new DCounts[size];
}

protected int hash(Double key) {
return DCounts.hashIndex(key);
}

protected ACount<Double> create(Double key, int id) {
protected final DCounts create(Double key, int id) {
return new DCounts(key, id);
}

Expand All @@ -52,7 +52,7 @@ public double[] getDictionary() {
e = e.next();
}
}

return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public Serialize(int N, IGenerate<MatrixBlock> gen, int k, String file) {
super(N, gen);
this.file = file;
this.k = k;

}

public void run() throws Exception, InterruptedException {
Expand All @@ -74,6 +75,9 @@ public void run() throws Exception, InterruptedException {
if(!directory.exists()) {
directory.mkdir();
}
if(k == 1){
ConfigurationManager.getCompilerConfig().set(ConfigType.PARALLEL_CP_WRITE_BINARYFORMATS, false);
}

warmup(() -> sumTask(k), N);
cleanup();
Expand Down

0 comments on commit b8445b9

Please sign in to comment.