use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.
the class SampleNeighbor method partitionGet.
@Override
public PartitionGetResult partitionGet(PartitionGetParam partParam) {
PartSampleNeighborParam param = (PartSampleNeighborParam) partParam;
ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
CSRPartition part = (CSRPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
IntCSRStorage storage = (IntCSRStorage) (part.getStorage());
Int2ObjectOpenHashMap<int[]> results = new Int2ObjectOpenHashMap<>();
int[] neighborOffsets = storage.getRowOffsets();
int[] neighbors = storage.getColumnIndices();
int startCol = (int) partParam.getPartKey().getStartCol();
int[] nodeIds = param.getNodeIds();
int count = param.getCount();
Random r = new Random();
for (int i = 0; i < nodeIds.length; i++) {
int nodeId = nodeIds[i];
// Get node neighbor number
int num = neighborOffsets[nodeId - startCol + 1] - neighborOffsets[nodeId - startCol];
int[] result;
if (num == 0) {
// If the neighbor number is 0, just return a int[0]
result = new int[0];
} else if (count <= 0 || num <= count) {
// If count <= 0 or the neighbor number is less or equal then count, just copy all neighbors to the result array
result = new int[num];
System.arraycopy(neighbors, neighborOffsets[nodeId - startCol], result, 0, num);
} else {
// If the neighbor number > count, just copy a range of neighbors to the result array, the copy position is random
int startPos = Math.abs(r.nextInt()) % num;
result = new int[count];
if (startPos + count <= num) {
System.arraycopy(neighbors, neighborOffsets[nodeId - startCol] + startPos, result, 0, count);
} else {
System.arraycopy(neighbors, neighborOffsets[nodeId - startCol] + startPos, result, 0, num - startPos);
System.arraycopy(neighbors, neighborOffsets[nodeId - startCol], result, num - startPos, count - (num - startPos));
}
}
results.put(nodeIds[i], result);
}
return new PartSampleNeighborResult(results);
}
use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.
the class InitNeighborAliasTable method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
PartInitNeighborAliasTableParam param = (PartInitNeighborAliasTableParam) partParam;
ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
RowBasedPartition part = (RowBasedPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
ServerLongAnyRow row = (ServerLongAnyRow) part.getRow(0);
ObjectIterator<Long2ObjectMap.Entry<NeighborsAliasTableElement>> iter = param.getNodeId2Neighbors().long2ObjectEntrySet().iterator();
row.startWrite();
try {
while (iter.hasNext()) {
Long2ObjectMap.Entry<NeighborsAliasTableElement> entry = iter.next();
NeighborsAliasTableElement element = entry.getValue();
if (element == null) {
row.set(entry.getLongKey(), null);
} else {
row.set(entry.getLongKey(), new NeighborsAliasTableElement(element.getNeighborIds(), element.getAccept(), element.getAlias()));
}
}
} finally {
row.endWrite();
}
}
use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.
the class GetNeighborAliasTable method partitionGet.
@Override
public PartitionGetResult partitionGet(PartitionGetParam partParam) {
PartGetNeighborAliasTableParam param = (PartGetNeighborAliasTableParam) partParam;
ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
ServerPartition part = matrix.getPartition(partParam.getPartKey().getPartitionId());
ServerLongAnyRow row = (ServerLongAnyRow) (((RowBasedPartition) part).getRow(0));
long[] nodeIds = param.getNodeIds();
long[][] neighbors = new long[nodeIds.length][];
int[] count = param.getCount();
Random r = new Random();
for (int i = 0; i < nodeIds.length; i++) {
long nodeId = nodeIds[i];
// Get node neighbor number
NeighborsAliasTableElement element = (NeighborsAliasTableElement) (row.get(nodeId));
if (element == null) {
neighbors[i] = null;
} else {
long[] nodeNeighbors = element.getNeighborIds();
if (nodeNeighbors == null || nodeNeighbors.length == 0 || count[i] <= 0) {
neighbors[i] = null;
} else {
neighbors[i] = new long[count[i]];
// start sampling by alias table for count times
float[] accept = element.getAccept();
int[] alias = element.getAlias();
for (int j = 0; j < count[i]; j++) {
int index = r.nextInt(nodeNeighbors.length);
float ac = r.nextFloat();
if (ac < accept[index]) {
neighbors[i][j] = nodeNeighbors[index];
} else {
neighbors[i][j] = nodeNeighbors[alias[index]];
}
}
}
}
}
return new PartGetNeighborAliasTableResult(part.getPartitionKey().getPartitionId(), neighbors);
}
use of com.tencent.angel.ps.storage.matrix.ServerMatrix in project angel by Tencent.
the class InitOutDegreeFunc method partitionUpdate.
@Override
public void partitionUpdate(PartitionUpdateParam partParam) {
PartInitOutDegreeParam param = (PartInitOutDegreeParam) partParam;
ServerMatrix matrix = psContext.getMatrixStorageManager().getMatrix(partParam.getMatrixId());
RowBasedPartition part = (RowBasedPartition) matrix.getPartition(partParam.getPartKey().getPartitionId());
ServerLongIntRow row = (ServerLongIntRow) part.getRow(0);
ObjectIterator<Long2IntMap.Entry> iter = param.getNodeIdToOutDegree().long2IntEntrySet().iterator();
row.startWrite();
try {
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
row.set(entry.getLongKey(), entry.getIntValue());
}
} finally {
row.endWrite();
}
}
Aggregations