Esempio n. 1
0
def run_test(*args, **kwargs):
    """
    A, A_trans, B, B_trans must be numpy array or matrix instances
    """
    epsilon = args[0]
    A = args[1]
    A_trans = args[2]
    B = args[3]
    B_trans = args[4]
    C = args[5]
    vcl_A = args[6]
    vcl_A_trans = args[7]
    vcl_B = args[8]
    vcl_B_trans = args[9]
    vcl_C = args[10]

    act_diff = math.fabs(diff(A, vcl_A))
    if act_diff > epsilon:
        raise Exception("Error copying A")

    act_diff = math.fabs(diff(B, vcl_B))
    if act_diff > epsilon:
        x = p.Matrix(vcl_B.shape, dtype=vcl_B.dtype, layout=p.ROW_MAJOR)
        p.Assign(x, vcl_B).execute()
        print(x.value)
        print(B)
        print(x == B)
        print(act_diff)
        raise Exception("Error copying B")

    #act_diff = math.fabs(diff(C, vcl_C))
    #if act_diff > epsilon:
    #    raise Exception("Error copying C")

    act_diff = math.fabs(diff(A_trans, vcl_A_trans))
    if act_diff > epsilon:
        raise Exception("Error copying A_trans")

    act_diff = math.fabs(diff(B_trans, vcl_B_trans))
    if act_diff > epsilon:
        raise Exception("Error copying B_trans")

    #act_diff = math.fabs(diff(C_trans, vcl_C_trans))
    #if act_diff > epsilon:
    #    raise Exception("Error copying C_trans")

    #A = vcl_A.value
    #A_trans = vcl_A_trans.value
    #B = vcl_B.value
    #B_trans = vcl_B_trans.value
    #C = vcl_C.value
    #C_trans = C.T

    # C +-= A * B
    C = A.dot(B)
    vcl_C = vcl_A * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C = A * B passed!")

    C += A.dot(B)
    vcl_C += vcl_A * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C += A * B passed!")

    C -= A.dot(B)
    vcl_C -= vcl_A * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C -= A * B passed!")

    # C +-= A * trans(B)
    C = A.dot(B_trans.T)
    vcl_C = vcl_A * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C = A * trans(B) passed!")

    C += A.dot(B_trans.T)
    vcl_C += vcl_A * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C += A * trans(B) passed!")

    C -= A.dot(B_trans.T)
    vcl_C -= vcl_A * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C -= A * trans(B) passed!")

    # C +-= trans(A) * B
    C = A_trans.T.dot(B)
    vcl_C = vcl_A_trans.T * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C = trans(A) * B passed!")

    C += A_trans.T.dot(B)
    vcl_C += vcl_A_trans.T * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C += trans(A) * B passed!")

    C -= A_trans.T.dot(B)
    vcl_C -= vcl_A_trans.T * vcl_B
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C -= trans(A) * B passed!")

    # C +-= trans(A) * trans(B)
    C = A_trans.T.dot(B_trans.T)
    vcl_C = vcl_A_trans.T * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C = trans(A) * trans(B) passed!")

    C += A_trans.T.dot(B_trans.T)
    vcl_C += vcl_A_trans.T * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C += trans(A) * trans(B) passed!")

    C -= A_trans.T.dot(B_trans.T)
    vcl_C -= vcl_A_trans.T * vcl_B_trans.T
    act_diff = math.fabs(diff(C, vcl_C))
    if (act_diff > epsilon):
        raise Exception(
            "Error at operation: matrix-matrix product; diff = %s" % act_diff)
    print("Test C -= trans(A) * trans(B) passed!")

    return os.EX_OK
Esempio n. 2
0
#!python
"""
PyViennaCL allows you to access and manipulate submatrices and subvectors using
the usual Pythonic apparatus of slices and ranges of objects. ViennaCL provides
object proxies to allow us to do these sub-manipulations in place.

Here, we give some basic examples.
"""

import pyviennacl as p
import numpy as np

# Create some small, simple Vector and Matrix instances
x = p.Vector(6, 1.0)
a = p.Matrix(6, 6, 1.0)

print("x is %s" % x)
print("a is\n%s" % a)

# Scale the first half of the Vector x
x[0:3] *= 2.0

# Show the new x
print("x is now %s" % x)

