|
| 1 | +[[algorithms-pregel-api]] |
| 2 | += Pregel API |
| 3 | + |
| 4 | +[abstract] |
| 5 | +-- |
| 6 | +This chapter provides explanations and examples for using the Pregel API in the Neo4j Graph Data Science library. |
| 7 | +-- |
| 8 | + |
| 9 | +[[algorithms-pregel-api-intro]] |
| 10 | +== Introduction |
| 11 | + |
| 12 | +Pregel is a vertex-centric computation model to define your own algorithms via a user-defined _compute_ function. |
| 13 | +Node values can be updated within the compute function and represent the algorithm result. |
| 14 | +The input graph contains default node values or node values from a graph projection. |
| 15 | + |
| 16 | +The compute function is executed in multiple iterations, also called _super-steps_. |
| 17 | +In each super-step the compute function is executed for each node in the graph. |
| 18 | +Within that function, a node can receive messages from its neighbor nodes. |
| 19 | +Based on the received messages and its currently stored value, a node can compute a new value. |
| 20 | +A node can also send messages to all of its neighbors which are received in the next super-step. |
| 21 | +The algorithm terminates after a fixed number of super-steps or if no messages are being sent between nodes. |
| 22 | + |
| 23 | +A Pregel computation is executed in parallel. |
| 24 | +Each thread executes the compute function for a batch of nodes. |
| 25 | + |
| 26 | +For more information about Pregel, have a look at https://kowshik.github.io/JPregel/pregel_paper.pdf. |
| 27 | + |
| 28 | +To implement your own Pregel algorithm, the Graph Data Science library provides a Java API, which is described below. |
| 29 | + |
| 30 | +For an example on how to expose a custom Pregel computation via a Neo4j procedure, have a look at the https://github.com/neo-technology/graph-analytics/tree/master/public/examples/PregelK1Coloring[K1-Coloring example]. |
| 31 | + |
| 32 | + |
| 33 | +== Pregel API |
| 34 | +.Initializing Pregel |
| 35 | +[source, java] |
| 36 | +---- |
| 37 | +package org.neo4j.graphalgo.beta.pregel; |
| 38 | +
|
| 39 | +public final class Pregel { |
| 40 | + // constructing an instance of Pregel |
| 41 | + public static Pregel withDefaultNodeValues( |
| 42 | + final Graph graph, |
| 43 | + final PregelConfig config, |
| 44 | + final PregelComputation computation, |
| 45 | + final int batchSize, |
| 46 | + final ExecutorService executor, |
| 47 | + final AllocationTracker tracker |
| 48 | + ) {...} |
| 49 | +
|
| 50 | + // running the Pregel instance to get node values as result |
| 51 | + public HugeDoubleArray run(final int maxIterations) {...} |
| 52 | +} |
| 53 | +---- |
| 54 | + |
| 55 | +To build a PregelConfig you can use the `ImmutablePregelConfig.builder()`. |
| 56 | + |
| 57 | +.Pregel Config |
| 58 | +[opts="header",cols="1,1,1,6"] |
| 59 | +|=== |
| 60 | +| Name | Type | Default Value | Description |
| 61 | +| initialNodeValue | Double | -1 | Initial value of the node in the Pregel context. |
| 62 | +| isAsynchronous | Boolean | false | Flag indicating if messages can be sent and received in the same super-step. |
| 63 | +| relationshipWeightProperty| String | null | Name of the relationship property that represents weight. |
| 64 | +| concurrency | Integer | 4 | Concurrency used when executing the Pregel computation. |
| 65 | +|=== |
| 66 | + |
| 67 | +To implement your own algorithm, you need to implement the `PregelComputation` interface. |
| 68 | + |
| 69 | +.The Pregel computation |
| 70 | +[source, java] |
| 71 | +---- |
| 72 | +@FunctionalInterface |
| 73 | +public interface PregelComputation { |
| 74 | + // specifying the algorithm logic. |
| 75 | + void compute(PregelContext context, long nodeId, Queue<Double> messages); |
| 76 | + // how relationship weights should be applied on the message |
| 77 | + default double applyRelationshipWeight(double nodeValue, double relationshipWeight) { return nodeValue; } |
| 78 | +} |
| 79 | +---- |
| 80 | + |
| 81 | +The compute function takes a context, the node id for which the method is being executed for, and the messages that were sent to that node. |
| 82 | +Using the context and the node id, one can access the current super-step, read and update the node value, send messages or vote to halt the computation. |
| 83 | + |
| 84 | +.The Pregel context |
| 85 | +[source, java] |
| 86 | +---- |
| 87 | +public final class PregelContext { |
| 88 | + // nodes voting to halt will be inactive and accept no new messages |
| 89 | + public void voteToHalt(long nodeId) {...}; |
| 90 | + // if its the first iteration |
| 91 | + public boolean isInitialSuperStep() {...}; |
| 92 | + // get the number of the current iteration |
| 93 | + public int getSuperstep() {...}; |
| 94 | + public double getNodeValue(long nodeId) {...}; |
| 95 | + public void setNodeValue(long nodeId, double value) {...}; |
| 96 | + // sending a message to the neighbours of a node |
| 97 | + public void sendMessages(long nodeId, double message) {...}; |
| 98 | + public int getDegree(long nodeId) {...}; |
| 99 | + // get the inital node value given by the PregelConfig |
| 100 | + public double getInitialNodeValue() {...}; |
| 101 | +} |
| 102 | +---- |
| 103 | + |
| 104 | + |
| 105 | +[[algorithms-pregel-api-example]] |
| 106 | +== Example |
| 107 | + |
| 108 | +.The following provides an example of Pregel computation: |
| 109 | +[source, java] |
| 110 | +---- |
| 111 | +import org.neo4j.graphalgo.beta.pregel.PregelComputation; |
| 112 | +import org.neo4j.graphalgo.beta.pregel.PregelContext; |
| 113 | +
|
| 114 | +import java.util.Queue; |
| 115 | +
|
| 116 | +public class ConnectedComponentsPregel implements PregelComputation { |
| 117 | +
|
| 118 | + @Override |
| 119 | + public void compute(PregelContext context, long nodeId, Queue<Double> messages) { |
| 120 | + // get the current componentId for the node from the context |
| 121 | + // if we are on the first iteration, the value is the default value from the PregelConfig |
| 122 | + // which we do not use |
| 123 | + double oldComponentId = context.getNodeValue(nodeId); |
| 124 | + double newComponentId = oldComponentId; |
| 125 | + if (context.isInitialSuperStep()) { |
| 126 | + // In the first round, we use use the nodeId as component instead of the default -1 |
| 127 | + newComponentId = nodeId; |
| 128 | + // need to check if there are any messages for this node |
| 129 | + } else if (messages != null && !messages.isEmpty()){ |
| 130 | + // the componentId is updated to the smallest componentId of its neighbors including itself |
| 131 | + Double nextComponentId; |
| 132 | + while ((nextComponentId = messages.poll()) != null) { |
| 133 | + if (nextComponentId.longValue() < newComponentId) { |
| 134 | + newComponentId = nextComponentId.longValue(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +
|
| 139 | + // update the node's componentId, both in the context and notify neighbors |
| 140 | + if (newComponentId != oldComponentId) { |
| 141 | + context.setNodeValue(nodeId, newComponentId); |
| 142 | + // send the new componentId to neighbors so that they also can be updated |
| 143 | + context.sendMessages(nodeId, newComponentId); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +---- |
| 148 | + |
| 149 | +.The following runs Pregel, using `ConnectedComponentsPregel` |
| 150 | +[source, java] |
| 151 | +---- |
| 152 | +import org.neo4j.graphalgo.core.utils.paged.HugeDoubleArray; |
| 153 | +import org.neo4j.graphalgo.core.concurrency.Pools; |
| 154 | +import org.neo4j.graphalgo.core.utils.paged.AllocationTracker; |
| 155 | +import org.neo4j.graphalgo.config.AlgoBaseConfig; |
| 156 | +
|
| 157 | +import org.neo4j.graphalgo.beta.pregel.ImmutablePregelConfig; |
| 158 | +import org.neo4j.graphalgo.beta.pregel.Pregel; |
| 159 | +import org.neo4j.graphalgo.beta.pregel.PregelConfig; |
| 160 | +import org.neo4j.graphalgo.beta.generator.RandomGraphGenerator; |
| 161 | +
|
| 162 | +
|
| 163 | +public class PregelExample { |
| 164 | + public static void main(String[] args) { |
| 165 | + int batchSize = 10; |
| 166 | + int maxIterations = 10; |
| 167 | +
|
| 168 | + PregelConfig config = ImmutablePregelConfig.builder() |
| 169 | + .isAsynchronous(true) |
| 170 | + .build(); |
| 171 | +
|
| 172 | + Pregel pregelJob = Pregel.withDefaultNodeValues( |
| 173 | + // generate a random graph with 100 nodes and average degree 10 |
| 174 | + RandomGraphGenerator.generate(100, 10), |
| 175 | + config, |
| 176 | + new ConnectedComponentsPregel(), |
| 177 | + batchSize, |
| 178 | + // run on the default GDS ExecutorService |
| 179 | + Pools.DEFAULT, |
| 180 | + // disable memory allocation tracking |
| 181 | + AllocationTracker.EMPTY |
| 182 | + ); |
| 183 | +
|
| 184 | + // the index in the nodeValues array is the nodeId from the graph |
| 185 | + HugeDoubleArray nodeValues = pregelJob.run(maxIterations); |
| 186 | + System.out.println(nodeValues.toString()); |
| 187 | + } |
| 188 | +} |
| 189 | +---- |
0 commit comments