From b51121eb5b15d2f4deb2886b7cc0e9d86b57a988 Mon Sep 17 00:00:00 2001 From: Rohan Yadav Date: Wed, 10 Mar 2021 17:27:09 -0800 Subject: [PATCH] taco: add a cache for tensors loaded from files Relates to #14. --- taco/bench.h | 63 +++++++++++++++++++++++++++++-------------------- taco/ufuncs.cpp | 8 +++++-- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/taco/bench.h b/taco/bench.h index 04cf3fc..c3a3162 100644 --- a/taco/bench.h +++ b/taco/bench.h @@ -38,9 +38,46 @@ ->ReportAggregatesOnly(true) \ ->UseRealTime() +template +taco::Tensor castToType(std::string name, taco::Tensor tensor) { + taco::Tensor result(name, tensor.getDimensions(), tensor.getFormat()); + std::vector coords(tensor.getOrder()); + for (auto& value : taco::iterate(tensor)) { + for (int i = 0; i < tensor.getOrder(); i++) { + coords[i] = value.first[i]; + } + result.insert(coords, T(value.second)); + } + result.pack(); + return result; +} + +struct TacoTensorFileCache { + template + taco::Tensor read(std::string path, T format) { + if (this->lastPath == path) { + return lastLoaded; + } + // TODO (rohany): Not worrying about whether the format was the same as what was asked for. + this->lastLoaded = taco::read(path, format); + this->lastPath = path; + return this->lastLoaded; + } + + template + taco::Tensor readIntoType(std::string name, std::string path, U format) { + auto tensor = this->read(path, format); + return castToType(name, tensor); + } + + taco::Tensor lastLoaded; + std::string lastPath; +}; + std::string getTacoTensorPath(); taco::TensorBase loadRandomTensor(std::string name, std::vector dims, float sparsity, taco::Format format); +// TODO (rohany): Cache the tensor shifts too. template taco::Tensor shiftLastMode(std::string name, taco::Tensor original) { taco::Tensor result(name, original.getDimensions(), original.getFormat()); @@ -63,30 +100,4 @@ taco::Tensor shiftLastMode(std::string name, taco::Tensor original) { return result; } -template -taco::Tensor castToType(std::string name, taco::Tensor tensor) { - taco::Tensor result(name, tensor.getDimensions(), tensor.getFormat()); - std::vector coords(tensor.getOrder()); - for (auto& value : taco::iterate(tensor)) { - for (int i = 0; i < tensor.getOrder(); i++) { - coords[i] = value.first[i]; - } - result.insert(coords, T(value.second)); - } - result.pack(); - return result; -} - -template -taco::Tensor readIntoType(std::string name, std::string path, taco::ModeFormat format) { - auto tensor = taco::read(path, format); - return castToType(name, tensor); -} - -template -taco::Tensor readIntoType(std::string name, std::string path, taco::Format format) { - auto tensor = taco::read(path, format); - return castToType(name, tensor); -} - #endif //TACO_BENCH_BENCH_H diff --git a/taco/ufuncs.cpp b/taco/ufuncs.cpp index 20855b7..81bc545 100644 --- a/taco/ufuncs.cpp +++ b/taco/ufuncs.cpp @@ -154,6 +154,10 @@ static void applyBenchSizes(benchmark::internal::Benchmark* b) { TACO_BENCH_ARGS(bench_ufunc_sparse, xor_0.01, 0.01, "xor")->Apply(applyBenchSizes); TACO_BENCH_ARGS(bench_ufunc_sparse, rightShift_0.01, 0.01, ">>")->Apply(applyBenchSizes); +// fileCache is a cache of paths to tensors so that reads of the same tensor +// don't need to perform duplicate disk IO. +TacoTensorFileCache fileCache; + static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Func op) { auto path = getTacoTensorPath(); auto frosttTensorPath = path; @@ -164,7 +168,7 @@ static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Fun frosttTensorPath += tnsPath; // TODO (rohany): What format do we want to do here? - auto frosttTensor = readIntoType("frostt", frosttTensorPath, Sparse); + auto frosttTensor = fileCache.readIntoType("frostt", frosttTensorPath, Sparse); Tensor other = shiftLastMode("other", frosttTensor); for (auto _ : state) { @@ -239,7 +243,7 @@ static void bench_suitesparse_ufunc(benchmark::State& state, Func op) { auto tensorName = taco::util::split(filename, ".")[0]; state.SetLabel(tensorName); - auto ssTensor = readIntoType("ssTensor", tensorPath, CSR); + auto ssTensor = fileCache.readIntoType("ssTensor", tensorPath, CSR); auto other = shiftLastMode("other", ssTensor); state.counters["dimx"] = ssTensor.getDimension(0);