from data_stat import Stat import torch, scipy import numpy as np import argparse import time import json import sys, os parser = argparse.ArgumentParser() parser.add_argument('matrix_file', help='the input matrix (.mtx) file') parser.add_argument('iterations', type=int, help='the number of iterations of multiplication to perform') args = parser.parse_args() device = 'cpu' matrix = scipy.io.mmread(args.matrix_file) matrix = torch.sparse_coo_tensor( np.vstack((matrix.row, matrix.col)), matrix.data, matrix.shape, device=device ).to_sparse_csr().type(torch.float) vector = torch.rand(matrix.shape[1], device=device) print(matrix, file=sys.stderr) print(vector, file=sys.stderr) start = time.time() for i in range(0, args.iterations): torch.sparse.mm(matrix, vector.unsqueeze(-1)).squeeze(-1) #print(i) end = time.time() result = dict() result[Stat.MATRIX_FILE.value] = os.path.splitext(os.path.basename(args.matrix_file))[0] print(f"Matrix: {result[Stat.MATRIX_FILE.value]}", file=sys.stderr) result[Stat.MATRIX_SHAPE.value] = matrix.shape print(f"Shape: {result[Stat.MATRIX_SHAPE.value]}", file=sys.stderr) result[Stat.MATRIX_SIZE.value] = matrix.shape[0] * matrix.shape[1] print(f"Size: {result[Stat.MATRIX_SIZE.value]}", file=sys.stderr) result[Stat.MATRIX_NNZ.value] = matrix.values().shape[0] print(f"NNZ: {result[Stat.MATRIX_NNZ.value]}", file=sys.stderr) result[Stat.MATRIX_DENSITY.value] = matrix.values().shape[0] / (matrix.shape[0] * matrix.shape[1]) print(f"Density: {result[Stat.MATRIX_DENSITY.value]}", file=sys.stderr) result[Stat.TIME_S.value] = end - start print(f"Time: {result[Stat.TIME_S.value]} seconds", file=sys.stderr) print(json.dumps(result))