# dgl.DGLGraph.send_and_recv¶

DGLGraph.send_and_recv(edges, message_func='default', reduce_func='default', apply_node_func='default', inplace=False)[source]

Send messages along edges and let destinations receive them.

Optionally, apply a function to update the node features after receive.

This is a convenient combination for performing send(self, self.edges, message_func) and recv(self, dst, reduce_func, apply_node_func), where dst are the destinations of the edges.

Parameters: edges (valid edges type) – Edges on which to apply func. See send() for valid edges type. message_func (callable, optional) – Message function on the edges. The function should be an Edge UDF. reduce_func (callable, optional) – Reduce function on the node. The function should be a Node UDF. apply_node_func (callable, optional) – Apply function on the nodes. The function should be a Node UDF. inplace (bool, optional) – If True, update will be done in place, but autograd will break.

Examples

Note

Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> g = dgl.DGLGraph()
>>> g.ndata['x'] = th.tensor([[1.], [2.], [3.]])

>>> # Define the function for sending node features as messages.
>>> def send_source(edges): return {'m': edges.src['x']}
>>> # Set the function defined to be the default message function.
>>> g.register_message_func(send_source)

>>> # Sum the messages received and use this to replace the original node feature.
>>> def simple_reduce(nodes): return {'x': nodes.mailbox['m'].sum(1)}
>>> # Set the function defined to be the default message reduce function.
>>> g.register_reduce_func(simple_reduce)


>>> g.send_and_recv(g.edges())
>>> g.ndata['x']
tensor([[1.],
[1.],
[2.]])


Note that the feature of node $$0$$ remains the same as it has no incoming edges.

Notes

On multigraphs, if u and v are specified, then the messages will be sent and received along all edges between u and v.