(binary_op, x_gpu, a_gpu, axis=None, out=None, stream=None)
return out
Applies a binary operation to a vector and each column/row of a matrix.

    The numpy broadcasting rules apply so this would yield the same result
    as `x_gpu.get()` op `a_gpu.get()` in host-code.

    Parameters
    ----------
    binary_op : string, ['+', '-', '/', '*' '%']
        The operator to apply
    x_gpu : pycuda.gpuarray.GPUArray
        Matrix to which to add the vector.
    a_gpu : pycuda.gpuarray.GPUArray
        Vector to add to `x_gpu`.
    axis : int (optional)
        The axis onto which the vector is added. By default this is
        determined automatically by using the first axis with the correct
        dimensionality.
    out : pycuda.gpuarray.GPUArray (optional)
        Optional destination matrix.
    stream : pycuda.driver.Stream (optional)
        Optional Stream in which to perform this calculation.

    Returns
    -------
    out : pycuda.gpuarray.GPUArray
        result of `x_gpu` + `a_gpu`
Applies a binary operation to a vector and each column/row of a matrix.
Applies a binary operation to a vector and each column / row of a matrix .
def binaryop_matvec(binary_op, x_gpu, a_gpu, axis=None, out=None, stream=None):
    """
    Applies a binary operation to a vector and each column/row of a matrix.

    The numpy broadcasting rules apply so this would yield the same result
    as `x_gpu.get()` op `a_gpu.get()` in host-code.

    Parameters
    ----------
    binary_op : string, ['+', '-', '/', '*' '%']
        The operator to apply
    x_gpu : pycuda.gpuarray.GPUArray
        Matrix to which to add the vector.
    a_gpu : pycuda.gpuarray.GPUArray
        Vector to add to `x_gpu`.
    axis : int (optional)
        The axis onto which the vector is added. By default this is
        determined automatically by using the first axis with the correct
        dimensionality.
    out : pycuda.gpuarray.GPUArray (optional)
        Optional destination matrix.
    stream : pycuda.driver.Stream (optional)
        Optional Stream in which to perform this calculation.

    Returns
    -------
    out : pycuda.gpuarray.GPUArray
        result of `x_gpu` + `a_gpu`
    """
    if axis is None:
        if len(a_gpu.shape) == 1:
            if a_gpu.shape[0] == x_gpu.shape[1]:
                axis = 1
            else:
                raise ValueError(
                    "operands could not be broadcast together "
                    "with shapes %s %s" % (x_gpu.shape, a_gpu.shape))
        elif a_gpu.shape[1] == x_gpu.shape[1]:  # numpy matches inner axes first
            axis = 1
        elif a_gpu.shape[0] == x_gpu.shape[0]:
            axis = 0
        else:
                raise ValueError(
                    "operands could not be broadcast together "
                    "with shapes %s %s" % (x_gpu.shape, a_gpu.shape))
    else:
        if axis < 0:
            axis += 2
        if axis > 1:
            raise ValueError('invalid axis')

    if binary_op not in ['+', '-', '/', '*', '%']:
        raise ValueError('invalid operator')

    row_kernel, col_kernel = _get_binaryop_vecmat_kernel(x_gpu.dtype, binary_op)
    n, m = np.int32(x_gpu.shape[0]), np.int32(x_gpu.shape[1])

    block = (24, 24, 1)
    gridx = int(n // block[0] + 1 * (n % block[0] != 0))
    gridy = int(m // block[1] + 1 * (m % block[1] != 0))
    grid = (gridx, gridy, 1)

    if out is None:
        alloc = _global_cublas_allocator
        out = gpuarray.empty_like(x_gpu)
    else:
        assert out.dtype == x_gpu.dtype
        assert out.shape == x_gpu.shape

    if x_gpu.flags.c_contiguous:
        if axis == 0:
            col_kernel(x_gpu, a_gpu, out, n, m,
                       block=block, grid=grid, stream=stream,
                       shared=24*x_gpu.dtype.itemsize)
        elif axis == 1:
            row_kernel(x_gpu, a_gpu, out, n, m,
                       block=block, grid=grid, stream=stream,
                       shared=24*x_gpu.dtype.itemsize)
    else:
        if axis == 0:
            row_kernel(x_gpu, a_gpu, out, m, n,
                       block=block, grid=grid, stream=stream,
                       shared=24*x_gpu.dtype.itemsize)
        elif axis == 1:
            col_kernel(x_gpu, a_gpu, out, m, n,
                       block=block, grid=grid, stream=stream,
                       shared=24*x_gpu.dtype.itemsize)
    return out
def binaryop_matvec ( binary_op , x_gpu , a_gpu , axis = None , out = None , stream = None ) : if axis is None : if len ( a_gpu . shape ) == 1 : if a_gpu . shape [ 0 ] == x_gpu . shape [ 1 ] : axis = 1 else : raise ValueError ( "operands could not be broadcast together " "with shapes %s %s" % ( x_gpu . shape , a_gpu . shape ) ) elif a_gpu . shape [ 1 ] == x_gpu . shape [ 1 ] : # numpy matches inner axes first axis = 1 elif a_gpu . shape [ 0 ] == x_gpu . shape [ 0 ] : axis = 0 else : raise ValueError ( "operands could not be broadcast together " "with shapes %s %s" % ( x_gpu . shape , a_gpu . shape ) ) else : if axis < 0 : axis += 2 if axis > 1 : raise ValueError ( 'invalid axis' ) if binary_op not in [ '+' , '-' , '/' , '*' , '%' ] : raise ValueError ( 'invalid operator' ) row_kernel , col_kernel = _get_binaryop_vecmat_kernel ( x_gpu . dtype , binary_op ) n , m = np . int32 ( x_gpu . shape [ 0 ] ) , np . int32 ( x_gpu . shape [ 1 ] ) block = ( 24 , 24 , 1 ) gridx = int ( n // block [ 0 ] + 1 * ( n % block [ 0 ] != 0 ) ) gridy = int ( m // block [ 1 ] + 1 * ( m % block [ 1 ] != 0 ) ) grid = ( gridx , gridy , 1 ) if out is None : alloc = _global_cublas_allocator out = gpuarray . empty_like ( x_gpu ) else : assert out . dtype == x_gpu . dtype assert out . shape == x_gpu . shape if x_gpu . flags . c_contiguous : if axis == 0 : col_kernel ( x_gpu , a_gpu , out , n , m , block = block , grid = grid , stream = stream , shared = 24 * x_gpu . dtype . itemsize ) elif axis == 1 : row_kernel ( x_gpu , a_gpu , out , n , m , block = block , grid = grid , stream = stream , shared = 24 * x_gpu . dtype . itemsize ) else : if axis == 0 : row_kernel ( x_gpu , a_gpu , out , m , n , block = block , grid = grid , stream = stream , shared = 24 * x_gpu . dtype . itemsize ) elif axis == 1 : col_kernel ( x_gpu , a_gpu , out , m , n , block = block , grid = grid , stream = stream , shared = 24 * x_gpu . dtype . itemsize ) return out