use of com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage in project angel by Tencent.
the class CompLongDoubleRowUpdateSplit method serialize.
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
LongDoubleVectorStorage storage = split.getStorage();
if (storage instanceof LongDoubleSparseVectorStorage) {
ObjectIterator<Long2DoubleMap.Entry> iter = storage.entryIterator();
buf.writeInt(storage.size());
Long2DoubleMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
buf.writeLong(entry.getLongKey());
buf.writeDouble(entry.getDoubleValue());
}
} else if (storage instanceof LongDoubleSortedVectorStorage) {
buf.writeInt(storage.size());
long[] indices = storage.getIndices();
double[] values = storage.getValues();
for (int i = 0; i < indices.length; i++) {
buf.writeLong(indices[i]);
buf.writeDouble(values[i]);
}
} else {
throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
}
}
use of com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage in project angel by Tencent.
the class RangeRouterUtils method splitLongDoubleVector.
public static KeyValuePart[] splitLongDoubleVector(MatrixMeta matrixMeta, LongDoubleVector vector) {
LongDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Get keys and values
LongDoubleSparseVectorStorage sparseStorage = (LongDoubleSparseVectorStorage) storage;
long[] keys = sparseStorage.getIndices();
double[] values = sparseStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, false);
} else {
// Key and value array pair
LongDoubleSortedVectorStorage sortStorage = (LongDoubleSortedVectorStorage) storage;
long[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, true);
}
}
use of com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage in project angel by Tencent.
the class HashRouterUtils method splitLongDoubleVector.
public static void splitLongDoubleVector(KeyHash hasher, MatrixMeta matrixMeta, LongDoubleVector vector, KeyValuePart[] dataParts) {
int dataPartNum = dataParts.length;
int dataPartNumMinus1 = dataPartNum - 1;
if (isPow2(dataPartNum)) {
LongDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
LongDoubleSparseVectorStorage sparseStorage = (LongDoubleSparseVectorStorage) storage;
ObjectIterator<Long2DoubleMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Long2DoubleMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getLongKey()) & dataPartNumMinus1;
((HashLongKeysDoubleValuesPart) dataParts[partId]).add(keyValue.getLongKey(), keyValue.getDoubleValue());
}
} else {
// Key and value array pair
LongDoubleSortedVectorStorage sortStorage = (LongDoubleSortedVectorStorage) storage;
long[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
((HashLongKeysDoubleValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
} else {
LongDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
LongDoubleSparseVectorStorage sparseStorage = (LongDoubleSparseVectorStorage) storage;
ObjectIterator<Long2DoubleMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Long2DoubleMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getLongKey()) % dataPartNum;
((HashLongKeysDoubleValuesPart) dataParts[partId]).add(keyValue.getLongKey(), keyValue.getDoubleValue());
}
} else {
// Key and value array pair
LongDoubleSortedVectorStorage sortStorage = (LongDoubleSortedVectorStorage) storage;
long[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
((HashLongKeysDoubleValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
}
}
use of com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompLongDoubleVector v1, LongFloatVector v2, Binary op) {
LongDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongDoubleSortedVectorStorage) {
resParts[i] = new LongDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
long subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
LongDoubleVector[] res = new LongDoubleVector[parts.length];
int i = 0;
for (LongDoubleVector part : parts) {
res[i] = new LongDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompLongDoubleVector v1, LongDoubleVector v2, Binary op) {
LongDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongDoubleSortedVectorStorage) {
resParts[i] = new LongDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
long subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
LongDoubleVector[] res = new LongDoubleVector[parts.length];
int i = 0;
for (LongDoubleVector part : parts) {
res[i] = new LongDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
Aggregations