Exemplo n.º 1
0
def __blas(name, a, b, alpha=1.0, c=None, beta=0.0, shape_matters=True):
    if not b is None:
        if not (a.ndim == 2 and b.ndim == 2):
            stderr.write("[ext] Matrices need to be two-dimensional.\n")
            return None

        if a.shape[1] != b.shape[0] and shape_matters:
            stderr.write("[ext] Wrong shape of matrices: first argument has shape {} and second has shape {}.\n".format(a.shape, b.shape))
            return None

        if not b.flags['C_CONTIGUOUS']:
            b = b.copy()
    else:
        b = np.empty(shape=(a.shape[0], a.shape[1]), dtype=a.dtype)

    if not a.flags['C_CONTIGUOUS']:
        a = a.copy()

    if c is None:
        c = np.empty(shape=(a.shape[0], b.shape[1]), dtype=a.dtype)
    elif not c.flags['C_CONTIGUOUS']:
        c = c.copy()

    if alpha != 1.0:
        a = a * alpha

    if beta != 0.0:
        c = c * beta

    ufuncs.extmethod(name, c, a, b) # modifies 'c'
    return c
Exemplo n.º 2
0
def __blas(name, a, b, alpha=1.0, c=None, beta=0.0, shape_matters=True):
    if not b is None:
        if not (a.ndim == 2 and b.ndim == 2):
            stderr.write("[ext] Matrices need to be two-dimensional.\n")
            return None

        if a.shape[1] != b.shape[0] and shape_matters:
            stderr.write(
                "[ext] Wrong shape of matrices: first argument has shape {} and second has shape {}.\n".format(a.shape,
                                                                                                               b.shape))
            return None

        if not b.flags['C_CONTIGUOUS']:
            b = b.copy()
    else:
        b = np.empty(shape=(a.shape[0], a.shape[1]), dtype=a.dtype)

    if not a.flags['C_CONTIGUOUS']:
        a = a.copy()

    if c is None:
        c = np.empty(shape=(a.shape[0], b.shape[1]), dtype=a.dtype)
    elif not c.flags['C_CONTIGUOUS']:
        c = c.copy()

    if alpha != 1.0:
        a = a * alpha

    if beta != 0.0:
        c = c * beta

    ufuncs.extmethod(name, c, a, b)  # modifies 'c'
    return c
def hsv2rgbArray(hsv):
    """
    Transform an hsv image to an rgb array

    :param hsv: the input image.  can be pil image or numpy array
    :return rgb: the output array

    This comes from scikit-image:
        https://github.com/scikit-image/scikit-image/blob/master/skimage/color/colorconv.py
    """
    hsv = numpyArray(hsv)
    if len(hsv.shape) == 2:
        # this is a black and white image, so treat it as value, with zero hue and saturation
        hs = np.empty((hsv.shape))
        return np.dstack((hs, hs, hsv))
    hi = np.floor(hsv[:, :, 0] * 6)
    f = hsv[:, :, 0] * 6 - hi
    p = hsv[:, :, 2] * (1 - hsv[:, :, 1])
    q = hsv[:, :, 2] * (1 - f * hsv[:, :, 1])
    t = hsv[:, :, 2] * (1 - (1 - f) * hsv[:, :, 1])
    v = hsv[:, :, 2]
    hi = np.dstack([hi, hi, hi]).astype(np.uint8) % 6
    out = np.choose(hi, [
        np.dstack((v, t, p)),
        np.dstack((q, v, p)),
        np.dstack((p, v, t)),
        np.dstack((p, q, v)),
        np.dstack((t, p, v)),
        np.dstack((v, p, q))
    ])
    return out
