use of com.tencent.angel.ml.math2.storage.IntFloatVectorStorage in project angel by Tencent.
the class MixedBinaryOutZAExecutor 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 < resPartValues.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++;
}
return new CompIntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.storage.IntFloatVectorStorage in project angel by Tencent.
the class SimpleBinaryOutAllExecutor method apply.
public static Vector apply(IntDoubleVector v1, IntFloatVector v2, Binary op) {
IntDoubleVector res;
if (v1.isDense() && v2.isDense()) {
res = v1.copy();
double[] resValues = res.getStorage().getValues();
float[] 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();
double[] 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<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getFloatValue());
}
} else {
IntFloatVectorStorage 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();
double[] resValues = res.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
float[] 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 {
IntFloatVectorStorage 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();
float[] 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]);
}
}
}
res = new IntDoubleVector(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 {
IntDoubleVectorStorage newStorage = v1.getStorage().emptyDense();
double[] resValues = newStorage.getValues();
float[] 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]);
}
}
}
res = new IntDoubleVector(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 {
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<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getFloatValue());
}
}
} else {
IntFloatVectorStorage 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 IntDoubleVector(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 {
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();
float[] 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 {
IntFloatVectorStorage 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 IntDoubleVector(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 {
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<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getFloatValue());
}
}
} else {
IntFloatVectorStorage 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 IntDoubleVector(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 {
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();
float[] 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++;
}
}
res = new IntDoubleVector(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.IntFloatVectorStorage 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.IntFloatVectorStorage in project angel by Tencent.
the class SimpleBinaryOutNonZAExecutor method apply.
public static Vector apply(IntFloatVector v1, IntDummyVector v2, Binary op) {
IntFloatVectorStorage newStorage = (IntFloatVectorStorage) StorageSwitch.apply(v1, v2, op);
if (v1.isDense()) {
float[] resValues = newStorage.getValues();
int[] v2Indices = v2.getIndices();
for (int idx : v2Indices) {
resValues[idx] = op.apply(resValues[idx], 1);
}
} else if (v1.isSparse()) {
int[] v2Indices = v2.getIndices();
if (((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sparseDenseStorageThreshold * v1.getDim())) {
float[] resValues = newStorage.getValues();
ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
newStorage.set(entry.getIntKey(), entry.getFloatValue());
}
for (int idx : v2Indices) {
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
} else {
// to avoid multi-rehash
int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
if (v1.size() + v2.size() < 1.5 * capacity) {
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
} else {
ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2FloatMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
newStorage.set(idx, entry.getFloatValue());
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(v1.get(idx), 1));
}
}
}
} else {
// sorted
int[] v1Indices = v1.getStorage().getIndices();
int[] v2Indices = v2.getIndices();
if (!op.isKeepStorage() && ((v1.size() + v2.size()) * Constant.intersectionCoeff > Constant.sortedDenseStorageThreshold * v1.getDim())) {
float[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
newStorage.set(v1Indices[i], v1Values[i]);
}
size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
newStorage.set(idx, op.apply(newStorage.get(idx), 1));
}
} else {
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
float[] v1Values = v1.getStorage().getValues();
while (v1Pointor < size1 || v2Pointor < size2) {
if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], 1));
v1Pointor++;
v2Pointor++;
} else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
v1Pointor++;
} else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
newStorage.set(v2Indices[v2Pointor], op.apply(0, 1));
v2Pointor++;
}
}
}
}
return new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatVectorStorage in project angel by Tencent.
the class IntFloatVector method argmin.
public int argmin() {
IntFloatVectorStorage idstorage = (IntFloatVectorStorage) storage;
if (idstorage.size() == 0)
return -1;
float minval = Float.MAX_VALUE;
int minidx = -1;
if (idstorage.isDense()) {
float[] val = idstorage.getValues();
int length = val.length;
for (int idx = 0; idx < length; idx++) {
if (val[idx] < minval) {
minval = val[idx];
minidx = idx;
}
}
} else if (idstorage.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = idstorage.entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
float val = entry.getFloatValue();
if (val < minval) {
minval = val;
minidx = idx;
}
}
} else {
int[] indices = idstorage.getIndices();
float[] val = idstorage.getValues();
int size = idstorage.size();
for (int i = 0; i < size; i++) {
int idx = indices[i];
if (val[i] < minval) {
minval = val[i];
minidx = idx;
}
}
}
return minidx;
}
Aggregations