use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntDummyVector v2, Binary op) {
IntDoubleVector[] 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 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;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
}
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.vector.IntDummyVector in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasFloatMatrix mat, boolean trans, IntDummyVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
float[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new float[n];
} else {
assert n == v.getDim();
resArr = new float[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
float[] data = mat.getData();
if (trans) {
for (int j = 0; j < c; j++) {
int[] idxs = v.getIndices();
for (int i : idxs) {
resArr[j] += data[i * c + j];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getIndices();
for (int j : idxs) {
resArr[i] += data[i * c + j];
}
}
}
IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class SimpleBinaryOutAllExecutor method apply.
public static Vector apply(IntDoubleVector v1, IntDummyVector v2, Binary op) {
IntDoubleVector res;
if (v1.isDense()) {
res = v1.copy();
double[] resValues = res.getStorage().getValues();
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
} else if (v1.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
ObjectIterator<Int2DoubleMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2DoubleMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getDoubleValue();
}
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else {
// sorted
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
}
return res;
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class SimpleBinaryOutAllExecutor method apply.
public static Vector apply(IntLongVector v1, IntDummyVector v2, Binary op) {
IntLongVector res;
if (v1.isDense()) {
res = v1.copy();
long[] resValues = res.getStorage().getValues();
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
} else if (v1.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntLongVectorStorage newStorage = v1.getStorage().emptyDense();
long[] resValues = newStorage.getValues();
ObjectIterator<Int2LongMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2LongMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getLongValue();
}
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
res = new IntLongVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else {
// sorted
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntLongVectorStorage newStorage = v1.getStorage().emptyDense();
long[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
long[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = op.apply(resValues[i], v2.get(i));
}
res = new IntLongVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
}
return res;
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class SimpleBinaryOutNonZAExecutor method apply.
public static Vector apply(IntIntVector v1, IntDummyVector v2, Binary op) {
IntIntVectorStorage newStorage = (IntIntVectorStorage) StorageSwitch.apply(v1, v2, op);
if (v1.isDense()) {
int[] resValues = newStorage.getValues();
int[] v2Indices = v2.getIndices();
for (int idx : v2Indices) {
resValues[idx] = op.apply(resValues[idx], 1);
}
} else if (v1.isSparse()) {
int[] v2Indices = v2.getIndices();
if (((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sparseDenseStorageThreshold * v1.getDim())) {
int[] resValues = newStorage.getValues();
ObjectIterator<Int2IntMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
newStorage.set(entry.getIntKey(), entry.getIntValue());
}
for (int idx : v2Indices) {
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
} else {
// to avoid multi-rehash
int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
if (v1.size() + v2.size() < 1.5 * capacity) {
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
} else {
ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2IntMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
newStorage.set(idx, entry.getIntValue());
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
}
}
} else {
// sorted
int[] v1Indices = v1.getStorage().getIndices();
int[] v2Indices = v2.getIndices();
if (!op.isKeepStorage() && ((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sortedDenseStorageThreshold * v1.getDim())) {
int[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
newStorage.set(v1Indices[i], v1Values[i]);
}
size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(newStorage.get(idx), 1));
}
} else {
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Values = v1.getStorage().getValues();
while (v1Pointor < size1 || v2Pointor < size2) {
if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], 1));
v1Pointor++;
v2Pointor++;
} else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
v1Pointor++;
} else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
newStorage.set(v2Indices[v2Pointor], op.apply(0, 1));
v2Pointor++;
}
}
}
}
return new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
Aggregations