Example #1
0
def reduce(x, op, root, buffer=None, vanilla=0):
    """Reduce elements in x to buffer (of the same size as x)
       at root applying operation op elementwise.
    """

    import types

    from mpiext import size
    numproc = size()  #Needed to determine buffer size

    #Input check
    errmsg = 'Root id (%s) must be an integer.' % root
    assert type(root) == types.IntType, errmsg

    #Create metadata about object
    protocol, typecode, size, shape = create_control_info(x)

    #Reduce
    if protocol == 'array':
        if buffer is None:
            import Numeric
            buffer = Numeric.zeros(size * numproc, typecode)

            # Modify shape along axis=0 to match size
            shape = list(shape)
            shape[0] *= numproc
            buffer = Numeric.reshape(buffer, shape)

        reduce_array(x, buffer, op, root)
    elif (protocol == 'vanilla' or protocol == 'string'):
        raise 'Protocol: %s unsupported for reduce' % protocol
    else:
        raise 'Unknown protocol: %s' % protocol

    return buffer
Example #2
0
def reduce(x, op, root, buffer=None, vanilla=0, bypass=False):
    """Reduce elements in x to buffer (of the same size as x)
       at root applying operation op elementwise.

       If bypass is True, all admin and error checks
       get bypassed to reduce the latency.
       The buffer must be specified explicitly in this case.
    """

    if bypass:
        reduce_array(x, buffer, op, root)
        return


    import types
    from mpiext import size
    numproc = size()         # Needed to determine buffer size


    # Input check
    errmsg = 'Root id (%s) must be an integer.' % root
    assert type(root) == types.IntType, errmsg

    # Create metadata about object
    protocol, typecode, size, shape = create_control_info(x)

    # Reduce
    if protocol == 'array':
        if buffer is None:
            buffer = zeros(size*numproc, typecode)

            # Modify shape along axis=0 to match size
            shape = list(shape)
            shape[0] *= numproc
            buffer = reshape(buffer, shape)


        msg = 'Data array and buffer must have same type '
        msg = 'in reduce. I got types "%s" and "%s"' % (x.dtype.char,
                                                        buffer.dtype.char)
        assert x.dtype.char == buffer.dtype.char, msg
        reduce_array(x, buffer, op, root)


    elif (protocol == 'vanilla' or protocol == 'string'):
        raise 'Protocol: %s unsupported for reduce' % protocol
    else:
        raise 'Unknown protocol: %s' % protocol

    return buffer
Example #3
0
def reduce(x, op, root, buffer=None, vanilla=0, bypass=False):
    """Reduce elements in x to buffer (of the same size as x)
       at root applying operation op elementwise.

       If bypass is True, all admin and error checks
       get bypassed to reduce the latency.
       The buffer must be specified explicitly in this case.
    """

    if bypass:
        reduce_array(x, buffer, op, root)
        return

    import types
    from mpiext import size
    numproc = size()  # Needed to determine buffer size

    # Input check
    errmsg = 'Root id (%s) must be an integer.' % root
    assert type(root) == types.IntType, errmsg

    # Create metadata about object
    protocol, typecode, size, shape = create_control_info(x)

    # Reduce
    if protocol == 'array':
        if buffer is None:
            buffer = zeros(size * numproc, typecode)

            # Modify shape along axis=0 to match size
            shape = list(shape)
            shape[0] *= numproc
            buffer = reshape(buffer, shape)

        msg = 'Data array and buffer must have same type '
        msg = 'in reduce. I got types "%s" and "%s"' % (x.dtype.char,
                                                        buffer.dtype.char)
        assert x.dtype.char == buffer.dtype.char, msg
        reduce_array(x, buffer, op, root)

    elif (protocol == 'vanilla' or protocol == 'string'):
        raise 'Protocol: %s unsupported for reduce' % protocol
    else:
        raise 'Unknown protocol: %s' % protocol

    return buffer
