From 45f3f35a87be9faaf0e52eac448ab701356c0b1d Mon Sep 17 00:00:00 2001 From: darthnoward Date: Mon, 5 Jun 2023 08:14:56 +0000 Subject: [PATCH 1/3] add gaussian matrix loader --- include/AMMBench.h | 1 + include/MatrixLoader/GaussianMatrixLoader.h | 90 +++++++++++++++++++++ src/MatrixLoader/CMakeLists.txt | 1 + src/MatrixLoader/GaussianMatrixLoader.cpp | 31 +++++++ src/MatrixLoader/MatrixLoaderTable.cpp | 2 + 5 files changed, 125 insertions(+) create mode 100644 include/MatrixLoader/GaussianMatrixLoader.h create mode 100644 src/MatrixLoader/GaussianMatrixLoader.cpp diff --git a/include/AMMBench.h b/include/AMMBench.h index c8482eda..f1326eee 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -76,6 +76,7 @@ #include #include #include +#include #include /** * @} diff --git a/include/MatrixLoader/GaussianMatrixLoader.h b/include/MatrixLoader/GaussianMatrixLoader.h new file mode 100644 index 00000000..5220b837 --- /dev/null +++ b/include/MatrixLoader/GaussianMatrixLoader.h @@ -0,0 +1,90 @@ +// +// Created by haolan on 6/5/23. +// + +#ifndef INTELLISTREAM_GAUSSIANMATRIXLOADER_H +#define INTELLISTREAM_GAUSSIANMATRIXLOADER_H +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_MatrixLOADER + * @{ + */ +/** + * @ingroup AMMBENCH_MatrixLOADER_Gaussian The Gaussian Random generator + * @{ + */ +/** + * @class GaussianMatrixLoader MatrixLoader/GaussianMatrixLoader.h + * @brief The Gaussian Random class of matrix loader + * @ingroup AMMBENCH_MatrixLOADER_Gaussian + * @note: + * - Must have a global config by @ref setConfig + * @note Default behavior +* - create +* - call @ref setConfig, this function will also generate the tensor A and B correspondingly +* - call @ref getA and @ref getB (assuming we are benchmarking torch.mm(A,B)) + * @note: require config parameters and default values + * - "aRow" The rows in matrix A, U64, 100 + * - "aCol" The cols in matrix B, U64, 1000 + * - "bCol" The rows in matrix B, U64, 500 + * - "seed" The seed of inline random generator,U64,114514 + * @note: default name tags + * "random": @ref GaussianMatrixLoader + */ + class GaussianMatrixLoader : public AbstractMatrixLoader { + protected: + torch::Tensor A, B; + uint64_t aRow, aCol, bCol, seed; + /** + * @brief Inline logic of reading a config file + * @param cfg the config + */ + void paraseConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief inline logic of generating A and B + */ + void generateAB(); + public: + GaussianMatrixLoader() = default; + + ~GaussianMatrixLoader() = default; + /** + * @brief Set the GLOBAL config map related to this loader + * @param cfg The config map + * @return bool whether the config is successfully set + * @note + */ + virtual bool setConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief get the A matrix + * @return the generated A matrix + */ + virtual torch::Tensor getA(); + /** + * @brief get the B matrix + * @return the generated B matrix + */ + virtual torch::Tensor getB(); + }; +/** + * @ingroup AMMBENCH_MatrixLOADER_Gaussian + * @typedef GaussianMatrixLoaderPtr + * @brief The class to describe a shared pointer to @ref GaussianMatrixLoader + + */ + typedef std::shared_ptr GaussianMatrixLoaderPtr; +/** + * @ingroup AMMBENCH_MatrixLOADER_Gaussian + * @def newGaussianMatrixLoader + * @brief (Macro) To creat a new @ref GaussianMatrixLoader under shared pointer. + */ +#define newGaussianMatrixLoader std::make_shared +/** + * @} + */ +/** + * @} + */ +} +#endif //INTELLISTREAM_GAUSSIANMATRIXLOADER_H diff --git a/src/MatrixLoader/CMakeLists.txt b/src/MatrixLoader/CMakeLists.txt index b0585356..6521f00c 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -2,5 +2,6 @@ add_sources( AbstractMatrixLoader.cpp RandomMatrixLoader.cpp SparseMatrixLoader.cpp + GaussianMatrixLoader.cpp MatrixLoaderTable.cpp ) \ No newline at end of file diff --git a/src/MatrixLoader/GaussianMatrixLoader.cpp b/src/MatrixLoader/GaussianMatrixLoader.cpp new file mode 100644 index 00000000..fc0cc5ba --- /dev/null +++ b/src/MatrixLoader/GaussianMatrixLoader.cpp @@ -0,0 +1,31 @@ +// +// Created by haolan on 6/5/23. +// +#include +#include +void AMMBench::GaussianMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { + aRow = cfg->tryU64("aRow", 100, true); + aCol = cfg->tryU64("aCol", 1000, true); + bCol = cfg->tryU64("bCol", 500, true); + seed = cfg->tryU64("seed", 114514, true); + INTELLI_INFO( + "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) + + "]"); +} +void AMMBench::GaussianMatrixLoader::generateAB() { + torch::manual_seed(seed); + A = torch::poisson(torch::ones({(long) aRow, (long) aCol})); + B = torch::poisson(torch::ones({(long) aCol, (long) bCol})); +} +//do nothing in abstract class +bool AMMBench::GaussianMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { + paraseConfig(cfg); + generateAB(); + return true; +} +torch::Tensor AMMBench::GaussianMatrixLoader::getA() { + return A; +} +torch::Tensor AMMBench::GaussianMatrixLoader::getB() { + return B; +} \ No newline at end of file diff --git a/src/MatrixLoader/MatrixLoaderTable.cpp b/src/MatrixLoader/MatrixLoaderTable.cpp index 80b82d15..d421ce45 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace AMMBench { /** * @note revise me if you need new loader @@ -12,6 +13,7 @@ namespace AMMBench { AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["random"] = newRandomMatrixLoader(); loaderMap["sparse"] = newSparseMatrixLoader(); + loaderMap["gaussian"] = newGaussianMatrixLoader(); } } // AMMBench \ No newline at end of file From ee4e820f8015c004f7aef8e51037fd7fa80b2e26 Mon Sep 17 00:00:00 2001 From: haolan Date: Mon, 5 Jun 2023 12:47:52 +0000 Subject: [PATCH 2/3] add exponential matrix loader --- include/AMMBench.h | 1 + .../MatrixLoader/ExponentialMatrixLoader.h | 90 +++++++++++++++++++ src/MatrixLoader/CMakeLists.txt | 1 + src/MatrixLoader/ExponentialMatrixLoader.cpp | 32 +++++++ src/MatrixLoader/GaussianMatrixLoader.cpp | 4 +- src/MatrixLoader/MatrixLoaderTable.cpp | 2 + 6 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 include/MatrixLoader/ExponentialMatrixLoader.h create mode 100644 src/MatrixLoader/ExponentialMatrixLoader.cpp diff --git a/include/AMMBench.h b/include/AMMBench.h index f1326eee..6cde5ff0 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -77,6 +77,7 @@ #include #include #include +#include #include /** * @} diff --git a/include/MatrixLoader/ExponentialMatrixLoader.h b/include/MatrixLoader/ExponentialMatrixLoader.h new file mode 100644 index 00000000..c7032acb --- /dev/null +++ b/include/MatrixLoader/ExponentialMatrixLoader.h @@ -0,0 +1,90 @@ +// +// Created by haolan on 6/5/23. +// + +#ifndef INTELLISTREAM_EXPONENTIALMATRIXLOADER_H +#define INTELLISTREAM_EXPONENTIALMATRIXLOADER_H +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_MatrixLOADER + * @{ + */ +/** + * @ingroup AMMBENCH_MatrixLOADER_Exponential The Exponential generator + * @{ + */ +/** + * @class ExponentialMatrixLoader MatrixLoader/ExponentialMatrixLoader.h + * @brief The Exponential class of matrix loader + * @ingroup AMMBENCH_MatrixLOADER_Exponential + * @note: + * - Must have a global config by @ref setConfig + * @note Default behavior +* - create +* - call @ref setConfig, this function will also generate the tensor A and B correspondingly +* - call @ref getA and @ref getB (assuming we are benchmarking torch.mm(A,B)) + * @note: require config parameters and default values + * - "aRow" The rows in matrix A, U64, 100 + * - "aCol" The cols in matrix B, U64, 1000 + * - "bCol" The rows in matrix B, U64, 500 + * - "seed" The seed of inline random generator,U64,114514 + * @note: default name tags + * "random": @ref ExponentialMatrixLoader + */ + class ExponentialMatrixLoader : public AbstractMatrixLoader { + protected: + torch::Tensor A, B; + uint64_t aRow, aCol, bCol, seed; + /** + * @brief Inline logic of reading a config file + * @param cfg the config + */ + void paraseConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief inline logic of generating A and B + */ + void generateAB(); + public: + ExponentialMatrixLoader() = default; + + ~ExponentialMatrixLoader() = default; + /** + * @brief Set the GLOBAL config map related to this loader + * @param cfg The config map + * @return bool whether the config is successfully set + * @note + */ + virtual bool setConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief get the A matrix + * @return the generated A matrix + */ + virtual torch::Tensor getA(); + /** + * @brief get the B matrix + * @return the generated B matrix + */ + virtual torch::Tensor getB(); + }; +/** + * @ingroup AMMBENCH_MatrixLOADER_Exponential + * @typedef ExponentialMatrixLoaderPtr + * @brief The class to describe a shared pointer to @ref ExponentialMatrixLoader + + */ + typedef std::shared_ptr ExponentialMatrixLoaderPtr; +/** + * @ingroup AMMBENCH_MatrixLOADER_Exponential + * @def newExponentialMatrixLoader + * @brief (Macro) To creat a new @ref ExponentialMatrixLoader under shared pointer. + */ +#define newExponentialMatrixLoader std::make_shared +/** + * @} + */ +/** + * @} + */ +} +#endif //INTELLISTREAM_EXPONENTIALMATRIXLOADER_H diff --git a/src/MatrixLoader/CMakeLists.txt b/src/MatrixLoader/CMakeLists.txt index 6521f00c..50cb9c49 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -3,5 +3,6 @@ add_sources( RandomMatrixLoader.cpp SparseMatrixLoader.cpp GaussianMatrixLoader.cpp + ExponentialMatrixLoader.cpp MatrixLoaderTable.cpp ) \ No newline at end of file diff --git a/src/MatrixLoader/ExponentialMatrixLoader.cpp b/src/MatrixLoader/ExponentialMatrixLoader.cpp new file mode 100644 index 00000000..8697ebfe --- /dev/null +++ b/src/MatrixLoader/ExponentialMatrixLoader.cpp @@ -0,0 +1,32 @@ +// +// Created by haolan on 6/5/23. +// +#include +#include +void AMMBench::ExponentialMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { + aRow = cfg->tryU64("aRow", 100, true); + aCol = cfg->tryU64("aCol", 1000, true); + bCol = cfg->tryU64("bCol", 500, true); + seed = cfg->tryU64("seed", 114514, true); + INTELLI_INFO( + "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) + + "]"); +} +void AMMBench::ExponentialMatrixLoader::generateAB() { + torch::manual_seed(seed); + A = torch::exponential(torch::empty({(long) aRow, (long) aCol})); + B = torch::exponential(torch::empty({(long) aCol, (long) bCol})); +} + +//do nothing in abstract class +bool AMMBench::ExponentialMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { + paraseConfig(cfg); + generateAB(); + return true; +} +torch::Tensor AMMBench::ExponentialMatrixLoader::getA() { + return A; +} +torch::Tensor AMMBench::ExponentialMatrixLoader::getB() { + return B; +} \ No newline at end of file diff --git a/src/MatrixLoader/GaussianMatrixLoader.cpp b/src/MatrixLoader/GaussianMatrixLoader.cpp index fc0cc5ba..0c8e3e83 100644 --- a/src/MatrixLoader/GaussianMatrixLoader.cpp +++ b/src/MatrixLoader/GaussianMatrixLoader.cpp @@ -14,8 +14,8 @@ void AMMBench::GaussianMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { } void AMMBench::GaussianMatrixLoader::generateAB() { torch::manual_seed(seed); - A = torch::poisson(torch::ones({(long) aRow, (long) aCol})); - B = torch::poisson(torch::ones({(long) aCol, (long) bCol})); + A = torch::randn({(long) aRow, (long) aCol}); + B = torch::randn({(long) aCol, (long) bCol}); } //do nothing in abstract class bool AMMBench::GaussianMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { diff --git a/src/MatrixLoader/MatrixLoaderTable.cpp b/src/MatrixLoader/MatrixLoaderTable.cpp index d421ce45..46da4ddb 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace AMMBench { /** @@ -14,6 +15,7 @@ AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["random"] = newRandomMatrixLoader(); loaderMap["sparse"] = newSparseMatrixLoader(); loaderMap["gaussian"] = newGaussianMatrixLoader(); + loaderMap["exponential"] = newExponentialMatrixLoader(); } } // AMMBench \ No newline at end of file From 786ed33c2d077604de4c934c8ccdfdeb4e40d0a5 Mon Sep 17 00:00:00 2001 From: darthnoward Date: Mon, 5 Jun 2023 13:36:36 +0000 Subject: [PATCH 3/3] add binomial matrix loader --- include/AMMBench.h | 1 + include/MatrixLoader/BinomialMatrixLoader.h | 90 +++++++++++++++++++++ src/MatrixLoader/BinomialMatrixLoader.cpp | 51 ++++++++++++ src/MatrixLoader/CMakeLists.txt | 1 + src/MatrixLoader/MatrixLoaderTable.cpp | 2 + 5 files changed, 145 insertions(+) create mode 100644 include/MatrixLoader/BinomialMatrixLoader.h create mode 100644 src/MatrixLoader/BinomialMatrixLoader.cpp diff --git a/include/AMMBench.h b/include/AMMBench.h index 6cde5ff0..fabee02d 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -78,6 +78,7 @@ #include #include #include +#include #include /** * @} diff --git a/include/MatrixLoader/BinomialMatrixLoader.h b/include/MatrixLoader/BinomialMatrixLoader.h new file mode 100644 index 00000000..58850a4c --- /dev/null +++ b/include/MatrixLoader/BinomialMatrixLoader.h @@ -0,0 +1,90 @@ +// +// Created by haolan on 6/5/23. +// + +#ifndef INTELLISTREAM_BINOMIALMATRIXLOADER_H +#define INTELLISTREAM_BINOMIALMATRIXLOADER_H +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_MatrixLOADER + * @{ + */ +/** + * @ingroup AMMBENCH_MatrixLOADER_Binomial The Binomial generator + * @{ + */ +/** + * @class BinomialMatrixLoader MatrixLoader/BinomialMatrixLoader.h + * @brief The Binomial class of matrix loader + * @ingroup AMMBENCH_MatrixLOADER_Binomial + * @note: + * - Must have a global config by @ref setConfig + * @note Default behavior +* - create +* - call @ref setConfig, this function will also generate the tensor A and B correspondingly +* - call @ref getA and @ref getB (assuming we are benchmarking torch.mm(A,B)) + * @note: require config parameters and default values + * - "aRow" The rows in matrix A, U64, 100 + * - "aCol" The cols in matrix B, U64, 1000 + * - "bCol" The rows in matrix B, U64, 500 + * - "seed" The seed of inline random generator,U64,114514 + * @note: default name tags + * "random": @ref BinomialMatrixLoader + */ + class BinomialMatrixLoader : public AbstractMatrixLoader { + protected: + torch::Tensor A, B; + uint64_t aRow, aCol, bCol, seed; + /** + * @brief Inline logic of reading a config file + * @param cfg the config + */ + void paraseConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief inline logic of generating A and B + */ + void generateAB(); + public: + BinomialMatrixLoader() = default; + + ~BinomialMatrixLoader() = default; + /** + * @brief Set the GLOBAL config map related to this loader + * @param cfg The config map + * @return bool whether the config is successfully set + * @note + */ + virtual bool setConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief get the A matrix + * @return the generated A matrix + */ + virtual torch::Tensor getA(); + /** + * @brief get the B matrix + * @return the generated B matrix + */ + virtual torch::Tensor getB(); + }; +/** + * @ingroup AMMBENCH_MatrixLOADER_Binomial + * @typedef BinomialMatrixLoaderPtr + * @brief The class to describe a shared pointer to @ref BinomialMatrixLoader + + */ + typedef std::shared_ptr BinomialMatrixLoaderPtr; +/** + * @ingroup AMMBENCH_MatrixLOADER_Binomial + * @def newBinomialMatrixLoader + * @brief (Macro) To creat a new @ref BinomialMatrixLoader under shared pointer. + */ +#define newBinomialMatrixLoader std::make_shared +/** + * @} + */ +/** + * @} + */ +} +#endif //INTELLISTREAM_BINOMIALMATRIXLOADER_H diff --git a/src/MatrixLoader/BinomialMatrixLoader.cpp b/src/MatrixLoader/BinomialMatrixLoader.cpp new file mode 100644 index 00000000..e21923dc --- /dev/null +++ b/src/MatrixLoader/BinomialMatrixLoader.cpp @@ -0,0 +1,51 @@ +// +// Created by haolan on 6/5/23. +// +#include +#include +void AMMBench::BinomialMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { + aRow = cfg->tryU64("aRow", 100, true); + aCol = cfg->tryU64("aCol", 1000, true); + bCol = cfg->tryU64("bCol", 500, true); + seed = cfg->tryU64("seed", 114514, true); + INTELLI_INFO( + "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) + + "]"); +} +void AMMBench::BinomialMatrixLoader::generateAB() { + torch::manual_seed(seed); + A = torch::zeros({(long) aRow, (long) aCol}); + B = torch::zeros({(long) aCol, (long) bCol}); + // parameter + const int trials = 10; + const double probability = 0.5; + + for(int i = 0; i < trials; i++) { + // Create a tensor filled with random numbers between 0 and 1 + torch::Tensor rand_tensor = torch::rand({(long) aRow, (long) aCol}); + + // Add the results of the Bernoulli trial to the binomial tensor + A += (rand_tensor < probability).to(torch::kInt); + } + + for(int i = 0; i < trials; i++) { + // Create a tensor filled with random numbers between 0 and 1 + torch::Tensor rand_tensor = torch::rand({(long) aCol, (long) bCol}); + + // Add the results of the Bernoulli trial to the binomial tensor + B += (rand_tensor < probability).to(torch::kInt); + } +} + +//do nothing in abstract class +bool AMMBench::BinomialMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { + paraseConfig(cfg); + generateAB(); + return true; +} +torch::Tensor AMMBench::BinomialMatrixLoader::getA() { + return A; +} +torch::Tensor AMMBench::BinomialMatrixLoader::getB() { + return B; +} \ No newline at end of file diff --git a/src/MatrixLoader/CMakeLists.txt b/src/MatrixLoader/CMakeLists.txt index 50cb9c49..fd98726c 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -4,5 +4,6 @@ add_sources( SparseMatrixLoader.cpp GaussianMatrixLoader.cpp ExponentialMatrixLoader.cpp + BinomialMatrixLoader.cpp MatrixLoaderTable.cpp ) \ No newline at end of file diff --git a/src/MatrixLoader/MatrixLoaderTable.cpp b/src/MatrixLoader/MatrixLoaderTable.cpp index 46da4ddb..a6b84de0 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace AMMBench { /** * @note revise me if you need new loader @@ -16,6 +17,7 @@ AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["sparse"] = newSparseMatrixLoader(); loaderMap["gaussian"] = newGaussianMatrixLoader(); loaderMap["exponential"] = newExponentialMatrixLoader(); + loaderMap["binomial"] = newBinomialMatrixLoader(); } } // AMMBench \ No newline at end of file