use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class HashRouterUtils method split.
/**
* Split keys by matrix partition
*
* @param matrixMeta matrix meta data
* @param vector Matrix vector
* @return partition key to key partition map
*/
public static KeyValuePart[] split(MatrixMeta matrixMeta, Vector vector) {
KeyHash hasher = HasherFactory.getHasher(matrixMeta.getRouterHash());
PartitionKey[] matrixParts = matrixMeta.getPartitionKeys();
KeyValuePart[] dataParts = new KeyValuePart[matrixParts.length];
int estSize = (int) (vector.getSize() / matrixMeta.getPartitionNum());
for (int i = 0; i < dataParts.length; i++) {
dataParts[i] = generateDataPart(vector.getRowId(), vector.getType(), estSize);
}
switch(vector.getType()) {
case T_DOUBLE_DENSE:
case T_DOUBLE_SPARSE:
{
splitIntDoubleVector(hasher, matrixMeta, (IntDoubleVector) vector, dataParts);
break;
}
case T_FLOAT_DENSE:
case T_FLOAT_SPARSE:
{
splitIntFloatVector(hasher, matrixMeta, (IntFloatVector) vector, dataParts);
break;
}
case T_INT_DENSE:
case T_INT_SPARSE:
{
splitIntIntVector(hasher, matrixMeta, (IntIntVector) vector, dataParts);
break;
}
case T_LONG_DENSE:
case T_LONG_SPARSE:
{
splitIntLongVector(hasher, matrixMeta, (IntLongVector) vector, dataParts);
break;
}
case T_DOUBLE_SPARSE_LONGKEY:
{
splitLongDoubleVector(hasher, matrixMeta, (LongDoubleVector) vector, dataParts);
break;
}
case T_FLOAT_SPARSE_LONGKEY:
{
splitLongFloatVector(hasher, matrixMeta, (LongFloatVector) vector, dataParts);
break;
}
case T_INT_SPARSE_LONGKEY:
{
splitLongIntVector(hasher, matrixMeta, (LongIntVector) vector, dataParts);
break;
}
case T_LONG_SPARSE_LONGKEY:
{
splitLongLongVector(hasher, matrixMeta, (LongLongVector) vector, dataParts);
break;
}
default:
{
throw new UnsupportedOperationException("Unsupport vector type " + vector.getType());
}
}
for (int i = 0; i < dataParts.length; i++) {
if (dataParts[i] != null) {
dataParts[i].setRowId(vector.getRowId());
}
}
return dataParts;
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class RangeRouterUtils method splitIntFloatVector.
public static KeyValuePart[] splitIntFloatVector(MatrixMeta matrixMeta, IntFloatVector vector) {
IntFloatVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Get keys and values
IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
int[] keys = sparseStorage.getIndices();
float[] values = sparseStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, false);
} else if (storage.isDense()) {
// Get values
IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
float[] values = denseStorage.getValues();
return split(matrixMeta, vector.getRowId(), values);
} else {
// Key and value array pair
IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
float[] values = sortStorage.getValues();
return split(matrixMeta, vector.getRowId(), keys, values, true);
}
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class PytorchPSFTask method run.
@Override
public void run(TaskContext taskContext) throws AngelException {
int feaNum = taskContext.getConf().getInt("col", 100000000);
int batchNNZ = taskContext.getConf().getInt("batch.nnz", 10000);
int updateTime = 0;
long startTs = System.currentTimeMillis();
try {
while (true) {
int[] indices = genIndexs(feaNum, batchNNZ);
IntFloatVector deltaVec = VFactory.sparseFloatVector(feaNum, batchNNZ);
for (int i = 0; i < indices.length; i++) {
deltaVec.set(indices[i], 1);
}
Vector[] updates = new Vector[1];
updates[0] = deltaVec;
MatrixClient client = taskContext.getMatrix("psf_test");
client.asyncUpdate(new IncrementRows(new IncrementRowsParam(client.getMatrixId(), updates))).get();
updateTime++;
if (updateTime % 100 == 0) {
LOG.info("update num = " + updateTime + ", avg update time=" + (System.currentTimeMillis() - startTs) / updateTime);
}
}
} catch (Throwable ie) {
}
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class InitNodeFeats method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
InitNodeFeatsPartParam param = (InitNodeFeatsPartParam) partParam;
ServerLongAnyRow row = (ServerLongAnyRow) psContext.getMatrixStorageManager().getRow(param.getPartKey(), 0);
long[] nodeIds = param.getNodeIds();
IntFloatVector[] feats = param.getFeats();
row.startWrite();
try {
for (int i = 0; i < nodeIds.length; i++) {
Node node = (Node) row.get(nodeIds[i]);
if (node == null) {
node = new Node();
row.set(nodeIds[i], node);
}
node.setFeats(feats[i]);
}
} finally {
row.endWrite();
}
}
use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntFloatVector v2, Binary op) {
IntFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
float[] 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;
}
Aggregations