示例#1
0
def test_check_dtype(rng):
    dtype = 'float32'
    m, n = 3, 4
    sA, sX, sY = gemv_system(queue, m, n, dtype, rng)
    dA, dX, dY = map(lambda x: x.astype('float64'), [sA, sX, sY])
    cA, cX, cY = map(lambda x: x.astype('complex64'), [sA, sX, sY])
    zA, zX, zY = map(lambda x: x.astype('complex128'), [sA, sX, sY])

    clsA, clsX, clsY = map(to_ocl, (sA, sX, sY))
    cldA, cldX, cldY = map(to_ocl, (dA, dX, dY))
    clcA, clcX, clcY = map(to_ocl, (cA, cX, cY))
    clzA, clzX, clzY = map(to_ocl, (zA, zX, zY))

    try:
        blas.setup()

        with pytest.raises(ValueError):
            blas.gemv(queue, cldA, clsX, clsY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clsA, cldX, clsY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clcA, cldX, clcY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clzA, cldX, clzY)
    finally:
        blas.teardown()
示例#2
0
def test_blas1(n, dtype, rng):
    tols = tolerances[dtype]

    x = np.zeros(n, dtype=dtype)
    y = np.zeros(n, dtype=dtype)
    x[:] = rng.uniform(-1, 1, size=n)
    y[:] = rng.uniform(-1, 1, size=n)
    alpha = rng.uniform(0.1, 0.9)

    clx, cly = map(to_ocl, [x, y])

    try:
        blas.setup()

        blas.swap(queue, clx, cly)
        assert np.allclose(clx.get(), y, **tols)
        assert np.allclose(cly.get(), x, **tols)

        clx.set(x)
        blas.scal(queue, alpha, clx)
        assert np.allclose(clx.get(), alpha * x, **tols)

        clx.set(x)
        blas.copy(queue, clx, cly)
        assert np.allclose(cly.get(), x, **tols)

        clx.set(x)
        cly.set(y)
        blas.axpy(queue, clx, cly, alpha=alpha)
        assert np.allclose(cly.get(), alpha * x + y, **tols)

    finally:
        blas.teardown()
示例#3
0
def test_gemv(m, n, dtype, rng):
    tols = tolerances[dtype]

    A, X, Y = gemv_system(queue, m, n, dtype, rng)
    clA, clX, clY = map(to_ocl, (A, X, Y))

    try:
        blas.setup()

        # normal gemv
        clX.set(X)
        clY.fill(0)
        blas.gemv(queue, clA, clX, clY)
        assert np.allclose(clY.get(), np.dot(A, X), **tols)

        # transposed gemv
        clX.fill(0)
        clY.set(Y)
        blas.gemv(queue, clA, clY, clX, transA=True)
        assert np.allclose(clX.get(), np.dot(A.T, Y), **tols)

        # sliced gemv
        clX.set(X)
        clY.fill(0)
        blas.gemv(queue, clA[:-1, 1:], clX[:-1], clY[1:])
        Yslice = clY.get()
        assert np.allclose(Yslice[0], 0, **tols)
        assert np.allclose(Yslice[1:], np.dot(A[:-1, 1:], X[:-1]), **tols)

    finally:
        blas.teardown()
示例#4
0
def test_gemm(m, k, n, dtype, rng):
    tols = tolerances[dtype]

    A = np.zeros((m, k), dtype=dtype)
    B = np.zeros((k, n), dtype=dtype)
    C = np.zeros((m, n), dtype=dtype)
    CT = np.zeros((n, m), dtype=dtype)
    A[...] = rng.uniform(-1, 1, size=A.shape)
    B[...] = rng.uniform(-1, 1, size=B.shape)

    clA, clB, clC, clCT = map(to_ocl, [A, B, C, CT])

    try:
        blas.setup()

        # normal gemm
        blas.gemm(queue, clA, clB, clC)
        assert np.allclose(clC.get(), np.dot(A, B), **tols)

        # double transposed gemm
        blas.gemm(queue, clB, clA, clCT, transA=True, transB=True)
        assert np.allclose(clCT.get(), np.dot(B.T, A.T), **tols)

        # sliced gemm
        clC.fill(0)
        blas.gemm(queue, clA[:-1, 1:], clB[:-1, 1:], clC[1:, :-1])
        Cslice = clC.get()
        assert np.allclose(Cslice[0, :], 0, **tols)
        assert np.allclose(Cslice[:, -1], 0, **tols)
        assert np.allclose(Cslice[1:, :-1],
                           np.dot(A[:-1, 1:], B[:-1, 1:]), **tols)

    finally:
        blas.teardown()
示例#5
0
def test_syrk(k, n, dtype, rng):
    tols = tolerances[dtype]

    A = np.zeros((n, k), dtype=dtype)
    C = np.zeros((n, n), dtype=dtype)
    D = np.zeros((k, k), dtype=dtype)
    A[...] = rng.uniform(-1, 1, size=A.shape)
    C[...] = rng.uniform(-1, 1, size=C.shape)
    D[...] = rng.uniform(-1, 1, size=D.shape)

    clA, clC, clD = map(to_ocl, [A, C, D])
    a = 0.9
    b = 0.5

    try:
        blas.setup()

        # normal syrk
        up = np.triu_indices(n)
        event = blas.syrk(queue, clA, clC, alpha=a, beta=b)
        assert np.allclose(clC.get()[up], (a*np.dot(A, A.T) + b*C)[up], **tols)
        assert isinstance(event, cl.Event)

        # transposed syrk
        up = np.triu_indices(k)
        blas.syrk(queue, clA, clD, transA=True, alpha=a, beta=b)
        assert np.allclose(clD.get()[up], (a*np.dot(A.T, A) + b*D)[up], **tols)
    finally:
        blas.teardown()
