Example usage for org.apache.hadoop.io FloatWritable FloatWritable

List of usage examples for org.apache.hadoop.io FloatWritable FloatWritable

Introduction

In this page you can find the example usage for org.apache.hadoop.io FloatWritable FloatWritable.

Prototype

public FloatWritable(float value) 

Source Link

Usage

From source file:org.apache.giraph.block_app.library.prepare_graph.WeaklyConnectedComponents.java

License:Apache License

/**
 * This is just used internally in weakly connected components if the graph
 * has float weights. This works fine here because we don't actually ever
 * use the weights, but should not be used outside of the
 * WeaklyConnectedComponents class and hence is private.
 *///w  w  w.  j av  a 2 s . c o  m
private static <V extends Writable> Piece<LongWritable, V, FloatWritable, LongWritable, Object> makeSymmetricFloatWeighted() {
    LongSet set = new LongOpenHashSet();
    FloatWritable floatWritable = new FloatWritable(1.0f);
    ConsumerWithVertex<LongWritable, V, FloatWritable, Iterable<LongWritable>> addEdges = (vertex,
            neighbors) -> {
        set.clear();
        for (Edge<LongWritable, FloatWritable> edge : vertex.getEdges()) {
            set.add(edge.getTargetVertexId().get());
        }
        for (LongWritable message : neighbors) {
            if (!set.contains(message.get())) {
                Edge<LongWritable, FloatWritable> edge = EdgeFactory.create(new LongWritable(message.get()),
                        floatWritable);
                vertex.addEdge(edge);
                set.add(message.get());
            }
        }
    };
    return Pieces.sendMessageToNeighbors("MakeSymmetricFloatWeighted", LongWritable.class,
            VertexSuppliers.<LongWritable, V, FloatWritable>vertexIdSupplier(), addEdges);
}

From source file:org.apache.giraph.block_app.test_setup.NumericTestGraph.java

License:Apache License

private static Function<Number, FloatWritable> numberToFloat() {
    return new Function<Number, FloatWritable>() {
        @Override/*from   w w  w. j  av a2  s . c om*/
        public FloatWritable apply(Number input) {
            return new FloatWritable(input.floatValue());
        }
    };
}

From source file:org.apache.giraph.combiner.FloatSumCombiner.java

License:Apache License

@Override
public FloatWritable createInitialMessage() {
    return new FloatWritable(0);
}

From source file:org.apache.giraph.comm.messages.primitives.IntFloatMessageStore.java

License:Apache License

@Override
public Iterable<FloatWritable> getVertexMessages(IntWritable vertexId) throws IOException {
    Int2FloatOpenHashMap partitionMap = getPartitionMap(vertexId);
    if (!partitionMap.containsKey(vertexId.get())) {
        return EmptyIterable.get();
    } else {/* w w  w .j  a va2  s.  com*/
        return Collections.singleton(new FloatWritable(partitionMap.get(vertexId.get())));
    }
}

From source file:org.apache.giraph.comm.messages.TestIntFloatPrimitiveMessageStores.java

License:Apache License

private static void insertIntFloatMessages(MessageStore<IntWritable, FloatWritable> messageStore)
        throws IOException {
    ByteArrayVertexIdMessages<IntWritable, FloatWritable> messages = createIntFloatMessages();
    messages.add(new IntWritable(0), new FloatWritable(1));
    messages.add(new IntWritable(2), new FloatWritable(3));
    messages.add(new IntWritable(0), new FloatWritable(4));
    messageStore.addPartitionMessages(0, messages);
    messages = createIntFloatMessages();
    messages.add(new IntWritable(1), new FloatWritable(1));
    messages.add(new IntWritable(1), new FloatWritable(3));
    messages.add(new IntWritable(1), new FloatWritable(4));
    messageStore.addPartitionMessages(1, messages);
    messages = createIntFloatMessages();
    messages.add(new IntWritable(0), new FloatWritable(5));
    messageStore.addPartitionMessages(0, messages);
}