Exemplo n.º 4
0
def tdma(a, b, c, d, workgrp_size=None):
    import pyopencl as cl
    import bohrium as bh

    assert a.shape == b.shape == c.shape == d.shape
    assert a.dtype == b.dtype == c.dtype == d.dtype

    # Check that PyOpenCL is installed and that the Bohrium runtime uses the OpenCL backend
    if not bh.interop_pyopencl.available():
        raise NotImplementedError("OpenCL not available")

    # Get the OpenCL context from Bohrium
    ctx = bh.interop_pyopencl.get_context()
    queue = cl.CommandQueue(ctx)

    ret = bh.empty(a.shape, dtype=a.dtype)
    a_buf, b_buf, c_buf, d_buf, ret_buf = map(bh.interop_pyopencl.get_buffer,
                                              (a, b, c, d, ret))

    prg = compile_tdma(ret.shape[-1],
                       bh.interop_pyopencl.type_np2opencl_str(a.dtype))
    global_size = functools.reduce(operator.mul, ret.shape[:-1])
    prg.tdma(queue, [global_size], workgrp_size, a_buf, b_buf, c_buf, d_buf,
             ret_buf)
    return ret
Exemplo n.º 5
0
 def init(self):
     """
     This function is used as a means to control til --dtype argument
     passed to the benchmark script and provide a uuid for benchmark output.
     """
     self.uuid = str(uuid.uuid4())
     for dtype in self.dtypes:
         yield ({0:bh.empty(self.size, bohrium=False, dtype=dtype)},
                "%s: " % str(dtype)
         )
Exemplo n.º 6
0
def norm(x, ord=None, axis=None):
    """
    This version of norm is not fully compliant with the NumPy version,
    it only supports computing 2-norm of a vector.
    """

    if ord != None:
        raise NotImplementedError("Unsupported value param ord=%s" % ord)
    if axis != None:
        raise NotImplementedError("Unsupported value of param ord=%s" % axis)

    r = np.sum(x*x)
    if issubclass(np.dtype(r.dtype).type, np.integer):
        r_f32 = np.empty(r.shape, dtype=np.float32)
        r_f32[:] = r
        r = r_f32
    return np.sqrt(r)
Exemplo n.º 7
0
def norm(x, ord=None, axis=None):
    """
    This version of norm is not fully compliant with the NumPy version,
    it only supports computing 2-norm of a vector.
    """

    if ord != None:
        raise NotImplementedError("Unsupported value param ord=%s" % ord)
    if axis != None:
        raise NotImplementedError("Unsupported value of param ord=%s" % axis)

    r = np.sum(x * x)
    if issubclass(np.dtype(r.dtype).type, np.integer):
        r_f32 = np.empty(r.shape, dtype=np.float32)
        r_f32[:] = r
        r = r_f32
    return np.sqrt(r)
Exemplo n.º 8
0
def matmul(a, b):
    """
    Matrix multiplication of two 2-D arrays.

    Parameters
    ----------
    a : array_like
        First argument.
    b : array_like
        Second argument.

    Returns
    -------
    output : ndarray
        Returns the matrix multiplication of `a` and `b`.

    Raises
    ------
    ValueError
        If the last dimension of `a` is not the same size as
        the second-to-last dimension of `b`.

    See Also
    --------
    dot : Dot product of two arrays.

    Examples
    --------
    >>> np.matmul(np.array([[1,2],[3,4]]),np.array([[5,6],[7,8]]))
    array([[19, 22],
           [43, 50]])
    """
    if not dtype_equal(a, b):
        raise ValueError("Input must be of same type")
    if a.ndim != 2 and b.ndim != 2:
        raise ValueError("Input must be 2-D.")

    if not (bhary.check(a) or bhary.check(b)):
        return numpy.dot(a, b)

    a = array_create.array(a)
    b = array_create.array(b)
    c = np.empty((a.shape[0], b.shape[1]), dtype=a.dtype)
    target.matmul(ufunc.get_bhc(c), ufunc.get_bhc(a), ufunc.get_bhc(b))
    return c
