use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class StreamSerdeUtils method deserializeIntDoubleVector.
private static IntDoubleVector deserializeIntDoubleVector(DataInputStream in, long dim) throws IOException {
int storageType = deserializeInt(in);
switch(storageType) {
case DENSE_STORAGE_TYPE:
return VFactory.denseDoubleVector(deserializeDoubles(in));
case SPARSE_STORAGE_TYPE:
int len = deserializeInt(in);
Int2DoubleOpenHashMap idToValueMap = new Int2DoubleOpenHashMap(len);
for (int i = 0; i < len; i++) {
idToValueMap.put(deserializeInt(in), deserializeDouble(in));
}
return new IntDoubleVector((int) dim, new IntDoubleSparseVectorStorage((int) dim, idToValueMap));
case SORTED_STORAGE_TYPE:
return VFactory.sortedDoubleVector((int) dim, deserializeInts(in), deserializeDoubles(in));
default:
throw new UnsupportedOperationException("Unsupport storage type " + storageType);
}
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector 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);
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector 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.IntDoubleVector 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.IntDoubleVector in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntIntVector 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 (v.isDense()) {
double[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new double[v.getDim()]);
if (trans) {
blas.dgemv("N", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
} else {
blas.dgemv("T", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getIntValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getIntValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
int[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
int[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Aggregations