示例#6
0
def test_check_dtype(rng):
    dtype = 'float32'
    m, n = 3, 4
    sA, sX, sY = gemv_system(queue, m, n, dtype, rng)
    dA, dX, dY = map(lambda x: x.astype('float64'), [sA, sX, sY])
    cA, cX, cY = map(lambda x: x.astype('complex64'), [sA, sX, sY])
    zA, zX, zY = map(lambda x: x.astype('complex128'), [sA, sX, sY])

    clsA, clsX, clsY = map(to_ocl, (sA, sX, sY))
    cldA, cldX, cldY = map(to_ocl, (dA, dX, dY))
    clcA, clcX, clcY = map(to_ocl, (cA, cX, cY))
    clzA, clzX, clzY = map(to_ocl, (zA, zX, zY))

    try:
        blas.setup()

        with pytest.raises(ValueError):
            blas.gemv(queue, cldA, clsX, clsY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clsA, cldX, clsY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clcA, cldX, clcY)

        with pytest.raises(ValueError):
            blas.gemv(queue, clzA, cldX, clzY)
    finally:
        blas.teardown()
示例#7
0
def test_gemm(m, k, n, dtype, rng):
    tols = tolerances[dtype]

    A = np.zeros((m, k), dtype=dtype)
    B = np.zeros((k, n), dtype=dtype)
    C = np.zeros((m, n), dtype=dtype)
    CT = np.zeros((n, m), dtype=dtype)
    A[...] = rng.uniform(-1, 1, size=A.shape)
    B[...] = rng.uniform(-1, 1, size=B.shape)

    clA, clB, clC, clCT = map(to_ocl, [A, B, C, CT])

    try:
        blas.setup()

        # normal gemm
        event = blas.gemm(queue, clA, clB, clC)
        assert np.allclose(clC.get(), np.dot(A, B), **tols)
        assert isinstance(event, cl.Event)

        # double transposed gemm
        blas.gemm(queue, clB, clA, clCT, transA=True, transB=True)
        assert np.allclose(clCT.get(), np.dot(B.T, A.T), **tols)

        # sliced gemm
        clC.fill(0)
        blas.gemm(queue, clA[:-1, 1:], clB[:-1, 1:], clC[1:, :-1])
        Cslice = clC.get()
        assert np.allclose(Cslice[0, :], 0, **tols)
        assert np.allclose(Cslice[:, -1], 0, **tols)
        assert np.allclose(Cslice[1:, :-1], np.dot(A[:-1, 1:], B[:-1, 1:]),
                           **tols)

    finally:
        blas.teardown()
示例#8
0
def test_gemv(m, n, dtype, rng):
    tols = tolerances[dtype]

    A, X, Y = gemv_system(queue, m, n, dtype, rng)
    clA, clX, clY = map(to_ocl, (A, X, Y))

    try:
        blas.setup()

        # normal gemv
        clX.set(X)
        clY.fill(0)
        event = blas.gemv(queue, clA, clX, clY)
        assert np.allclose(clY.get(), np.dot(A, X), **tols)
        assert isinstance(event, cl.Event)

        # transposed gemv
        clX.fill(0)
        clY.set(Y)
        blas.gemv(queue, clA, clY, clX, transA=True)
        assert np.allclose(clX.get(), np.dot(A.T, Y), **tols)

        # sliced gemv
        clX.set(X)
        clY.fill(0)
        blas.gemv(queue, clA[:-1, 1:], clX[:-1], clY[1:])
        Yslice = clY.get()
        assert np.allclose(Yslice[0], 0, **tols)
        assert np.allclose(Yslice[1:], np.dot(A[:-1, 1:], X[:-1]), **tols)

    finally:
        blas.teardown()
示例#9
0
def test_blas1(n, dtype, rng):
    tols = tolerances[dtype]

    x = np.zeros(n, dtype=dtype)
    y = np.zeros(n, dtype=dtype)
    x[:] = rng.uniform(-1, 1, size=n)
    y[:] = rng.uniform(-1, 1, size=n)
    alpha = rng.uniform(0.1, 0.9)

    clx, cly = map(to_ocl, [x, y])

    try:
        blas.setup()

        blas.swap(queue, clx, cly)
        assert np.allclose(clx.get(), y, **tols)
        assert np.allclose(cly.get(), x, **tols)

        clx.set(x)
        blas.scal(queue, alpha, clx)
        assert np.allclose(clx.get(), alpha * x, **tols)

        clx.set(x)
        blas.copy(queue, clx, cly)
        assert np.allclose(cly.get(), x, **tols)

        clx.set(x)
        cly.set(y)
        blas.axpy(queue, clx, cly, alpha=alpha)
        assert np.allclose(cly.get(), alpha * x + y, **tols)

    finally:
        blas.teardown()