def test_multithread_evaluation(): # As of version 1.14.4 of cffi, the FFI.compile method is not thread safe. This tests that evaluation of different # kernels is thread safe. A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0], dimensions=(2, 3), format='ds') x = Tensor.from_aos([[0], [1], [2]], [3.0, 2.5, 2.0], dimensions=(3, ), format='d') def run_eval(): # Generate a random expression so that the cache cannot be hit return evaluate(f'y{randrange(1024)}(i) = A(i,j) * x(j)', 'd', A=A, x=x) n = 4 with ThreadPool(n) as p: results = p.starmap(run_eval, [()] * n) expected = run_eval() for actual in results: assert actual == expected
def test_unsorted_coordinates(format, indices, vals, permutation): data = [ ((0, 0), 6), ((0, 2), 9), ((0, 3), 8), ((2, 0), 5), ((2, 3), 7), ] permutated_data = [data[i] for i in permutation] coordinates, values = zip(*permutated_data) A = Tensor.from_aos(coordinates, values, dimensions=(3, 4), format=format) assert A.taco_indices == indices assert A.taco_vals == vals A = Tensor.from_soa(zip(*coordinates), values, dimensions=(3, 4), format=format) assert A.taco_indices == indices assert A.taco_vals == vals A = Tensor.from_dok(dict(permutated_data), dimensions=(3, 4), format=format) assert A.taco_indices == indices assert A.taco_vals == vals
def test_csr_matrix_vector_product(): A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0], dimensions=(2, 3), format='ds') x = Tensor.from_aos([[0], [1], [2]], [3.0, 2.5, 2.0], dimensions=(3, ), format='d') expected = Tensor.from_aos([[0], [1]], [-5.0, 14.0], dimensions=(2, ), format='d') function = tensor_method('y(i) = A(i,j) * x(j)', dict(A='ds', x='d'), 'd') actual = function(A, x) assert actual == expected actual = evaluate('y(i) = A(i,j) * x(j)', 'd', A=A, x=x) assert actual == expected
def test_csr_matrix_plus_csr_matrix(): A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0], dimensions=(2, 3), format='ds') B = Tensor.from_aos([[1, 1], [1, 2], [0, 2]], [-3.0, 4.0, 3.5], dimensions=(2, 3), format='ds') expected = Tensor.from_aos([[1, 0], [0, 1], [1, 2], [1, 1], [0, 2]], [2.0, -2.0, 8.0, -3.0, 3.5], dimensions=(2, 3), format='ds') function = tensor_method('C(i,j) = A(i,j) + B(i,j)', dict(A='ds', B='ds'), 'ds') actual = function(A, B) assert actual == expected actual = evaluate('C(i,j) = A(i,j) * B(i,j)', 'ds', A=A, B=B) assert actual == expected
def test_from_aos(): format = Format((Mode.dense, Mode.compressed), (1, 0)) x = Tensor.from_aos( [(1, 0), (1, 1), (2, 1), (3, 1)], [4.5, 3.2, -3.0, 5.0], dimensions=(4, 3), format=format, ) assert x.order == 2 assert x.dimensions == (4, 3) assert x.modes == (Mode.dense, Mode.compressed) assert x.mode_ordering == (1, 0) assert x.format == format assert x.to_dok() == { (1, 0): 4.5, (1, 1): 3.2, (2, 1): -3.0, (3, 1): 5.0, }
def a_ds(): return Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0], dimensions=(2, 3), format='ds')
def c_ds_add(): return Tensor.from_aos([[1, 0], [0, 1], [1, 2], [1, 1], [0, 2]], [2.0, -2.0, 8.0, -3.0, 3.5], dimensions=(2, 3), format='ds')
def b_ds(): return Tensor.from_aos([[1, 1], [1, 2], [0, 2]], [-3.0, 4.0, 3.5], dimensions=(2, 3), format='ds')