use of com.tencent.angel.ml.math2.vector.CompIntDoubleVector in project angel by Tencent.
the class MixedDotExecutor method apply.
private static double apply(CompIntDoubleVector v1, IntLongVector v2) {
double dotValue = 0.0;
if (v2.isDense()) {
int base = 0;
long[] v2Values = v2.getStorage().getValues();
for (IntDoubleVector part : v1.getPartitions()) {
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2Values[idx];
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getDoubleValue() * v2Values[idx];
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2Values[idx];
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
dotValue += v1.get(idx) * entry.getLongValue();
}
} else if (v2.isSorted() && v1.size() > v2.size()) {
// v2 is sorted
int[] v2Indices = v2.getStorage().getIndices();
long[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
dotValue += v1.get(idx) * v2Values[i];
}
} else {
int base = 0;
for (IntDoubleVector part : v1.getPartitions()) {
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2.get(idx);
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getDoubleValue() * v2.get(idx);
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2.get(idx);
}
}
base += part.getDim();
}
}
return dotValue;
}
use of com.tencent.angel.ml.math2.vector.CompIntDoubleVector in project angel by Tencent.
the class MixedDotExecutor method apply.
private static double apply(CompIntDoubleVector v1, IntDoubleVector v2) {
double dotValue = 0.0;
if (v2.isDense()) {
int base = 0;
double[] v2Values = v2.getStorage().getValues();
for (IntDoubleVector part : v1.getPartitions()) {
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2Values[idx];
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getDoubleValue() * v2Values[idx];
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2Values[idx];
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
dotValue += v1.get(idx) * entry.getDoubleValue();
}
} else if (v2.isSorted() && v1.size() > v2.size()) {
// v2 is sorted
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
dotValue += v1.get(idx) * v2Values[i];
}
} else {
int base = 0;
for (IntDoubleVector part : v1.getPartitions()) {
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2.get(idx);
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getDoubleValue() * v2.get(idx);
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2.get(idx);
}
}
base += part.getDim();
}
}
return dotValue;
}
use of com.tencent.angel.ml.math2.vector.CompIntDoubleVector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntLongVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
long[] 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.vector.CompIntDoubleVector 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.CompIntDoubleVector in project angel by Tencent.
the class MatrixUtils method rbCompDense2Blas.
public static BlasDoubleMatrix rbCompDense2Blas(RBCompIntDoubleMatrix mat) {
assert mat != null;
int dim = (int) mat.getDim();
int subDim = mat.getSubDim();
CompIntDoubleVector[] rows = mat.getRows();
double[] data = new double[rows.length * dim];
int rowId = 0;
for (CompIntDoubleVector row : rows) {
IntDoubleVector[] partitions = row.getPartitions();
int partId = 0;
for (IntDoubleVector part : partitions) {
assert part.isDense();
double[] src = part.getStorage().getValues();
System.arraycopy(src, 0, data, rowId * dim + partId * subDim, src.length);
partId += 1;
}
rowId += 1;
}
return MFactory.denseDoubleMatrix(rows.length, dim, data);
}
Aggregations