use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class MixedDotExecutor method apply.
private static double apply(CompIntFloatVector v1, IntLongVector v2) {
double dotValue = 0.0;
if (v2.isDense()) {
int base = 0;
long[] v2Values = v2.getStorage().getValues();
for (IntFloatVector part : v1.getPartitions()) {
if (part.isDense()) {
float[] 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<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getFloatValue() * v2Values[idx];
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
float[] 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 (IntFloatVector part : v1.getPartitions()) {
if (part.isDense()) {
float[] 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<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getFloatValue() * v2.get(idx);
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
float[] 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.IntFloatVector in project angel by Tencent.
the class MixedBinaryInNonZAExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntIntVector v2, Binary op) {
IntFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntFloatVector part : parts) {
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[k];
float[] newValues = resPart.getValues();
if (part.isDense()) {
float[] partValue = part.getStorage().getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i + base;
newValues[i] = op.apply(partValue[i], v2Values[idx]);
}
} else if (part.isSparse()) {
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<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getFloatValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < newValues.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
if (op.isKeepStorage()) {
int dim = part.getDim();
int[] resIndices = resPart.getIndices();
float[] resValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < dim; i++) {
resIndices[i] = i;
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
newValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
newValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntFloatVectorStorage partStorage = part.getStorage();
for (int i = 0; i < newValues.length; i++) {
if (partStorage.hasKey(i)) {
newValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
newValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else if (v2.isSparse()) {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntFloatSortedVectorStorage) {
resParts[i] = new IntFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int gidx = entry.getIntKey();
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
}
} else {
// sorted
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntFloatSortedVectorStorage) {
resParts[i] = new IntFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int gidx = v2Indices[i];
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
IntFloatVector[] res = new IntFloatVector[parts.length];
int i = 0;
for (IntFloatVector part : parts) {
res[i] = new IntFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntFloatVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class MixedBinaryInZAExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntDummyVector v2, Binary op) {
IntFloatVector[] 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)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 1));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntFloatVector part = parts[i];
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[i];
if (part.isDense()) {
float[] partValues = part.getStorage().getValues();
float[] 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<Int2FloatMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2FloatMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getFloatValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
float[] 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();
float[] 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();
}
}
IntFloatVector[] res = new IntFloatVector[parts.length];
int i = 0;
for (IntFloatVector part : parts) {
res[i] = new IntFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntFloatVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class MixedBinaryInZAExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntLongVector v2, Binary op) {
IntFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int base = 0;
long[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < parts.length; i++) {
IntFloatVector part = parts[i];
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[i];
if (part.isDense()) {
float[] resPartValues = resPart.getValues();
float[] partValues = part.getStorage().getValues();
for (int j = 0; j < partValues.length; j++) {
resPartValues[j] = op.apply(partValues[j], v2Values[base + j]);
}
} else if (part.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getFloatValue(), v2Values[idx + base]));
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
float[] resPartValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2Values[idx + base]);
}
} else {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPart.set(idx, op.apply(partValues[j], v2Values[idx + base]));
}
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getLongValue()));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntFloatVector part = parts[i];
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[i];
if (part.isDense()) {
float[] partValues = part.getStorage().getValues();
float[] 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<Int2FloatMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2FloatMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getFloatValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
float[] 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();
float[] 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();
}
}
} else {
// sorted
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getStorage().getIndices();
long[] v2Values = v2.getStorage().getValues();
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)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntFloatVector part = parts[i];
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[i];
if (part.isDense()) {
float[] partValues = part.getStorage().getValues();
float[] 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<Int2FloatMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2FloatMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getFloatValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
float[] 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();
float[] 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();
}
}
}
IntFloatVector[] res = new IntFloatVector[parts.length];
int i = 0;
for (IntFloatVector part : parts) {
res[i] = new IntFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntFloatVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat, IntFloatVector v, boolean onCol, Binary op) {
float[] data = mat.getData();
int m = mat.getNumRows(), n = mat.getNumCols();
int size = v.size();
byte[] flag = null;
if (!v.isDense()) {
flag = new byte[v.getDim()];
}
if (onCol && op.isInplace()) {
if (v.isDense()) {
float[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
float value = values[i];
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else if (v.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getFloatValue();
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
float[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
float value = values[k];
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], value);
}
}
}
if (!v.isDense()) {
switch(op.getOpType()) {
case INTERSECTION:
for (int i = 0; i < m; i++) {
if (flag[i] == 0) {
for (int j = 0; j < n; j++) {
data[i * n + j] = 0;
}
}
}
case UNION:
break;
case ALL:
for (int i = 0; i < m; i++) {
if (flag[i] == 0) {
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], 0);
}
}
}
}
}
return mat;
} else if (onCol && !op.isInplace()) {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
float[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
float value = values[i];
for (int j = 0; j < n; j++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else if (v.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getFloatValue();
for (int j = 0; j < n; j++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
float[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
float value = values[k];
for (int j = 0; j < n; j++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
}
if (!v.isDense()) {
switch(op.getOpType()) {
case INTERSECTION:
break;
case UNION:
break;
case ALL:
for (int i = 0; i < m; i++) {
if (flag[i] == 0) {
for (int j = 0; j < n; j++) {
newData[i * n + j] = op.apply(data[i * n + j], 0);
}
}
}
}
}
return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
} else if (!onCol && op.isInplace()) {
if (v.isDense()) {
float[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], values[j]);
}
}
} else if (v.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
float value = entry.getFloatValue();
flag[j] = 1;
for (int i = 0; i < m; i++) {
data[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
float[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
float value = values[k];
flag[j] = 1;
for (int i = 0; i < m; i++) {
data[i * n + j] = op.apply(data[i * n + j], value);
}
}
}
if (!v.isDense()) {
switch(op.getOpType()) {
case INTERSECTION:
for (int j = 0; j < n; j++) {
if (flag[j] == 0) {
for (int i = 0; i < m; i++) {
data[i * n + j] = 0;
}
}
}
case UNION:
break;
case ALL:
for (int j = 0; j < n; j++) {
if (flag[j] == 0) {
for (int i = 0; i < m; i++) {
data[i * n + j] = op.apply(data[i * n + j], 0);
}
}
}
}
}
return mat;
} else {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
float[] values = v.getStorage().getValues();
for (int j = 0; j < n; j++) {
float value = values[j];
for (int i = 0; i < m; i++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else if (v.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
float value = entry.getFloatValue();
for (int i = 0; i < m; i++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
float[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
flag[j] = 1;
float value = values[k];
for (int i = 0; i < m; i++) {
newData[i * n + j] = op.apply(data[i * n + j], value);
}
}
}
if (!v.isDense()) {
switch(op.getOpType()) {
case INTERSECTION:
break;
case UNION:
break;
case ALL:
for (int j = 0; j < n; j++) {
if (flag[j] == 0) {
for (int i = 0; i < m; i++) {
newData[i * n + j] = op.apply(data[i * n + j], 0);
}
}
}
}
}
return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
}
}
Aggregations