# Create a smaller matrix from a submatrix of a
b = a[3:6, 3:6] * 4.0

# Set the upper-left corner of the matrix to 4.0s
a[0:3, 0:3] = b
Esempio n. 3
0
content here. If you are not so familiar, you might need 10 minutes.
"""

# Import PyViennaCL and NumPy
import pyviennacl as p
import numpy as np

# Create our datastructures on the host
x = [1.0, 2.0, 3.0, 4.0, 5.0]  # We can create PyViennaCL Vectors from lists
a = np.array([[1.0, 2.0, 3.0], [0.0, 3.0, 4.0],
              [0.0, 0.0,
               5.0]])  # We can create PyViennaCL Matrices from arrays

# Create corresponding ViennaCL datastructures on the compute device
y = p.Vector()
b = p.Matrix(a)  # This is a dense matrix

# Copy the data back to the host and check that it's equal
z = y.value  # z is now a 1-D numpy array with dtype float64
c = b.value  # c is now a 2-D numpy array with dtype float64

if (z == x).all() and (c == a).all():
    print("Successfully transferred data to and from the compute device!")

# We can modify elements of the ViennaCL device structures, but since this
# incurs a compute kernel initialisation and buffer transfer, it is very slow!
print('1111111')
y[0] = float(0.0)
print('22222222')
b[2, 2] = float(-1.0)
def run_test(*args, **kwargs):
    """
    A, A_trans, B, B_trans must be numpy array or matrix instances
    """
    epsilon = args[0]
    A = args[1]
    A_trans = args[2]
    B = args[3]
    B_trans = args[4]
    C = args[5]
    vcl_A = args[6]
    vcl_A_trans = args[7]
    vcl_B = args[8]
    vcl_B_trans = args[9]
    vcl_C = args[10]

    dtype = kwargs['dtype']

    alpha = p.Scalar(dtype(3.1415))
    beta = p.HostScalar(dtype(2.718))

    # Test initialisers
    # + GPU scalar TODO
    #X = p.Matrix(A.shape, alpha)
    #if not (X == (np.ones(A.shape, dtype = dtype) * alpha.value)).all():
    #    raise RuntimeError("Failed: GPU scalar matrix init")
    #print("Test: initialisation of matrix with GPU scalar passed")

    # + CPU scalar TODO
    Y = p.Matrix(A.shape, beta.value)  # TODO
    if not (Y == (np.ones(A.shape, dtype=dtype) * beta.value)).all():
        raise RuntimeError("Failed: CPU scalar matrix init")
    print("Test: initialisation of matrix with CPU scalar passed")

    # + ndarray
    X = p.Matrix(np.ones(A.shape, dtype=dtype) * beta.value)
    if not (X == (np.ones(A.shape, dtype=dtype) * beta.value)).all():
        raise RuntimeError("Failed: ndarray matrix init")
    print("Test: initialisation of matrix with ndarray passed")

    # + Matrix
    X = p.Matrix(Y)
    if not (X == Y).all():
        raise RuntimeError("Failed: Matrix Matrix init")
    print("Test: initialisation of matrix with Matrix passed")

    # + CompressedMatrix -- TODO: sparse matrices + dtypes
    #Y = p.CompressedMatrix(X)
    #X = p.Matrix(Y)
    #if not (X == Y).all():
    #    raise RuntimeError("Failed: Matrix CompressedMatrix init")
    #print("Test: initialisation of matrix with CompressedMatrix passed")

    # In-place add
    X = vcl_A.value
    X += vcl_B.value
    vcl_A += vcl_B
    if not (vcl_A == X).all():
        raise RuntimeError("Failed: in-place add")
    print("Test: in-place add passed")

    # Scaled in-place add
    X += alpha.value * vcl_B.value
    vcl_A += alpha * vcl_B
    if not (vcl_A == X).all():
        raise RuntimeError("Failed: scaled in-place add")
    print("Test: scaled in-place add passed")

    # Add
    Y = vcl_A.value + vcl_B.value
    Z = vcl_A + vcl_B
    if not (Y == Z).all():
        raise RuntimeError("Failed: add")
    print("Test: add passed")

    # Scaled add (left)
    Y = dtype(alpha.value) * vcl_B.value + vcl_C.value
    Z = alpha * vcl_B + vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:
        raise RuntimeError("Failed: scaled add (left)")
    print("Test: scaled add (left) passed")

    # Scaled add (right)
    Y = vcl_B.value + dtype(alpha.value) * vcl_C.value
    Z = vcl_B + alpha * vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:  # (Z == Y).all():
        raise RuntimeError("Failed: scaled add (left)")
    print("Test: scaled add (right) passed")

    # Scaled add (both)
    Y = alpha.value * vcl_B.value + alpha.value * vcl_C.value
    Z = alpha * vcl_B + alpha * vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:
        raise RuntimeError("Failed: scaled add (both)")
    print("Test: scaled add (both) passed")

    # In-place sub
    X = vcl_A.value
    X -= vcl_B.value
    vcl_A -= vcl_B
    if not (vcl_A == X).all():
        raise RuntimeError("Failed: in-place sub")
    print("Test: in-place sub passed")

    # Scaled in-place sub
    X -= alpha.value * vcl_B.value
    vcl_A -= alpha * vcl_B
    if not (vcl_A == X).all():
        raise RuntimeError("Failed: scaled in-place sub")
    print("Test: scaled in-place sub passed")

    # Sub
    Y = vcl_A.value - vcl_B.value
    Z = vcl_A - vcl_B
    if not (Y == Z).all():
        raise RuntimeError("Failed: sub")
    print("Test: sub passed")

    # Scaled sub (left)
    Y = alpha.value * vcl_B.value - vcl_C.value
    Z = alpha * vcl_B - vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:
        raise RuntimeError("Failed: scaled sub (left)")
    print("Test: scaled sub (left) passed")

    # Scaled sub (right)
    Y = vcl_B.value - alpha.value * vcl_C.value
    Z = vcl_B - alpha * vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:
        raise RuntimeError("Failed: scaled sub (right)")
    print("Test: scaled sub (right) passed")

    # Scaled sub (both)
    Y = alpha.value * vcl_B.value - alpha.value * vcl_C.value
    Z = alpha * vcl_B - alpha * vcl_C
    act_diff = math.fabs(diff(Y, Z))
    if act_diff > epsilon:
        raise RuntimeError("Failed: scaled sub (both)")
    print("Test: scaled sub (both) passed")

    # Scalar multiplication (CPU scalar) -- not supported yet
    #gamma_py = beta.value * beta.value
    #gamma_vcl = beta * beta
    # ...
    # Scalar multiplication (GPU scalar)

    # Matrix-vector multiplication
    vec = p.Vector(vcl_A.shape[0], 3.1415, dtype=dtype)
    X = vcl_A * vec
    Y = vcl_A.value.dot(vec.value)
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: matrix-vector multiplication")
    print("Test: matrix-vector multiplication passed")

    # Matrix divided by scalar
    X = vcl_A.value / alpha.value
    Y = vcl_A / alpha
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: matrix-scalar division")
    print("Test: matrix-scalar division passed")

    # Binary elementwise operations -- prod and div
    X = vcl_A.value * vcl_B.value
    Y = p.ElementProd(vcl_A, vcl_B)
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise matrix-matrix multiplication")
    print("Test: elementwise matrix-matrix multiplication passed")

    X = vcl_A.value**vcl_B.value
    Y = vcl_A**vcl_B
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise matrix-matrix exponentiation")
    print("Test: elementwise matrix-matrix exponentiation passed")

    X = vcl_A.value / vcl_B.value
    Y = p.ElementDiv(vcl_A, vcl_B)
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise matrix-matrix division")
    print("Test: elementwise matrix-matrix division passed")

    # Unary elementwise operations
    # - abs TODO
    #X = abs(vcl_A.value)
    #Y = p.ElementAbs(vcl_A)
    #act_diff = math.fabs(diff(X, Y))
    #if act_diff > epsilon:
    #    raise RuntimeError("Failed: elementwise abs")
    #print("Test: elementwise abs passed")

    # - acos
    X = np.arccos(vcl_A.value)
    Y = p.ElementAcos(vcl_A).result  # TODO THIS SHOULDN'T BE REQUIRED
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise acos")
    print("Test: elementwise acos passed")

    # - asin
    X = np.arcsin(vcl_A.value)
    Y = p.ElementAsin(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise asin")
    print("Test: elementwise asin passed")

    # - atan
    X = np.arctan(vcl_A.value)
    Y = p.ElementAtan(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise atan")
    print("Test: elementwise atan passed")

    # - ceil
    X = np.ceil(vcl_A.value)
    Y = p.ElementCeil(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise ceil")
    print("Test: elementwise ceil passed")

    # - cos
    X = np.cos(vcl_A.value)
    Y = p.ElementCos(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise cos")
    print("Test: elementwise cos passed")

    # - cosh
    X = np.cosh(vcl_A.value)
    Y = p.ElementCosh(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise cosh")
    print("Test: elementwise cosh passed")

    # - exp
    X = np.exp(vcl_A.value)
    Y = p.ElementExp(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise exp")
    print("Test: elementwise exp passed")

    # - fabs
    X = np.fabs(vcl_A.value)
    Y = p.ElementFabs(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise fabs")
    print("Test: elementwise fabs passed")

    # - floor
    X = np.floor(vcl_A.value)
    Y = p.ElementFloor(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise floor")
    print("Test: elementwise floor passed")

    # - log
    X = np.log(vcl_A.value)
    Y = p.ElementLog(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise log")
    print("Test: elementwise log passed")

    # - log10
    X = np.log10(vcl_A.value)
    Y = p.ElementLog10(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise log10")
    print("Test: elementwise log10 passed")

    # - sin
    X = np.sin(vcl_A.value)
    Y = p.ElementSin(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise sin")
    print("Test: elementwise sin passed")

    # - sinh
    X = np.sinh(vcl_A.value)
    Y = p.ElementSinh(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise sinh")
    print("Test: elementwise sinh passed")

    # - sqrt
    X = np.sqrt(vcl_A.value)
    Y = p.ElementSqrt(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise sqrt")
    print("Test: elementwise sqrt passed")

    # - tan
    X = np.tan(vcl_A.value)
    Y = p.ElementTan(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise tan")
    print("Test: elementwise tan passed")

    # - tanh
    X = np.tanh(vcl_A.value)
    Y = p.ElementTanh(vcl_A).result
    act_diff = math.fabs(diff(X, Y))
    if act_diff > epsilon:
        raise RuntimeError("Failed: elementwise tanh")
    print("Test: elementwise tanh passed")

    # - trans TODO ?!!!
    #X = vcl_A.value.T
    #Y = vcl_A.T.result #p.Trans(vcl_A).result
    #act_diff = math.fabs(diff(X, Y))
    #if act_diff > epsilon:
    #    raise RuntimeError("Failed: elementwise trans")
    #print("Test: elementwise trans passed")

    # - norm1 -- TODO ONLY FOR VECTORS
    # - norm2 -- TODO ONLY FOR VECTORS
    # - norm_inf -- TODO ONLY FOR VECTORS

    return os.EX_OK
Esempio n. 5
0
import random

# We want a square N x N system.
N = 5 

# Create a NumPy matrix with float32 precision to hold the data on the host.
# Firstly, we create an empty matrix, then fill the upper triangle with values.
A = np.zeros((N, N), dtype = np.float32)

for i in range(N):
    for j in range(N):
        if j >= i:
            A[i, j] = np.float32(random.randint(0,1000) / 100.0)

# Transfer the system matrix to the compute device
A = p.Matrix(A)

print("A is\n%s" % A)

# Create a right-hand-side vector on the host with random elements
# and transfer it to the compute device
b = p.Vector(np.random.rand(N).astype(np.float32))

print("b is %s" % b)

# Solve the system; note the choice of tag to denote an upper triangular system
x = p.solve(A, b, p.upper_tag())

# Copy the solution from the device to host and display it
print("Solution of Ax = b for x:\n%s" % x)
Esempio n. 6
0
Here, we demonstrate the different notation for these products.
"""

import pyviennacl as p
import numpy as np

# Let's construct some random 1-D and 2-D arrays
v = np.random.rand(5)
w = np.random.rand(5)

f = np.random.rand(5, 5)
g = np.random.rand(5, 5)

# Now transfer them to the compute device
x, y = p.Vector(v), p.Vector(w)
a, b = p.Matrix(f), p.Matrix(g)

print("a is\n%s" % a)
print("b is\n%s" % b)

print("x is %s" % x)
print("y is %s" % y)

#
# Scaling
#

# Represent the scaling of x by 2.0
z = x * 2.0

# Compute and print the result