Example #4
0
def raw_reduce(s, d, nums, op, source, vanilla=0):
    """Wrapper for MPI_Reduce.
     Reduce nums elements in s to d (of the same size) at source
     applying operation op.
     
     Automatically determine appropriate protocol
     and call corresponding send function.
    
  """

    protocol = get_control_info(s, vanilla)[0]
    if protocol == 'array':
        if not s.typecode() == d.typecode():
            raise "Input array and buffer must have the same typecode"
        reduce_array(s, d, nums, op, source)
    elif (protocol == 'vanilla' or protocol == 'string'):
        raise "Protocol: %s unsupported for reduce" % protocol
    else:
        raise "Unknown values for protocol: %s" % protocol

    return d
Example #5
0
def raw_reduce(s, d, nums, op, source, vanilla=0):
  """Wrapper for MPI_Reduce.
     Reduce nums elements in s to d (of the same size) at source
     applying operation op.
     
     Automatically determine appropriate protocol
     and call corresponding send function.
    
  """

  protocol = get_control_info(s, vanilla)[0]
  if protocol == 'array':
    if not s.typecode() == d.typecode():
      raise "Input array and buffer must have the same typecode"
    reduce_array(s, d, nums, op, source)          
  elif (protocol == 'vanilla' or protocol == 'string'):
    raise "Protocol: %s unsupported for reduce" %protocol
  else:
    raise "Unknown values for protocol: %s" %protocol  
    
  return d         
Example #6
0
def reduce(s, nums, op, source, vanilla=0):
    """Wrapper for easy MPI Gather receive.
     Receive data from source with tag.
     
     Create appropriate buffer and receive data.
  """

    control_info = get_control_info(s)

    protocol = control_info[0]
    typecode = control_info[1]
    s_size = nums

    if protocol == 'array':
        import Numeric
        x = Numeric.zeros(s_size * size(), typecode)
        reduce_array(s, x, s_size, op, source)
    elif (protocol == 'vanilla' or protocol == 'string'):
        raise "Protocol: %s unsupported for reduce" % protocol
    else:
        raise "Unknown values for protocol: %s" % protocol

    return x
Example #7
0
def reduce(s, nums, op, source, vanilla=0):
  """Wrapper for easy MPI Gather receive.
     Receive data from source with tag.
     
     Create appropriate buffer and receive data.
  """

  control_info = get_control_info(s)
  
  protocol = control_info[0]
  typecode = control_info[1]
  s_size =  nums   
  
  if protocol == 'array':
    import Numeric
    x = Numeric.zeros(s_size * size(),typecode)
    reduce_array(s, x, s_size, op, source)    
  elif (protocol == 'vanilla' or protocol == 'string'):
    raise "Protocol: %s unsupported for reduce" %protocol
  else:
    raise "Unknown values for protocol: %s" %protocol
      
  return x  
Example #8
0
def reduce(x, op, root, buffer=None, vanilla=0):
    """Reduce elements in x to buffer (of the same size as x)
       at root applying operation op elementwise.
    """

    import types
    
    from mpiext import size
    numproc = size()         #Needed to determine buffer size


    #Input check
    errmsg = 'Root id (%s) must be an integer.' %root
    assert type(root) == types.IntType, errmsg

    #Create metadata about object
    protocol, typecode, size, shape = create_control_info(x)


    #Reduce
    if protocol == 'array':
        if buffer is None:
            import Numeric
            buffer = Numeric.zeros(size*numproc, typecode)
      
            # Modify shape along axis=0 to match size
            shape = list(shape)
            shape[0] *= numproc
            buffer = Numeric.reshape(buffer, shape)
      
        reduce_array(x, buffer, op, root)    
    elif (protocol == 'vanilla' or protocol == 'string'):
        raise 'Protocol: %s unsupported for reduce' %protocol
    else:
        raise 'Unknown protocol: %s' %protocol
      
    return buffer