use of com.tencent.angel.ml.math2.vector.IntDoubleVector 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.IntDoubleVector 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.IntDoubleVector in project angel by Tencent.
the class ByteBufSerdeUtils method main.
public static void main(String[] args) {
boolean b = true;
ByteBuf buf = ByteBufUtils.newByteBuf(serializedBooleanLen(b), true);
serializeBoolean(buf, b);
assert serializedBooleanLen(b) == buf.writerIndex();
boolean db = deserializeBoolean(buf);
assert b == db;
buf.release();
byte by = 1;
buf = ByteBufUtils.newByteBuf(serializedByteLen(by), true);
serializeByte(buf, by);
assert serializedByteLen(by) == buf.writerIndex();
byte dby = deserializeByte(buf);
assert by == dby;
buf.release();
char c = 'a';
buf = ByteBufUtils.newByteBuf(serializedCharLen(c), true);
serializeChar(buf, c);
assert serializedCharLen(c) == buf.writerIndex();
char dc = deserializeChar(buf);
assert c == dc;
buf.release();
int i = 124;
buf = ByteBufUtils.newByteBuf(serializedIntLen(i), true);
serializeInt(buf, i);
assert serializedIntLen(i) == buf.writerIndex();
int di = deserializeInt(buf);
assert i == di;
buf.release();
long l = 124L;
buf = ByteBufUtils.newByteBuf(serializedLongLen(l), true);
serializeLong(buf, l);
assert serializedLongLen(l) == buf.writerIndex();
long dl = deserializeLong(buf);
assert l == dl;
buf.release();
float f = 12.34f;
buf = ByteBufUtils.newByteBuf(serializedFloatLen(f), true);
serializeFloat(buf, f);
assert serializedFloatLen(f) == buf.writerIndex();
float df = deserializeFloat(buf);
assert f == df;
double d = 12.34;
buf = ByteBufUtils.newByteBuf(serializedDoubleLen(d), true);
serializeDouble(buf, d);
assert serializedDoubleLen(d) == buf.writerIndex();
double dd = deserializeDouble(buf);
assert d == dd;
buf.release();
String s = "123";
buf = ByteBufUtils.newByteBuf(serializedUTF8Len(s), true);
serializeUTF8(buf, s);
assert serializedUTF8Len(s) == buf.writerIndex();
String ds = deserializeUTF8(buf);
assert s.equalsIgnoreCase(ds);
buf.release();
int[] ia = new int[3];
for (i = 0; i < ia.length; i++) {
ia[i] = i;
}
buf = ByteBufUtils.newByteBuf(serializedIntsLen(ia, 0, ia.length), true);
serializeInts(buf, ia);
assert serializedIntsLen(ia, 0, ia.length) == buf.writerIndex();
int[] dia = deserializeInts(buf);
assert ia.length == dia.length;
for (i = 0; i < ia.length; i++) {
assert ia[i] == dia[i];
}
buf.release();
float[] fa = new float[3];
for (i = 0; i < ia.length; i++) {
fa[i] = i;
}
buf = ByteBufUtils.newByteBuf(serializedFloatsLen(fa, 0, fa.length), true);
serializeFloats(buf, fa);
assert serializedFloatsLen(fa, 0, fa.length) == buf.writerIndex();
float[] dfa = deserializeFloats(buf);
assert fa.length == dfa.length;
for (i = 0; i < fa.length; i++) {
assert fa[i] == dfa[i];
}
buf.release();
double[] da = new double[3];
for (i = 0; i < ia.length; i++) {
da[i] = i;
}
buf = ByteBufUtils.newByteBuf(serializedDoublesLen(da, 0, da.length), true);
serializeDoubles(buf, da, 0, da.length);
assert serializedDoublesLen(da, 0, fa.length) == buf.writerIndex();
double[] dda = deserializeDoubles(buf);
assert da.length == dda.length;
for (i = 0; i < da.length; i++) {
assert da[i] == dda[i];
}
buf.release();
long[] la = new long[3];
for (i = 0; i < la.length; i++) {
la[i] = i;
}
buf = ByteBufUtils.newByteBuf(serializedLongsLen(la, 0, la.length), true);
serializeLongs(buf, la, 0, da.length);
assert serializedLongsLen(la, 0, la.length) == buf.writerIndex();
long[] dla = deserializeLongs(buf);
assert la.length == dla.length;
for (i = 0; i < la.length; i++) {
assert la[i] == dla[i];
}
buf.release();
IntDoubleVector didVector = VFactory.denseDoubleVector(da);
buf = ByteBufUtils.newByteBuf(serializedVectorLen(didVector));
serializeVector(buf, didVector);
assert serializedVectorLen(didVector) == buf.writerIndex();
IntDoubleVector ddidVector = (IntDoubleVector) deserializeVector(buf);
for (i = 0; i < da.length; i++) {
assert ddidVector.get(i) == da[i];
}
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class ByteBufSerdeUtils method serializedIntDoubleVectorLen.
public static int serializedIntDoubleVectorLen(IntDoubleVector vector) {
int len = 0;
IntDoubleVectorStorage storage = vector.getStorage();
if (storage.isDense()) {
len += serializedIntLen(DENSE_STORAGE_TYPE);
len += serializedDoublesLen(storage.getValues());
} else if (storage.isSparse()) {
len += serializedIntLen(SPARSE_STORAGE_TYPE);
len += serializedIntLen(storage.size());
len += storage.size() * (INT_LENGTH + DOUBLE_LENGTH);
} else if (storage.isSorted()) {
len += serializedIntLen(SORTED_STORAGE_TYPE);
len += serializedIntsLen(vector.getStorage().getIndices());
len += serializedDoublesLen(vector.getStorage().getValues());
} else {
throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
}
return len;
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class ByteBufSerdeUtils method deserializeIntDoubleVector.
private static IntDoubleVector deserializeIntDoubleVector(ByteBuf in, long dim) {
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);
}
}
Aggregations