use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class BlasFloatMatrix method setRow.
public Matrix setRow(int i, Vector v) {
if (v instanceof IntFloatVector) {
float[] rowData;
if (v.isDense()) {
rowData = ((IntFloatVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new float[numCols];
ObjectIterator<Int2FloatMap.Entry> iter = ((IntFloatVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getFloatValue();
}
} else {
// sorted
rowData = new float[numCols];
int[] idxs = ((IntFloatVector) v).getStorage().getIndices();
float[] values = ((IntFloatVector) v).getStorage().getValues();
int size = ((IntFloatVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
System.arraycopy(rowData, 0, data, i * numCols, numCols);
} else if (v instanceof IntLongVector) {
long[] rowData;
if (v.isDense()) {
rowData = ((IntLongVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new long[numCols];
ObjectIterator<Int2LongMap.Entry> iter = ((IntLongVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getLongValue();
}
} else {
// sorted
rowData = new long[numCols];
int[] idxs = ((IntLongVector) v).getStorage().getIndices();
long[] values = ((IntLongVector) v).getStorage().getValues();
int size = ((IntLongVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
for (int j = 0; j < numCols; j++) {
data[i * numCols + j] = rowData[j];
}
} else if (v instanceof IntIntVector) {
int[] rowData;
if (v.isDense()) {
rowData = ((IntIntVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new int[numCols];
ObjectIterator<Int2IntMap.Entry> iter = ((IntIntVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getIntValue();
}
} else {
// sorted
rowData = new int[numCols];
int[] idxs = ((IntIntVector) v).getStorage().getIndices();
int[] values = ((IntIntVector) v).getStorage().getValues();
int size = ((IntIntVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
for (int j = 0; j < numCols; j++) {
data[i * numCols + j] = rowData[j];
}
} else if (v instanceof IntDummyVector) {
int[] rowData = new int[numCols];
int[] idxs = ((IntDummyVector) v).getIndices();
int size = ((IntDummyVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = 1;
}
for (int j = 0; j < numCols; j++) {
data[i * numCols + j] = rowData[j];
}
} else {
throw new AngelException("The operation is not supported!");
}
return this;
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class MixedBinaryOutAllExecutor 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++;
}
return new CompIntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class MixedBinaryOutZAExecutor method apply.
private static Vector apply(CompIntIntVector v1, IntDummyVector v2, Binary op) {
IntIntVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getIndices();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 1));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntIntVector part = parts[i];
IntIntVectorStorage resPart = (IntIntVectorStorage) resParts[i];
if (part.isDense()) {
int[] partValues = part.getStorage().getValues();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2IntMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2IntMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getIntValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
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++;
}
return new CompIntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class MixedBinaryOutNonZAExecutor 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();
int[] v2Indices = v2.getIndices();
for (int i = 0; i < v2Indices.length; i++) {
int gidx = v2Indices[i];
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 1));
}
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++;
}
return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.vector.IntDummyVector in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntDummyVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
double[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new double[n];
} else {
assert n == v.getDim();
resArr = new double[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
double[] 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];
}
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Aggregations