use of com.tencent.angel.ml.math2.storage.IntIntVectorStorage in project angel by Tencent.
the class SimpleBinaryInAllExecutor method apply.
public static Vector apply(IntDoubleVector v1, IntIntVector v2, Binary op) {
if (v1.isDense() && v2.isDense()) {
double[] resValues = v1.getStorage().getValues();
int[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < resValues.length; idx++) {
resValues[idx] = op.apply(resValues[idx], v2Values[idx]);
}
} else if (v1.isDense() && v2.isSparse()) {
double[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isDense() && v2.isSorted()) {
double[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(entry.getDoubleValue(), v2Values[idx]);
}
} else {
for (int i = 0; i < resValues.length; i++) {
if (v1.getStorage().hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
}
} else {
IntDoubleVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v1Storage.hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
ObjectIterator<Int2DoubleMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2DoubleMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getDoubleValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
ObjectIterator<Int2DoubleMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2DoubleMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getDoubleValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
if (!op.isCompare()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = Double.NaN;
}
}
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], 0);
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
resValues[v2Indices[v2Pointor]] = op.apply(0, v2Values[v2Pointor]);
v2Pointor++;
}
}
v1.setStorage(newStorage);
}
} else {
throw new AngelException("The operation is not support!");
}
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntIntVectorStorage in project angel by Tencent.
the class SimpleBinaryInAllExecutor method apply.
public static Vector apply(IntIntVector v1, IntIntVector v2, Binary op) {
if (v1.isDense() && v2.isDense()) {
int[] resValues = v1.getStorage().getValues();
int[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < resValues.length; idx++) {
resValues[idx] = op.apply(resValues[idx], v2Values[idx]);
}
} else if (v1.isDense() && v2.isSparse()) {
int[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isDense() && v2.isSorted()) {
int[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
ObjectIterator<Int2IntMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(entry.getIntValue(), v2Values[idx]);
}
} else {
for (int i = 0; i < resValues.length; i++) {
if (v1.getStorage().hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
}
} else {
IntIntVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v1Storage.hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2IntMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getIntValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2IntMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getIntValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
if (!op.isCompare()) {
if (size1 != v1.getDim() && size2 != v2.getDim()) {
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = 0 / 0;
}
}
}
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], 0);
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
resValues[v2Indices[v2Pointor]] = op.apply(0, v2Values[v2Pointor]);
v2Pointor++;
}
}
v1.setStorage(newStorage);
}
} else {
throw new AngelException("The operation is not support!");
}
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntIntVectorStorage 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.storage.IntIntVectorStorage in project angel by Tencent.
the class SimpleBinaryOutAllExecutor method apply.
public static Vector apply(IntFloatVector v1, IntIntVector v2, Binary op) {
IntFloatVector res;
if (v1.isDense() && v2.isDense()) {
res = v1.copy();
float[] resValues = res.getStorage().getValues();
int[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < resValues.length; idx++) {
resValues[idx] = op.apply(resValues[idx], v2Values[idx]);
}
} else if (v1.isDense() && v2.isSparse()) {
res = v1.copy();
float[] resValues = res.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isDense() && v2.isSorted()) {
res = v1.copy();
float[] resValues = res.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(entry.getFloatValue(), v2Values[idx]);
}
} else {
for (int i = 0; i < resValues.length; i++) {
if (v1.getStorage().hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
}
} else {
IntFloatVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v1Storage.hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSparse() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2FloatMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getFloatValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSparse() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2FloatMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getFloatValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
if (!op.isCompare()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = Float.NaN;
}
}
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], 0);
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
resValues[v2Indices[v2Pointor]] = op.apply(0, v2Values[v2Pointor]);
v2Pointor++;
}
}
res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else {
throw new AngelException("The operation is not support!");
}
return res;
}
use of com.tencent.angel.ml.math2.storage.IntIntVectorStorage in project angel by Tencent.
the class SimpleBinaryOutAllExecutor method apply.
public static Vector apply(IntIntVector v1, IntIntVector v2, Binary op) {
IntIntVector res;
if (v1.isDense() && v2.isDense()) {
res = v1.copy();
int[] resValues = res.getStorage().getValues();
int[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < resValues.length; idx++) {
resValues[idx] = op.apply(resValues[idx], v2Values[idx]);
}
} else if (v1.isDense() && v2.isSparse()) {
res = v1.copy();
int[] resValues = res.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isDense() && v2.isSorted()) {
res = v1.copy();
int[] resValues = res.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
ObjectIterator<Int2IntMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(entry.getIntValue(), v2Values[idx]);
}
} else {
for (int i = 0; i < resValues.length; i++) {
if (v1.getStorage().hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
}
} else {
IntIntVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v1Storage.hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSparse() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2IntMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getIntValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSparse() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
ObjectIterator<Int2IntMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2IntMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getIntValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getIntValue());
}
}
} else {
IntIntVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else if (v1.isSorted() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntIntVectorStorage newStorage = v1.getStorage().emptyDense();
int[] resValues = newStorage.getValues();
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Indices = v1.getStorage().getIndices();
int[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
if (!op.isCompare()) {
if (size1 != v1.getDim() && size2 != v2.getDim()) {
for (int i = 0; i < v1.getDim(); i++) {
resValues[i] = 0 / 0;
}
}
}
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], 0);
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
resValues[v2Indices[v2Pointor]] = op.apply(0, v2Values[v2Pointor]);
v2Pointor++;
}
}
res = new IntIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
} else {
throw new AngelException("The operation is not support!");
}
return res;
}
Aggregations