Search in sources :

Example 26 with Int2ObjectOpenHashMap

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);
    }
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) VillagerProfession(net.minecraft.world.entity.npc.VillagerProfession) List(java.util.List) NonNullList(net.minecraft.core.NonNullList) VillagerTradesEvent(net.minecraftforge.event.village.VillagerTradesEvent)

Example 27 with Int2ObjectOpenHashMap

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;
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) MatrixClock(com.tencent.angel.protobuf.generated.MLProtos.MatrixClock) Int2IntOpenHashMap(it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap)

Example 28 with Int2ObjectOpenHashMap

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()));
    }
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InitNeighborParam(com.tencent.angel.graph.client.initneighbor.InitNeighborParam) SampleNeighborResult(com.tencent.angel.graph.client.sampleneighbor.SampleNeighborResult) SampleNeighbor(com.tencent.angel.graph.client.sampleneighbor.SampleNeighbor) InitNeighbor(com.tencent.angel.graph.client.initneighbor.InitNeighbor) ParameterServer(com.tencent.angel.ps.ParameterServer) Entry(it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry) TConnection(com.tencent.angel.ipc.TConnection) InitNeighborOverParam(com.tencent.angel.graph.client.initneighbor.InitNeighborOverParam) SampleNeighborParam(com.tencent.angel.graph.client.sampleneighbor.SampleNeighborParam) Worker(com.tencent.angel.worker.Worker) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) InitNeighborOver(com.tencent.angel.graph.client.initneighbor.InitNeighborOver) MasterProtocol(com.tencent.angel.master.MasterProtocol) PSErrorRequest(com.tencent.angel.protobuf.generated.PSMasterServiceProtos.PSErrorRequest) Location(com.tencent.angel.common.location.Location) Test(org.junit.Test)

Example 29 with Int2ObjectOpenHashMap

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);
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Random(java.util.Random) ServerMatrix(com.tencent.angel.ps.storage.matrix.ServerMatrix) IntCSRStorage(com.tencent.angel.ps.storage.partition.storage.IntCSRStorage) CSRPartition(com.tencent.angel.ps.storage.partition.CSRPartition)

Aggregations

Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)29 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)8 List (java.util.List)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Int2IntOpenHashMap (it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap)5 HashMap (java.util.HashMap)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 Preconditions (com.google.common.base.Preconditions)2 Stopwatch (com.google.common.base.Stopwatch)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Maps (com.google.common.collect.Maps)2 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)2 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)2 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)2 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)2 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2