|
| 1 | +#include "bench.h" |
| 2 | +#include "benchmark/benchmark.h" |
| 3 | + |
| 4 | +#include "taco/tensor.h" |
| 5 | +#include "taco/format.h" |
| 6 | +#include "taco/index_notation/index_notation.h" |
| 7 | + |
| 8 | +using namespace taco; |
| 9 | + |
| 10 | +// bench_add_sparse_threshold adds two tensors of a given dimension and sparsity. |
| 11 | +// It does this range on the TACO side to see when TACO becomes faster than NumPY. |
| 12 | +static void bench_add_sparse_threshold(benchmark::State& state, float sparsity) { |
| 13 | + int dim = state.range(0); |
| 14 | + Tensor<float> matrix1("A", {dim, dim}, CSR); |
| 15 | + Tensor<float> matrix2("B", {dim, dim}, CSR); |
| 16 | + Tensor<float> result("C", {dim, dim}, CSR); |
| 17 | + |
| 18 | + srand(4357); |
| 19 | + // TODO (rohany): Move this into a helper method. |
| 20 | + for (int i = 0; i < dim; i++) { |
| 21 | + for (int j = 0; j < dim; j++) { |
| 22 | + float rand_float = (float)rand()/(float)(RAND_MAX); |
| 23 | + if (rand_float < sparsity) { |
| 24 | + matrix1.insert({i, j}, (float) ((int) (rand_float*3/sparsity))); |
| 25 | + } |
| 26 | + |
| 27 | + rand_float = (float)rand()/(float)(RAND_MAX); |
| 28 | + if (rand_float < sparsity) { |
| 29 | + matrix2.insert({i, j}, (float) ((int) (rand_float*3/sparsity))); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + matrix1.pack(); matrix2.pack(); |
| 34 | + |
| 35 | + IndexVar i, j; |
| 36 | + result(i, j) = matrix1(i, j) + matrix2(i, j); |
| 37 | + result.compile(); |
| 38 | + result.assemble(); |
| 39 | + |
| 40 | + for (auto _ : state) { |
| 41 | + result.compute(); |
| 42 | + } |
| 43 | +} |
| 44 | +static void applyBenchSizes(benchmark::internal::Benchmark* b) { |
| 45 | + b->ArgsProduct({{250, 500, 750, 1000, 2500, 5000, 7500, 8000}}); |
| 46 | +} |
| 47 | +// TACO_BENCH_ARG(bench_add_sparse_threshold, 0.001, 0.001)->Apply(applyBenchSizes); |
| 48 | +// TACO_BENCH_ARG(bench_add_sparse_threshold, 0.01, 0.01)->Apply(applyBenchSizes); |
| 49 | +// TACO_BENCH_ARG(bench_add_sparse_threshold, 0.05, 0.05)->Apply(applyBenchSizes); |
| 50 | +TACO_BENCH_ARG(bench_add_sparse_threshold, 0.25, 0.25)->Apply(applyBenchSizes); |
0 commit comments