use of com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor 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];
if (part.isDense()) {
float[] partValue = part.getStorage().getValues();
float[] 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()) {
float[] 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<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 < 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();
float[] resPartValues = resPart.getValues();
if (op.isKeepStorage()) {
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++) {
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 {
IntFloatVectorStorage 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();
float[] 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 {
IntFloatVectorStorage 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 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();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
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.storage.IntFloatSparseVectorStorage in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntDummyVector v2, Binary op) {
IntFloatVector[] 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 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();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(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.storage.IntFloatSparseVectorStorage in project angel by Tencent.
the class CsrFloatMatrix method getRow.
@Override
public Vector getRow(int idx) {
IntArrayList cols = new IntArrayList();
FloatArrayList data = new FloatArrayList();
int rowNum = indptr.length - 1;
assert (idx < rowNum);
for (int i = indptr[idx]; i < indptr[idx + 1]; i++) {
cols.add(indices[i]);
data.add(values[i]);
}
IntFloatSparseVectorStorage storage = new IntFloatSparseVectorStorage(shape[1], cols.toIntArray(), data.toFloatArray());
return new IntFloatVector(getMatrixId(), idx, getClock(), shape[1], storage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage in project angel by Tencent.
the class CooIntFloatMatrix method getRow.
@Override
public Vector getRow(int idx) {
IntArrayList cols = new IntArrayList();
FloatArrayList data = new FloatArrayList();
for (int i = 0; i < rowIndices.length; i++) {
if (rowIndices[i] == idx) {
cols.add(colIndices[i]);
data.add(values[i]);
}
}
IntFloatSparseVectorStorage storage = new IntFloatSparseVectorStorage(shape[1], cols.toIntArray(), data.toFloatArray());
return new IntFloatVector(getMatrixId(), idx, getClock(), shape[1], storage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage in project angel by Tencent.
the class MixedBinaryOutAllExecutor 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()) {
long[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntFloatVector part : parts) {
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[k];
if (part.isDense()) {
float[] partValue = part.getStorage().getValues();
float[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i;
resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
}
} 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 < resPart.size(); 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[] resPartIndices = resPart.getIndices();
float[] resPartValues = resPart.getValues();
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++) {
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 {
IntFloatVectorStorage 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 {
float[] resPartValues = resPart.getValues();
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++) {
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 {
IntFloatVectorStorage 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 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();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
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++;
}
return new CompIntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Aggregations