diff --git a/include/AMMBench.h b/include/AMMBench.h index c8482eda..fabee02d 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -76,6 +76,9 @@ #include #include #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/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/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/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 b0585356..fd98726c 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -2,5 +2,8 @@ add_sources( AbstractMatrixLoader.cpp RandomMatrixLoader.cpp SparseMatrixLoader.cpp + GaussianMatrixLoader.cpp + ExponentialMatrixLoader.cpp + BinomialMatrixLoader.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 new file mode 100644 index 00000000..0c8e3e83 --- /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::randn({(long) aRow, (long) aCol}); + B = torch::randn({(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..a6b84de0 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include namespace AMMBench { /** * @note revise me if you need new loader @@ -12,6 +15,9 @@ namespace AMMBench { AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["random"] = newRandomMatrixLoader(); loaderMap["sparse"] = newSparseMatrixLoader(); + loaderMap["gaussian"] = newGaussianMatrixLoader(); + loaderMap["exponential"] = newExponentialMatrixLoader(); + loaderMap["binomial"] = newBinomialMatrixLoader(); } } // AMMBench \ No newline at end of file