{"id":16515276,"url":"https://github.com/dusanerdeljan/tensor-math-library","last_synced_at":"2025-10-28T04:32:49.450Z","repository":{"id":84214181,"uuid":"263028232","full_name":"dusanerdeljan/tensor-math-library","owner":"dusanerdeljan","description":"Header only lazy evaluation tensor math library with multi-backend parallel eager execution support (TBB, OpenMP, Parallel STL and in the future CUDA and OpenCL)","archived":false,"fork":false,"pushed_at":"2020-10-31T20:23:16.000Z","size":380,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-08T15:52:35.740Z","etag":null,"topics":["cuda","eager-execution","lazy-evaluation","matrix-library","opencl","openmp","parallel-computing","tbb","tensor-library"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dusanerdeljan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-11T11:53:41.000Z","updated_at":"2023-05-25T10:56:24.000Z","dependencies_parsed_at":"2023-05-24T01:15:12.062Z","dependency_job_id":null,"html_url":"https://github.com/dusanerdeljan/tensor-math-library","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dusanerdeljan/tensor-math-library","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanerdeljan%2Ftensor-math-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanerdeljan%2Ftensor-math-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanerdeljan%2Ftensor-math-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanerdeljan%2Ftensor-math-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dusanerdeljan","download_url":"https://codeload.github.com/dusanerdeljan/tensor-math-library/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanerdeljan%2Ftensor-math-library/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281386563,"owners_count":26492014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-28T02:00:06.022Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cuda","eager-execution","lazy-evaluation","matrix-library","opencl","openmp","parallel-computing","tbb","tensor-library"],"created_at":"2024-10-11T16:16:31.114Z","updated_at":"2025-10-28T04:32:49.430Z","avatar_url":"https://github.com/dusanerdeljan.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tensor-math-library\nHeader only lazy evaluation tensor math library with multi-backend parallel eager execution support (TBB, OpenMP, Parallel STL and in the future CUDA and OpenCL). For now, TML only supports matrix math.\n\n## Future improvements\n\n * CUDA and OpenCL parallel backends\n * Temp-free eager execution\n * Tensor extension with autograd\n * Support for fixed-sized stack allocated matrices and tensors\n\n## Lazy evaluation\n\nLazy evaluation means that an expression is evaluated as late as possible, meaning an expression will be evaluated when assigned to Matrix object. All the lazy evaluation functions live in ```tml::lazy``` namespace and all the overloaded operators are also lazily evaluated.\n\n```cpp\n#include \"include/tml.hpp\"\n\nint main() \n{\n  tml::Matrix\u003cdouble\u003e m1 = tml::Matrix\u003cdouble\u003e::Arange(10, 10);\n  tml::Matrix\u003cdouble\u003e m2 = tml::Matrix\u003cdouble\u003e::Arange(10, 10);\n  auto res = m1 + m2 + tml::lazy::Log(2.0 + m1) * tml::lazy::Sin(m2);\n  return 0;\n}\n```\nThe type of variable `res` is not Matrix, it is ExprOP\u003cdouble, T\u003e and nothing has been computed. This expression is computed only when you assign it to a Matrix type variable and no temporary objects are created. \n\nNow the expression is computed:\n```cpp\ntml::Matrix\u003cdouble\u003e res = m1 + m2 + tml::lazy::Log(2.0 + m1) * tml::lazy::Sin(m2);\n```\n\nTo illustrate how the above expression is being evaluated, you can imagine it as a single for loop which is being exucted in parallel (if at least one parallel backend is supported).\n```cpp\ntypedef tml::Matrix\u003cdouble\u003e::const_iterator iter;\nauto resIt = res.begin();\nfor (iter it1 = m1.cbegin(), it2 = m2.cbegin(); it1 != m1.cend(); ++it1, ++it2, ++resIt)\n  *resIt = *it1 + *it2 + std::log(2.0 + *it1) * std::sin(*it2);\n```\n\n### Potential errors\n\nAll the parameters in lazy expressions are captured by reference so you have to keep an eye on the objects lifetime. For example, consider the following function:\n\n```cpp\ntemplate\u003ctypename T\u003e\nauto WrongExample(const ExprOP\u003cdouble, T\u003e\u0026 expression)\n{\n\ttml::Matrix\u003cdouble\u003e temp = tml::Matrix\u003cdouble\u003e::Arange(10, 10);\n\treturn expression + temp;\n}\n```\nThe problem with above function is that variable `temp` is destroyed at the end of the function, and is being captued by reference in the `expression + temp` lazy expression object, which can result in undefined behaviour.\n\n## Parallel eager execution\n\nTML also offers the ability to evaluate expressions eagerly. All the eager evaluation functions live in ```tml::eager``` namespace.\nWhen using functions from ```tml::eager``` namespace, user can specify the execution policy (serial or parallel execution), where serial execution is the default option. \n\nIt is important to mention that any lazy expression object that is passed to the eager function will be evaluated in the body of the function, and thus a temporary variable will be created.\n\n### Example usage of eager functions\n\n```cpp\nauto res = tml::eager::Matmul(m1, m2, tml::execution::tbb); // now res is of type tml::Matrix\u003cdouble\u003e\nauto res = tml::eager::Matmul(m1, m2, tml::execution::omp); // now res is of type tml::Matrix\u003cdouble\u003e\nauto res = tml::eager::Matmul(m1, m2, tml::execution::stl); // now res is of type tml::Matrix\u003cdouble\u003e\n```\n\n### Combination of lazy and eager functions\n\n```cpp\nauto res = tml::eager::Matmul(m1, m2 + 1.0 + tml::lazy::Square(m2), tml::execution::tbb);\n```\nIn the above example expression `m2 + 1.0 + tml::lazy::Square(m2)` is evaluated lazily and the matrix product is then eagerly executed in parallel.\n\n## Compilation\n\nTML has been tested on Windows 10 (MSVC2015 and MSVC2019) and Linux Ubuntu 18.04 and 20.04 operating systems.\n\n### Preprocessor options\n\n * TML_HAS_TBB - Macro tells TML that Thread Building Blocks library is available\n * TML_HAS_OMP - Macro tells TML that OpenMP support is enabled\n * TML_HAS_CPP17_STL - Macro tells TML that language standard is set to C++ 17\n\n#### MSVC 2015 or above and Intel C++ compiler\n\n * Make sure to set language standard to C++ 11 or higher (preferably C++ 17)\n * In the preprocessor definitions tab define macros as explained above (if you are compiling in debug then also define _SCL_SECURE_NO_WARNINGS)\n * Enable OpenMP support and enhanced instruction sets (preferably AVX2)\n * Configure optimizations and other options at will\n \n#### g++ 9 or above\n\nExample compilation command:\n```\n g++ Main.cpp -std=c++17 -ltbb -fopenmp -O3 -D TML_HAS_TBB -D TML_HAS_OMP -D TML_HAS_CPP17_STL\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdusanerdeljan%2Ftensor-math-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdusanerdeljan%2Ftensor-math-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdusanerdeljan%2Ftensor-math-library/lists"}