def passed_test(dtype, as_matrix, provide_C, trans_a, trans_b): """ Run one general matrix-matrix multiplication test. Arguments: dtype: either 'float64' or 'float32', the NumPy dtype to test as_matrix: True to test a NumPy matrix, False to test a NumPy ndarray provide_C: True if C is to be provided to the BLASpy function, False otherwise trans_a: BLASpy trans_a parameter to test trans_b: BLASpy trans_b parameter to test Returns: True if the expected result is within the margin of error of the actual result, False otherwise. """ # generate random sizes for matrix dimensions m = randint(N_MIN, N_MAX) n = randint(N_MIN, N_MAX) k = randint(N_MIN, N_MAX) # create random scalars and matrices to test alpha = uniform(SCAL_MIN, SCAL_MAX) beta = uniform(SCAL_MIN, SCAL_MAX) A = random_matrix((m if trans_a == 'n' else k), (k if trans_a == 'n' else m), dtype, as_matrix) B = random_matrix((k if trans_b == 'n' else n), (n if trans_b == 'n' else k), dtype, as_matrix) C = random_matrix(m, n, dtype, as_matrix) if provide_C else None # create copies/views of A, B, and C that can be used to calculate the expected result A_2 = A if trans_a == 'n' else A.T B_2 = B if trans_b == 'n' else B.T C_2 = copy(C) if C is not None else zeros((m, n)) # compute the expected result C_2 = beta * C_2 + alpha * dot(A_2, B_2) # get the actual result C = gemm(A, B, C, trans_a, trans_b, alpha, beta) # compare the actual result to the expected result and return result of the test return allclose(C, C_2, RTOL, ATOL)
def timing_test(dtype, trans_a, trans_b, n, k, trials): """ Run one general matrix-matrix multiplication test. Arguments: dtype: either 'float64' or 'float32', the NumPy dtype to test as_matrix: True to test a NumPy matrix, False to test a NumPy ndarray provide_C: True if C is to be provided to the BLASpy function, False otherwise trans_a: BLASpy trans_a parameter to test trans_b: BLASpy trans_b parameter to test Returns: A tuple of the BLASpy total runtime and NumPy total runtime. """ as_matrix = True np_time = 0.0 bp_time = 0.0 for i in range(trials): # create random scalars and matrices to test alpha = uniform(SCAL_MIN, SCAL_MAX) beta = uniform(SCAL_MIN, SCAL_MAX) A = random_matrix((n if trans_a == 'n' else k), (k if trans_a == 'n' else n), dtype, as_matrix) B = random_matrix((k if trans_b == 'n' else n), (n if trans_b == 'n' else k), dtype, as_matrix) C = random_matrix(n, n, dtype, as_matrix) # create copies/views for NumPy A_2 = A if trans_a == 'n' else A.T B_2 = B if trans_b == 'n' else B.T C_2 = copy(C) if i % 2 == 0: # BLASpy first start = time.time() gemm(A, B, C, trans_a, trans_b, alpha, beta) end = time.time() bp_time += end - start # then NumPy start = time.time() beta * C_2 + alpha * dot(A_2, B_2) end = time.time() np_time += end - start else: # NumPy first start = time.time() beta * C_2 + alpha * dot(A_2, B_2) end = time.time() np_time += end - start # then BLASpy start = time.time() gemm(A, B, C, trans_a, trans_b, alpha, beta) end = time.time() bp_time += end - start return bp_time / trials, np_time / trials