use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class ColumnFormat method saveIntDoubleRows.
private void saveIntDoubleRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
Vector vec = ServerRowUtils.getVector((ServerIntDoubleRow) rows[0]);
// int size = rows.length;
int indexOffset = (int) part.getPartitionKey().getStartCol();
IntDoubleVectorStorage storage = ((IntDoubleVector) vec).getStorage();
IntDoublesCol col = new IntDoublesCol(0, new double[rows.length]);
int startCol = (int) rows[0].getStartCol();
int endCol = (int) rows[0].getEndCol();
if (storage.isDense()) {
for (int i = startCol; i < endCol; i++) {
col.colId = i;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerIntDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
} else {
if (saveContext.sortFirst()) {
int[] indices = storage.getIndices();
Sort.quickSort(indices, 0, indices.length - 1);
for (int i = 0; i < indices.length; i++) {
col.colId = indices[i] + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerIntDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
} else {
ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
while (iter.hasNext()) {
col.colId = iter.next().getIntKey() + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerIntDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
}
}
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class ColumnFormat method saveLongDoubleRows.
private void saveLongDoubleRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
Vector vec = ServerRowUtils.getVector((ServerLongDoubleRow) rows[0]);
// int size = rows.size();
long indexOffset = part.getPartitionKey().getStartCol();
LongDoublesCol col = new LongDoublesCol(0, new double[rows.length]);
if (vec instanceof IntDoubleVector) {
IntDoubleVectorStorage storage = ((IntDoubleVector) vec).getStorage();
long startCol = rows[0].getStartCol();
long endCol = rows[0].getEndCol();
if (storage.isDense()) {
for (long i = startCol; i < endCol; i++) {
col.colId = i;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerLongDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
} else {
if (saveContext.sortFirst()) {
int[] indices = storage.getIndices();
Sort.quickSort(indices, 0, indices.length - 1);
for (int i = 0; i < indices.length; i++) {
col.colId = indices[i] + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerLongDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
} else {
ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
while (iter.hasNext()) {
col.colId = iter.next().getIntKey() + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerLongDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
}
}
} else {
LongDoubleVectorStorage storage = ((LongDoubleVector) vec).getStorage();
if (saveContext.sortFirst()) {
long[] indices = storage.getIndices();
Sort.quickSort(indices, 0, indices.length - 1);
for (int i = 0; i < indices.length; i++) {
col.colId = indices[i] + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerLongDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
} else {
ObjectIterator<Long2DoubleMap.Entry> iter = storage.entryIterator();
while (iter.hasNext()) {
col.colId = iter.next().getLongKey() + indexOffset;
for (int j = 0; j < rows.length; j++) {
col.colElems[j] = ((ServerLongDoubleRow) (rows[j])).get(col.colId);
}
save(col, output);
}
}
}
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class ByteBufSerdeUtils method serializedIntDoubleVectorLen.
public static int serializedIntDoubleVectorLen(IntDoubleVector vector) {
int len = 0;
IntDoubleVectorStorage storage = vector.getStorage();
if (storage.isDense()) {
len += serializedIntLen(DENSE_STORAGE_TYPE);
len += serializedDoublesLen(storage.getValues());
} else if (storage.isSparse()) {
len += serializedIntLen(SPARSE_STORAGE_TYPE);
len += serializedIntLen(storage.size());
len += storage.size() * (INT_LENGTH + DOUBLE_LENGTH);
} else if (storage.isSorted()) {
len += serializedIntLen(SORTED_STORAGE_TYPE);
len += serializedIntsLen(vector.getStorage().getIndices());
len += serializedDoublesLen(vector.getStorage().getValues());
} else {
throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
}
return len;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class MixedBinaryInNonZAExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntIntVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
double[] newValues = resPart.getValues();
if (part.isDense()) {
double[] 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<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), 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();
double[] resValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
double[] 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();
double[] 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 {
IntDoubleVectorStorage 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 IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(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;
((IntDoubleVectorStorage) 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 IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(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;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class MixedBinaryInNonZAExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntDummyVector v2, Binary op) {
IntDoubleVector[] 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 IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getIndices();
for (int i = 0; i < v2Indices.length; i++) {
int gidx = v2Indices[i];
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 1));
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
Aggregations