use of com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage in project angel by Tencent.
the class RangeRouterUtils method splitIntIntVector.
public static KeyValuePart[] splitIntIntVector(MatrixMeta matrixMeta, IntIntVector vector) {
IntIntVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Get keys and values
IntIntSparseVectorStorage sparseStorage = (IntIntSparseVectorStorage) storage;
int[] keys = sparseStorage.getIndices();
int[] values = sparseStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, false);
} else if (storage.isDense()) {
// Get values
IntIntDenseVectorStorage denseStorage = (IntIntDenseVectorStorage) storage;
int[] values = denseStorage.getValues();
return split(matrixMeta, vector.getRowId(), values);
} else {
// Key and value array pair
IntIntSortedVectorStorage sortStorage = (IntIntSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
int[] values = sortStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, true);
}
}
use of com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage in project angel by Tencent.
the class HashRouterUtils method splitIntIntVector.
public static void splitIntIntVector(KeyHash hasher, MatrixMeta matrixMeta, IntIntVector vector, KeyValuePart[] dataParts) {
int dataPartNum = dataParts.length;
int dataPartNumMinus1 = dataPartNum - 1;
if (isPow2(dataPartNum)) {
IntIntVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntIntSparseVectorStorage sparseStorage = (IntIntSparseVectorStorage) storage;
ObjectIterator<Int2IntMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
((HashIntKeysIntValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getIntValue());
}
} else if (storage.isDense()) {
// Get values
IntIntDenseVectorStorage denseStorage = (IntIntDenseVectorStorage) storage;
int[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
((HashIntKeysIntValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntIntSortedVectorStorage sortStorage = (IntIntSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
int[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
((HashIntKeysIntValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
} else {
IntIntVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntIntSparseVectorStorage sparseStorage = (IntIntSparseVectorStorage) storage;
ObjectIterator<Int2IntMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
((HashIntKeysIntValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getIntValue());
}
} else if (storage.isDense()) {
// Get values
IntIntDenseVectorStorage denseStorage = (IntIntDenseVectorStorage) storage;
int[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) % dataPartNum;
((HashIntKeysIntValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntIntSortedVectorStorage sortStorage = (IntIntSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
int[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
((HashIntKeysIntValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
}
}
use of com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntIntVector v1, IntIntVector v2, Binary op) {
IntIntVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntIntVector part : parts) {
IntIntVectorStorage resPart = (IntIntVectorStorage) resParts[k];
if (part.isDense()) {
int[] partValue = part.getStorage().getValues();
int[] 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()) {
int[] 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<Int2IntMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getIntValue(), 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();
int[] resPartValues = resPart.getValues();
if (op.isKeepStorage()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
int[] 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 {
IntIntVectorStorage 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();
int[] 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 {
IntIntVectorStorage 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 IntIntSortedVectorStorage) {
resParts[i] = new IntIntSparseVectorStorage(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)) {
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntIntVector[] res = new IntIntVector[parts.length];
int i = 0;
for (IntIntVector part : parts) {
res[i] = new IntIntVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntIntVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntIntVector v1, IntDummyVector v2, Binary op) {
IntIntVector[] 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 IntIntSortedVectorStorage) {
resParts[i] = new IntIntSparseVectorStorage(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;
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
}
IntIntVector[] res = new IntIntVector[parts.length];
int i = 0;
for (IntIntVector part : parts) {
res[i] = new IntIntVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntIntVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage in project angel by Tencent.
the class CompIntIntRowUpdateSplit method serialize.
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
IntIntVectorStorage storage = split.getStorage();
if (storage instanceof IntIntSparseVectorStorage) {
buf.writeInt(storage.size());
ObjectIterator<Int2IntMap.Entry> iter = storage.entryIterator();
Int2IntMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
buf.writeInt(entry.getIntKey());
buf.writeInt(entry.getIntValue());
}
} else if (storage instanceof IntIntSortedVectorStorage) {
buf.writeInt(storage.size());
int[] indices = storage.getIndices();
int[] values = storage.getValues();
for (int i = 0; i < indices.length; i++) {
buf.writeInt(indices[i]);
buf.writeInt(values[i]);
}
} else if (storage instanceof IntIntDenseVectorStorage) {
int[] values = storage.getValues();
int writeSize = values.length < maxItemNum ? values.length : maxItemNum;
buf.writeInt(writeSize);
for (int i = 0; i < writeSize; i++) {
buf.writeInt(values[i]);
}
} else {
throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
}
}
Aggregations