Exemplo n.º 9
0
def matmul(a,b):
    """
    Matrix multiplication of two 2-D arrays.

    Parameters
    ----------
    a : array_like
        First argument.
    b : array_like
        Second argument.

    Returns
    -------
    output : ndarray
        Returns the matrix multiplication of `a` and `b`.

    Raises
    ------
    ValueError
        If the last dimension of `a` is not the same size as
        the second-to-last dimension of `b`.

    See Also
    --------
    dot : Dot product of two arrays.

    Examples
    --------
    >>> np.matmul(np.array([[1,2],[3,4]]),np.array([[5,6],[7,8]]))
    array([[19, 22],
           [43, 50]])
    """
    if not dtype_equal(a,b):
        raise ValueError("Input must be of same type")
    if a.ndim != 2 and b.ndim != 2:
        raise ValueError("Input must be 2-D.")

    if not(bhary.check(a) or bhary.check(b)):
    	return numpy.dot(a, b)

    a = array_create.array(a)
    b = array_create.array(b)
    c = np.empty((a.shape[0], b.shape[1]), dtype=a.dtype)
    target.matmul(ufunc.get_bhc(c), ufunc.get_bhc(a), ufunc.get_bhc(b))
    return c
Exemplo n.º 10
0
    def init(self):
        """
        This function is used as a means to control til --dtype argument
        passed to the benchmark script and provide a uuid for benchmark output.
        """
        # Lets make sure that benchpress is installed
        try:
            import benchpress
        except ImportError:
            print("ERROR: benchpress is not installed -- skipping test.")
            raise StopIteration()

        self.uuid = str(uuid.uuid4())

        for dtype in self.dtypes:
            yield ({
                0: bh.empty(self.size, bohrium=False, dtype=dtype)
            }, "%s: " % str(dtype))