From source file:org.apache.giraph.examples.SimpleMutateGraphComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, DoubleWritable, FloatWritable> vertex,
        Iterable<DoubleWritable> messages) throws IOException {
    SimpleMutateGraphVertexWorkerContext workerContext = getWorkerContext();
    if (getSuperstep() == 0) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 1) {
        // Send messages to vertices that are sure not to exist
        // (creating them)
        LongWritable destVertexId = new LongWritable(rangeVertexIdStart(1) + vertex.getId().get());
        sendMessage(destVertexId, new DoubleWritable(0.0));
    } else if (getSuperstep() == 2) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 3) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount * 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices()
                    + " vertices when should have " + vertexCount * 2 + " on superstep " + getSuperstep());
        }//from w  w  w .j  a v  a 2  s  .com
        long edgeCount = workerContext.getEdgeCount();
        if (edgeCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + edgeCount + " on superstep " + getSuperstep());
        }
        // Create vertices that are sure not to exist (doubling vertices)
        LongWritable vertexIndex = new LongWritable(rangeVertexIdStart(3) + vertex.getId().get());
        addVertexRequest(vertexIndex, new DoubleWritable(0.0));
        // Add edges to those remote vertices as well
        addEdgeRequest(vertexIndex, EdgeFactory.create(vertex.getId(), new FloatWritable(0.0f)));
    } else if (getSuperstep() == 4) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 5) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount * 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices() + " when should have "
                    + vertexCount * 2 + " on superstep " + getSuperstep());
        }
        long edgeCount = workerContext.getEdgeCount();
        if (edgeCount + vertexCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + edgeCount + vertexCount + " on superstep " + getSuperstep());
        }
        // Remove the edges created in superstep 3
        LongWritable vertexIndex = new LongWritable(rangeVertexIdStart(3) + vertex.getId().get());
        workerContext.increaseEdgesRemoved();
        removeEdgesRequest(vertexIndex, vertex.getId());
    } else if (getSuperstep() == 6) {
        // Remove all the vertices created in superstep 3
        if (vertex.getId().compareTo(new LongWritable(rangeVertexIdStart(3))) >= 0) {
            removeVertexRequest(vertex.getId());
        }
    } else if (getSuperstep() == 7) {
        long origEdgeCount = workerContext.getOrigEdgeCount();
        if (origEdgeCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + origEdgeCount + " on superstep " + getSuperstep());
        }
    } else if (getSuperstep() == 8) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount / 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices()
                    + " vertices when should have " + vertexCount / 2 + " on superstep " + getSuperstep());
        }
    } else {
        vertex.voteToHalt();
    }
}

From source file:org.apache.giraph.examples.SimpleMutateGraphVertex.java

License:Apache License

@Override
public void compute(Iterable<DoubleWritable> messages) throws IOException {
    SimpleMutateGraphVertexWorkerContext workerContext = (SimpleMutateGraphVertexWorkerContext) getWorkerContext();
    if (getSuperstep() == 0) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 1) {
        // Send messages to vertices that are sure not to exist
        // (creating them)
        LongWritable destVertexId = new LongWritable(rangeVertexIdStart(1) + getId().get());
        sendMessage(destVertexId, new DoubleWritable(0.0));
    } else if (getSuperstep() == 2) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 3) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount * 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices()
                    + " vertices when should have " + vertexCount * 2 + " on superstep " + getSuperstep());
        }//from   w w  w.  jav  a2 s.c  o m
        long edgeCount = workerContext.getEdgeCount();
        if (edgeCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + edgeCount + " on superstep " + getSuperstep());
        }
        // Create vertices that are sure not to exist (doubling vertices)
        LongWritable vertexIndex = new LongWritable(rangeVertexIdStart(3) + getId().get());
        addVertexRequest(vertexIndex, new DoubleWritable(0.0));
        // Add edges to those remote vertices as well
        addEdgeRequest(vertexIndex, EdgeFactory.create(getId(), new FloatWritable(0.0f)));
    } else if (getSuperstep() == 4) {
        LOG.debug("Reached superstep " + getSuperstep());
    } else if (getSuperstep() == 5) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount * 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices() + " when should have "
                    + vertexCount * 2 + " on superstep " + getSuperstep());
        }
        long edgeCount = workerContext.getEdgeCount();
        if (edgeCount + vertexCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + edgeCount + vertexCount + " on superstep " + getSuperstep());
        }
        // Remove the edges created in superstep 3
        LongWritable vertexIndex = new LongWritable(rangeVertexIdStart(3) + getId().get());
        workerContext.increaseEdgesRemoved();
        removeEdgesRequest(vertexIndex, getId());
    } else if (getSuperstep() == 6) {
        // Remove all the vertices created in superstep 3
        if (getId().compareTo(new LongWritable(rangeVertexIdStart(3))) >= 0) {
            removeVertexRequest(getId());
        }
    } else if (getSuperstep() == 7) {
        long origEdgeCount = workerContext.getOrigEdgeCount();
        if (origEdgeCount != getTotalNumEdges()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumEdges()
                    + " edges when should have " + origEdgeCount + " on superstep " + getSuperstep());
        }
    } else if (getSuperstep() == 8) {
        long vertexCount = workerContext.getVertexCount();
        if (vertexCount / 2 != getTotalNumVertices()) {
            throw new IllegalStateException("Impossible to have " + getTotalNumVertices()
                    + " vertices when should have " + vertexCount / 2 + " on superstep " + getSuperstep());
        }
    } else {
        voteToHalt();
    }
}

