use of com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage in project angel by Tencent.
the class CompIntDoubleRowUpdateSplit method serialize.
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
IntDoubleVectorStorage storage = split.getStorage();
if (storage instanceof IntDoubleSparseVectorStorage) {
buf.writeInt(storage.size());
ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
Int2DoubleMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
buf.writeInt(entry.getIntKey());
buf.writeDouble(entry.getDoubleValue());
}
} else if (storage instanceof IntDoubleSortedVectorStorage) {
buf.writeInt(storage.size());
int[] indices = storage.getIndices();
double[] values = storage.getValues();
for (int i = 0; i < indices.length; i++) {
buf.writeInt(indices[i]);
buf.writeDouble(values[i]);
}
} else if (storage instanceof IntDoubleDenseVectorStorage) {
double[] values = storage.getValues();
int writeSize = values.length < maxItemNum ? values.length : maxItemNum;
buf.writeInt(writeSize);
for (int i = 0; i < writeSize; i++) {
buf.writeDouble(values[i]);
}
} else {
throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
}
}
use of com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage in project angel by Tencent.
the class RangeRouterUtils method splitIntDoubleVector.
public static KeyValuePart[] splitIntDoubleVector(MatrixMeta matrixMeta, IntDoubleVector vector) {
IntDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Get keys and values
IntDoubleSparseVectorStorage sparseStorage = (IntDoubleSparseVectorStorage) storage;
int[] keys = sparseStorage.getIndices();
double[] values = sparseStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, false);
} else if (storage.isDense()) {
// Get values
IntDoubleDenseVectorStorage denseStorage = (IntDoubleDenseVectorStorage) storage;
double[] values = denseStorage.getValues();
return split(matrixMeta, vector.getRowId(), values);
} else {
// Key and value array pair
IntDoubleSortedVectorStorage sortStorage = (IntDoubleSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, true);
}
}
use of com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage in project angel by Tencent.
the class HashRouterUtils method splitIntDoubleVector.
public static void splitIntDoubleVector(KeyHash hasher, MatrixMeta matrixMeta, IntDoubleVector vector, KeyValuePart[] dataParts) {
int dataPartNum = dataParts.length;
int dataPartNumMinus1 = dataPartNum - 1;
if (isPow2(dataPartNum)) {
IntDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntDoubleSparseVectorStorage sparseStorage = (IntDoubleSparseVectorStorage) storage;
ObjectIterator<Int2DoubleMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getDoubleValue());
}
} else if (storage.isDense()) {
// Get values
IntDoubleDenseVectorStorage denseStorage = (IntDoubleDenseVectorStorage) storage;
double[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntDoubleSortedVectorStorage sortStorage = (IntDoubleSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
} else {
IntDoubleVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntDoubleSparseVectorStorage sparseStorage = (IntDoubleSparseVectorStorage) storage;
ObjectIterator<Int2DoubleMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getDoubleValue());
}
} else if (storage.isDense()) {
// Get values
IntDoubleDenseVectorStorage denseStorage = (IntDoubleDenseVectorStorage) storage;
double[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) % dataPartNum;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntDoubleSortedVectorStorage sortStorage = (IntDoubleSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
double[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
((HashIntKeysDoubleValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
}
}
use of com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntDoubleVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
double[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
if (part.isDense()) {
double[] partValue = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i + base;
resPartValues[i] = op.apply(partValue[i], v2Values[idx]);
}
} else if (part.isSparse()) {
double[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPartValues.length; i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
if (op.isKeepStorage()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntFloatVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
float[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
if (part.isDense()) {
double[] partValue = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i + base;
resPartValues[i] = op.apply(partValue[i], v2Values[idx]);
}
} else if (part.isSparse()) {
double[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPartValues.length; i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
if (op.isKeepStorage()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
Aggregations