use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project MinecraftForge by MinecraftForge.
the class VillagerTradingManager method postVillagerEvents.
/**
* Posts a VillagerTradesEvent for each registered profession.
*/
private static void postVillagerEvents() {
for (VillagerProfession prof : ForgeRegistries.PROFESSIONS) {
Int2ObjectMap<ItemListing[]> trades = VANILLA_TRADES.getOrDefault(prof, new Int2ObjectOpenHashMap<>());
Int2ObjectMap<List<ItemListing>> mutableTrades = new Int2ObjectOpenHashMap<>();
for (int i = 1; i < 6; i++) {
mutableTrades.put(i, NonNullList.create());
}
trades.int2ObjectEntrySet().forEach(e -> {
Arrays.stream(e.getValue()).forEach(mutableTrades.get(e.getIntKey())::add);
});
MinecraftForge.EVENT_BUS.post(new VillagerTradesEvent(mutableTrades, prof));
Int2ObjectMap<ItemListing[]> newTrades = new Int2ObjectOpenHashMap<>();
mutableTrades.int2ObjectEntrySet().forEach(e -> newTrades.put(e.getIntKey(), e.getValue().toArray(new ItemListing[0])));
VillagerTrades.TRADES.put(prof, newTrades);
}
}
use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project angel by Tencent.
the class MasterClient method getTaskMatrixClocks.
/**
* Get task clocks for all matrices from Master
*
* @return task clocks for all matrices from Master
* @throws ServiceException
*/
public Int2ObjectOpenHashMap<Int2IntOpenHashMap> getTaskMatrixClocks() throws ServiceException {
GetTaskMatrixClockResponse response = masterProxy.getTaskMatrixClocks(null, GetTaskMatrixClockRequest.newBuilder().build());
Int2ObjectOpenHashMap<Int2IntOpenHashMap> taskIdToMatrixClocksMap = new Int2ObjectOpenHashMap<>(response.getTaskMatrixClocksCount());
List<TaskMatrixClock> taskMatrixClocks = response.getTaskMatrixClocksList();
int size = taskMatrixClocks.size();
int matrixNum;
for (int i = 0; i < size; i++) {
Int2IntOpenHashMap matrixIdToClockMap = new Int2IntOpenHashMap(taskMatrixClocks.get(i).getMatrixClocksCount());
taskIdToMatrixClocksMap.put(taskMatrixClocks.get(i).getTaskId().getTaskIndex(), matrixIdToClockMap);
List<MatrixClock> matrixClocks = taskMatrixClocks.get(i).getMatrixClocksList();
matrixNum = matrixClocks.size();
for (int j = 0; j < matrixNum; j++) {
matrixIdToClockMap.put(matrixClocks.get(j).getMatrixId(), matrixClocks.get(j).getClock());
}
}
return taskIdToMatrixClocksMap;
}
use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project angel by Tencent.
the class InitNeighborTest method testCSR.
@Test
public void testCSR() throws Exception {
Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
MatrixClient client = worker.getPSAgent().getMatrixClient(SPARSE_INT_MAT, 0);
int matrixId = client.getMatrixId();
ParameterServer ps = LocalClusterContext.get().getPS(psAttempt0Id).getPS();
Location masterLoc = LocalClusterContext.get().getMaster().getAppMaster().getAppContext().getMasterService().getLocation();
TConnection connection = TConnectionManager.getConnection(ps.getConf());
MasterProtocol master = connection.getMasterService(masterLoc.getIp(), masterLoc.getPort());
// Init node neighbors
Int2ObjectOpenHashMap<int[]> nodeIdToNeighbors = new Int2ObjectOpenHashMap<>();
nodeIdToNeighbors.put(1, new int[] { 2, 3 });
nodeIdToNeighbors.put(2, new int[] { 4 });
InitNeighbor func = new InitNeighbor(new InitNeighborParam(matrixId, nodeIdToNeighbors));
client.asyncUpdate(func).get();
nodeIdToNeighbors.clear();
nodeIdToNeighbors.put(1, new int[] { 4, 5, 6 });
nodeIdToNeighbors.put(2, new int[] { 5 });
nodeIdToNeighbors.put(4, new int[] { 5, 6 });
func = new InitNeighbor(new InitNeighborParam(matrixId, nodeIdToNeighbors));
client.asyncUpdate(func).get();
nodeIdToNeighbors.clear();
nodeIdToNeighbors.put(3, new int[] { 4, 5, 6 });
nodeIdToNeighbors.put(5, new int[] { 6 });
nodeIdToNeighbors.put(8, new int[] { 3, 4 });
func = new InitNeighbor(new InitNeighborParam(matrixId, nodeIdToNeighbors));
client.asyncUpdate(func).get();
nodeIdToNeighbors.clear();
client.asyncUpdate(new InitNeighborOver(new InitNeighborOverParam(matrixId))).get();
// Sample the neighbors
int[] nodeIds = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
SampleNeighborParam param = new SampleNeighborParam(matrixId, nodeIds, -1);
Int2ObjectOpenHashMap<int[]> result = ((SampleNeighborResult) (client.get(new SampleNeighbor(param)))).getNodeIdToNeighbors();
ObjectIterator<Entry<int[]>> iter = result.int2ObjectEntrySet().fastIterator();
LOG.info("==============================sample neighbors result============================");
Entry<int[]> entry;
while (iter.hasNext()) {
entry = iter.next();
LOG.info("node id = " + entry.getIntKey() + ", neighbors = " + Arrays.toString(entry.getValue()));
}
client.checkpoint(0);
ps.stop(-1);
PSErrorRequest request = PSErrorRequest.newBuilder().setPsAttemptId(ProtobufUtil.convertToIdProto(psAttempt0Id)).setMsg("out of memory").build();
master.psError(null, request);
Thread.sleep(10000);
result = ((SampleNeighborResult) (client.get(new SampleNeighbor(param)))).getNodeIdToNeighbors();
iter = result.int2ObjectEntrySet().fastIterator();
LOG.info("==============================sample neighbors result============================");
while (iter.hasNext()) {
entry = iter.next();
LOG.info("node id = " + entry.getIntKey() + ", neighbors = " + Arrays.toString(entry.getValue()));
}
}
use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap 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);
}
Aggregations