docstr: parallel reduce on binary tree
  
  if flat:
      value is an entry in the sequence
  else:
      value is a list of entries in the sequence
  
  if all:
      broadcast final result to all nodes
  else:
      only root gets final result
docstr_tok: ["parallel","reduce","on","binary","tree","if","flat",":","value","is","an","entry","in","the","sequence","else",":","value","is","a","list","of","entries","in","the","sequence","if","all",":","broadcast","final","result","to","all","nodes","else",":","only","root","gets","final","result"]
code: def reduce(self, f, value, flat=True, all=False):
      """parallel reduce on binary tree
  
      if flat:
          value is an entry in the sequence
      else:
          value is a list of entries in the sequence
  
      if all:
          broadcast final result to all nodes
      else:
          only root gets final result
      """
      if not flat:
          value = reduce(f, value)
  
      for i in range(self.nchildren):
          value = f(value, self.recv_downstream())
  
      if not self.root:
          self.send_upstream(value)
  
      if all:
          if self.root:
              self.publish(value)
          else:
              value = self.consume()
      return value
  
imports: import re
  import socket
  from functools import reduce
  import zmq
  from ipyparallel.serialize import deserialize_object, serialize_object
  from ipyparallel.util import disambiguate_url
label_desc: Use image node_id 11 for calling the BinaryTreeCommunicator obj's underlying member method code with example usage: obj.reduce(f, value, flat, all) and returns: value