From source file:org.apache.giraph.examples.SimpleShortestPathsComputationTest.java

License:Apache License

/**
 * Test the behavior when a shorter path to a vertex has been found
 *///from   w  w w . j  a v  a 2 s  .c om
@Test
public void testOnShorterPathFound() throws Exception {
    Vertex<LongWritable, DoubleWritable, FloatWritable> vertex = new DefaultVertex<LongWritable, DoubleWritable, FloatWritable>();
    SimpleShortestPathsComputation computation = new SimpleShortestPathsComputation();
    MockUtils.MockedEnvironment<LongWritable, DoubleWritable, FloatWritable, DoubleWritable> env = MockUtils
            .prepareVertexAndComputation(vertex, new LongWritable(7L), new DoubleWritable(Double.MAX_VALUE),
                    false, computation, 1L);
    Mockito.when(SOURCE_ID.get(env.getConfiguration())).thenReturn(2L);

    vertex.addEdge(EdgeFactory.create(new LongWritable(10L), new FloatWritable(2.5f)));
    vertex.addEdge(EdgeFactory.create(new LongWritable(20L), new FloatWritable(0.5f)));

    computation.compute(vertex, Lists.newArrayList(new DoubleWritable(2), new DoubleWritable(1.5)));

    assertTrue(vertex.isHalted());
    assertEquals(1.5d, vertex.getValue().get(), 0d);

    env.verifyMessageSent(new LongWritable(10L), new DoubleWritable(4));
    env.verifyMessageSent(new LongWritable(20L), new DoubleWritable(2));
}

From source file:org.apache.giraph.examples.SimpleShortestPathsComputationTest.java

License:Apache License

/**
 * Test the behavior when a new, but not shorter path to a vertex has been
 * found.//w w w.j a  va  2s  .co  m
 */
@Test
public void testOnNoShorterPathFound() throws Exception {
    Vertex<LongWritable, DoubleWritable, FloatWritable> vertex = new DefaultVertex<LongWritable, DoubleWritable, FloatWritable>();
    SimpleShortestPathsComputation computation = new SimpleShortestPathsComputation();
    MockUtils.MockedEnvironment<LongWritable, DoubleWritable, FloatWritable, DoubleWritable> env = MockUtils
            .prepareVertexAndComputation(vertex, new LongWritable(7L), new DoubleWritable(0.5), false,
                    computation, 1L);
    Mockito.when(SOURCE_ID.get(env.getConfiguration())).thenReturn(2L);

    vertex.addEdge(EdgeFactory.create(new LongWritable(10L), new FloatWritable(2.5f)));
    vertex.addEdge(EdgeFactory.create(new LongWritable(20L), new FloatWritable(0.5f)));

    computation.compute(vertex, Lists.newArrayList(new DoubleWritable(2), new DoubleWritable(1.5)));

    assertTrue(vertex.isHalted());
    assertEquals(0.5d, vertex.getValue().get(), 0d);

    env.verifyNoMessageSent();
}

From source file:org.apache.giraph.examples.SimpleShortestPathsVertexTest.java

License:Apache License

/**
 * Test the behavior when a shorter path to a vertex has been found
 *//*from   w w  w .  j  a v  a 2s  .  c  o  m*/
@Test
public void testOnShorterPathFound() throws Exception {
    SimpleShortestPathsVertex vertex = new SimpleShortestPathsVertex();

    MockUtils.MockedEnvironment<LongWritable, DoubleWritable, FloatWritable, DoubleWritable> env = MockUtils
            .prepareVertex(vertex, 1L, new LongWritable(7L), new DoubleWritable(Double.MAX_VALUE), false);

    Mockito.when(SOURCE_ID.get(env.getConfiguration())).thenReturn(2L);

    vertex.addEdge(EdgeFactory.create(new LongWritable(10L), new FloatWritable(2.5f)));
    vertex.addEdge(EdgeFactory.create(new LongWritable(20L), new FloatWritable(0.5f)));

    vertex.compute(Lists.newArrayList(new DoubleWritable(2), new DoubleWritable(1.5)));

    assertTrue(vertex.isHalted());
    assertEquals(1.5d, vertex.getValue().get(), 0d);

    env.verifyMessageSent(new LongWritable(10L), new DoubleWritable(4));
    env.verifyMessageSent(new LongWritable(20L), new DoubleWritable(2));
}