Exemplo n.º 11
0
    def init(self):
        """
        This function is used as a means to control til --dtype argument
        passed to the benchmark script and provide a uuid for benchmark output.
        """
        #Lets make sure that benchpress is installed
        try:
            p = subprocess.Popen(
                ['bp-info', '--benchmarks'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            out, err = p.communicate()
            rc = p.returncode
        except OSError:
            print("ERROR: benchpress is not installed -- skipping test.")
            raise StopIteration()

        self.uuid = str(uuid.uuid4())
        for dtype in self.dtypes:
            yield ({0:bh.empty(self.size, bohrium=False, dtype=dtype)},
                   "%s: " % str(dtype)
            )
Exemplo n.º 12
0
import bohrium as np
from math import pi
import matplotlib.pyplot as plt
import matplotlib.colors as colors

fig = plt.figure()
plt.xticks([])
plt.yticks([])

k = 5  # number of plane waves
stripes = 37  # number of stripes per wave
N = 512  # image size in pixels
ite = 30  # iterations
phases = np.arange(0, 2 * pi, 2 * pi / ite)

image = np.empty((N, N))

d = np.arange(-N / 2, N / 2, dtype=np.float64)

xv, yv = np.meshgrid(d, d)
theta = np.arctan2(yv, xv)
r = np.log(np.sqrt(xv * xv + yv * yv))
r[np.isinf(r) == True] = 0

tcos = theta * np.cos(np.arange(0, pi, pi / k))[:, np.newaxis, np.newaxis]
rsin = r * np.sin(np.arange(0, pi, pi / k))[:, np.newaxis, np.newaxis]
inner = (tcos - rsin) * stripes

cinner = np.cos(inner)
sinner = np.sin(inner)
Exemplo n.º 13
0
def main():
    B = util.Benchmark()

    nx      = B.size[0]
    ny      = B.size[1]
    nz      = B.size[2]
    ITER    = B.size[3]

    NO_OBST = 1
    omega   = 1.0
    density = 1.0
    deltaU  = 1e-7
    t1      = 1/3.0
    t2      = 1/18.0
    t3      = 1/36.0

    B.start()
    F       = np.ones((19, nx, ny, nz), dtype=np.float64)
    F[:]    = density/19.0
    FEQ     = np.ones((19, nx, ny, nz), dtype=np.float64)
    FEQ[:]  = density/19.0
    T       = np.zeros((19, nx, ny, nz), dtype=np.float64)

    #Create the scenery.
    BOUND   = np.zeros((nx, ny, nz), dtype=np.float64)
    BOUNDi  = np.ones((nx, ny, nz), dtype=np.float64)
    """
    if not NO_OBST:
        for i in xrange(nx):
            for j in xrange(ny):
                for k in xrange(nz):
                    if ((i-4)**2+(j-5)**2+(k-6)**2) < 6:
                        BOUND[i,j,k] += 1.0
                        BOUNDi[i,j,k] += 0.0
    BOUND[:,0,:]    += 1.0
    BOUNDi[:,0,:]   *= 0.0
    """

    if util.Benchmark().bohrium:
        np.flush()
    for ts in xrange(0, ITER):

        ##Propagate / Streaming step
        T[:] = F
        #nearest-neighbours
        F[1,:,:,0]   = T[1,:,:,-1]
        F[1,:,:,1:]  = T[1,:,:,:-1]
        F[2,:,:,:-1] = T[2,:,:,1:]
        F[2,:,:,-1]  = T[2,:,:,0]
        F[3,:,0,:]   = T[3,:,-1,:]
        F[3,:,1:,:]  = T[3,:,:-1,:]
        F[4,:,:-1,:] = T[4,:,1:,:]
        F[4,:,-1,:]  = T[4,:,0,:]
        F[5,0,:,:]   = T[5,-1,:,:]
        F[5,1:,:,:]  = T[5,:-1,:,:]
        F[6,:-1,:,:] = T[6,1:,:,:]
        F[6,-1,:,:]  = T[6,0,:,:]
        #next-nearest neighbours
        F[7,0 ,0 ,:] = T[7,-1 , -1,:]
        F[7,0 ,1:,:] = T[7,-1 ,:-1,:]
        F[7,1:,0 ,:] = T[7,:-1, -1,:]
        F[7,1:,1:,:] = T[7,:-1,:-1,:]

        F[8,0 ,:-1,:] = T[8,-1 ,1:,:]
        F[8,0 , -1,:] = T[8,-1 ,0 ,:]
        F[8,1:,:-1,:] = T[8,:-1,1:,:]
        F[8,1:, -1,:] = T[8,:-1,0 ,:]

        F[9,:-1,0 ,:] = T[9,1:, -1,:]
        F[9,:-1,1:,:] = T[9,1:,:-1,:]
        F[9,-1 ,0 ,:] = T[9,0 ,  0,:]
        F[9,-1 ,1:,:] = T[9,0 ,:-1,:]

        F[10,:-1,:-1,:] = T[10,1:,1:,:]
        F[10,:-1, -1,:] = T[10,1:,0 ,:]
        F[10,-1 ,:-1,:] = T[10,0 ,1:,:]
        F[10,-1 , -1,:] = T[10,0 ,0 ,:]

        F[11,0 ,:,0 ] = T[11,0  ,:, -1]
        F[11,0 ,:,1:] = T[11,0  ,:,:-1]
        F[11,1:,:,0 ] = T[11,:-1,:, -1]
        F[11,1:,:,1:] = T[11,:-1,:,:-1]

        F[12,0 ,:,:-1] = T[12, -1,:,1:]
        F[12,0 ,:, -1] = T[12, -1,:,0 ]
        F[12,1:,:,:-1] = T[12,:-1,:,1:]
        F[12,1:,:, -1] = T[12,:-1,:,0 ]

        F[13,:-1,:,0 ] = T[13,1:,:, -1]
        F[13,:-1,:,1:] = T[13,1:,:,:-1]
        F[13, -1,:,0 ] = T[13,0 ,:, -1]
        F[13, -1,:,1:] = T[13,0 ,:,:-1]

        F[14,:-1,:,:-1] = T[14,1:,:,1:]
        F[14,:-1,:, -1] = T[14,1:,:,0 ]
        F[14,-1 ,:,:-1] = T[14,0 ,:,1:]
        F[14,-1 ,:, -1] = T[14,0 ,:,0 ]

        F[15,:,0 ,0 ] = T[15,:, -1, -1]
        F[15,:,0 ,1:] = T[15,:, -1,:-1]
        F[15,:,1:,0 ] = T[15,:,:-1, -1]
        F[15,:,1:,1:] = T[15,:,:-1,:-1]

        F[16,:,0 ,:-1] = T[16,:, -1,1:]
        F[16,:,0 , -1] = T[16,:, -1,0 ]
        F[16,:,1:,:-1] = T[16,:,:-1,1:]
        F[16,:,1:, -1] = T[16,:,:-1,0 ]

        F[17,:,:-1,0 ] = T[17,:,1:, -1]
        F[17,:,:-1,1:] = T[17,:,1:,:-1]
        F[17,:, -1,0 ] = T[17,:,0 , -1]
        F[17,:, -1,1:] = T[17,:,0 ,:-1]

        F[18,:,:-1,:-1] = T[18,:,1:,1:]
        F[18,:,:-1, -1] = T[18,:,1:,0 ]
        F[18,:,-1 ,:-1] = T[18,:,0 ,1:]
        F[18,:,-1 , -1] = T[18,:,0 ,0 ]
        #Densities bouncing back at next timestep
        BB = np.empty(F.shape)
        T[:] = F
        T[1:,:,:,:] *= BOUND[np.newaxis,:,:,:]
        BB[2 ,:,:,:] += T[1 ,:,:,:]
        BB[1 ,:,:,:] += T[2 ,:,:,:]
        BB[4 ,:,:,:] += T[3 ,:,:,:]
        BB[3 ,:,:,:] += T[4 ,:,:,:]
        BB[6 ,:,:,:] += T[5 ,:,:,:]
        BB[5 ,:,:,:] += T[6 ,:,:,:]
        BB[10,:,:,:] += T[7 ,:,:,:]
        BB[9 ,:,:,:] += T[8 ,:,:,:]
        BB[8 ,:,:,:] += T[9 ,:,:,:]
        BB[7 ,:,:,:] += T[10,:,:,:]
        BB[14,:,:,:] += T[11,:,:,:]
        BB[13,:,:,:] += T[12,:,:,:]
        BB[12,:,:,:] += T[13,:,:,:]
        BB[11,:,:,:] += T[14,:,:,:]
        BB[18,:,:,:] += T[15,:,:,:]
        BB[17,:,:,:] += T[16,:,:,:]
        BB[16,:,:,:] += T[17,:,:,:]
        BB[15,:,:,:] += T[18,:,:,:]

        # Relax calculate equilibrium state (FEQ) with equivalent speed and density to F
        DENSITY = np.add.reduce(F)

        #UX = F[5,:,:,:].copy()
        UX = np.ones(F[5,:,:,:].shape, dtype=np.float64)
        UX[:,:,:] = F[5,:,:,:]

        UX += F[7,:,:,:]
        UX += F[8,:,:,:]
        UX += F[11,:,:,:]
        UX += F[12,:,:,:]
        UX -= F[6,:,:,:]
        UX -= F[9,:,:,:]
        UX -= F[10,:,:,:]
        UX -= F[13,:,:,:]
        UX -= F[14,:,:,:]
        UX /=DENSITY

        #UY = F[3,:,:,:].copy()
        UY = np.ones(F[3,:,:,:].shape, dtype=np.float64)
        UY[:,:,:] = F[3,:,:,:]

        UY += F[7,:,:,:]
        UY += F[9,:,:,:]
        UY += F[15,:,:,:]
        UY += F[16,:,:,:]
        UY -= F[4,:,:,:]
        UY -= F[8,:,:,:]
        UY -= F[10,:,:,:]
        UY -= F[17,:,:,:]
        UY -= F[18,:,:,:]
        UY /=DENSITY

        #UZ = F[1,:,:,:].copy()
        UZ = np.ones(F[1,:,:,:].shape, dtype=np.float64)
        UZ[:,:,:] = F[1,:,:,:]

        UZ += F[11,:,:,:]
        UZ += F[13,:,:,:]
        UZ += F[15,:,:,:]
        UZ += F[17,:,:,:]
        UZ -= F[2,:,:,:]
        UZ -= F[12,:,:,:]
        UZ -= F[14,:,:,:]
        UZ -= F[16,:,:,:]
        UZ -= F[18,:,:,:]
        UZ /=DENSITY

        UX[0,:,:] += deltaU #Increase inlet pressure
                            #Set bourderies to zero.
        UX[:,:,:] *= BOUNDi
        UY[:,:,:] *= BOUNDi
        UZ[:,:,:] *= BOUNDi
        DENSITY[:,:,:] *= BOUNDi

        U_SQU = UX**2 + UY**2 + UZ**2

        # Calculate equilibrium distribution: stationary
        FEQ[0,:,:,:] = (t1*DENSITY)*(1.0-3.0*U_SQU/2.0)
        # nearest-neighbours
        T1 = 3.0/2.0*U_SQU
        tDENSITY = t2*DENSITY
        FEQ[1,:,:,:]=tDENSITY*(1.0 + 3.0*UZ + 9.0/2.0*UZ**2 - T1)
        FEQ[2,:,:,:]=tDENSITY*(1.0 - 3.0*UZ + 9.0/2.0*UZ**2 - T1)
        FEQ[3,:,:,:]=tDENSITY*(1.0 + 3.0*UY + 9.0/2.0*UY**2 - T1)
        FEQ[4,:,:,:]=tDENSITY*(1.0 - 3.0*UY + 9.0/2.0*UY**2 - T1)
        FEQ[5,:,:,:]=tDENSITY*(1.0 + 3.0*UX + 9.0/2.0*UX**2 - T1)
        FEQ[6,:,:,:]=tDENSITY*(1.0 - 3.0*UX + 9.0/2.0*UX**2 - T1)
        # next-nearest neighbours
        T1 = 3.0*U_SQU/2.0
        tDENSITY = t3*DENSITY
        U8 = UX+UY
        FEQ[7,:,:,:] =tDENSITY*(1.0 + 3.0*U8  + 9.0/2.0*(U8)**2  - T1)
        U9 = UX-UY
        FEQ[8,:,:,:] =tDENSITY*(1.0 + 3.0*U9  + 9.0/2.0*(U9)**2  - T1)
        U10 = -UX+UY
        FEQ[9,:,:,:] =tDENSITY*(1.0 + 3.0*U10 + 9.0/2.0*(U10)**2 - T1)
        U8 *= -1.0
        FEQ[10,:,:,:]=tDENSITY*(1.0 + 3.0*U8 + 9.0/2.0*(U8)**2 - T1)
        U12 = UX+UZ
        FEQ[11,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1)
        U12 *= 1.0
        FEQ[14,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1)
        U13 = UX-UZ
        FEQ[12,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1)
        U13 *= -1.0
        FEQ[13,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1)
        U16 = UY+UZ
        FEQ[15,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1)
        U17 = UY-UZ
        FEQ[16,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1)
        U17 *= -1.0
        FEQ[17,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1)
        U16 *= -1.0
        FEQ[18,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1)
        F *= (1.0-omega)
        F += omega * FEQ

        #Densities bouncing back at next timestep
        F[1:,:,:,:] *= BOUNDi[np.newaxis,:,:,:]
        F[1:,:,:,:] += BB[1:,:,:,:]

        del BB
        del T1
        del UX, UY, UZ
        del U_SQU
        del DENSITY, tDENSITY
        del U8, U9, U10, U12, U13, U16, U17
        if util.Benchmark().bohrium:
            np.flush()

    B.stop()
    B.pprint()

    if B.outputfn:
        B.tofile(B.outputfn, {'res': UX})

    """