├── .github ├── FUNDING.yml └── workflows │ └── cmake.yml ├── .gitignore ├── CMakeLists.txt ├── HPR_BLASConfig.cmake ├── LICENSE ├── README.md ├── applications ├── CMakeLists.txt ├── apf │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ ├── cpp_apfloat.cpp │ ├── cpp_int.cpp │ ├── foreach.cpp │ └── gnu_float128.cpp ├── blas │ ├── CMakeLists.txt │ ├── Hilbert │ │ ├── Hilbert-10x10.log │ │ ├── Hilbert-11x11.log │ │ ├── Hilbert-12x12.log │ │ ├── Hilbert-13x13.log │ │ ├── Hilbert-14x14.log │ │ ├── Hilbert-15x15.log │ │ ├── Hilbert-16x16.log │ │ ├── Hilbert-17x17.log │ │ ├── Hilbert-18x18.log │ │ ├── Hilbert-19x19.log │ │ ├── Hilbert-20x20.log │ │ └── generator.log │ ├── README.md │ ├── cholesky.cpp │ ├── common.hpp │ ├── fused_dot.cpp │ ├── fused_mm.cpp │ ├── fused_mv.cpp │ ├── hilbert_matrix.cpp │ ├── kronecker_product.cpp │ ├── l1_axpy.cpp │ ├── linpack_unchained.cpp │ └── lu_decomposition.cpp ├── calculus │ ├── CMakeLists.txt │ ├── common.hpp │ └── finite_difference.cpp ├── dsp │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ ├── fft.cpp │ └── fir_filter.cpp ├── estimation │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ ├── kalman.hpp │ └── kalman_filter.cpp ├── integer │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ └── factorial.cpp ├── matpak │ ├── CMakeLists.txt │ ├── README.md │ ├── bands.cpp │ ├── checkerboard.cpp │ ├── common.hpp │ ├── diag.cpp │ ├── diam.cpp │ ├── ek.cpp │ ├── elemat.cpp │ ├── fliplr.cpp │ ├── fliptranspose.cpp │ ├── flipud.cpp │ ├── gt.cpp │ ├── hadamard.cpp │ ├── hankel.cpp │ ├── iscentro.cpp │ ├── isdiagdom.cpp │ ├── isequal.cpp │ ├── ism.cpp │ ├── isnormal.cpp │ ├── kron.cpp │ ├── ones.cpp │ ├── reshape.cpp │ ├── rot90.cpp │ ├── rowsto.cpp │ ├── size.cpp │ ├── sum.cpp │ └── toeplitz.cpp ├── mlp │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ └── neural_network.cpp ├── numerical │ ├── CMakeLists.txt │ ├── README.md │ ├── common.hpp │ ├── conjugate_gradient.cpp │ └── newton_raphson.cpp ├── polynomial │ ├── CMakeLists.txt │ ├── README.md │ ├── bernstein.cpp │ ├── challenge.cpp │ └── chebyshev.cpp └── stdlib │ ├── CMakeLists.txt │ └── log.cpp ├── background ├── Appendix_C_Generating_Random_Correlation_Matrices.pdf ├── BLAS-LAPACK-trends-icl-utk-950-2017.pdf ├── CLBlast-a-tuned-OpenCL-BLAS-library-1705.05249.pdf ├── EM-iterative-method-1609.00670.pdf ├── KLU-direct-sparse-solver.pdf ├── accelerated-methods-for-performing-the-LDLT-decomposition.pdf ├── einstein-tensor-notation.pdf ├── exact-dot-product-arith2017.pdf ├── generating-random-orthogonal-matrices.pdf ├── generation-of-random-orthogonal-matrices.pdf ├── kalman_intro.pdf └── posits-the-good-bad-ugly.pdf ├── benchmarks ├── CMakeLists.txt └── arithmetic │ ├── CMakeLists.txt │ └── performance.cpp ├── blas ├── CMakeLists.txt ├── L0 │ ├── CMakeLists.txt │ ├── fam.cpp │ └── fma.cpp ├── L1 │ ├── CMakeLists.txt │ ├── asum.cpp │ ├── axpy.cpp │ ├── common.hpp │ └── dot.cpp ├── L2 │ ├── CMakeLists.txt │ ├── common.hpp │ └── mv.cpp └── L3 │ ├── CMakeLists.txt │ ├── blocked_matrix.cpp │ ├── common.hpp │ ├── lu_decomposition.cpp │ ├── matmul.cpp │ ├── matmul_morton.cpp │ └── matrix_types.cpp ├── build.sh ├── c_api ├── lib │ ├── CMakeLists.txt │ └── hprblas_c_api.cpp └── test │ ├── CMakeLists.txt │ └── matmul.c ├── cmake ├── AddCompilerFlag.cmake ├── Banners.cmake ├── CheckCCompilerFlag.cmake ├── CheckCXXCompilerFlag.cmake ├── CheckMicCCompilerFlag.cmake ├── CheckMicCXXCompilerFlag.cmake ├── Config.cmake ├── HPR_BLASCommon.cmake ├── OptimizeForArchitecture.cmake └── hprblas-logos.txt ├── images └── cmake-configuration.png ├── include ├── generators │ └── matrix_generators.hpp ├── hprblas ├── hprblas.h ├── hprblas.hpp ├── matpak │ ├── all.hpp │ ├── any.hpp │ ├── bands.hpp │ ├── checkerboard.hpp │ ├── contents.md │ ├── ek.hpp │ ├── elemat.hpp │ ├── fliplr.hpp │ ├── fliptranspose.hpp │ ├── flipud.hpp │ ├── gt.hpp │ ├── hadamard.hpp │ ├── hankel.hpp │ ├── isa │ │ ├── iscentro.hpp │ │ ├── isdiagdom.hpp │ │ ├── isequal.hpp │ │ ├── isirred.hpp │ │ ├── ism.hpp │ │ ├── isnonneg.hpp │ │ ├── isnormal.hpp │ │ └── isskewcentro.hpp │ ├── kron.hpp │ ├── lt.hpp │ ├── mkcentro.hpp │ ├── mkskewcentro.hpp │ ├── ones.hpp │ ├── relop.hpp │ ├── reshape.hpp │ ├── rot90.hpp │ ├── rowsto.hpp │ ├── size.hpp │ ├── sum.hpp │ └── toeplitz.hpp ├── mtl_extensions.hpp ├── norms.hpp ├── polynomials │ ├── README.md │ └── bernstein.hpp ├── solvers │ ├── cholesky.hpp │ ├── gauss_jordan.hpp │ ├── ldlt.hpp │ └── lu_decomposition.hpp └── utils │ ├── matvec.hpp │ ├── print_utils.hpp │ └── vector_utils.hpp ├── set_env.sh ├── solvers ├── CMakeLists.txt ├── README.md ├── cholesky.cpp ├── gauss_jordan.cpp ├── gaussian_elimination.cpp └── multi_precision.cpp ├── tests ├── posit │ ├── 128bit_posit.cpp │ ├── 48bit_posit.cpp │ ├── 64bit_posit.cpp │ ├── CMakeLists.txt │ ├── big_posits.cpp │ └── common.hpp ├── posit_test_helpers.hpp ├── test_helpers.hpp └── valid_test_helpers.hpp └── tools ├── benchmark ├── CMakeLists.txt ├── error.cpp ├── number_systems.cpp └── perf.cpp ├── characterization ├── 12-threads-16core-Ryzen.txt ├── 16-threads-16core-Ryzen.txt ├── CMakeLists.txt └── fts_cache_test.cpp └── matrix_generation ├── CMakeLists.txt ├── asymmetric.cpp ├── common.hpp ├── symmetric.cpp ├── test.cpp └── uniform_random.cpp /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [stillwater-sc] 4 | open_collective: universal-number-library 5 | -------------------------------------------------------------------------------- /.github/workflows/cmake.yml: -------------------------------------------------------------------------------- 1 | name: CMake 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | env: 10 | # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) 11 | BUILD_TYPE: Release 12 | 13 | jobs: 14 | build: 15 | # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. 16 | # You can convert this to a matrix build if you need cross-platform coverage. 17 | # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix 18 | runs-on: ubuntu-latest 19 | 20 | steps: 21 | - uses: actions/checkout@v2 22 | 23 | - name: Configure CMake 24 | # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. 25 | # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type 26 | run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} 27 | 28 | - name: Build 29 | # Build your program with the given configuration 30 | run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} 31 | 32 | - name: Test 33 | working-directory: ${{github.workspace}}/build 34 | # Execute tests defined by the CMake configuration. 35 | # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail 36 | run: ctest -C ${{env.BUILD_TYPE}} 37 | 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # git identity 2 | .gitconfig 3 | 4 | # Build directory 5 | build 6 | 7 | # CLion IDE config caches 8 | .idea 9 | cmake-build-debug 10 | 11 | # VS IDE config caches 12 | .vs 13 | 14 | # KDE IDE config caches 15 | .kdev4 16 | *.kdev4 17 | 18 | # Log files 19 | # we are using logs for reporting application results 20 | #*.log 21 | -------------------------------------------------------------------------------- /HPR_BLASConfig.cmake: -------------------------------------------------------------------------------- 1 | set(HPR_BLAS_COMMON_CONFIG "${HPR_BLAS_DIR}/tools/cmake/HPR_BLASCommon.cmake") 2 | if( EXISTS ${HPR_BLAS_COMMON_CONFIG}) 3 | include(${HPR_BLAS_COMMON_CONFIG}) 4 | else() 5 | message(ERROR " Could not find the HPR-BLAS library configuration at ${HPR_BLAS_COMMON_CONFIG}") 6 | endif() 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Stillwater Supercomputing, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HPR-BLAS (pronounced hyper-blas) 2 | 3 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/ac548d4ded9a4a6fa6ef90556c3abefc)](https://app.codacy.com/gh/stillwater-sc/hpr-blas?utm_source=github.com&utm_medium=referral&utm_content=stillwater-sc/hpr-blas&utm_campaign=Badge_Grade_Dashboard) 4 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fstillwater-sc%2Fhpr-blas.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fstillwater-sc%2Fhpr-blas?ref=badge_shield) 5 | 6 | Hyper-BLAS: High-Performance Reproducible BLAS using posit arithmetic and user controlled rounding 7 | 8 | # Introduction 9 | 10 | Round-off makes floating point addition nonassociative, and different orders of summation often produce different results. 11 | On a parallel machine, the order of summation will vary from run to run, or even subroutine-call to subroutine-call, depending on scheduling of available resources, so results will different from run to run. 12 | HPR-BLAS offers high-performance reproducible BLAS independent of concurrency levels of the underlying hardware. 13 | 14 | The obvious benefit for reproducibility is that debugging in real-time, multi-core systems is now feasible. 15 | The lack of IEEE Floating Point associativity has made certain bugs elusive in real-time, multi-core systems. 16 | HPR-BLAS is associative, so any roundoff-related errors caused by spurious events can be debugged productively. 17 | 18 | HPR-BLAS enables new applications in very demanding environments. For example, in fracture mechanics, 19 | a shutgun approach is used to generate many random simulations looking for rare events. When one occurs, 20 | an accurate resimulation is started while computing additional information. Reproducibility is essential 21 | to yield correct results. 22 | 23 | HPR-BLAS is using posit arithmetic. Posit arithmetic is a tapered floating point system with perfect symmetry around 0 and +-infinity. 24 | Posits allow custom tailoring of accuracy and dynamic range to the demands of the application. 25 | 26 | 27 | This is particularly important for Deep Learning applications where each layer in the network has unique requirements for accuracy and dynamic range. 28 | Furthermore, posits have a higher informational density than IEEE floats providing additional savings on memory and communication bandwidth and storage. 29 | 30 | # How to build 31 | 32 | The HPR-BLAS library is a pure template library so no library build step is required. However, HPR-BLAS has two dependencies 33 | 1. [Boost](http://www.boost.org) 34 | Boost provides free peer-reviewed portable C++ source libraries that work well with the C++ Standard Library. 35 | 2. [MTL](http://simunova.com) 36 | Matrix Template Library, a high-performance, high-productivity, scalable, parallel linear algebra library for multi-core, multi-GPU, multi-KPU, and distributed memory machines. 37 | 3. [Universal](https://github.com/stillwater-sc/universal) 38 | Universal Library contains different universal number representations and implementations. HPR-BLAS uses the posit/valid number systems. 39 | 40 | To build the programs in the HPR-BLAS library, we are using CMake, so please install the latest version of [CMake](https://cmake.org/download). 41 | 42 | Install [Boost](http://www.boost.org). Typically just a download of a compressed file that you decompress, and point to with an environment variable BOOST_ROOT. More details can be found on the BOOST web site. 43 | 44 | Clone the [MTL library](http://simunova.com). 45 | ``` 46 | git clone https://github.com/simunova/mtl4 47 | ``` 48 | 49 | Next step is to clone the Universal library: 50 | ``` 51 | git clone https://github.com/stillwater-sc/universal 52 | ``` 53 | 54 | Either set environment variables MTL_DIR and UNIVERSAL_DIR to point to the root directories where you installed MTL and the Universal library, or use the CMake tool and set these variables in the CMake user interface. MTL has many options to integrate other high-performance linear algebra libraries and tracing options, but the defaults are sufficient to get started. 55 | 56 | Below shows a successful cmake configuration. 57 | 58 | ![CMake Configuration](images/cmake-configuration.png) 59 | 60 | 61 | 62 | 63 | ## License 64 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fstillwater-sc%2Fhpr-blas.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fstillwater-sc%2Fhpr-blas?ref=badge_large) -------------------------------------------------------------------------------- /applications/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory("blas") 2 | add_subdirectory("dsp") 3 | add_subdirectory("mlp") 4 | add_subdirectory("estimation") 5 | add_subdirectory("numerical") 6 | add_subdirectory("polynomial") 7 | add_subdirectory("calculus") 8 | add_subdirectory("integer") 9 | add_subdirectory("stdlib") 10 | add_subdirectory("matpak") 11 | add_subdirectory("apf") 12 | -------------------------------------------------------------------------------- /applications/apf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "apf" "Applications/Oracles" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/apf/README.md: -------------------------------------------------------------------------------- 1 | # applications/apf 2 | 3 | Arbitrary Precision Floats applications 4 | 5 | # Arbitrary Precision Floats 6 | 7 | For reference applications and algorithms that do not have a formal, analytical golden reference, 8 | high precision, and sometimes arbitrary precision reference calculation is useful to establish 9 | a baseline of numerical error in the approximation. 10 | -------------------------------------------------------------------------------- /applications/apf/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | #pragma once 6 | 7 | #include // uint8_t, etc. 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | -------------------------------------------------------------------------------- /applications/apf/cpp_apfloat.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | const char* pistr = "3.141592653589793238462643383279"; 10 | constexpr float pi = 3.141592653589793238462643383279; 11 | // 3.14159265358979311599796346854 12 | // B 8 4 | = 15 digits of accuracy 13 | 14 | int main() 15 | { 16 | using namespace sw::universal; 17 | using namespace boost::multiprecision; 18 | 19 | /* cfloat print behavior you need to emulate: 20 | native single precision : 0b0.11111110.00000000000000000000000 : 170141183460469231731687303715884105728.0000000000 <-- fixed format takes precision as the number of bits after the fixed point 21 | cfloat single precision : 0b0.11111110.00000000000000000000000 : 170141183460469231731687303715884105728.00000000000000000000000 <-- you are taking the precision of the mantissa 22 | boost single precision : : 170141183460469231731687303715884105728.0000000000 23 | native single precision : 0b0.11111110.00000000000000000000000 : 1.7014118346e+38 <-- scientific format takes precision as the number of bits after the decimal point 24 | cfloat single precision : 0b0.11111110.00000000000000000000000 : 1.70141e+38 <-- you are not honoring precision 25 | boost single precision : : 1.7014118346e+38 26 | */ 27 | auto oldPrecision = std::cout.precision(); 28 | constexpr std::streamsize newPrecision = 10; 29 | { 30 | float v = 1.0f * pow(2.0f, 127.0f); 31 | float spnat = v; 32 | cfloat<32, 8, uint32_t, true> spcf = v; 33 | cpp_bin_float_single spmp = v; 34 | std::cout << std::setprecision(newPrecision); 35 | 36 | std::cout << std::fixed; 37 | std::cout << "native single precision : " << to_binary(spnat) << " : " << spnat << '\n'; 38 | std::cout << "cfloat single precision : " << to_binary(spcf) << " : " << spcf << '\n'; 39 | std::cout << "boost single precision : " << " " << " : " << spmp << '\n'; 40 | std::cout << std::scientific; 41 | std::cout << "native single precision : " << to_binary(spnat) << " : " << spnat << '\n'; 42 | std::cout << "cfloat single precision : " << to_binary(spcf) << " : " << spcf << '\n'; 43 | std::cout << "boost single precision : " << " " << " : " << spmp << '\n'; 44 | 45 | std::cout << spmp << '\n'; 46 | std::cout << std::setw(newPrecision) << std::string(pistr) << '\n'; 47 | } 48 | return 0; 49 | { 50 | cpp_bin_float_quad qp(pistr); 51 | std::cout << "quad precision : " << qp << '\n'; 52 | std::cout << std::setprecision(newPrecision); 53 | std::cout << std::setw(30) << qp << '\n'; 54 | std::cout << std::setw(30) << std::string(pistr) << '\n'; 55 | } 56 | std::cout << std::setprecision(oldPrecision); 57 | return 0; 58 | 59 | { 60 | // Operations at fixed precision and full numeric_limits support: 61 | cpp_bin_float_100 b = 2; 62 | std::cout << std::numeric_limits::digits << std::endl; 63 | std::cout << std::numeric_limits::digits10 << std::endl; 64 | // We can use any C++ std lib function, lets print all the digits as well: 65 | std::cout << std::setprecision(std::numeric_limits::max_digits10) 66 | << log(b) << std::endl; // print log(2) 67 | // We can also use any function from Boost.Math: 68 | std::cout << boost::math::tgamma(b) << std::endl; 69 | // These even work when the argument is an expression template: 70 | std::cout << boost::math::tgamma(b * b) << std::endl; 71 | // And since we have an extended exponent range we can generate some really large 72 | // numbers here (4.0238726007709377354370243e+2564): 73 | std::cout << boost::math::tgamma(cpp_bin_float_100(1000)) << std::endl; 74 | } 75 | 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /applications/apf/cpp_int.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | int main() 11 | { 12 | using namespace sw::universal; 13 | using namespace boost::multiprecision; 14 | 15 | auto oldPrecision = std::cout.precision(); 16 | constexpr std::streamsize newPrecision = 10; 17 | 18 | { 19 | std::int64_t a, b, c, d; 20 | 21 | a = 0x7fffffffffffffff; 22 | b = 1; 23 | c = a + b; 24 | 25 | std::cout << a << " + " << b << " = " << c << " " << to_binary(c) << '\n'; 26 | } 27 | { 28 | cpp_int a, b, c; 29 | 30 | a = 0x7fffffffffffffff; 31 | b = 1; 32 | c = a + b; 33 | 34 | std::cout << a << " + " << b << " = " << c << '\n'; 35 | } 36 | 37 | 38 | return EXIT_SUCCESS; 39 | } 40 | -------------------------------------------------------------------------------- /applications/apf/foreach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MTL_WITH_INITLIST 1 5 | #include 6 | //#include 7 | 8 | /* 9 | template 10 | int countValue(const std::vector& V, const Real& ref) { 11 | int c = 0; 12 | for (auto v: V) { 13 | if (v == ref) ++c; 14 | } 15 | return c; 16 | } 17 | */ 18 | 19 | int main() { 20 | using namespace std; 21 | 22 | using Vector = mtl::dense_vector; 23 | 24 | Vector v(10); 25 | 26 | v = 1; 27 | cout << v << endl; 28 | 29 | return EXIT_SUCCESS; 30 | } 31 | -------------------------------------------------------------------------------- /applications/apf/gnu_float128.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_FULL_VER 2 | #include 3 | int main() { 4 | 5 | std::cout << "Sorry compiler is neither GCC nor Intel: Boost was unenable to configure quad-precision." << std::endl; 6 | 7 | return 0; 8 | } 9 | #else 10 | #include 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | using namespace boost::multiprecision; 17 | 18 | // Operations at 128-bit precision and full numeric_limits support: 19 | float128 b = 2; 20 | // There are 113-bits of precision: 21 | std::cout << std::numeric_limits::digits << std::endl; 22 | // Or 34 decimal places: 23 | std::cout << std::numeric_limits::digits10 << std::endl; 24 | // We can use any C++ std lib function, lets print all the digits as well: 25 | std::cout << std::setprecision(std::numeric_limits::max_digits10) 26 | << log(b) << std::endl; // print log(2) = 0.693147180559945309417232121458176575 27 | // We can also use any function from Boost.Math: 28 | std::cout << boost::math::tgamma(b) << std::endl; 29 | // And since we have an extended exponent range we can generate some really large 30 | // numbers here (4.02387260077093773543702433923004111e+2564): 31 | std::cout << boost::math::tgamma(float128(1000)) << std::endl; 32 | // 33 | // We can declare constants using GCC or Intel's native types, and the Q suffix, 34 | // these can be declared constexpr if required: 35 | 36 | constexpr float128 pi = 3.1415926535897932384626433832795028841971693993751058Q; 37 | 38 | std::cout << "quad-precision PI = " << pi << std::endl; 39 | 40 | return 0; 41 | } 42 | #endif -------------------------------------------------------------------------------- /applications/blas/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "blas" "BLAS/High Performance Reproducible" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/blas/Hilbert/generator.log: -------------------------------------------------------------------------------- 1 | 2 | 3 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 4 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-10x10.log 5 | 6 | real 0m0.723s 7 | user 0m0.015s 8 | sys 0m0.015s 9 | 10 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 11 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-11x11.log 12 | 13 | real 0m0.945s 14 | user 0m0.000s 15 | sys 0m0.015s 16 | 17 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 18 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-12x12.log 19 | 20 | real 0m1.562s 21 | user 0m0.000s 22 | sys 0m0.015s 23 | 24 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 25 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-13x13.log 26 | 27 | real 0m3.022s 28 | user 0m0.000s 29 | sys 0m0.046s 30 | 31 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 32 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-14x14.log 33 | 34 | real 0m8.195s 35 | user 0m0.000s 36 | sys 0m0.031s 37 | 38 | tomtz@SW-LAPTOP-250 MINGW64 /C/Users/tomtz/Documents/dev/clones/hpr-blas/build/applications/blas/Release (master) 39 | $ time ./blas_l3_fused_mm.exe > ../../../../applications/blas/Hilbert-18x18.log 40 | 41 | real 27m58.734s 42 | user 0m0.000s 43 | sys 0m0.015s 44 | 45 | tomtz@sw-desktop-300 MINGW64 /F/Users/tomtz/dev/clones/hpr-blas/build/applications/blas/Release (master) 46 | time ./h19x19.exe > ../../../../applications/blas/Hilbert/Hilbert-19x19.log 47 | 48 | real 115m43.204s 49 | user 0m0.000s 50 | sys 0m0.000s 51 | 52 | tomtz@sw-desktop-300 MINGW64 /F/Users/tomtz/dev/clones/hpr-blas/build/applications/blas/Release (master) 53 | $ time ./h20x20.exe > ../../../../applications/blas/Hilbert/Hilbert-20x20.log 54 | 55 | real 457m14.671s 56 | user 0m0.000s 57 | sys 0m0.031s 58 | 59 | 60 | -------------------------------------------------------------------------------- /applications/blas/README.md: -------------------------------------------------------------------------------- 1 | # examples/blas Basic Linear Algebra subroutine examples # How to build The examples are automatically build by cmake. # Fused-dot product The key differentiator of posits to deliver error-free linear algebra. -------------------------------------------------------------------------------- /applications/blas/common.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // common.hpp : include file for standard system include files, 3 | // or project specific include files that are used frequently, but 4 | // are changed infrequently 5 | // 6 | #include // uint8_t, etc. 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | /* 16 | Best practice for C++ is to define a constexpr literal for these values 17 | so that they can be properly incorporated in compile-time optimizations. 18 | 19 | Mathematical C++ Symbol Decimal Representation 20 | Expression 21 | pi M_PI 3.14159265358979323846 22 | pi/2 M_PI_2 1.57079632679489661923 23 | pi/4 M_PI_4 0.785398163397448309616 24 | 1/pi M_1_PI 0.318309886183790671538 25 | 2/pi M_2_PI 0.636619772367581343076 26 | 2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390 27 | sqrt(2) M_SQRT2 1.41421356237309504880 28 | 1/sqrt(2) M_SQRT1_2 0.707106781186547524401 29 | e M_E 2.71828182845904523536 30 | log_2(e) M_LOG2E 1.44269504088896340736 31 | log_10(e) M_LOG10E 0.434294481903251827651 32 | log_e(2) M_LN2 0.693147180559945309417 33 | log_e(10) M_LN10 2.30258509299404568402 34 | 35 | */ 36 | 37 | #ifdef _WINDOWS 38 | // Including SDKDDKVer.h defines the highest available Windows platform. 39 | 40 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 41 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 42 | 43 | #include 44 | #endif // WINDOWS -------------------------------------------------------------------------------- /applications/blas/fused_mm.cpp: -------------------------------------------------------------------------------- 1 | // fused_mm.cpp: example program to demonstrate BLAS L# Reproducible Matrix-Matrix product 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | 7 | // warning C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' 8 | #pragma warning(disable : 4996) 9 | 10 | // enable posit arithmetic exceptions 11 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 12 | #include 13 | // matrix generators 14 | #include 15 | #include 16 | 17 | int main(int argc, char** argv) 18 | try { 19 | using namespace std; 20 | using namespace mtl; 21 | using namespace sw::universal; 22 | using namespace sw::hprblas; 23 | 24 | int nrOfFailedTestCases = 0; 25 | 26 | // configure the number system 27 | constexpr size_t nbits = 64; 28 | constexpr size_t es = 2; 29 | using Scalar = typename sw::universal::posit; 30 | using Matrix = typename mtl::mat::dense2D; 31 | 32 | size_t N = 10; 33 | Matrix H(N, N); 34 | GenerateHilbertMatrix(H); 35 | 36 | Matrix Hinv(N, N); 37 | GenerateHilbertMatrixInverse(Hinv); 38 | 39 | // using an inefficient linear form 40 | Matrix I1(N, N); 41 | I1 = sw::hprblas::fmm(H, Hinv); 42 | Scalar lcm = Scalar(HilbertScalingFactor(N)); 43 | I1 = I1 / lcm; 44 | printMatrix(cout, "H * H^-1", I1); 45 | 46 | // using a blocked form 47 | constexpr size_t blockSize = 7; 48 | Matrix I2(N, N); 49 | I2 = sw::hprblas::bfmm(H, Hinv, blockSize); 50 | I2 = I2 / lcm; 51 | printMatrix(cout, "H * H^-1", I2); 52 | 53 | mtl::mat::dense2D eye(N, N); 54 | eye = Scalar(1); 55 | 56 | if (!isEqual(I1, eye)) ++nrOfFailedTestCases; 57 | 58 | if (nrOfFailedTestCases) cout << "FAIL" << endl; else cout << "PASS" << endl; 59 | 60 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 61 | } 62 | catch (char const* msg) { 63 | std::cerr << msg << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (const sw::universal::posit_arithmetic_exception& err) { 67 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | catch (const sw::universal::quire_exception& err) { 71 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 72 | return EXIT_FAILURE; 73 | } 74 | catch (const sw::universal::posit_internal_exception& err) { 75 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 76 | return EXIT_FAILURE; 77 | } 78 | catch (const std::runtime_error& err) { 79 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 80 | return EXIT_FAILURE; 81 | } 82 | catch (...) { 83 | std::cerr << "Caught unknown exception" << std::endl; 84 | return EXIT_FAILURE; 85 | } 86 | -------------------------------------------------------------------------------- /applications/blas/fused_mv.cpp: -------------------------------------------------------------------------------- 1 | // fused_mv.cpp: example program to demonstrate BLAS L2 Reproducible Matrix-Vector product 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | 7 | // enable posit arithmetic exceptions 8 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 9 | #include 10 | 11 | using namespace sw::universal; 12 | 13 | int main(int argc, char** argv) 14 | try { 15 | using namespace std; 16 | using namespace mtl; 17 | using namespace sw::universal; 18 | using namespace sw::hprblas; 19 | 20 | int nrOfFailedTestCases = 0; 21 | 22 | using Scalar = posit<32, 2>; 23 | using Matrix = mtl::dense2D; 24 | using Vector = mtl::dense_vector; 25 | size_t m = 5; 26 | size_t n = 4; 27 | size_t N = m * n; 28 | Matrix A(N, N); 29 | laplacian_setup(A, (unsigned int)m, (unsigned int)n); 30 | Vector x(N), b(N); 31 | x = 1; 32 | matvec(b, A, x); 33 | cout << "Matrix A:\n" << A << endl; 34 | cout << "Scaled vector:\n" << b << endl; 35 | 36 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 37 | } 38 | catch (char const* msg) { 39 | std::cerr << msg << std::endl; 40 | return EXIT_FAILURE; 41 | } 42 | catch (const posit_arithmetic_exception& err) { 43 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 44 | return EXIT_FAILURE; 45 | } 46 | catch (const quire_exception& err) { 47 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 48 | return EXIT_FAILURE; 49 | } 50 | catch (const posit_internal_exception& err) { 51 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (const std::runtime_error& err) { 55 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (...) { 59 | std::cerr << "Caught unknown exception" << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | -------------------------------------------------------------------------------- /applications/blas/kronecker_product.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() 5 | try { 6 | using namespace mtl; 7 | 8 | dense2D A(2, 2), B(2, 2), C(4, 4); 9 | 10 | for (size_t r= 0; r < 2; ++r) 11 | for (size_t c= 0; c < 2; ++c) { 12 | A[r][c]= (r+1) * 10 + c+1; 13 | B[r][c]= (r+1) * 1000 + (c+1) * 100; 14 | } 15 | 16 | C= kron(A, B); 17 | std::cout << "kron(A, B) is\n" << C; 18 | 19 | MTL_THROW_IF(C[0][0] != 12100, mtl::runtime_error("Wrong value in C[0][0]")); 20 | MTL_THROW_IF(C[3][3] != 48400, mtl::runtime_error("Wrong value in C[3][3]")); 21 | 22 | return EXIT_SUCCESS; 23 | } 24 | catch (const mtl::runtime_error& e) { 25 | std::cerr << "Caught an MTL runtime error: " << e.what() << std::endl; 26 | return EXIT_FAILURE; 27 | } -------------------------------------------------------------------------------- /applications/blas/l1_axpy.cpp: -------------------------------------------------------------------------------- 1 | // l1_axpy.cpp: example program contrasting a BLAS L1 ?axpy routine between FLOAT and POSIT 2 | // 3 | // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | #include "common.hpp" 7 | // enable posit arithmetic exceptions 8 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 9 | #include 10 | 11 | // axpy: a times x plus y 12 | template 13 | void axpy(size_t n, scale_T a, const vector_T& x, size_t incx, vector_T& y, size_t incy) { 14 | size_t cnt, ix, iy; 15 | for (cnt = 0, ix = 0, iy = 0; cnt < n && ix < x.size() && iy < y.size(); ++cnt, ix += incx, iy += incy) { 16 | y[iy] += a * x[ix]; 17 | } 18 | } 19 | 20 | // print a vector 21 | template 22 | void printStridedVector(std::ostream& ostr, size_t n, vector_T& x, size_t incx = 1) { 23 | size_t cnt, ix; 24 | for (cnt = 0, ix = 0; cnt < n && ix < x.size(); ++cnt, ix += incx) { 25 | cnt == 0 ? ostr << "[" << x[ix] : ostr << ", " << x[ix]; 26 | } 27 | ostr << "]"; 28 | } 29 | 30 | int main(int argc, char** argv) 31 | try { 32 | using namespace sw::universal; 33 | 34 | constexpr size_t nbits = 16; 35 | constexpr size_t es = 1; 36 | //constexpr size_t vecSize = 32; 37 | 38 | int nrOfFailedTestCases = 0; 39 | 40 | constexpr int d = 5; 41 | std::vector< posit > v1 = { 1.0, 2.0, 3.0, 4.0, 5.0 }; 42 | std::vector< posit > v2(d); 43 | posit alpha(SpecificValue::minpos); 44 | 45 | std::cout << "AXPY is\n"; 46 | printStridedVector(std::cout, d, v1, 1); std::cout << '\n'; 47 | printStridedVector(std::cout, d, v2, 1); std::cout << '\n'; 48 | 49 | axpy(d, alpha, v1, 1, v2, 1); 50 | 51 | printStridedVector(std::cout, d, v2, 1); std::cout << '\n'; 52 | 53 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 54 | } 55 | catch (char const* msg) { 56 | std::cerr << msg << std::endl; 57 | return EXIT_FAILURE; 58 | } 59 | catch (const sw::universal::posit_arithmetic_exception& err) { 60 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 61 | return EXIT_FAILURE; 62 | } 63 | catch (const sw::universal::quire_exception& err) { 64 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 65 | return EXIT_FAILURE; 66 | } 67 | catch (const sw::universal::posit_internal_exception& err) { 68 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 69 | return EXIT_FAILURE; 70 | } 71 | catch (const std::runtime_error& err) { 72 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 73 | return EXIT_FAILURE; 74 | } 75 | catch (...) { 76 | std::cerr << "Caught unknown exception" << std::endl; 77 | return EXIT_FAILURE; 78 | } 79 | -------------------------------------------------------------------------------- /applications/blas/lu_decomposition.cpp: -------------------------------------------------------------------------------- 1 | // lu_decomposition.cpp example program comparing float vs posit equation solver 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include "common.hpp" 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | using namespace sw::universal; 17 | 18 | // Turn it off for now 19 | #define USE_POSIT 20 | 21 | int main(int argc, char** argv) 22 | try { 23 | const size_t nbits = 16; 24 | const size_t es = 1; 25 | const size_t vecSize = 32; 26 | 27 | #ifdef USE_POSIT 28 | using Matrix = mtl::dense2D< posit >; 29 | using Vector = mtl::dense_vector< posit >; 30 | #else 31 | using Matrix = mtl::dense2D; 32 | using Vector = mtl::dense_vector; 33 | #endif 34 | 35 | 36 | Matrix A(4, 4), L(4, 4), U(4, 4), AA(4, 4); 37 | Vector v(4); 38 | double c = 1.0; 39 | 40 | for (unsigned i = 0; i < 4; i++) 41 | for (unsigned j = 0; j < 4; j++) { 42 | U[i][j] = i <= j ? c * (i + j + 2) : (0); 43 | L[i][j] = i > j ? c * (i + j + 1) : (i == j ? (1) : (0)); 44 | } 45 | 46 | std::cout << "L is:\n" << L << "U is:\n" << U; 47 | A = L * U; 48 | std::cout << "A is:\n" << A; 49 | AA = adjoint(A); 50 | 51 | for (unsigned i = 0; i < 4; i++) 52 | v[i] = double(i); 53 | 54 | Vector b(A*v), b2(adjoint(A)*v); 55 | 56 | Matrix LU(A); 57 | lu(LU); 58 | std::cout << "LU decomposition of A is:\n" << LU; 59 | 60 | Matrix B(lu_f(A)); 61 | std::cout << "LU decomposition of A (as function result) is:\n" << B; 62 | 63 | Vector v1(lu_solve_straight(A, b)); 64 | std::cout << "v1 is " << v1 << "\n"; 65 | 66 | Vector v2(lu_solve(A, b)); 67 | std::cout << "v2 is " << v2 << "\n"; 68 | 69 | mtl::dense_vector P; 70 | lu(A, P); 71 | std::cout << "LU with pivoting is \n" << with_format(A, 5, 2) << "Permutation is " << P << "\n"; 72 | Vector v3(lu_apply(A, P, b)); 73 | std::cout << "v3 is " << v3 << "\n"; 74 | 75 | Vector v4(lu_adjoint_apply(A, P, b2)); 76 | std::cout << "v4 is " << v4 << "\n"; 77 | 78 | Vector v5(lu_adjoint_solve(AA, b)); 79 | std::cout << "v5 is " << v5 << "\n"; 80 | 81 | int nrOfFailedTestCases = 0; 82 | return nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS; 83 | } 84 | catch (char const* msg) { 85 | cerr << msg << endl; 86 | return EXIT_FAILURE; 87 | } 88 | catch (const posit_arithmetic_exception& err) { 89 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 90 | return EXIT_FAILURE; 91 | } 92 | catch (const quire_exception& err) { 93 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 94 | return EXIT_FAILURE; 95 | } 96 | catch (const posit_internal_exception& err) { 97 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 98 | return EXIT_FAILURE; 99 | } 100 | catch (std::runtime_error& err) { 101 | std::cerr << err.what() << std::endl; 102 | return EXIT_FAILURE; 103 | } 104 | catch (...) { 105 | cerr << "caught unknown exception" << endl; 106 | return EXIT_FAILURE; 107 | } 108 | -------------------------------------------------------------------------------- /applications/calculus/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "calculus" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/calculus/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | #pragma once 6 | 7 | #include // uint8_t, etc. 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | -------------------------------------------------------------------------------- /applications/dsp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "dsp" "Applications/Signal Processing" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/dsp/README.md: -------------------------------------------------------------------------------- 1 | # examples/dsp Digital Signal Processing examples # How to build The examples are automatically build by cmake. # FIR filter This is a Finite Impulse Response filter using posits that are custom fitted to an AD converter acquisition pipeline. It is a demonstration of the benefits of custom posit configurations and the simples example of error-free execution. -------------------------------------------------------------------------------- /applications/dsp/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include // uint8_t, etc. 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #define MTL_INITIALIZER_LISTS 18 | #include 19 | -------------------------------------------------------------------------------- /applications/dsp/fir_filter.cpp: -------------------------------------------------------------------------------- 1 | // fir_filter.cpp example program showing a FIR filter using error-free custom posit configurations 2 | // 3 | // Copyright (C) 2017 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | 8 | #include "common.hpp" 9 | 10 | 11 | /* 12 | 13 | Mathematical C++ Symbol Decimal Representation 14 | Expression 15 | pi M_PI 3.14159265358979323846 16 | pi/2 M_PI_2 1.57079632679489661923 17 | pi/4 M_PI_4 0.785398163397448309616 18 | 1/pi M_1_PI 0.318309886183790671538 19 | 2/pi M_2_PI 0.636619772367581343076 20 | 2/sqrt(pi) M_2_SQRTPI 1.12837916709551257390 21 | sqrt(2) M_SQRT2 1.41421356237309504880 22 | 1/sqrt(2) M_SQRT1_2 0.707106781186547524401 23 | e M_E 2.71828182845904523536 24 | log_2(e) M_LOG2E 1.44269504088896340736 25 | log_10(e) M_LOG10E 0.434294481903251827651 26 | log_e(2) M_LN2 0.693147180559945309417 27 | log_e(10) M_LN10 2.30258509299404568402 28 | 29 | */ 30 | 31 | const double pi = 3.14159265358979323846; // best practice for C++ 32 | 33 | int main(int argc, char** argv) 34 | try { 35 | using namespace std; 36 | using namespace sw::universal; 37 | 38 | const size_t nbits = 16; 39 | const size_t es = 1; 40 | const size_t vecSize = 32; 41 | int nrOfFailedTestCases = 0; 42 | 43 | posit p; 44 | vector< posit > sinusoid(vecSize), weights(vecSize); 45 | 46 | for (int i = 0; i < vecSize; i++) { 47 | sinusoid[i] = sin((float(i) / float(vecSize)) *2.0 * pi); 48 | 49 | weights[i] = 0.5f; 50 | } 51 | 52 | // dot product 53 | posit fir; 54 | fir = 0.0f; 55 | for (int i = 0; i < vecSize; i++) { 56 | fir += sinusoid[i] * weights[i]; 57 | } 58 | cout << "Value is " << fir << endl; 59 | 60 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 61 | } 62 | catch (char const* msg) { 63 | std::cerr << msg << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (const sw::universal::posit_arithmetic_exception& err) { 67 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | catch (const sw::universal::quire_exception& err) { 71 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 72 | return EXIT_FAILURE; 73 | } 74 | catch (const sw::universal::posit_internal_exception& err) { 75 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 76 | return EXIT_FAILURE; 77 | } 78 | catch (std::runtime_error& err) { 79 | std::cerr << err.what() << std::endl; 80 | return EXIT_FAILURE; 81 | } 82 | catch (...) { 83 | std::cerr << "Caught unknown exception" << std::endl; 84 | return EXIT_FAILURE; 85 | } 86 | -------------------------------------------------------------------------------- /applications/estimation/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "estimation" "Applications/Estimation" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/estimation/README.md: -------------------------------------------------------------------------------- 1 | # examples/estimation 2 | 3 | Digital Signal Processing examples, Kalman filter 4 | 5 | # Kalman filter 6 | 7 | This is a linear Kalman filter using [MTL4](http://www.simunova.com). 8 | Based on the algorithm presented in the paper [An Introduction to the Kalman Filter](http://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf). 9 | 10 | It is a demonstration of the benefits of custom posit configurations and the error-free linear algebra. 11 | -------------------------------------------------------------------------------- /applications/estimation/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include // uint8_t, etc. 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | -------------------------------------------------------------------------------- /applications/estimation/kalman.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // kalman.hpp 3 | // 4 | // Copyright (C) 2017-2018 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 7 | #include 8 | 9 | template 10 | class KalmanFilter { 11 | public: 12 | 13 | /** 14 | * Create a Kalman filter with the specified matrices. 15 | * A - System dynamics matrix 16 | * C - Output matrix 17 | * Q - Process noise covariance 18 | * R - Measurement noise covariance 19 | * P - Estimate error covariance 20 | */ 21 | KalmanFilter( 22 | double dt, 23 | const mtl::dense2D& A, 24 | const mtl::dense2D& C, 25 | const mtl::dense2D& Q, 26 | const mtl::dense2D& R, 27 | const mtl::dense2D& P 28 | ) : A(A), C(C), Q(Q), R(R), P0(P), 29 | m(num_rows(C)), n(num_rows(A)), dt(dt), initialized(false), 30 | I(n, n), x_hat(n), t(0), t0(0) 31 | { 32 | I = mtl::mat::identity2D(m,n); // I.setIdentity(); 33 | } 34 | KalmanFilter() {} 35 | 36 | /** 37 | * Initialize the filter with initial states as zero. 38 | */ 39 | void init() { 40 | x_hat.setToZero(); 41 | P = P0; 42 | t0 = 0; 43 | t = t0; 44 | } 45 | 46 | /** 47 | * Initialize the filter with a guess for initial states. 48 | */ 49 | void init(double _t0, const mtl::dense_vector& _x0) { 50 | x_hat = _x0; 51 | P = P0; 52 | t0 = _t0; 53 | t = t0; 54 | } 55 | 56 | /** 57 | * Update the estimated state based on measured values. The 58 | * time step is assumed to remain constant. 59 | */ 60 | void update(const mtl::dense_vector& y) { 61 | mtl::dense_vector x_hat_new(n); 62 | x_hat_new = A * x_hat; 63 | // P = A * P * trans(A) + Q; 64 | // K = P * trans(C) * (C * P * trans(C) + R).inverse(); 65 | // x_hat_new += K * (y - C * x_hat_new); 66 | // P = (I - K * C) * P; 67 | x_hat = x_hat_new; 68 | } 69 | 70 | /** 71 | * Update the estimated state based on measured values, 72 | * using the given time step and dynamics matrix. 73 | */ 74 | void update(const mtl::dense_vector& _y, double _dt, const mtl::dense2D _A) { 75 | A = _A; 76 | dt = _dt; 77 | update(_y); 78 | } 79 | 80 | /** 81 | * Return the current state and time. 82 | */ 83 | mtl::dense_vector state() { return x_hat; }; 84 | double time() { return t; }; 85 | 86 | private: 87 | bool initialized; 88 | 89 | // Matrices for computation 90 | mtl::dense2D A, C, Q, R, P, K, P0; 91 | 92 | // System dimensions 93 | size_t m, n; 94 | 95 | // Initial and current time 96 | double t0, t; 97 | 98 | // Discrete time step 99 | double dt; 100 | 101 | // n-size identity 102 | mtl::dense2D I; 103 | 104 | // Estimated states 105 | mtl::dense_vector x_hat; 106 | }; 107 | -------------------------------------------------------------------------------- /applications/integer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "integer" "Applications/Integer" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/integer/README.md: -------------------------------------------------------------------------------- 1 | # applications/apf 2 | 3 | Arbitrary Precision Integer applications 4 | 5 | # Arbitrary Precision Floats 6 | 7 | For reference applications and algorithms that do not have a formal, analytical golden reference, 8 | high precision, and sometimes arbitrary precision reference calculation is useful to establish 9 | a baseline of numerical error in the approximation. 10 | -------------------------------------------------------------------------------- /applications/integer/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | #pragma once 6 | 7 | #include // uint8_t, etc. 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | -------------------------------------------------------------------------------- /applications/integer/factorial.cpp: -------------------------------------------------------------------------------- 1 | // factorial.cpp: example program to demonstrate factorials with arbitrary precision number systems 2 | // 3 | // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | #include "common.hpp" 7 | // bring in different number systems 8 | #include 9 | #include 10 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 11 | #include 12 | // bring in the factorial function 13 | #include 14 | 15 | int main() 16 | try { 17 | using namespace std; 18 | using namespace boost::multiprecision; 19 | using namespace sw::function; 20 | using namespace sw::universal; 21 | 22 | int N = 30; 23 | int128_t v = factorial(N); 24 | std::cout << typeid(v).name() << " : \n" << v << std::endl; 25 | 26 | cpp_int u = factorial(N); 27 | std::cout << typeid(u).name() << " : \n" << u << std::endl; 28 | 29 | integer<128> w = factorial< integer<128> >(N); 30 | std::cout << typeid(w).name() << " : \n" << w << std::endl; 31 | 32 | using Posit64 = posit<64, 3>; 33 | Posit64 p64 = factorial(N); 34 | cout << typeid(p64).name() << " : \n" << setprecision(40) << p64 << endl; 35 | 36 | using Posit128 = posit<128, 4>; 37 | Posit128 p128 = factorial(N); 38 | cout << typeid(p128).name() << " : \n" << setprecision(40) << p128 << endl; 39 | 40 | using Posit256 = posit<256, 4>; 41 | Posit256 p256 = factorial(N); 42 | cout << typeid(p256).name() << " : \n" << setprecision(40) << p256 << endl; 43 | 44 | return EXIT_SUCCESS; 45 | } 46 | catch (char const* msg) { 47 | std::cerr << msg << std::endl; 48 | return EXIT_FAILURE; 49 | } 50 | catch (const sw::universal::posit_arithmetic_exception& err) { 51 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (const sw::universal::quire_exception& err) { 55 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (const sw::universal::posit_internal_exception& err) { 59 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | catch (std::runtime_error& err) { 63 | std::cerr << err.what() << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (...) { 67 | std::cerr << "Caught unknown exception" << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | -------------------------------------------------------------------------------- /applications/matpak/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "matpak" "Applications/Matrix Utilities" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/matpak/README.md: -------------------------------------------------------------------------------- 1 | # Matrix Utility Package 2 | 3 | MATPAK is a collection of free functions of matrix utilities. It is included in the HPRBLAS project. Some functions make use of [MTL4](http://www.simunova.com). 4 | 5 | MATPAK contains replicates several standard MATLAB functions such as `fliplr`, `flipup`, `toeplitz`, `hankel`, and `rot90`. The project was originally developed to facilitate research on row stochastic centrosymmetric matrices. 6 | 7 | # Contents in Brief 8 | 9 | Let A, B be matrices and v a vector. 10 | 11 | bands - extracts diagonals from a matrix (e.g., tridiagonal) 12 | diag - creates a diagonal matrix or extracts the diagonal 13 | diam(A) - calculates diameter of a matrix 14 | ek - Standard basis element. `ek(i,n)` 15 | fliplr - Flip matrix elements left/right 16 | flipud - Flip matrix up/down 17 | gt - greater than, e.g., A > B, returns 1/0 matrix 18 | hankel - Generate hankel matrix 19 | iscentro - determines if A is centrosymmetric (bool) 20 | isequal - determines if two matrices are equal (bool) 21 | ism - determines if a matrix is an M-matrix (bool) 22 | isnormal - determines if a matrix A is normal 23 | kron(A,B) - Kronecker product of A with B 24 | mkcentro - makes a centrosymmetric matrix 25 | reshape - Maps m x n --> r x c where mn = rc 26 | rot90 - Rotates a matrix 90 degrees counterclockwise 27 | rowsto - Generates a row stochastic matrix 28 | size - caclulates the size of a matrix 29 | sum - computes sum of entries per dimension 30 | toeplitz - Generates a Toeplitz matrix (see hankel) 31 | 32 | 33 | -------------------------------------------------------------------------------- /applications/matpak/bands.cpp: -------------------------------------------------------------------------------- 1 | // bands.cpp : extracts banded matrix (e.g., tridiagonal) 2 | // from a given matrix A 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | // COMMON LIBRARIES 10 | #include 11 | #include // includes mtl 12 | 13 | // DEPENDENCIES 14 | #include 15 | #include 16 | 17 | // Selects posits or floats 18 | #define USE_POSIT 1 19 | 20 | int main () 21 | { 22 | // COMMON NAMESPACES 23 | using namespace mtl; 24 | using namespace sw::universal; 25 | using namespace sw::hprblas; 26 | using namespace sw::hprblas::matpak; 27 | 28 | std::cout << std::setprecision(5); 29 | 30 | /* 31 | // --- POSITS --- 32 | constexpr size_t nbits = 32; 33 | constexpr size_t es = 2; 34 | using Scalar = posit; 35 | using Matrix = mtl::mat::dense2D< Scalar >; 36 | Matrix A = rowsto< Matrix >(5,5); // 37 | std::cout << A << std::endl; 38 | fliplr(A); 39 | std::cout << A << std::endl; 40 | */ 41 | 42 | // --- FLOATS --- 43 | using Scalar = double; 44 | using Matrix = mtl::mat::dense2D< Scalar >; 45 | Matrix A = rowsto< Matrix >(5,5); // 46 | std::cout << A << std::endl; 47 | 48 | Matrix v = {{0,-1,0,1,0}}; 49 | std::cout << bands(A,v) << std::endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /applications/matpak/checkerboard.cpp: -------------------------------------------------------------------------------- 1 | // checkerboard.cpp: Returns +1 / -1 checkerboard pattern matrix 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // COMMON LIBRARIES 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | // Selects posits or floats 15 | #define USE_POSIT 0 16 | 17 | int main () 18 | { 19 | using namespace std; 20 | using namespace mtl; 21 | using namespace sw::universal; 22 | using namespace sw::hprblas; 23 | using namespace sw::hprblas::matpak; 24 | 25 | #if USE_POSIT 26 | constexpr size_t nbits = 16; 27 | constexpr size_t es = 1; 28 | using Scalar = posit; 29 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 30 | #else 31 | using Scalar = double; 32 | #endif 33 | using Matrix = mtl::mat::dense2D; 34 | std::cout << checkerboard(8) << std::endl; 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /applications/matpak/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include // uint8_t, etc. 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | -------------------------------------------------------------------------------- /applications/matpak/diag.cpp: -------------------------------------------------------------------------------- 1 | // diag.cpp : 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // Calculate the diameter of a matrix. 9 | // Functions needed: max, norm, mtl::iall=: 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | 19 | /* 20 | Returns a view of a matrix A from diagonal begin to end. 21 | 22 | The main diagonal is numbered 0; the off-diagonal below the main one is -1. 23 | Accordingly, the off-diagonal above the main is 1. The parameters begin and end 24 | specify a right-open interval. For, instance bands(A, -1, 2) yields a tridiagonal matrix. 25 | 26 | See: http://new.simunova.com/en/mtl4/ 27 | */ 28 | 29 | int main () 30 | { 31 | using namespace std; 32 | using namespace mtl; 33 | using namespace sw::universal; 34 | using namespace sw::hprblas; 35 | using namespace sw::hprblas::matpak; 36 | cout << setprecision(5); 37 | { 38 | using Scalar = posit<16,1>; 39 | // using Scalar = double; 40 | using Matrix = mtl::dense2D; 41 | using Vector = mtl::dense_vector; 42 | Matrix A = sw::hprblas::matpak::rowsto< Matrix >(5,5); 43 | 44 | auto B = mtl::mat::bands(A, 0, 1); 45 | size_t m = num_rows(B); 46 | Vector d(m); 47 | 48 | for (size_t i=0;i, posit<16, 1> >::type' 70 | (aka 'sw::universal::posit<16, 1>') to 'const 71 | mtl::operations::bracket_proxy, 72 | mtl::mat::parameters >, mtl::mat::dense2D, 74 | mtl::mat::parameters > &, sw::universal::posit<16, 1> &>' for 1st argument 76 | struct bracket_proxy 77 | */ 78 | #endif 79 | 80 | 81 | std::cout << d << std::endl; 82 | } 83 | 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /applications/matpak/diam.cpp: -------------------------------------------------------------------------------- 1 | // diam.cpp : 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | // Calculate the diameter of a matrix. 10 | // Functions needed: max, norm, mtl::iall=: 11 | #include 12 | #include 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | int main () 19 | { 20 | using namespace std; 21 | using namespace mtl; 22 | using namespace sw::universal; 23 | using namespace sw::hprblas; 24 | using namespace sw::hprblas::matpak; 25 | cout << setprecision(5); 26 | { 27 | using Scalar = posit<16,1>; 28 | // using Scalar = double; 29 | auto v = ek(3,8); 30 | std::cout << v << std::endl; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /applications/matpak/ek.cpp: -------------------------------------------------------------------------------- 1 | // ek.cpp : Standard basis vector 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Selects posits or floats 13 | #define USE_POSIT 1 14 | 15 | int main () 16 | { 17 | using namespace std; 18 | using namespace mtl; 19 | using namespace sw::universal; 20 | using namespace sw::hprblas; 21 | using namespace sw::hprblas::matpak; 22 | 23 | #if USE_POSIT 24 | constexpr size_t nbits = 16; 25 | constexpr size_t es = 1; 26 | using Scalar = posit; 27 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 28 | #else 29 | using Scalar = double; 30 | #endif 31 | 32 | 33 | // Main Program 34 | auto v = ek(3,8); 35 | std::cout << setprecision(5); 36 | std::cout << v << std::endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /applications/matpak/elemat.cpp: -------------------------------------------------------------------------------- 1 | // elemat.cpp : Elementary Matrix 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Selects posits or floats 13 | #define USE_POSIT 1 14 | 15 | int main () 16 | { 17 | using namespace std; 18 | using namespace mtl; 19 | using namespace sw::universal; 20 | using namespace sw::hprblas; 21 | using namespace sw::hprblas::matpak; 22 | 23 | #if USE_POSIT 24 | constexpr size_t nbits = 16; 25 | constexpr size_t es = 1; 26 | using Scalar = posit; 27 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 28 | #else 29 | using Scalar = double; 30 | #endif 31 | // --------------------------------------------- // 32 | 33 | using Matrix = mtl::mat::dense2D< Scalar >; 34 | Matrix A = elemat< Matrix >(8,3,4,2); // 35 | cout << setprecision(5); 36 | std::cout << A << std::endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /applications/matpak/fliplr.cpp: -------------------------------------------------------------------------------- 1 | // fliplr.cpp : Flip matix in left/right direction. 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // Selects posits or floats 13 | #define USE_POSIT 1 14 | 15 | int main () 16 | { 17 | using namespace std; 18 | using namespace mtl; 19 | using namespace sw::universal; 20 | using namespace sw::hprblas; 21 | using namespace sw::hprblas::matpak; 22 | cout << setprecision(5); 23 | 24 | #if USE_POSIT 25 | constexpr size_t nbits = 32; 26 | constexpr size_t es = 2; 27 | using Scalar = posit; 28 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 29 | #else 30 | using Scalar = double; 31 | #endif 32 | 33 | // --- 34 | using Matrix = mtl::mat::dense2D< Scalar >; 35 | Matrix A = rowsto< Matrix >(5,5); // 36 | std::cout << A << std::endl; 37 | auto B = fliplr(A); 38 | std::cout << B << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /applications/matpak/fliptranspose.cpp: -------------------------------------------------------------------------------- 1 | // fliptranspose.cpp : Reflect Matrix across counter-identity 2 | // used to create Counter-identity matrix 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | int main () 19 | { 20 | using namespace std; 21 | using namespace mtl; 22 | using namespace sw::universal; 23 | using namespace sw::hprblas; 24 | using namespace sw::hprblas::matpak; 25 | cout << setprecision(8); 26 | 27 | #if USE_POSIT 28 | constexpr size_t nbits = 16; 29 | constexpr size_t es = 1; 30 | using Scalar = posit; 31 | cout << "\n\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 32 | #else 33 | using Scalar = double; 34 | #endif 35 | 36 | using Matrix = mtl::mat::dense2D< Scalar >; 37 | Matrix A = rowsto< Matrix >(5,5); // 38 | std::cout << A << std::endl; 39 | std::cout << fliptranspose(A) << std::endl; 40 | 41 | //return 0; 42 | } 43 | -------------------------------------------------------------------------------- /applications/matpak/flipud.cpp: -------------------------------------------------------------------------------- 1 | // flipud.cpp : Flip matrix in up/down direction. 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // Selects posits or floats 14 | #define USE_POSIT 1 15 | 16 | int main () 17 | { 18 | using namespace std; 19 | using namespace mtl; 20 | using namespace sw::universal; 21 | using namespace sw::hprblas; 22 | using namespace sw::hprblas::matpak; 23 | cout << setprecision(5); 24 | 25 | #if USE_POSIT 26 | constexpr size_t nbits = 16; 27 | constexpr size_t es = 1; 28 | using Scalar = posit; 29 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 30 | #else 31 | using Scalar = double; 32 | #endif 33 | // --------------------------------------------- // 34 | 35 | using Matrix = mtl::mat::dense2D< Scalar >; 36 | Matrix A = rowsto< Matrix >(5,5); // 37 | std::cout << A << std::endl; 38 | flipud(A); 39 | std::cout << A << std::endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /applications/matpak/gt.cpp: -------------------------------------------------------------------------------- 1 | // gt.cpp : Greater than, e.g., A>B 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // Selects posits or floats 15 | #define USE_POSIT 1 16 | 17 | 18 | int main () 19 | { 20 | using namespace std; 21 | using namespace mtl; 22 | using namespace sw::universal; 23 | using namespace sw::hprblas; 24 | using namespace sw::hprblas::matpak; 25 | cout << setprecision(5); 26 | 27 | #if USE_POSIT 28 | constexpr size_t nbits = 16; 29 | constexpr size_t es = 1; 30 | using Scalar = posit; 31 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 32 | #else 33 | using Scalar = double; 34 | #endif 35 | 36 | // ----------------------------- // 37 | using Matrix = mtl::mat::dense2D< Scalar >; 38 | Matrix A = rowsto< Matrix >(5,5); // 39 | Matrix B = rowsto< Matrix >(5,5); // 40 | 41 | // B = A; 42 | std::cout << gt(A, B) << std::endl; 43 | auto C = A > B; 44 | std::cout << C << std::endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /applications/matpak/hadamard.cpp: -------------------------------------------------------------------------------- 1 | // hadamard.cpp : Hadamard product of A.*B 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // COMMON LIBRARIES 9 | #include 10 | #include 11 | 12 | // DEPENDENCIES 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | 19 | int main () 20 | { 21 | using namespace std; 22 | using namespace mtl; 23 | using namespace sw::universal; 24 | using namespace sw::hprblas; 25 | using namespace sw::hprblas::matpak; 26 | cout << setprecision(5); 27 | 28 | #if USE_POSIT 29 | constexpr size_t nbits = 32; 30 | constexpr size_t es = 2; 31 | using Scalar = posit; 32 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 33 | #else 34 | using Scalar = double; 35 | #endif 36 | 37 | using Matrix = mtl::mat::dense2D< Scalar >; 38 | 39 | Matrix A = { 40 | {10, 2, 3, 4}, 41 | {5, 7, 6, 2}, 42 | {8, 7, 30, 5}, 43 | {1, 1, 7, 41} 44 | }; 45 | 46 | Matrix B = { 47 | {1, 0, 2, 4}, 48 | {5, 2, 1, 9}, 49 | {8, 7, 3, 5}, 50 | {4, 3, 2, 1} 51 | }; 52 | 53 | Matrix C = hadamard(A,B); 54 | cout << "A.*B = \n" << A << "\n * \n" << B << "\n = \n" << C << "\n\n" << endl; 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /applications/matpak/hankel.cpp: -------------------------------------------------------------------------------- 1 | // hankel.cpp : Test Hankel matrix (test hankel.hpp) 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Selects posits or floats 13 | #define USE_POSIT 1 14 | 15 | int main () 16 | { 17 | using namespace std; 18 | using namespace mtl; 19 | using namespace sw::universal; 20 | using namespace sw::hprblas; 21 | using namespace sw::hprblas::matpak; 22 | cout << setprecision(5); 23 | 24 | #if USE_POSIT 25 | constexpr size_t nbits = 16; 26 | constexpr size_t es = 1; 27 | using Scalar = posit; 28 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 29 | #else 30 | using Scalar = double; 31 | #endif 32 | 33 | // ---- 34 | using Vector = mtl::vec::dense_vector< Scalar >; 35 | Vector c{1, 2, 3, 4, 5}; 36 | Vector r{7, 5, 6, 7, 8}; 37 | auto A = hankel(c,r); 38 | std::cout << A << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /applications/matpak/iscentro.cpp: -------------------------------------------------------------------------------- 1 | // iscentro.cpp : Determine if a matrix is centrosymmetric 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Selects posits or floats 13 | #define USE_POSIT 1 14 | 15 | int main () 16 | { 17 | using namespace std; 18 | using namespace mtl; 19 | using namespace sw::universal; 20 | using namespace sw::hprblas; 21 | using namespace sw::hprblas::matpak; 22 | cout << setprecision(5); 23 | 24 | #if USE_POSIT 25 | constexpr size_t nbits = 16; 26 | constexpr size_t es = 1; 27 | using Scalar = posit; 28 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 29 | #else 30 | using Scalar = double; 31 | #endif 32 | 33 | using Matrix = mtl::mat::dense2D< Scalar >; 34 | // Matrix A = rowsto< Matrix >(5,5); // 35 | Matrix A = { 36 | {1, 2, 3, 4}, 37 | {5, 6, 7, 8}, 38 | {8, 7, 6, 5}, 39 | {4, 3, 2, 1} 40 | }; 41 | std::cout << A << std::endl; 42 | 43 | int x = 0; 44 | if(iscentro(A)){ 45 | x = 1; 46 | } 47 | std::cout << x << std::endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /applications/matpak/isdiagdom.cpp: -------------------------------------------------------------------------------- 1 | // isdiagdom.cpp : Is the matrix A diagonally domainant 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // COMMON LIBRARIES 9 | #include 10 | #include 11 | 12 | // DEPENDENCIES 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | 19 | int main () 20 | { 21 | using namespace std; 22 | using namespace mtl; 23 | using namespace sw::universal; 24 | using namespace sw::hprblas; 25 | using namespace sw::hprblas::matpak; 26 | cout << setprecision(5); 27 | 28 | #if USE_POSIT 29 | constexpr size_t nbits = 32; 30 | constexpr size_t es = 2; 31 | using Scalar = posit; 32 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 33 | #else 34 | using Scalar = double; 35 | #endif 36 | 37 | using Matrix = mtl::mat::dense2D< Scalar >; 38 | 39 | Matrix A = { 40 | {10, 2, 3, 4}, 41 | {5, 26, 7, 8}, 42 | {8, 7, 30, 5}, 43 | {4, 3, 2, 11} 44 | }; 45 | cout << "The diagonal domainance of A is: " << isdiagdom(A) << "\n\n" << endl; 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /applications/matpak/isequal.cpp: -------------------------------------------------------------------------------- 1 | // isequal.cpp : Are two matrices equal? e.g., A == B 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // Selects posits or floats 15 | #define USE_POSIT 1 16 | 17 | 18 | int main () 19 | { 20 | using namespace std; 21 | using namespace mtl; 22 | using namespace sw::universal; 23 | using namespace sw::hprblas; 24 | using namespace sw::hprblas::matpak; 25 | cout << setprecision(5); 26 | 27 | #if USE_POSIT 28 | constexpr size_t nbits = 32; 29 | constexpr size_t es = 2; 30 | using Scalar = posit; 31 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 32 | #else 33 | using Scalar = double; 34 | #endif 35 | 36 | using Matrix = mtl::mat::dense2D< Scalar >; 37 | Matrix A = rowsto< Matrix >(5,5); // 38 | Matrix B = rowsto< Matrix >(5,5); // 39 | 40 | // B = A; 41 | std::cout << isequal(A,B) << std::endl; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /applications/matpak/ism.cpp: -------------------------------------------------------------------------------- 1 | // ism.cpp: test to validate that a matrix is an M matrix 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | // Selects posits or floats 12 | #define USE_POSIT 0 13 | 14 | template 15 | bool isMatrixAnMMatrix(const Matrix& A) { 16 | return false; 17 | } 18 | 19 | int main(int argc, char** argv) 20 | try { 21 | using namespace std; 22 | using namespace mtl; 23 | using namespace sw::universal; 24 | using namespace sw::hprblas; 25 | 26 | #if USE_POSIT 27 | using Ty = sw::universal::posit<8, 0>; 28 | using Matrix = mtl::dense2D< Ty >; 29 | using Vector = mtl::dense_vector< Ty >; 30 | #else 31 | using Ty = float; 32 | using Matrix = mtl::dense2D; 33 | using Vector = mtl::dense_vector; 34 | #endif 35 | 36 | constexpr int n = 3; // Number of states 37 | 38 | mtl::dense2D A(n, n); // System dynamics matrix 39 | 40 | A = 1; // create identity matrix 41 | 42 | if (sw::hprblas::ism(A)) { 43 | cout << "A is an M-matrix\n" << A << '\n'; 44 | } else { 45 | cout << "A is not an M-Matrix\n" << A << '\n'; 46 | } 47 | 48 | return EXIT_SUCCESS; 49 | } 50 | catch (char const* msg) { 51 | std::cerr << msg << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (const sw::universal::posit_arithmetic_exception& err) { 55 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (const sw::universal::quire_exception& err) { 59 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | catch (const sw::universal::posit_internal_exception& err) { 63 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (std::runtime_error& err) { 67 | std::cerr << err.what() << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | catch (...) { 71 | std::cerr << "Caught unknown exception" << std::endl; 72 | return EXIT_FAILURE; 73 | } 74 | -------------------------------------------------------------------------------- /applications/matpak/isnormal.cpp: -------------------------------------------------------------------------------- 1 | // isnormal.cpp : Determines if a matrix is normal (i.e., A'A = AA'?) 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // Selects posits or floats 14 | #define USE_POSIT 1 15 | 16 | int main () 17 | { 18 | using namespace std; 19 | using namespace mtl; 20 | using namespace sw::universal; 21 | using namespace sw::hprblas; 22 | using namespace sw::hprblas::matpak; 23 | cout << setprecision(5); 24 | 25 | #if USE_POSIT 26 | constexpr size_t nbits = 16; 27 | constexpr size_t es = 1; 28 | using Scalar = posit; 29 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 30 | #else 31 | using Scalar = double; 32 | #endif 33 | 34 | // 35 | using Matrix = mtl::mat::dense2D< Scalar >; 36 | Matrix A = rowsto< Matrix >(5,5); // 37 | std::cout << A << std::endl; 38 | Matrix A_transpose(mtl::mat::trans(A)); 39 | std::cout << A_transpose << std::endl; 40 | std::cout << A*A_transpose << std::endl; 41 | std::cout << A_transpose*A << std::endl; 42 | 43 | if(isnormal(A,0.00001)){ 44 | std::cout << "Matrix A is normal" << std::endl; 45 | }else{ 46 | std::cout << "Matrix A is NOT normal" << std::endl; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /applications/matpak/kron.cpp: -------------------------------------------------------------------------------- 1 | // kron.cpp : Kronecker Product of two matrices 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | /* From MATLAB: 10 | kron(X,Y) is the Kronecker tensor product of X and Y. 11 | The result is a large matrix formed by taking all possible 12 | products between the elements of X and those of Y. For 13 | example, if X is 2 by 3, then kron(X,Y) is 14 | 15 | [ X(1,1)*Y X(1,2)*Y X(1,3)*Y 16 | X(2,1)*Y X(2,2)*Y X(2,3)*Y ] 17 | 18 | If either X or Y is sparse, only nonzero elements are multiplied 19 | in the computation, and the result is sparse. 20 | 21 | A \in M(p,q) and B \in M(m,n), then kron(A,B) \in M(mxp, nxq) 22 | 23 | */ 24 | 25 | 26 | // COMMON LIBRARIES 27 | #include 28 | #include 29 | #include 30 | 31 | // Selects posits or floats 32 | #define USE_POSIT 0 33 | 34 | int main () 35 | { 36 | using namespace std; 37 | using namespace mtl; 38 | using namespace sw::universal; 39 | using namespace sw::hprblas; 40 | using namespace sw::hprblas::matpak; 41 | cout << setprecision(5); 42 | 43 | #if USE_POSIT 44 | constexpr size_t nbits = 16; 45 | constexpr size_t es = 1; 46 | using Scalar = posit; 47 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 48 | #else 49 | using Scalar = double; 50 | #endif 51 | 52 | using Matrix = mtl::mat::dense2D< Scalar >; 53 | Matrix A = { 54 | {1, 2, 3}, 55 | {4, 5, 6} 56 | }; 57 | Matrix B = { 58 | {1, 2, 3}, 59 | {4, 5, 6}, 60 | {7, 8, 9} 61 | }; 62 | std::cout << kron(A,B) << std::endl; 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /applications/matpak/ones.cpp: -------------------------------------------------------------------------------- 1 | // ones.cpp : Matrix of all ones 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // COMMON LIBRARIES 9 | #include 10 | #include // includes mtl 11 | 12 | // DEPENDENCIES 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | int main () 19 | { 20 | // COMMON NAMESPACES 21 | using namespace std; 22 | using namespace mtl; 23 | using namespace sw::universal; 24 | using namespace sw::hprblas; 25 | using namespace sw::hprblas::matpak; 26 | 27 | cout << setprecision(5); 28 | 29 | /* 30 | // --- POSITS --- 31 | constexpr size_t nbits = 32; 32 | constexpr size_t es = 2; 33 | using Scalar = posit; 34 | using Matrix = mtl::mat::dense2D< Scalar >; 35 | Matrix A = rowsto< Matrix >(5,5); // 36 | std::cout << A << std::endl; 37 | fliplr(A); 38 | std::cout << A << std::endl; 39 | */ 40 | 41 | // --- FLOATS --- 42 | using Scalar = double; 43 | using Matrix = mtl::mat::dense2D< Scalar >; 44 | auto A = Ones(5,7); // 45 | cout << A << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /applications/matpak/reshape.cpp: -------------------------------------------------------------------------------- 1 | // reshape.cpp : Reshapes an m x n matrix to a p x q 2 | // Note: m x n = p x q 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // Selects posits or floats 15 | #define USE_POSIT 0 16 | 17 | int main () 18 | { 19 | using namespace std; 20 | using namespace mtl; 21 | using namespace sw::universal; 22 | using namespace sw::hprblas; 23 | using namespace sw::hprblas::matpak; 24 | cout << setprecision(5); 25 | 26 | #if USE_POSIT 27 | constexpr size_t nbits = 32; 28 | constexpr size_t es = 2; 29 | using Scalar = posit; 30 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 31 | #else 32 | using Scalar = double; 33 | #endif 34 | 35 | typedef mtl::mat::parameters column_matrix; 36 | using Matrix = mtl::dense2D ; 37 | Matrix A = rowsto< Matrix >(6,2); // 38 | std::cout << A << std::endl; 39 | auto T = reshape(A,4,3); 40 | std::cout << T << std::endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /applications/matpak/rot90.cpp: -------------------------------------------------------------------------------- 1 | // rot90.cpp : Rotates a matrix 90 degrees couter-clockwise 2 | // Notes: used in making counter-identity matrix 3 | // i.e., rot90(eye(n)) 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // Selects posits or floats 16 | #define USE_POSIT 1 17 | 18 | int main () 19 | { 20 | using namespace std; 21 | using namespace mtl; 22 | using namespace sw::universal; 23 | using namespace sw::hprblas; 24 | using namespace sw::hprblas::matpak; 25 | cout << setprecision(5); 26 | 27 | #if USE_POSIT 28 | constexpr size_t nbits = 32; 29 | constexpr size_t es = 2; 30 | using Scalar = posit; 31 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 32 | #else 33 | using Scalar = double; 34 | #endif 35 | 36 | using Matrix = mtl::mat::dense2D< Scalar >; 37 | Matrix A = rowsto< Matrix >(5,5); // 38 | std::cout << A << std::endl; 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /applications/matpak/rowsto.cpp: -------------------------------------------------------------------------------- 1 | // rowsto.cpp : Generates row stochastic matrix 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | #include 10 | #include 11 | //#include 12 | 13 | // Selects posits or floats 14 | #define USE_POSIT 1 15 | 16 | 17 | int main () 18 | { 19 | using namespace std; 20 | using namespace mtl; 21 | using namespace sw::universal; 22 | using namespace sw::hprblas; 23 | cout << setprecision(20); 24 | 25 | #if USE_POSIT 26 | constexpr size_t nbits = 32; 27 | constexpr size_t es = 2; 28 | using Scalar = posit; 29 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 30 | #else 31 | using Scalar = double; 32 | #endif 33 | 34 | using Matrix = mtl::mat::dense2D< Scalar >; 35 | Matrix A = sw::hprblas::matpak::rowsto< Matrix >(5,5); // 36 | std::cout << A << std::endl; 37 | for (size_t i=0;i<5;++i){ 38 | cout << sum(A[i][iall])< 11 | #include 12 | 13 | // #include // not needed for size, using for other tests. remove in production 14 | 15 | // DEPENDENCIES 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | // Selects posits or floats 23 | #define USE_POSIT 0 24 | 25 | int main () 26 | { 27 | // COMMON NAMESPACES 28 | using namespace std; 29 | using namespace mtl; using mtl::iall; 30 | using namespace sw::universal; 31 | using namespace sw::hprblas; 32 | using namespace sw::hprblas::matpak; 33 | 34 | cout << setprecision(5); 35 | 36 | 37 | 38 | 39 | #if USE_POSIT 40 | constexpr size_t nbits = 32; 41 | constexpr size_t es = 2; 42 | using Scalar = posit; 43 | using Matrix = mtl::mat::dense2D< Scalar >; 44 | cout << "\nUsing POSIT<" << nbits << "," << es << ">\n" << endl; 45 | #else 46 | using Scalar = double; 47 | using Matrix = mtl::mat::dense2D< Scalar >; 48 | #endif 49 | 50 | 51 | Matrix A = rowsto< Matrix >(7,7); // 52 | cout << "Matrix A = \n" << A << endl; 53 | cout << "Size A = " << size(A) << endl; 54 | 55 | // Matrix B = uniform_rand(6,6); 56 | // cout << "Matrix B = \n" << B << endl; 57 | 58 | 59 | //submatrix from matrix per irange 60 | using mtl::irange; 61 | irange row(2, 4), col(1, 7); 62 | dense2D B1= A[row][col]; 63 | std::cout << "B1 is\n" << B1 << "\n"; 64 | 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /applications/matpak/sum.cpp: -------------------------------------------------------------------------------- 1 | // sum.cpp : sum of elements in matrix along dimensions specified 2 | // S = sum(A,dim) 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: James Quinlan 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | // COMMON LIBRARIES 9 | #include 10 | #include 11 | 12 | // DEPENDENCIES 13 | #include 14 | #include 15 | 16 | // Selects posits or floats 17 | #define USE_POSIT 1 18 | 19 | 20 | int main () 21 | { 22 | // COMMON NAMESPACES 23 | using namespace std; 24 | using namespace mtl; 25 | using namespace sw::universal; 26 | using namespace sw::hprblas; 27 | using namespace sw::hprblas::matpak; 28 | 29 | cout << setprecision(5); 30 | 31 | #if USE_POSIT 32 | constexpr size_t nbits = 32; 33 | constexpr size_t es = 2; 34 | using Scalar = posit; 35 | cout << "\n\n Using POSIT<" << nbits << "," << es << ">\n" << endl; 36 | #else 37 | using Scalar = double; 38 | #endif 39 | 40 | using Matrix = mtl::mat::dense2D< Scalar >; 41 | Matrix A = rowsto< Matrix >(5,5); // 42 | 43 | cout << A << endl; 44 | cout << "Sum of all elements\n " << sum(A,0) << endl; 45 | cout << "Column sums = \n" << sum(A,1) << endl; 46 | cout << "Row sums = \n" << sum(A,2) << endl; 47 | // cout << "Tensor, Layer sums\n " << sum(A,3) << endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /applications/matpak/toeplitz.cpp: -------------------------------------------------------------------------------- 1 | // toeplitz.cpp : Generate Toeplitz matrix 2 | // Example: A = toeplitz(c,r); where c and r are vectors 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | // Selects posits or floats 15 | #define USE_POSIT 1 16 | 17 | int main () 18 | { 19 | using namespace std; 20 | using namespace mtl; 21 | using namespace sw::universal; 22 | using namespace sw::hprblas; 23 | using namespace sw::hprblas::matpak; 24 | 25 | cout << setprecision(5); 26 | { 27 | using Scalar = double; 28 | using Vector = mtl::vec::dense_vector< Scalar >; 29 | Vector c{1, 2, 3, 4}; 30 | Vector r{7, 5, 6, 7, 8}; 31 | auto A = toeplitz(c,r); 32 | std::cout << A << std::endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /applications/mlp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "mlp" "Applications/DNN" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/mlp/README.md: -------------------------------------------------------------------------------- 1 | # examples/neural-net 2 | 3 | Artificial Intelligence Neural Network example 4 | 5 | # Multilayer Perceptron 6 | 7 | This is a Neural Network using [MTL4](http://www.simunova.com). 8 | 9 | It is a demonstration of the benefits of custom posit configurations for neural network tensors. 10 | -------------------------------------------------------------------------------- /applications/mlp/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include // uint8_t, etc. 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | -------------------------------------------------------------------------------- /applications/numerical/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "numerical" "Applications/Numerical" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/numerical/README.md: -------------------------------------------------------------------------------- 1 | # examples/numerical 2 | 3 | Numerical Analysis Examples 4 | 5 | # Newton-Raphson 6 | 7 | A Newton-Raphson algorithm using valids. 8 | 9 | It is a demonstration of the benefits of custom posit/valid configurations and the error-free linear algebra. 10 | 11 | In numerical analysis, Newton's method (also known as the Newton–Raphson method), named after Isaac Newton and Joseph Raphson, is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function. It is one example of a root-finding algorithm. 12 | 13 | {\displaystyle x:f(x)=0\,.} x:f(x)=0\,. 14 | 15 | The Newton–Raphson method in one variable is implemented as follows: 16 | 17 | The method starts with a function f defined over the real numbers x, the function's derivative f ′, and an initial guess x0 for a root of the function f. If the function satisfies the assumptions made in the derivation of the formula and the initial guess is close, then a better approximation x1 is 18 | 19 | {\displaystyle x_{1}=x_{0}-{\frac {f(x_{0})}{f'(x_{0})}}\,.} x_{1}=x_{0}-{\frac {f(x_{0})}{f'(x_{0})}}\,. 20 | Geometrically, (x1, 0) is the intersection of the x-axis and the tangent of the graph of f at (x0, f (x0)). 21 | 22 | The process is repeated as 23 | 24 | {\displaystyle x_{n+1}=x_{n}-{\frac {f(x_{n})}{f'(x_{n})}}\,} x_{n+1}=x_{n}-{\frac {f(x_{n})}{f'(x_{n})}}\, 25 | until a sufficiently accurate value is reached. 26 | -------------------------------------------------------------------------------- /applications/numerical/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | #pragma once 6 | 7 | #include // uint8_t, etc. 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | -------------------------------------------------------------------------------- /applications/polynomial/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "polynomial" "Applications/Polynomials" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /applications/polynomial/README.md: -------------------------------------------------------------------------------- 1 | # applications/polynomial 2 | 3 | Polynomial evaluation examples 4 | 5 | # Challange Problem 6 | 7 | For the more typical case where we are using rounded real numbers for everything, the solution is described in many publications by Ulrich Kulisch. The most recent one is in his Computer Arithmetic and Validity, 2nd edition, Section 9.6.2 "Accurate evaluation of polynomials". I reference this in the posits4.pdf document that's on posithub.org. A challenge problem from Kulisch: 8 | 9 | P[t_] := 8118t^4 -11482t^3 +t^2 +5741t-2030 10 | 11 | evaluated at a t value near 1/√2, where it has an exact root. The polynomial is cast into the form of a lower-triangular system: 12 | 13 | x_1 = 8118 14 | x_2 = x1 * t - 11482 15 | x_3 = x2 * t + 1 16 | x_4 = x3 * t + 5741 17 | x_5 = x4 * t - 2030 18 | 19 | where x_5 is the desired polynomial value. Solving the system, refining the answer using a residual computed with the quire, and perhaps iterating, nails the correct value within 0.5 ULP. 20 | -------------------------------------------------------------------------------- /applications/polynomial/bernstein.cpp: -------------------------------------------------------------------------------- 1 | // bernstein.cpp: evaluation of Bernstein polynomials 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: 5 | // 6 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 7 | #include 8 | //#include 9 | 10 | int main(int argc, char** argv) 11 | try { 12 | using namespace std; 13 | using namespace sw::universal; 14 | 15 | using Scalar = posit<32, 2>; 16 | using Matrix = mtl::mat::dense2D; 17 | 18 | // Matrix B = bernstein(5); 19 | // cout << B << endl; 20 | 21 | return EXIT_SUCCESS; 22 | } 23 | catch (char const* msg) { 24 | std::cerr << msg << std::endl; 25 | return EXIT_FAILURE; 26 | } 27 | catch (const sw::universal::posit_arithmetic_exception& err) { 28 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 29 | return EXIT_FAILURE; 30 | } 31 | catch (const sw::universal::quire_exception& err) { 32 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 33 | return EXIT_FAILURE; 34 | } 35 | catch (const sw::universal::posit_internal_exception& err) { 36 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 37 | return EXIT_FAILURE; 38 | } 39 | catch (std::runtime_error& err) { 40 | std::cerr << err.what() << std::endl; 41 | return EXIT_FAILURE; 42 | } 43 | catch (...) { 44 | std::cerr << "Caught unknown exception" << std::endl; 45 | return EXIT_FAILURE; 46 | } 47 | -------------------------------------------------------------------------------- /applications/polynomial/challenge.cpp: -------------------------------------------------------------------------------- 1 | // challenge.cpp: A challenge problem from Ulrich Kulisch 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include 8 | 9 | 10 | int main(int argc, char** argv) 11 | try { 12 | using namespace std; 13 | using namespace sw::universal; 14 | 15 | 16 | 17 | return EXIT_SUCCESS; 18 | } 19 | catch (char const* msg) { 20 | std::cerr << msg << std::endl; 21 | return EXIT_FAILURE; 22 | } 23 | catch (const sw::universal::posit_arithmetic_exception& err) { 24 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 25 | return EXIT_FAILURE; 26 | } 27 | catch (const sw::universal::quire_exception& err) { 28 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 29 | return EXIT_FAILURE; 30 | } 31 | catch (const sw::universal::posit_internal_exception& err) { 32 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 33 | return EXIT_FAILURE; 34 | } 35 | catch (std::runtime_error& err) { 36 | std::cerr << err.what() << std::endl; 37 | return EXIT_FAILURE; 38 | } 39 | catch (...) { 40 | std::cerr << "Caught unknown exception" << std::endl; 41 | return EXIT_FAILURE; 42 | } 43 | -------------------------------------------------------------------------------- /applications/stdlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "stdlib" "Applications/stdlib" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /background/Appendix_C_Generating_Random_Correlation_Matrices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/Appendix_C_Generating_Random_Correlation_Matrices.pdf -------------------------------------------------------------------------------- /background/BLAS-LAPACK-trends-icl-utk-950-2017.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/BLAS-LAPACK-trends-icl-utk-950-2017.pdf -------------------------------------------------------------------------------- /background/CLBlast-a-tuned-OpenCL-BLAS-library-1705.05249.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/CLBlast-a-tuned-OpenCL-BLAS-library-1705.05249.pdf -------------------------------------------------------------------------------- /background/EM-iterative-method-1609.00670.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/EM-iterative-method-1609.00670.pdf -------------------------------------------------------------------------------- /background/KLU-direct-sparse-solver.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/KLU-direct-sparse-solver.pdf -------------------------------------------------------------------------------- /background/accelerated-methods-for-performing-the-LDLT-decomposition.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/accelerated-methods-for-performing-the-LDLT-decomposition.pdf -------------------------------------------------------------------------------- /background/einstein-tensor-notation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/einstein-tensor-notation.pdf -------------------------------------------------------------------------------- /background/exact-dot-product-arith2017.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/exact-dot-product-arith2017.pdf -------------------------------------------------------------------------------- /background/generating-random-orthogonal-matrices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/generating-random-orthogonal-matrices.pdf -------------------------------------------------------------------------------- /background/generation-of-random-orthogonal-matrices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/generation-of-random-orthogonal-matrices.pdf -------------------------------------------------------------------------------- /background/kalman_intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/kalman_intro.pdf -------------------------------------------------------------------------------- /background/posits-the-good-bad-ugly.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/background/posits-the-good-bad-ugly.pdf -------------------------------------------------------------------------------- /benchmarks/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory("arithmetic") 2 | -------------------------------------------------------------------------------- /benchmarks/arithmetic/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "pb" "Benchmarks/Arithmetic/Performance" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /blas/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory("L0") 2 | add_subdirectory("L1") 3 | add_subdirectory("L2") 4 | add_subdirectory("L3") -------------------------------------------------------------------------------- /blas/L0/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "l0" "BLAS/L0" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /blas/L0/fam.cpp: -------------------------------------------------------------------------------- 1 | // fam.cpp: example program contrasting fused accumulate-multiply functionality between IEEE and POSIT 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | 8 | #include 9 | 10 | // generate specific test case that you can trace with the trace conditions in posit.h 11 | // for most bugs they are traceable with _trace_conversion and _trace_sub 12 | // Fused Add-Multiply = (a + b)*c 13 | template 14 | void GenerateFAMTestCase(Ty a, Ty b, Ty c) { 15 | #if 0 16 | Ty with_fam, without_fam; 17 | sw::universal::posit pa(a), pb(b), pc(c), pref, pfam; 18 | 19 | ++pa; // perturb a by adding 1 machine epsilon 20 | a = Ty(pa); // and update the input to reflect the new value 21 | pb = b; 22 | pc = c; 23 | // (a + b)*c = a*c + b*c = fma(b,c,a*c) 24 | with_fam = std::fmal(b, c, long double(a)*long double(c)); 25 | without_fam = (a + b) * c; 26 | pref = with_fam; 27 | pfam = sw::universal::fam(pa, pb, pc); 28 | std::cout << "posit<" << nbits << "," << es << ">" << std::endl; 29 | std::cout << std::setprecision(nbits - 2); 30 | std::cout << std::setw(nbits) << a << " * " << std::setw(nbits) << b << " + " << std::setw(nbits) << c << " = " << std::setw(nbits) << without_fam << " not fused" << std::endl; 31 | std::cout << std::setw(nbits) << a << " * " << std::setw(nbits) << b << " + " << std::setw(nbits) << c << " = " << std::setw(nbits) << with_fam << " fused" << std::endl; 32 | std::cout << std::setw(nbits) << pa << " * " << std::setw(nbits) << pb << " + " << std::setw(nbits) << pc << " = " << std::setw(nbits) << pfam << std::endl; 33 | std::cout << pa.get() << " * " << pb.get() << " + " << pc.get() << " = " << pfam.get() << " (reference: " << pref.get() << ") "; 34 | std::cout << (pref == pfam ? "PASS" : "FAIL") << std::endl << std::endl; 35 | std::cout << std::setprecision(5); 36 | #endif 37 | 38 | } 39 | 40 | int main(int argc, char** argv) 41 | try { 42 | using namespace std; 43 | //using namespace sw::universal; it is more informative to use the namespace explicitely 44 | 45 | int nrOfFailedTestCases = 0; 46 | 47 | // NOTE: pick values that have an exact binary representation 48 | // otherwise you will be fighting round-off error getting into the calculation 49 | // which muddles the actual round-off you are trying to quantify 50 | GenerateFAMTestCase<32 ,2, double>(0.125, 10.0, -1.25); 51 | GenerateFAMTestCase<48, 2, double>(0.125, 10.0, -1.25); 52 | GenerateFAMTestCase<56, 2, double>(0.125, 10.0, -1.25); 53 | 54 | constexpr size_t nbits = 32; 55 | constexpr size_t es = 2; 56 | 57 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 58 | } 59 | catch (char const* msg) { 60 | std::cerr << msg << std::endl; 61 | return EXIT_FAILURE; 62 | } 63 | catch (const sw::universal::posit_arithmetic_exception& err) { 64 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 65 | return EXIT_FAILURE; 66 | } 67 | catch (const sw::universal::quire_exception& err) { 68 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 69 | return EXIT_FAILURE; 70 | } 71 | catch (const sw::universal::posit_internal_exception& err) { 72 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 73 | return EXIT_FAILURE; 74 | } 75 | catch (std::runtime_error& err) { 76 | std::cerr << err.what() << std::endl; 77 | return EXIT_FAILURE; 78 | } 79 | catch (...) { 80 | std::cerr << "Caught unknown exception" << std::endl; 81 | return EXIT_FAILURE; 82 | } 83 | -------------------------------------------------------------------------------- /blas/L0/fma.cpp: -------------------------------------------------------------------------------- 1 | // fma.cpp: example program contrasting fused multiply-accumulate functionality between IEEE and POSIT 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | 8 | #include 9 | 10 | // generate specific test case that you can trace with the trace conditions in posit.h 11 | // for most bugs they are traceable with _trace_conversion and _trace_sub 12 | template 13 | void GenerateFMATestCase(Ty a, Ty b, Ty c) { 14 | Ty with_fma, without_fma; 15 | sw::universal::posit pa(a), pb(b), pc(c), pref, pfma; 16 | 17 | ++pa; // perturb a by adding 1 machine epsilon 18 | a = Ty(pa); // and update the input to reflect the new value 19 | pb = b; 20 | pc = c; 21 | with_fma = std::fma(a, b, c); 22 | without_fma = a*b + c; 23 | pref = with_fma; 24 | pfma = sw::universal::fma(pa, pb, pc); 25 | std::cout << "posit<" << nbits << "," << es << ">" << std::endl; 26 | std::cout << std::setprecision(nbits - 2); 27 | std::cout << std::setw(nbits) << a << " * " << std::setw(nbits) << b << " + " << std::setw(nbits) << c << " = " << std::setw(nbits) << without_fma << " not fused" << std::endl; 28 | std::cout << std::setw(nbits) << a << " * " << std::setw(nbits) << b << " + " << std::setw(nbits) << c << " = " << std::setw(nbits) << with_fma << " fused" << std::endl; 29 | std::cout << std::setw(nbits) << pa << " * " << std::setw(nbits) << pb << " + " << std::setw(nbits) << pc << " = " << std::setw(nbits) << pfma << std::endl; 30 | std::cout << pa.get() << " * " << pb.get() << " + " << pc.get() << " = " << pfma.get() << " (reference: " << pref.get() << ") "; 31 | std::cout << (pref == pfma ? "PASS" : "FAIL") << std::endl << std::endl; 32 | std::cout << std::setprecision(5); 33 | } 34 | 35 | int main(int argc, char** argv) 36 | try { 37 | using namespace std; 38 | //using namespace sw::universal; it is more informative to use the namespace explicitely 39 | 40 | int nrOfFailedTestCases = 0; 41 | 42 | // NOTE: pick values that have an exact binary representation 43 | // otherwise you will be fighting round-off error getting into the calculation 44 | // which muddles the actual round-off you are trying to quantify 45 | GenerateFMATestCase<32 ,2, double>(0.125, 10.0, -1.25); 46 | GenerateFMATestCase<48, 2, double>(0.125, 10.0, -1.25); 47 | GenerateFMATestCase<56, 2, double>(0.125, 10.0, -1.25); 48 | 49 | constexpr size_t nbits = 32; 50 | constexpr size_t es = 2; 51 | 52 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 53 | } 54 | catch (char const* msg) { 55 | std::cerr << msg << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (const sw::universal::posit_arithmetic_exception& err) { 59 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | catch (const sw::universal::quire_exception& err) { 63 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (const sw::universal::posit_internal_exception& err) { 67 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | catch (std::runtime_error& err) { 71 | std::cerr << err.what() << std::endl; 72 | return EXIT_FAILURE; 73 | } 74 | catch (...) { 75 | std::cerr << "Caught unknown exception" << std::endl; 76 | return EXIT_FAILURE; 77 | } 78 | -------------------------------------------------------------------------------- /blas/L1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "l1" "BLAS/L1" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /blas/L1/asum.cpp: -------------------------------------------------------------------------------- 1 | // l1_asum.cpp: example program contrasting a BLAS L1 ?asum routine between FLOAT and POSIT 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | // place configuration compile switches before including hprblas 8 | #include 9 | 10 | using namespace std; 11 | using namespace sw::universal; 12 | 13 | template 14 | void myFunction(int N) { 15 | Vector v(N); 16 | v = Real(1.0); 17 | cout << 5 * v << endl; 18 | } 19 | 20 | int main(int argc, char** argv) 21 | try { 22 | constexpr size_t nbits = 20; 23 | constexpr size_t es = 1; 24 | 25 | int nrOfFailedTestCases = 0; 26 | 27 | cout << "ASUM is " << 1 << endl; 28 | 29 | using namespace mtl; 30 | using Real = posit; 31 | using Vector = mtl::dense_vector; 32 | 33 | myFunction(10); 34 | myFunction(10); 35 | 36 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 37 | } 38 | catch (char const* msg) { 39 | cerr << msg << endl; 40 | return EXIT_FAILURE; 41 | } 42 | catch (const posit_arithmetic_exception& err) { 43 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 44 | return EXIT_FAILURE; 45 | } 46 | catch (const quire_exception& err) { 47 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 48 | return EXIT_FAILURE; 49 | } 50 | catch (const posit_internal_exception& err) { 51 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (std::runtime_error& err) { 55 | std::cerr << err.what() << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (...) { 59 | cerr << "Caught unknown exception" << endl; 60 | return EXIT_FAILURE; 61 | } 62 | -------------------------------------------------------------------------------- /blas/L1/axpy.cpp: -------------------------------------------------------------------------------- 1 | // axpy.cpp: example program contrasting a BLAS L1 ?axpy routine between FLOAT and POSIT 2 | // 3 | // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | #include 7 | 8 | /* 9 | An axpy operation, that is, a * X + Y, has resolution-canceling rounding error 10 | when the scales of the product and the Y element are disproporitional. 11 | 12 | Reproducibility is challenged when an FMA or regular mul followed 13 | by an add is used; we have either one or two rounding events. 14 | 15 | */ 16 | 17 | template 18 | void axpy_test(const std::string& tag, int vecSize, Element x_value, Element y_value) 19 | { 20 | Vector X(vecSize), Y(vecSize), AXPY(vecSize); 21 | X = x_value; 22 | Y = y_value; 23 | double alpha = 0.1; 24 | AXPY = alpha*X + Y; 25 | 26 | std::cout << tag << std::endl; 27 | std::cout << AXPY << std::endl; 28 | } 29 | 30 | 31 | int main(int argc, char** argv) 32 | try { 33 | using namespace mtl; 34 | //using namespace sw::universal; 35 | using namespace sw::hprblas; 36 | 37 | int nrOfFailedTestCases = 0; 38 | 39 | const size_t nbits = 32; 40 | const size_t es = 2; 41 | const size_t vecSize = 32; 42 | 43 | { 44 | using Vector = mtl::dense_vector >; 45 | axpy_test("Double AXPY is ", vecSize, 10.0, -1.0); 46 | } 47 | { 48 | using Vector = mtl::dense_vector, mtl::vec::parameters >; 49 | axpy_test,Vector>("posit<32,2> AXPY is ", vecSize, sw::universal::posit(10.0), sw::universal::posit(-1.0)); 50 | } 51 | 52 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 53 | } 54 | catch (char const* msg) { 55 | std::cerr << msg << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (const sw::universal::posit_arithmetic_exception& err) { 59 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | catch (const sw::universal::quire_exception& err) { 63 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (const sw::universal::posit_internal_exception& err) { 67 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | catch (std::runtime_error& err) { 71 | std::cerr << err.what() << std::endl; 72 | return EXIT_FAILURE; 73 | } 74 | catch (...) { 75 | std::cerr << "Caught unknown exception" << std::endl; 76 | return EXIT_FAILURE; 77 | } 78 | -------------------------------------------------------------------------------- /blas/L1/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | #pragma once 3 | 4 | #include // uint8_t, etc. 5 | #include // for frexp/frexpf 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | 16 | #ifdef WINDOWS 17 | // Including SDKDDKVer.h defines the highest available Windows platform. 18 | 19 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 20 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 21 | 22 | #include 23 | #endif // WINDOWS 24 | -------------------------------------------------------------------------------- /blas/L2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "l2" "BLAS/L2" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /blas/L2/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | #pragma once 3 | 4 | #include // uint8_t, etc. 5 | #include // for frexp/frexpf 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | 16 | #ifdef WINDOWS 17 | // Including SDKDDKVer.h defines the highest available Windows platform. 18 | 19 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 20 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 21 | 22 | #include 23 | #endif // WINDOWS 24 | -------------------------------------------------------------------------------- /blas/L2/mv.cpp: -------------------------------------------------------------------------------- 1 | // mv.cpp : example program to demonstrate BLAS L2 Matrix-Vector product 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | #include 8 | // utilities to generate and print vectors and matrices 9 | #include "utils/matvec.hpp" 10 | 11 | int main(int argc, char** argv) 12 | try { 13 | using namespace std; 14 | using namespace sw::hprblas; 15 | 16 | const size_t nbits = 8; 17 | const size_t es = 0; 18 | const size_t vecSize = 32; 19 | 20 | int nrOfFailedTestCases = 0; 21 | 22 | cout << "DOT product examples" << endl; 23 | vector x(vecSize), y(vecSize); 24 | double result; 25 | 26 | randomVectorFillAroundOneEPS(vecSize, x); // sampleVector("x", x); 27 | randomVectorFillAroundOneEPS(vecSize, y); // sampleVector("y", y); 28 | result = sw::hprblas::dot(vecSize, x, 1, y, 1); 29 | cout << "DOT product is " << setprecision(20) << result << endl; 30 | 31 | using Posit = sw::universal::posit; 32 | vector px(vecSize), py(vecSize); 33 | randomVectorFillAroundOneEPS(vecSize, px); // sampleVector("px", px); 34 | randomVectorFillAroundOneEPS(vecSize, py); // sampleVector("py", py); 35 | result = (double)sw::hprblas::dot(vecSize, px, 1, py, 1); 36 | cout << "DOT product is " << setprecision(20) << result << endl; 37 | sampleVector("px", px); // <-- currently shows bad conversions.... 38 | 39 | Posit p; 40 | p = 1.001; cout << p << endl; 41 | p = 0.999; cout << p << endl; 42 | 43 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 44 | } 45 | catch (char const* msg) { 46 | std::cerr << msg << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | catch (const sw::universal::posit_arithmetic_exception& err) { 50 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 51 | return EXIT_FAILURE; 52 | } 53 | catch (const sw::universal::quire_exception& err) { 54 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 55 | return EXIT_FAILURE; 56 | } 57 | catch (const sw::universal::posit_internal_exception& err) { 58 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 59 | return EXIT_FAILURE; 60 | } 61 | catch (std::runtime_error& err) { 62 | std::cerr << err.what() << std::endl; 63 | return EXIT_FAILURE; 64 | } 65 | catch (...) { 66 | std::cerr << "Caught unknown exception" << std::endl; 67 | return EXIT_FAILURE; 68 | } 69 | -------------------------------------------------------------------------------- /blas/L3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "l3" "BLAS/L3" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /blas/L3/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | #pragma once 3 | 4 | #include // uint8_t, etc. 5 | #include // for frexp/frexpf 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | 18 | #ifdef _WINDOWS 19 | // Including SDKDDKVer.h defines the highest available Windows platform. 20 | 21 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 22 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 23 | 24 | #include 25 | 26 | #define MTL_WITH_INITLIST 27 | #endif // WINDOWS 28 | 29 | #if defined(__clang__) 30 | /* Clang/LLVM. ---------------------------------------------- */ 31 | 32 | #elif defined(__ICC) || defined(__INTEL_COMPILER) 33 | /* Intel ICC/ICPC. ------------------------------------------ */ 34 | 35 | #elif defined(__GNUC__) || defined(__GNUG__) 36 | /* GNU GCC/G++. --------------------------------------------- */ 37 | 38 | #elif defined(__HP_cc) || defined(__HP_aCC) 39 | /* Hewlett-Packard C/aC++. ---------------------------------- */ 40 | 41 | #elif defined(__IBMC__) || defined(__IBMCPP__) 42 | /* IBM XL C/C++. -------------------------------------------- */ 43 | 44 | #elif defined(_MSC_VER) 45 | /* Microsoft Visual Studio. --------------------------------- */ 46 | 47 | #elif defined(__PGI) 48 | /* Portland Group PGCC/PGCPP. ------------------------------- */ 49 | 50 | #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 51 | /* Oracle Solaris Studio. ----------------------------------- */ 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /blas/L3/matrix_types.cpp: -------------------------------------------------------------------------------- 1 | // matrix_types.cpp: examples of different matrix types 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | // warning C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. 8 | // \mtl4\boost/numeric/mtl/operation/update.hpp(159): warning C4244: 'argument': conversion from 'const double' to 'float', possible loss of data 9 | #pragma warning( disable : 4996 4244) 10 | #include "common.hpp" 11 | #include 12 | 13 | template 14 | void fill_and_print(Matrix& A, char name) 15 | { 16 | // Set values in traditional way 17 | A = 1.2, 3.4, 18 | 5.6, 7.8; 19 | 20 | // Just print them 21 | std::cout << name << " is \n" << A << "\n"; 22 | } 23 | 24 | int main(int argc, char** argv) 25 | try { 26 | using namespace sw::universal; 27 | using namespace sw::hprblas; 28 | 29 | #if defined(MTL_WITH_VARIADIC_TEMPLATE) && defined(MTL_WITH_TEMPLATE_ALIAS) 30 | using namespace mtl; 31 | 32 | // Compressed matrix 33 | matrix A(2, 2); 34 | fill_and_print(A, 'A'); 35 | 36 | // Banded matrix 37 | matrix B(2, 2); 38 | fill_and_print(B, 'B'); 39 | 40 | // Matrix in the ELLPACK format 41 | matrix C(2, 2); 42 | fill_and_print(C, 'C'); 43 | 44 | // Coordinate matrix 45 | // matrix D(2, 2); 46 | // fill_and_print(D, 'D'); 47 | 48 | // Morton-order matrix with the default mask 49 | matrix E(2, 2); 50 | fill_and_print(E, 'E'); 51 | 52 | // Matrix with a Morton mask is of course a Morton-order matrix 53 | matrix> F(2, 2); 54 | fill_and_print(F, 'F'); 55 | #endif 56 | 57 | return EXIT_SUCCESS; 58 | } 59 | catch (char const* msg) { 60 | std::cerr << msg << std::endl; 61 | return EXIT_FAILURE; 62 | } 63 | catch (const sw::universal::posit_arithmetic_exception& err) { 64 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 65 | return EXIT_FAILURE; 66 | } 67 | catch (const sw::universal::quire_exception& err) { 68 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 69 | return EXIT_FAILURE; 70 | } 71 | catch (const sw::universal::posit_internal_exception& err) { 72 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 73 | return EXIT_FAILURE; 74 | } 75 | catch (std::runtime_error& err) { 76 | std::cerr << err.what() << std::endl; 77 | return EXIT_FAILURE; 78 | } 79 | catch (...) { 80 | std::cerr << "Caught unknown exception" << std::endl; 81 | return EXIT_FAILURE; 82 | } 83 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cmake -DMTL_ROOT=$MTL_ROOT -DUNIVERSAL_ROOT=$UNIVERSAL_ROOT .. 4 | -------------------------------------------------------------------------------- /c_api/lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(lib_name hprblas_c_api) 2 | set(folder Libraries) 3 | 4 | add_library(${lib_name} STATIC hprblas_c_api.cpp ../../applications/apf/foreach.cpp) 5 | set_target_properties(${lib_name} PROPERTIES FOLDER ${folder}) 6 | 7 | if(C_API_LIB_PIC) 8 | if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 10 | endif() 11 | endif() 12 | 13 | install(TARGETS ${lib_name} DESTINATION lib) 14 | install(FILES hprblas_c_api.h DESTINATION include) 15 | 16 | # TODO: for adaptive precision algorithms there are hundreds of valuable posit configurations that the C++ version supports 17 | # we will need a stub generator to capture them all in a productive manner 18 | # OR: we simply require that you use the C++ library as C and Fortran environments would have difficulty creating 19 | # adaptive precision algorithms anyways. 20 | -------------------------------------------------------------------------------- /c_api/lib/hprblas_c_api.cpp: -------------------------------------------------------------------------------- 1 | // hprblas_c_api.cpp: C++ shim to deliver a C API for HPRBLAS 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | // fused matrix matrix multiply using posit8_t type 13 | void posit8_fmm(posit8_t* C, unsigned M, unsigned N, unsigned K, posit8_t* A, posit8_t* B) { 14 | } 15 | // fused matrix matrix multiply using posit16 type 16 | void posit16_fmm(posit16_t* C, unsigned M, unsigned N, unsigned K, posit16_t* A, posit16_t* B) { 17 | } 18 | // fused matrix matrix multiply using posit32 type 19 | void posit32_fmm(posit32_t* C, unsigned M, unsigned N, unsigned K, posit32_t* A, posit32_t* B) { 20 | } 21 | // fused matrix matrix multiply using posit64 type 22 | void posit64_fmm(posit64_t* C, unsigned M, unsigned N, unsigned K, posit64_t* A, posit64_t* B) { 23 | } 24 | // fused matrix matrix multiply using posit128 type 25 | void posit128_fmm(posit128_t* C, unsigned M, unsigned N, unsigned K, posit128_t* A, posit128_t* B) { 26 | } 27 | // fused matrix matrix multiply using posit256 type 28 | void posit256_fmm(posit256_t* C, unsigned M, unsigned N, unsigned K, posit256_t* A, posit256_t* B) { 29 | } 30 | #ifdef __cplusplus 31 | } 32 | #endif -------------------------------------------------------------------------------- /c_api/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.c*") 2 | 3 | #### 4 | # macro to read all source files in a directory 5 | # and create a test target for each source file 6 | macro (compile_and_link_all testing prefix folder) 7 | # cycle through the sources 8 | # For the according directories, we assume that each cpp file is a separate test 9 | # so, create a executable target and an associated test target 10 | foreach (source ${ARGN}) 11 | get_filename_component (test ${source} NAME_WE) 12 | string(REPLACE " " ";" new_source ${source}) 13 | set(test_name ${prefix}_${test}) 14 | message(STATUS "Add test ${test_name} from source ${new_source}.") 15 | add_executable (${test_name} ${new_source}) 16 | #message(STATUS "args: ${testing} - ${prefix} - ${folder}") 17 | set_target_properties(${test_name} PROPERTIES FOLDER ${folder}) 18 | if (UNIX) 19 | target_link_libraries(${test_name} hprblas_c_api m) 20 | endif(UNIX) 21 | if (MSVC) 22 | target_link_libraries(${test_name} hprblas_c_api) 23 | endif(MSVC) 24 | if (${testing} STREQUAL "true") 25 | if (UNIVERSAL_CMAKE_TRACE) 26 | message(STATUS "testing: ${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}") 27 | endif() 28 | add_test(${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}) 29 | endif() 30 | endforeach (source) 31 | endmacro (compile_and_link_all) 32 | 33 | compile_and_link_all("true" "c_api" "C-API" "${SOURCES}") 34 | -------------------------------------------------------------------------------- /c_api/test/matmul.c: -------------------------------------------------------------------------------- 1 | // matmul.c: example C program using a fused matrix matrix multiply 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include 8 | 9 | typedef struct { 10 | unsigned rows; 11 | unsigned cols; 12 | unsigned sizeOfElement; 13 | void *data; 14 | } Matrix; 15 | 16 | Matrix* newMatrix(unsigned m, unsigned n, unsigned sizeOfElement) { 17 | Matrix *M = malloc(sizeof(Matrix)); 18 | M->rows = m; 19 | M->cols = n; 20 | M->sizeOfElement = sizeOfElement; 21 | M->data = malloc(m * n * sizeOfElement); 22 | return M; 23 | } 24 | 25 | void clearMatrix(unsigned m, unsigned n, unsigned sizeOfElement, void* M) { 26 | for (unsigned i = 0; i < m; ++i) { 27 | 28 | } 29 | } 30 | 31 | int main(int argc, char* argv[]) { 32 | Matrix *A, *B, *C; 33 | 34 | int N = 5; 35 | A = newMatrix(5, 5, sizeof(double)); 36 | B = newMatrix(5, 5, sizeof(double)); 37 | C = newMatrix(5, 5, sizeof(double)); 38 | 39 | // create input matrices 40 | 41 | 42 | // free matrices 43 | 44 | } -------------------------------------------------------------------------------- /cmake/Banners.cmake: -------------------------------------------------------------------------------- 1 | ######################################################################################## 2 | # banners.cmake 3 | # 4 | # collection of font art for Universal 5 | 6 | #### 7 | # macro to print out a banner 8 | macro (print_speed_banner) 9 | message("") 10 | message("___ / / /__ __ \\__ __ \\ ___ __ )__ /___ |_ ___/") 11 | message("__ /_/ /__ /_/ /_ /_/ /________ __ |_ / __ /| |____ \\") 12 | message("_ __ / _ ____/_ _, _/_/_____/ /_/ /_ /___ ___ |___/ /") 13 | message("/_/ /_/ /_/ /_/ |_| /_____/ /_____/_/ |_/____/") 14 | message("") 15 | endmacro (print_speed_banner) 16 | 17 | macro (print_header) 18 | print_speed_banner() 19 | endmacro (print_header) 20 | 21 | macro (print_footer) 22 | print_speed_banner() 23 | endmacro (print_footer) 24 | -------------------------------------------------------------------------------- /cmake/CheckCCompilerFlag.cmake: -------------------------------------------------------------------------------- 1 | # - Check whether the C compiler supports a given flag. 2 | # CHECK_C_COMPILER_FLAG( ) 3 | # - the compiler flag 4 | # - variable to store the result 5 | # This internally calls the check_c_source_compiles macro. 6 | # See help for CheckCSourceCompiles for a listing of variables 7 | # that can modify the build. 8 | 9 | #============================================================================= 10 | # Copyright 2006-2009 Kitware, Inc. 11 | # Copyright 2006 Alexander Neundorf 12 | # Copyright 2011-2013 Matthias Kretz 13 | # 14 | # Redistribution and use in source and binary forms, with or without 15 | # modification, are permitted provided that the following conditions are 16 | # met: 17 | # 18 | # * Redistributions of source code must retain the above copyright notice, 19 | # this list of conditions and the following disclaimer. 20 | # 21 | # * Redistributions in binary form must reproduce the above copyright notice, 22 | # this list of conditions and the following disclaimer in the documentation 23 | # and/or other materials provided with the distribution. 24 | # 25 | # * The names of Kitware, Inc., the Insight Consortium, or the names of 26 | # any consortium members, or of any contributors, may not be used to 27 | # endorse or promote products derived from this software without 28 | # specific prior written permission. 29 | # 30 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' 31 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR 34 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 36 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 37 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 38 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 | #============================================================================= 41 | 42 | INCLUDE(CheckCSourceCompiles) 43 | 44 | MACRO (CHECK_C_COMPILER_FLAG _FLAG _RESULT) 45 | SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") 46 | SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") 47 | if(${ARGC} GREATER 2) 48 | SET(TEST_SOURCE "${ARGV2}") 49 | else() 50 | SET(TEST_SOURCE "int main() { return 0;}") 51 | endif() 52 | CHECK_C_SOURCE_COMPILES("${TEST_SOURCE}" ${_RESULT} 53 | # Some compilers do not fail with a bad flag 54 | FAIL_REGEX "error: bad value (.*) for .* switch" # GNU 55 | FAIL_REGEX "argument unused during compilation" # clang 56 | FAIL_REGEX "is valid for .* but not for C" # GNU 57 | FAIL_REGEX "unrecognized .*option" # GNU 58 | FAIL_REGEX "ignored for target" # GNU 59 | FAIL_REGEX "ignoring unknown option" # MSVC 60 | FAIL_REGEX "warning D9002" # MSVC 61 | FAIL_REGEX "[Uu]nknown option" # HP 62 | FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro 63 | FAIL_REGEX "command option .* is not recognized" # XL 64 | FAIL_REGEX "WARNING: unknown flag:" # Open64 65 | FAIL_REGEX "command line error" # ICC 66 | FAIL_REGEX "command line warning" # ICC 67 | FAIL_REGEX "#10236:" # ICC: File not found 68 | FAIL_REGEX " #10159: " # ICC 69 | FAIL_REGEX " #10353: " # ICC: option '-mfma' ignored, suggest using '-march=core-avx2' 70 | ) 71 | SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") 72 | ENDMACRO (CHECK_C_COMPILER_FLAG) 73 | 74 | -------------------------------------------------------------------------------- /cmake/CheckCXXCompilerFlag.cmake: -------------------------------------------------------------------------------- 1 | # - Check whether the CXX compiler supports a given flag. 2 | # CHECK_CXX_COMPILER_FLAG( ) 3 | # - the compiler flag 4 | # - variable to store the result 5 | # This internally calls the check_cxx_source_compiles macro. See help 6 | # for CheckCXXSourceCompiles for a listing of variables that can 7 | # modify the build. 8 | 9 | #============================================================================= 10 | # Copyright 2006-2009 Kitware, Inc. 11 | # Copyright 2006 Alexander Neundorf 12 | # Copyright 2011-2013 Matthias Kretz 13 | # 14 | # Redistribution and use in source and binary forms, with or without 15 | # modification, are permitted provided that the following conditions are 16 | # met: 17 | # 18 | # * Redistributions of source code must retain the above copyright notice, 19 | # this list of conditions and the following disclaimer. 20 | # 21 | # * Redistributions in binary form must reproduce the above copyright notice, 22 | # this list of conditions and the following disclaimer in the documentation 23 | # and/or other materials provided with the distribution. 24 | # 25 | # * The names of Kitware, Inc., the Insight Consortium, or the names of 26 | # any consortium members, or of any contributors, may not be used to 27 | # endorse or promote products derived from this software without 28 | # specific prior written permission. 29 | # 30 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' 31 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR 34 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 36 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 37 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 38 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 | #============================================================================= 41 | 42 | INCLUDE(CheckCXXSourceCompiles) 43 | 44 | MACRO (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT) 45 | SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") 46 | SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") 47 | if(${ARGC} GREATER 2) 48 | SET(TEST_SOURCE "${ARGV2}") 49 | else() 50 | SET(TEST_SOURCE "int main() { return 0;}") 51 | endif() 52 | CHECK_CXX_SOURCE_COMPILES("${TEST_SOURCE}" ${_RESULT} 53 | # Some compilers do not fail with a bad flag 54 | FAIL_REGEX "error: bad value (.*) for .* switch" # GNU 55 | FAIL_REGEX "argument unused during compilation" # clang 56 | FAIL_REGEX "is valid for .* but not for C\\\\+\\\\+" # GNU 57 | FAIL_REGEX "unrecognized .*option" # GNU 58 | FAIL_REGEX "ignored for target" # GNU 59 | FAIL_REGEX "ignoring unknown option" # MSVC 60 | FAIL_REGEX "warning D9002" # MSVC 61 | FAIL_REGEX "[Uu]nknown option" # HP 62 | FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro 63 | FAIL_REGEX "command option .* is not recognized" # XL 64 | FAIL_REGEX "WARNING: unknown flag:" # Open64 65 | FAIL_REGEX "command line error" # ICC 66 | FAIL_REGEX "command line warning" # ICC 67 | FAIL_REGEX "#10236:" # ICC: File not found 68 | FAIL_REGEX " #10159: " # ICC 69 | FAIL_REGEX " #10353: " # ICC: option '-mfma' ignored, suggest using '-march=core-avx2' 70 | ) 71 | SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") 72 | ENDMACRO (CHECK_CXX_COMPILER_FLAG) 73 | 74 | -------------------------------------------------------------------------------- /cmake/HPR_BLASCommon.cmake: -------------------------------------------------------------------------------- 1 | ############################ 2 | # This configuration file defines some cmake variables: 3 | # HPR_BLAS_INCLUDE_DIRS: list of include directories for the HPR-BLAS library 4 | # HPR_BLAS_LIBRARIES: libraries needed 5 | # HPR_BLAS_CXX_DEFINITIONS: definitions to enable the requested interfaces 6 | # HPR_BLAS_VERSION: version 7 | # HPR_BLAS_MAJOR_VERSION: major version 8 | # HPR_BLAS_MINOR_VERSION: minor version 9 | # 10 | #supported components: 11 | # 12 | 13 | unset(HPR_BLAS_LIBRARIES ) 14 | unset(HPR_BLAS_CXX_DEFINITIONS ) 15 | unset(HPR_BLAS_INCLUDE_DIR ) 16 | 17 | set(HPR_BLAS_MAJOR_VERSION 0) 18 | set(HPR_BLAS_MINOR_VERSION 1) 19 | set(HPR_BLAS_PATCH_VERSION 1) 20 | 21 | if (MSVC) 22 | add_definitions(/wd4522) # multiple assignment ops for single type, to be investigated further if avoidable 23 | endif() 24 | 25 | if (USE_ASSERTS) 26 | list(APPEND HPR_BLAS_CXX_DEFINITIONS "-DHPR_BLAS_ASSERT_FOR_THROW") 27 | endif() 28 | 29 | if(EXISTS ${HPR_BLAS_DIR}/include/hprblas.hpp) 30 | list(APPEND HPR_BLAS_INCLUDE_DIR "${HPR_BLAS_DIR}/include") 31 | else() 32 | message(ERROR " HPR_BLAS was not found") 33 | # assuming a dev tree where the repos are stored along side each other, i.e. 34 | # .../dev/clones/universal 35 | # .../dev/clones/mtl4 36 | # .../dev/clones/hpr-blas 37 | # .../dev/clones/hpr-dsp 38 | list(APPEND HPR_BLAS_INCLUDE_DIRS "${HPR_BLAS_DIR}/../hpr-blas/include") 39 | endif(EXISTS ${HPR_BLAS_DIR}/include/hprblas.hpp) 40 | 41 | macro(hpr_blas_check_cxx_compiler_flag FLAG RESULT) 42 | # counts entirely on compiler's return code, maybe better to combine it with check_cxx_compiler_flag 43 | file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx" "int main() { return 0;}\n") 44 | try_compile(${RESULT} 45 | ${CMAKE_BINARY_DIR} 46 | ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx 47 | COMPILE_DEFINITIONS ${FLAG}) 48 | endmacro() 49 | 50 | -------------------------------------------------------------------------------- /images/cmake-configuration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/images/cmake-configuration.png -------------------------------------------------------------------------------- /include/hprblas: -------------------------------------------------------------------------------- 1 | // HPRBLAS standard header 2 | // 3 | // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | #ifndef _HPRBLAS_STANDARD_HEADER_ 7 | #define _HPRBLAS_STANDARD_HEADER_ 8 | 9 | //////////////////////////////////////////////////////////////////////////////////////// 10 | /// BEHAVIORAL COMPILATION SWITCHES for posit library configuration 11 | 12 | //////////////////////////////////////////////////////////////////////////////////////// 13 | // enable/disable special posit format I/O 14 | // POSIT_ROUNDING_ERROR_FREE_IO_FORMAT 15 | // default is to print (long double) values 16 | // #define POSIT_ROUNDING_ERROR_FREE_IO_FORMAT 0 17 | 18 | //////////////////////////////////////////////////////////////////////////////////////// 19 | // enable/disable the ability to use literals in binary logic and arithmetic operators 20 | // POSIT_ENABLE_LITERALS) 21 | // default is to enable them 22 | // #define POSIT_ENABLE_LITERALS 1 23 | 24 | //////////////////////////////////////////////////////////////////////////////////////// 25 | // enable throwing specific exceptions for posit arithmetic errors 26 | // left to application to enable 27 | // POSIT_THROW_ARITHMETIC_EXCEPTION 28 | // default is to use NaR as a signalling error 29 | // #define POSIT_THROW_ARITHMETIC_EXCEPTION 0 30 | 31 | //////////////////////////////////////////////////////////////////////////////////////// 32 | /// INCLUDE FILES posit library 33 | #include 34 | 35 | /////////////////////////////////////////////////////////////////////////////////////// 36 | /// useful mathematical property functions 37 | #include 38 | 39 | /////////////////////////////////////////////////////////////////////////////////////// 40 | /// the underlying matrix/vector machinery 41 | #ifndef MTL_WITH_INITLIST 42 | #define MTL_WITH_INITLIST 43 | #endif 44 | #include 45 | 46 | /////////////////////////////////////////////////////////////////////////////////////// 47 | /// the High-Performance Reproducible Basic Linear Algebra Subroutines 48 | /// L1, L2, and L3 matrix/vector operations 49 | #include 50 | /// norms (l1, l2, linf, Frobenius) using HPR methods 51 | #include 52 | 53 | /////////////////////////////////////////////////////////////////////////////////////// 54 | /// linear system solvers 55 | #include 56 | #include 57 | #include 58 | #include 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /include/hprblas.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // hprblas.h: generic C API bindings for the HPRBLAS API 3 | // 4 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | // set up the correct C11 infrastructure 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // standard posit C types 16 | #include 17 | #include 18 | 19 | #ifdef __cplusplus 20 | // export a C interface if used by C++ source code 21 | extern "C" { 22 | #endif 23 | 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /include/matpak/all.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // all.hpp : all elements satisfy criteria? 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | 10 | // NOTES: 11 | /* 12 | 1. need to have relational operator as input. Or I want to 13 | call the function as all(A>0). Additionally, need to check per dimension 14 | (i.e., rows or columns or 'all'). So, call should be: all(A>0,2) 15 | would check if all elements of A are greater than zero, by rows. 16 | 17 | 2. Should be able to short-circuit. Assume true, then if element in row 18 | is false, make 'all' in that row false...no need to check other row entries. 19 | 20 | 3. lambda function - chebfun 21 | [](){ function body} 22 | [local variables]() { function body } 23 | [](x) { return x*x; } 24 | 25 | [captures]( params) { function body; } 26 | 27 | See https://en.cppreference.com/w/cpp/language/lambda 28 | 29 | void any(const Matrix& A, (*f)()) 30 | Matrix operator<(const Matrix& A, const Matrix& B) 31 | A < B; 32 | 33 | Matrix operator<(Matrix& A, Matrix& B) 34 | Matrix operator+(Matrix& A, Matrix& B) 35 | 36 | Matrix operator<(Matrix& A, Matrix& B) 37 | Matrix operator+(Matrix& A, Matrix& B) 38 | // matrix element-wise sum 39 | template 40 | matrix operator+(const matrix& A, const matrix& B) { 41 | matrix Sum(A); 42 | return Sum += B; 43 | } 44 | 45 | // matrix element-wise sum 46 | template 47 | matrix operator+(const matrix& A, const matrix& B) { 48 | matrix Sum(A); 49 | return Sum += B; 50 | } 51 | 52 | 53 | sw::unum::matrix class? 54 | // matrix element-wise sum 55 | matrix& operator+=(const matrix& rhs) { 56 | // check if the matrices are compatible 57 | if (_m != rhs._m || _n != rhs._n) { 58 | std::cerr << "Element-wise matrix sum received incompatible matrices (" 59 | << _m << ", " << _n << ") += (" << rhs._m << ", " << rhs._n << ")\n"; 60 | return *this; // return without changing 61 | } 62 | for (size_type e = 0; e < _m * _n; ++e) { 63 | data[e] += rhs.data[e]; 64 | } 65 | return *this; 66 | } 67 | 68 | 4. Look at: include/universal/blas/matrix.hpp 69 | See vector.hpp for class template. 70 | 71 | 72 | 73 | */ 74 | 75 | #include 76 | 77 | namespace sw { namespace hprblas { namespace matpak { 78 | 79 | template 80 | Matrix all(const Matrix&A, int dim = 1) { 81 | auto m = num_rows(A); 82 | auto n = num_cols(A); 83 | 84 | size_t r = 0; 85 | size_t c = 0; 86 | size_t b = 0; 87 | 88 | switch (dim) 89 | { 90 | case 1: Matrix B(1,n) = true; c = n - 1; b = c; break; 91 | case 2: Matrix B(m,1) = true; r = m - 1; b = r; break; 92 | case 3: Matrix B(1,1) = true; break; 93 | default: Matrix B(1,1) = true; 94 | } 95 | 96 | for(size_type i = 0; i < m; ++i){ 97 | for(size_type j = 0; j < n; ++j){ 98 | if(A[i][j]) < 0){ 99 | B[i][j] = false; 100 | } 101 | } 102 | } 103 | return B; 104 | } 105 | }}} -------------------------------------------------------------------------------- /include/matpak/any.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/include/matpak/any.hpp -------------------------------------------------------------------------------- /include/matpak/bands.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // bands.hpp : Returns matrix mask M of diagonal bands 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | Matrix bands(const Matrix&A, const Matrix&v = {0}) { 13 | auto m = num_rows(A); 14 | auto n = num_cols(A); 15 | auto N = num_rows(v)*num_cols(v); 16 | 17 | Matrix M(m,n); M = 0; 18 | 19 | for(size_t k = 0; k<= N; ++k){ 20 | if(v[0][k] <= 0){ // Sub Diagonals (+Diagonal) 21 | for( size_t i = abs(v[0][k]); i <= m ; ++i) { 22 | size_t j = i + v[0][k]; 23 | M[i][j] = 1; 24 | } 25 | }else{ // Super Diagonals 26 | for( size_t j = v[0][k]; j <= n ; ++j) { 27 | size_t i = j - v[0][k]; 28 | M[i][j] = 1; 29 | } 30 | } 31 | } 32 | return M; 33 | } 34 | }}} -------------------------------------------------------------------------------- /include/matpak/checkerboard.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // checkerboard.hpp: Returns +1 / -1 checkerboard pattern matrix 3 | // 4 | // Definition: A is checkerboard if sign(a(i,j)) = (-1)^(i+j) 5 | // 6 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 7 | // Author: James Quinlan 8 | // 9 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 10 | #include 11 | 12 | namespace sw { namespace hprblas { namespace matpak { 13 | 14 | template 15 | Matrix checkerboard(size_t m) { 16 | using value_type = typename Matrix::value_type; 17 | // using Vector = mtl::vec::dense_vector; 18 | 19 | Matrix C(m,m); 20 | for(size_t i = 0; i < m; ++i){ 21 | for(size_t j = 0; j < m; ++j){ 22 | C[i][j] = pow(-1 , i+j); 23 | } 24 | } 25 | 26 | /* 27 | if ( m % 2 != 0 ){ 28 | n = (m+1)/2; 29 | }else{ 30 | n = m/2; 31 | } 32 | */ 33 | 34 | 35 | /* 36 | Vector A(n); 37 | A = 1; 38 | Vector B = {1, -1}; 39 | 40 | auto C = kron(A,B); 41 | C = toeplitz(C) 42 | */ 43 | return C; 44 | } 45 | }}} -------------------------------------------------------------------------------- /include/matpak/contents.md: -------------------------------------------------------------------------------- 1 | # Matrix Utility Package 2 | 3 | MATPAK is a collection of free functions of matrix utilities. It is included in the HPRBLAS project. Some functions make use of [MTL4](http://www.simunova.com). 4 | 5 | MATPAK contains replicates several standard MATLAB functions such as `fliplr`, `flipup`, `toeplitz`, `hankel`, and `rot90`. The project was originally developed to facilitate research on row stochastic centrosymmetric matrices. 6 | 7 | # Contents in Brief 8 | 9 | Let A, B be matrices and v a vector. 10 | 11 | bands - extracts diagonals from a matrix (e.g., tridiagonal) 12 | diag - creates a diagonal matrix or extracts the diagonal 13 | diam(A) - calculates diameter of a matrix 14 | ek - Standard basis element. `ek(i,n)` 15 | fliplr - Flip matrix elements left/right 16 | flipud - Flip matrix up/down 17 | gt - greater than, e.g., A > B, returns 1/0 matrix 18 | hankel - Generate hankel matrix 19 | iscentro - determines if A is centrosymmetric (bool) 20 | isequal - determines if two matrices are equal (bool) 21 | ism - determines if a matrix is an M-matrix (bool) 22 | isnormal - determines if a matrix A is normal 23 | kron(A,B) - Kronecker product of A with B 24 | mkcentro - makes a centrosymmetric matrix 25 | reshape - Maps m x n --> r x c where mn = rc 26 | rot90 - Rotates a matrix 90 degrees counterclockwise 27 | rowsto - Generates a row stochastic matrix 28 | size - caclulates the size of a matrix 29 | sum - computes sum of entries per dimension 30 | toeplitz - Generates a Toeplitz matrix (see hankel) 31 | 32 | 33 | 34 | # Contents in Detail 35 | 36 | ## any 37 | 38 | ## diag 39 | Returns the diagonal of matrix A. If A is a vector (i.e., n x 1 matrix), diag(A) returns a n x n diagonal matrix with diagonal elements A. 40 | 41 | ``` 42 | diag(A) 43 | ``` 44 | 45 | 46 | ## diam 47 | Returns the diameter of a matrix. 48 | 49 | ``` 50 | diam(A) 51 | ``` 52 | 53 | 54 | 55 | ## ek 56 | Returns and elementary vector (0, 0, 0, ..., 1, 0, ..., 0) 57 | ``` 58 | // k = index containing 1 59 | // n = length of vector 60 | ek(k, n) 61 | ``` 62 | 63 | 64 | 65 | ## Hankel 66 | Returns a Hankel matrix. 67 | hankel(c,r) is a non-symmetric Toeplitz matrix having C as its 68 | first column and R as its first row rotated 90 degrees. 69 | 70 | ``` 71 | hankel(c,r) 72 | ``` 73 | 74 | ----------------------- 75 | 76 | # ISA 77 | 78 | ## iscentro 79 | Determines if a matrix is centrosymmetric. 80 | 81 | ``` 82 | iscentro(A) 83 | ``` 84 | 85 | ## isdiagdom 86 | Determines if a matrix is diagonally dominate. 87 | 88 | ``` 89 | isdiagdom(A) 90 | ``` 91 | 92 | ## isequal 93 | Determines which elements are equal in two m x n matrices, A and B. 94 | 95 | ``` 96 | isequal(A,B) 97 | ``` 98 | 99 | ## ism 100 | Determines if a matrix is an M-matrix. 101 | An M-matrix is a Z-matrix (non-positive off-diagonal entries) with real part of eigenvalues nonnegative. 102 | There are several equivalent definitions of M-matrix. Most spatial finite difference discretizations yield M-matrices. Moreover, M-matrix guarantees the monotonicity of the solution (i.e., non-oscillations in discrete solution, i.e., Maximum Principle preserved). See Varga,R.S. (1966). On a discrete maximum principle. SIAM J.Numer.Anal. 3, 355–359. 103 | 104 | ``` 105 | ism(A) 106 | ``` 107 | 108 | ## isnormal 109 | Determines if a matrix is *normal*: A'A = AA'. Returns boolean. 110 | 111 | ``` 112 | isnormal(A) 113 | ``` 114 | 115 | --------------------- 116 | 117 | 118 | 119 | ## rot90 120 | Rotate array 90 degrees counterclockwise. 121 | 122 | ``` 123 | rot90(A) 124 | ``` 125 | 126 | 127 | ## rowsto 128 | Generates a random m x n row stochastic matrix. 129 | 130 | ``` 131 | rowsto(m,n) 132 | ``` 133 | 134 | 135 | 136 | 137 | ## size 138 | Determines if a square matrix A is centrosymmetric. Returns boolean. 139 | ``` 140 | iscentro(A) 141 | ``` 142 | 143 | 144 | 145 | ## sum 146 | Given a matrix A, sum(A,d) sums columns (d=1), rows (d=2), or all (d=0) entries. 147 | 148 | ``` 149 | sum(A,d) 150 | ``` 151 | 152 | 153 | 154 | 155 | ## Toeplitz 156 | Returns a Toeplitz matrix. 157 | toeplitz(c,r) is a non-symmetric Toeplitz matrix having C as its 158 | first column and R as its first row. 159 | 160 | ``` 161 | toeplitz(c,r) 162 | ``` 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /include/matpak/ek.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // ek.hpp : i-th standard basis vector of length n 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | mtl::vec::dense_vector ek(size_t i, size_t n ) { 13 | mtl::vec::dense_vector v(n, Scalar(0)); 14 | v[i] = 1; 15 | return v; 16 | } 17 | }}} -------------------------------------------------------------------------------- /include/matpak/elemat.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // elemat.hpp : Returns an Elementary Matrix 3 | // n = rank of matrix 4 | // k = scalar (multiplier) 5 | // ri = row to be effected 6 | // rj = row to be scaled by k and added to ri 7 | // 8 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 9 | // Author: James Quinlan 10 | // 11 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 12 | 13 | namespace sw { namespace hprblas { namespace matpak { 14 | 15 | template 16 | Matrix elemat(const size_t&n, const size_t&k, const size_t&ri, const size_t&rj) { 17 | typedef typename Matrix::value_type value_type; 18 | typedef typename Matrix::size_type size_type; 19 | 20 | Matrix A(n,n); 21 | A = 1; 22 | A[ri][rj] = k; 23 | return A; 24 | } 25 | }}} -------------------------------------------------------------------------------- /include/matpak/fliplr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // fliplr.hpp : Flip matix in left/right direction. 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | Matrix fliplr(const Matrix&A) { 13 | typedef typename Matrix::value_type value_type; 14 | typedef typename Matrix::size_type size_type; 15 | 16 | size_type r = num_rows(A); 17 | size_type c = num_cols(A); 18 | 19 | Matrix B(r,c); 20 | 21 | for(size_t i = 0; i < c; ++i){ 22 | B[mtl::iall][i] = A[mtl::iall][c - i - 1]; 23 | } 24 | return B; 25 | } 26 | }}} -------------------------------------------------------------------------------- /include/matpak/fliptranspose.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // fliptranspose.hpp : Flip and tranpose matrix A 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | 11 | namespace sw { namespace hprblas { namespace matpak { 12 | 13 | template 14 | Matrix fliptranspose(const Matrix&A) { 15 | auto m = num_rows(A); 16 | auto n = num_cols(A); 17 | 18 | if (m!=n) return Matrix {0}; 19 | 20 | Matrix I(n,n); 21 | I = 1; 22 | 23 | Matrix J = rot90(I); // Counter-identity 24 | Matrix B(m,n); 25 | Matrix T(mtl::mat::trans(A)); 26 | B = J*T*J; 27 | 28 | return B; 29 | } 30 | }}} -------------------------------------------------------------------------------- /include/matpak/flipud.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // flipud.hpp : Flip matrix in up/down direction. 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | Matrix flipud(const Matrix&A) { 13 | typedef typename Matrix::value_type value_type; 14 | typedef typename Matrix::size_type size_type; 15 | 16 | size_type r = num_rows(A); 17 | size_type c = num_cols(A); 18 | 19 | Matrix B(r,c); 20 | 21 | for(size_t i = 0; i < r; ++i){ 22 | B[i][mtl::iall] = A[r - i - 1][mtl::iall]; 23 | } 24 | return B; 25 | } 26 | }}} -------------------------------------------------------------------------------- /include/matpak/gt.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // gt.hpp : A > B (determines if A > B element-wise) 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | #include 9 | 10 | namespace sw { namespace hprblas { namespace matpak { 11 | 12 | template 13 | Matrix operator>(const Matrix&A , const Matrix&B){ 14 | return relop(A,RELOP_GT,B); 15 | } 16 | 17 | template 18 | Matrix gt(const Matrix&A, const Matrix&B) { 19 | return relop(A,RELOP_GT,B); 20 | } 21 | }}} -------------------------------------------------------------------------------- /include/matpak/hadamard.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // hadamard.hpp : Hadamard product of A.*B. 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | Matrix hadamard(const Matrix&A, const Matrix&B) { 13 | typedef typename Matrix::value_type value_type; 14 | typedef typename Matrix::size_type size_type; 15 | 16 | size_type m = num_rows(A); 17 | size_type n = num_cols(A); 18 | 19 | if( m!=num_rows(B) || n != num_cols(B) ){return Matrix {0};} 20 | 21 | Matrix C(m,n); 22 | int k = 1; 23 | 24 | for(int i = 0; i <= num_rows(A); ++i){ 25 | for(size_t j = 0; j <= num_cols(A); ++j){ 26 | C[i][j] = A[i][j]*B[i][j]; 27 | } 28 | } 29 | return C; 30 | } 31 | }}} -------------------------------------------------------------------------------- /include/matpak/hankel.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // hankel.hpp : Generate Hankel matrix 3 | // CALL hankel(c,r) where c = column & r = row 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | #include 11 | #include 12 | 13 | namespace sw { namespace hprblas { namespace matpak { 14 | 15 | template 16 | mtl::dense2D hankel(const Vector&c, const Vector&r) { 17 | typedef typename Vector::value_type value_type; 18 | 19 | auto A = toeplitz(c,r); 20 | auto B = rot90(A); 21 | 22 | return B; 23 | } 24 | }}} 25 | -------------------------------------------------------------------------------- /include/matpak/isa/iscentro.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // iscentro.hpp : Determine if matrix is centrosymmetric 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | #include 11 | 12 | namespace sw { namespace hprblas { namespace matpak { 13 | 14 | template 15 | bool iscentro(const Matrix&A) { 16 | auto m = num_rows(A); 17 | auto n = num_cols(A); 18 | 19 | if (m!=n) return false; 20 | 21 | Matrix I(n,n); 22 | I = 1; // Identity matrix 23 | 24 | Matrix J = rot90(I); // Counter-identity 25 | Matrix JAJ(m,n); 26 | JAJ = J*A*J; 27 | 28 | return isequal(JAJ,A); 29 | } 30 | }}} -------------------------------------------------------------------------------- /include/matpak/isa/isdiagdom.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // isdiagdom.hpp : Determine if matrix is diagonally dominant 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | bool isdiagdom(const Matrix&A) { 13 | auto m = num_rows(A); 14 | auto n = num_cols(A); 15 | 16 | // Diagonally Dominant is n x n by definition 17 | if (m!=n) return false; 18 | 19 | using Scalar = typename Matrix::value_type; 20 | 21 | Scalar R = 0; 22 | 23 | for(int i=0; i 12 | bool isequal(const Matrix&A, const Matrix&B, const typename Matrix::value_type& tolerance = 0.001) { 13 | typedef typename Matrix::size_type size_type; 14 | 15 | size_type ra = num_rows(A); 16 | size_type ca = num_cols(A); 17 | 18 | size_type rb = num_rows(B); 19 | size_type cb = num_cols(B); 20 | 21 | if (ra!=rb || ca!=cb){ 22 | return false; 23 | } 24 | 25 | for(size_type i = 0; i < ra; ++i){ 26 | for(size_type j = 0; j < ca; ++j){ 27 | if(abs(A[i][j]-B[i][j]) > tolerance) return false; 28 | } 29 | } 30 | return true; 31 | } 32 | }}} 33 | -------------------------------------------------------------------------------- /include/matpak/isa/isirred.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // isirred.hpp : Deteremines if A is an irreducible matrix 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | namespace sw { namespace hprblas { namespace matpak { 11 | 12 | template 13 | bool isirred(const Matrix&A) { 14 | typedef typename Matrix::value_type value_type; 15 | typedef typename Matrix::size_type size_type; 16 | 17 | size_type m = num_rows(A); 18 | size_type n = num_cols(A); 19 | 20 | // Must be nonnegative square matrix 21 | if (m!=n){return false}; 22 | if (!isnonneg(A)){return false}; 23 | 24 | Matrix I(n,n); 25 | I = 1; 26 | 27 | Matrix B(n,n); 28 | B = (I+A)^(n-1); 29 | 30 | if(isnonneg(B)) return true; 31 | } 32 | }}} -------------------------------------------------------------------------------- /include/matpak/isa/ism.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // ism.hpp : Check if a nxn matrix is an M-matrix 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | /* 8 | 9 | 10 | function y=ismmat(M) 11 | % 12 | % ISMMAT - Determines if a n x n is a M-matrix 13 | % 14 | % INPUT: 15 | % A = n x n Z-matrix (all off diagonal entries non positive) 16 | % 17 | % OUTPUT: 18 | % y = Boolean variable 0/1 19 | % 20 | % NOTES: A real symmetric M-matrix is POSITIVE DEFINITE 21 | % 22 | % ---------------------------------------------------------------------- % 23 | 24 | y=0; 25 | 26 | [m,n]=size(M); 27 | 28 | % Square? 29 | if m~=n 30 | y=0; 31 | return 32 | end 33 | I=eye(n); 34 | 35 | % Check if Z-matrix (all off diag. entries are <=0) 36 | %Z=M-diag(diag(M)); 37 | %if prod(prod(Z<=0))~=1 38 | % y=0; 39 | % return 40 | %end 41 | 42 | if ~iszmat(M) 43 | y=0; 44 | return 45 | end 46 | 47 | % Check if invertible 48 | if det(M)==0 49 | y=0; 50 | return 51 | end 52 | 53 | % Check if specRadius(I-D^{-1}M)<1 <==> M-matrix 54 | D=diag(diag(M)); 55 | D=inv(D); 56 | if max(abs(eigs(I-D*M)))<1 57 | y=1; 58 | end 59 | 60 | end 61 | */ 62 | 63 | namespace sw { namespace hprblas { 64 | 65 | // TEST IF MATRIX IS AN M-Matrix 66 | 67 | // 68 | template 69 | bool ism(const Matrix& A) { 70 | bool is_m_matrix = false; 71 | 72 | // body 73 | 74 | 75 | // Return 76 | return is_m_matrix; 77 | } 78 | 79 | 80 | 81 | 82 | }} // namespace sw::hprblas 83 | -------------------------------------------------------------------------------- /include/matpak/isa/isnonneg.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // isnonneg.hpp : Deteremines if A is nonnegative 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | bool isnonneg(const Matrix&A) { 13 | typedef typename Matrix::value_type value_type; 14 | typedef typename Matrix::size_type size_type; 15 | 16 | size_type m = num_rows(A); 17 | size_type n = num_cols(A); 18 | 19 | for(size_t i = 0; i< m; ++i){ 20 | for(size_t j = 0; j< n; ++j){ 21 | if(A[i][j]<0) return false; 22 | } 23 | } 24 | return true; 25 | } 26 | }}} -------------------------------------------------------------------------------- /include/matpak/isa/isnormal.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // isnormal.hpp : Checks if a matrix A is normal 3 | // Def: (Normal matrices) A'A = AA' 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | #include 11 | 12 | namespace sw { namespace hprblas { namespace matpak { 13 | 14 | template 15 | bool isnormal(const Matrix&A, const typename Matrix::value_type& tolerance = 0.001) { 16 | Matrix A_transpose(mtl::mat::trans(A)); 17 | return isequal(A_transpose*A, A*A_transpose, tolerance); 18 | } 19 | }}} -------------------------------------------------------------------------------- /include/matpak/isa/isskewcentro.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/include/matpak/isa/isskewcentro.hpp -------------------------------------------------------------------------------- /include/matpak/kron.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // kron.hpp : Kronecker tensor product of A and B 3 | // 4 | // The result is formed by taking all possible 5 | // products between the elements of A and those of B. 6 | // A in M(m,n) and B in M(p,q), then kron(A,B) in M(mxp, nxq) 7 | // 8 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 9 | // Author: James Quinlan 10 | // 11 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 12 | 13 | namespace sw { namespace hprblas { namespace matpak { 14 | 15 | template 16 | Matrix kron(const Matrix&A, const Matrix&B) { 17 | typedef typename Matrix::value_type value_type; 18 | typedef typename Matrix::size_type size_type; 19 | 20 | size_type m = num_rows(A); 21 | size_type n = num_cols(A); 22 | size_type p = num_rows(B); 23 | size_type q = num_cols(B); 24 | 25 | Matrix C(m*p,n*q); 26 | 27 | for(size_t i = 0; i < m; ++i){ 28 | for(size_t j = 0; j < n; ++j){ 29 | 30 | Matrix X(p,q); 31 | X = A[i][j]*B[mtl::iall][mtl::iall]; 32 | 33 | for(size_t r = 0; r < p; ++r){ 34 | for(size_t c = 0; c < q; ++c){ 35 | C[r + p*i][c + q*j] = X[r][c]; 36 | } // c 37 | } // r 38 | } // j 39 | } // i 40 | 41 | return C; 42 | } 43 | }}} -------------------------------------------------------------------------------- /include/matpak/lt.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // lt.hpp : A > B (determines if A > B element-wise) 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | 11 | namespace sw { namespace hprblas { namespace matpak { 12 | 13 | template 14 | Matrix lt(const Matrix&A, const Matrix&B) { 15 | return relop(A,RELOP_LT,B); 16 | } 17 | }}} -------------------------------------------------------------------------------- /include/matpak/mkcentro.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/include/matpak/mkcentro.hpp -------------------------------------------------------------------------------- /include/matpak/mkskewcentro.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stillwater-sc/hpr-blas/3757af5b09e00a9bcaa9d07a9ba88178d41138cc/include/matpak/mkskewcentro.hpp -------------------------------------------------------------------------------- /include/matpak/ones.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // ones.hpp: Ones matrix 3 | // 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | namespace sw { namespace hprblas { namespace matpak { 11 | 12 | template 13 | Matrix Ones(size_t m, size_t n) { 14 | using value_type = typename Matrix::value_type; 15 | 16 | Matrix C(m,n); 17 | for (size_t i=0; i < m; ++i){ 18 | for (size_t j = 0; j B (determines if A > B element-wise) 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | bool validate_shape(const Matrix&A, const Matrix&B){ 13 | typedef typename Matrix::size_type size_type; 14 | size_type ra = num_rows(A); 15 | size_type ca = num_cols(A); 16 | 17 | size_type rb = num_rows(B); 18 | size_type cb = num_cols(B); 19 | 20 | if (ra!=rb || ca!=cb){ 21 | return false; 22 | } 23 | return true; 24 | } 25 | enum RelOp{RELOP_EQ, RELOP_NEQ, RELOP_GT, RELOP_GTE, RELOP_LT, RELOP_LTE}; 26 | 27 | 28 | template 29 | Matrix relop(const Matrix&A, const RelOp& op, const Matrix&B){ 30 | 31 | if(!validate_shape(A,B)) return Matrix {0}; 32 | 33 | typedef typename Matrix::size_type size_type; 34 | 35 | size_type m = num_rows(A); 36 | size_type n = num_cols(A); 37 | 38 | Matrix C(m,n); 39 | 40 | for(size_type i = 0; i < m; ++i){ 41 | for(size_type j = 0; j < n; ++j){ 42 | switch (op){ 43 | case RELOP_EQ: 44 | if(A[i][j] == B[i][j]) C[i][j] = 1; 45 | case RELOP_NEQ: 46 | if(A[i][j] != B[i][j]) C[i][j] = 1; 47 | case RELOP_LT: 48 | if(A[i][j] < B[i][j]) C[i][j] = 1; 49 | case RELOP_LTE: 50 | if(A[i][j] <= B[i][j]) C[i][j] = 1; 51 | case RELOP_GT: 52 | if(A[i][j] > B[i][j]) C[i][j] = 1; 53 | case RELOP_GTE: 54 | if(A[i][j] >= B[i][j]) C[i][j] = 1; 55 | } 56 | } 57 | } 58 | return C; 59 | } 60 | 61 | }}} -------------------------------------------------------------------------------- /include/matpak/reshape.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // reshape.hpp : Maps m x n --> r x c where mn = rc. 3 | // reshape is row-wise. 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | #include 10 | 11 | namespace sw { namespace hprblas { namespace matpak { 12 | 13 | // NOTE: Matrix arg is not marked const bc the copy constructor with matrix elements 14 | // is not const. (See Line 16) 15 | 16 | template 17 | Matrix reshape(Matrix&A, size_t m, size_t n) { 18 | typedef typename Matrix::value_type value_type; 19 | typedef typename Matrix::size_type size_type; 20 | 21 | size_type ra = num_rows(A); 22 | size_type ca = num_cols(A); 23 | 24 | if (ra*ca!=m*n){ 25 | return Matrix {0}; 26 | } 27 | Matrix B(m, n, A.elements()); 28 | return B; 29 | } 30 | }}} 31 | -------------------------------------------------------------------------------- /include/matpak/rot90.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // rot90.hpp : Rotate matrix 90 degrees counter clockwise 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | #include 10 | 11 | namespace sw { namespace hprblas { namespace matpak { 12 | 13 | template 14 | Matrix rot90(const Matrix&A) { 15 | Matrix T(mtl::mat::trans(A)); 16 | return flipud(T); 17 | } 18 | }}} -------------------------------------------------------------------------------- /include/matpak/rowsto.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // rowsto.hpp : Generates a random m x n row stochastic matrix 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | #include 9 | #include 10 | 11 | namespace sw { namespace hprblas { namespace matpak { 12 | 13 | template 14 | Matrix rowsto(size_t m, size_t n) { 15 | typedef typename Matrix::value_type value_type; 16 | 17 | // Use random_device to generate a seed for Mersenne twister engine. 18 | std::random_device rd{}; 19 | 20 | // Use Mersenne twister engine to generate pseudo-random numbers. 21 | std::mt19937 engine{ rd() }; 22 | 23 | // "Filter" MT engine's output to generate pseudo-random double values, 24 | // **uniformly distributed** on the closed interval [lowerbound, upperbound]. 25 | // (Note that the range is [inclusive, inclusive].) 26 | constexpr double lowerbound = 0; 27 | constexpr double upperbound = 10; 28 | 29 | std::uniform_real_distribution dist{ lowerbound, upperbound }; 30 | 31 | Matrix A(m,n); 32 | int i ,j; 33 | 34 | for(i = 0; i < m; ++i) { 35 | for( j = 0; j < n; ++j) { 36 | A[i][j] = value_type(dist(engine)); 37 | } 38 | value_type x = mtl::sum(A[i][mtl::iall]); 39 | for( j = 0; j < n; ++j) { 40 | A[i][j] = A[i][j]/x; 41 | } 42 | } 43 | return A; 44 | } 45 | }}} -------------------------------------------------------------------------------- /include/matpak/size.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // size.hpp : determines size of matrix in rows and columns [m, n] 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // Author: James Quinlan 6 | // 7 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 8 | 9 | namespace sw { namespace hprblas { namespace matpak { 10 | 11 | template 12 | Matrix size(const Matrix&A) { 13 | typedef typename Matrix::size_type size_type; 14 | 15 | size_type m = num_rows(A); 16 | size_type n = num_cols(A); 17 | 18 | Matrix S(1,2); 19 | S[0][0] = m; 20 | S[0][1] = n; 21 | 22 | return S; 23 | } 24 | }}} -------------------------------------------------------------------------------- /include/matpak/sum.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // sum.hpp : sum of elements in matrix along dimensions specified 3 | // S = sum(A,dim) 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | namespace sw { namespace hprblas { namespace matpak { 11 | 12 | template 13 | Matrix sum(const Matrix&A, const int& dim) { 14 | typedef typename Matrix::size_type size_type; 15 | typedef typename Matrix::value_type value_type; 16 | 17 | size_type m = num_rows(A); 18 | size_type n = num_cols(A); 19 | 20 | 21 | if (dim == 1){ // Column sum 22 | Matrix S(1,n); 23 | for(size_type j = 0; j < n; ++j){ 24 | value_type temp_sum = 0; 25 | for(size_type i = 0; i < m; ++i){ 26 | temp_sum += A[i][j]; 27 | } 28 | S[0][j] = temp_sum; 29 | } 30 | return S; 31 | }else if(dim == 2){ // Row sums 32 | Matrix S(m,1); 33 | for(size_type i = 0; i < m; ++i){ 34 | value_type temp_sum = 0; 35 | for(size_type j = 0; j < n; ++j){ 36 | temp_sum += A[i][j]; 37 | } 38 | S[i][0] = temp_sum; 39 | } 40 | return S; 41 | }else if(dim == 0){ // Sum All elements 42 | Matrix S(1,1); 43 | value_type temp_sum = 0; 44 | for(size_type i = 0; i < m; ++i){ 45 | for(size_type j = 0; j < n; ++j){ 46 | temp_sum += A[i][j]; 47 | } 48 | } 49 | S[0][0] = temp_sum; 50 | return S; 51 | } // end if 52 | return Matrix{}; 53 | } 54 | }}} -------------------------------------------------------------------------------- /include/matpak/toeplitz.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // toeplitz.hpp : Generate Toeplitz matrix 3 | // CALL toeplitz(c,r) where c = column & r = row 4 | // 5 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 6 | // Author: James Quinlan 7 | // 8 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 9 | 10 | namespace sw { namespace hprblas { namespace matpak { 11 | 12 | template 13 | mtl::dense2D toeplitz(const Vector&c, const Vector&r) { 14 | typedef typename Vector::value_type value_type; 15 | 16 | using Matrix = mtl::dense2D; 17 | 18 | size_t m = num_rows(c); 19 | size_t n = num_rows(r); 20 | 21 | if (c[0]!=r[0]){ 22 | std::cout << 23 | "Warning: First element of input column does not match first element of input row." << 24 | "\n\nColumn wins diagonal conflict.\n"; 25 | } 26 | 27 | Matrix A(m,n); 28 | 29 | for(size_t i = 0; i< m; ++i){ 30 | for(size_t j = 0; j< n; ++j){ 31 | if(i==j){ 32 | A[i][j]=c[0]; 33 | }else if(i < j){ 34 | A[i][j] = r[j - i ]; 35 | }else{ // i >j 36 | A[i][j] = c[i - j]; 37 | } 38 | } 39 | } 40 | return A; 41 | } 42 | }}} -------------------------------------------------------------------------------- /include/mtl_extensions.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // mtl_extensions.hpp : include file containing templated utilities to work with matrices 3 | // 4 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 7 | #include 8 | #include 9 | #include 10 | 11 | namespace mtl { 12 | namespace mat { 13 | 14 | // fill a dense matrix with random values between [lowerbound, upperbound] 15 | template 16 | void uniform_rand(Matrix& A, double lowerbound = 0.0, double upperbound = 1.0) 17 | { 18 | // Use random_device to generate a seed for Mersenne twister engine. 19 | std::random_device rd{}; 20 | // Use Mersenne twister engine to generate pseudo-random numbers. 21 | std::mt19937 engine{ rd() }; 22 | // "Filter" MT engine's output to generate pseudo-random double values, 23 | // **uniformly distributed** on the closed interval [lowerbound, upperbound]. 24 | // (Note that the range is [inclusive, inclusive].) 25 | std::uniform_real_distribution dist{ lowerbound, upperbound }; 26 | // Pattern to generate pseudo-random number. 27 | // double rnd_value = dist(engine); 28 | 29 | typedef typename Collection::value_type value_type; 30 | typedef typename Collection::size_type size_type; 31 | 32 | // inserters add to the elements, so we need to set the value to 0 before we begin 33 | A = 0.0; 34 | { // extra block unfortunately needed for VS2013 35 | // Create inserter for matrix m 36 | inserter ins(A, num_cols(A)); 37 | 38 | // generate and insert random values in A 39 | for (size_type r = 0; r < num_rows(A); r++) { 40 | for (size_type c = 0; c < num_cols(A); c++) { 41 | ins[r][c] << value_type(dist(engine)); 42 | } 43 | } 44 | // Destructor of ins sets final state of m 45 | } 46 | } 47 | 48 | } // namespace mat 49 | } // namespace mtl -------------------------------------------------------------------------------- /include/polynomials/README.md: -------------------------------------------------------------------------------- 1 | # Polynomials 2 | 3 | Free functions representing special polynomial expansions and approximations. 4 | 5 | * Euler 6 | * Taylor 7 | * Legendre 8 | * Hermite 9 | * Chebyshev 10 | * Bernstein 11 | * Bessel 12 | * Bernoulli 13 | 14 | A monomial is a polynomial that consists of exactly one term. 15 | A binomial is a polynomial that consists of exactly two terms. 16 | A trinomial is a polynomial that consists of exactly three terms. 17 | 18 | -------------------------------------------------------------------------------- /include/polynomials/bernstein.hpp: -------------------------------------------------------------------------------- 1 | // bernstein.hpp: evaluation of Bernstein polynomials 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // Author: Allan Leal 5 | // 6 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 7 | #include 8 | 9 | // sign returns (-1)^exponent 10 | int sign(int exponent) { 11 | return ( (exponent % 2) == 0 ) ? 1 : -1; 12 | } 13 | 14 | /* 15 | The Bernstein matrix of order N is an NxN matrix A which can be used to 16 | transform a vector of power basis coefficients C representing a polynomial 17 | P(X) to a corresponding Bernstein basis coefficient vector B: 18 | B = A * C 19 | */ 20 | 21 | // bernstein returns the Bernstein matrix 22 | template 23 | Matrix bernstein(int n) { 24 | using Scalar = typename Matrix::value_type; 25 | 26 | if (n < 0) { 27 | std::cerr << "Degree of the Bernstein matrix can't be negative: " << n << std::endl; 28 | return Matrix{}; 29 | } 30 | Matrix A(n, n); 31 | for (int j = 0; j < n; ++j ) { 32 | for (int i = 0; i <= j; ++i ) { 33 | A[i][j] = sign(j - i) * 34 | sw::function::binomial(n - 1 - i, j - i) * 35 | sw::function::binomial(n - 1, i); 36 | } 37 | } 38 | return A; 39 | } 40 | -------------------------------------------------------------------------------- /include/solvers/gauss_jordan.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // gauss_jordan.hpp: Gauss-Jordan matrix inversion 3 | // 4 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 7 | 8 | #include 9 | 10 | namespace sw { 11 | namespace hprblas { 12 | 13 | template 14 | Matrix GaussJordanInversion(const Matrix& A) { 15 | using Scalar = typename mtl::Collection::value_type; 16 | 17 | size_t m = A.num_rows(); 18 | size_t n = A.num_cols(); 19 | Matrix a(n, m), inv(m, n); 20 | a = A; // you need a deep copy 21 | inv = Scalar(1); 22 | 23 | // Performing elementary operations 24 | for (unsigned i = 0; i < m; ++i) { 25 | if (a[i][i] == 0) { 26 | unsigned c = 1; 27 | while (a[i + c][i] == 0 && (i + c) < n) ++c; 28 | if ((i + c) == n) break; 29 | 30 | for (unsigned j = i, k = 0; k < n; ++k) { 31 | std::swap(a[j][k], a[j + c][k]); 32 | std::cerr << "TBD" << std::endl; // need to create a permutation matrix 33 | } 34 | } 35 | // transform to diagonal matrix 36 | for (unsigned j = 0; j < m; j++) { 37 | if (i != j) { 38 | Scalar scale = a[j][i] / a[i][i]; 39 | for (unsigned k = 0; k < n; ++k) { 40 | a[j][k] = a[j][k] - a[i][k] * scale; 41 | inv[j][k] = inv[j][k] - inv[i][k] * scale; 42 | } 43 | } 44 | //std::cout << i << "," << j << std::endl; 45 | //sw::hprblas::printMatrix(std::cout, "a", a); 46 | //sw::hprblas::printMatrix(std::cout, "inv", inv); 47 | } 48 | } 49 | // transform to identity matrix 50 | for (unsigned i = 0; i < m; ++i) { 51 | Scalar normalize = a[i][i]; 52 | a[i][i] = Scalar(1); 53 | for (unsigned j = 0; j < n; ++j) { 54 | inv[i][j] /= normalize; 55 | } 56 | } 57 | //printMatrix(std::cout, "conversion", a); 58 | return inv; 59 | } 60 | 61 | } // namespace hprblas 62 | } // namespace sw -------------------------------------------------------------------------------- /include/solvers/ldlt.hpp: -------------------------------------------------------------------------------- 1 | // ldlt.hpp: LDL^T decomposition of a square matrix 2 | // 3 | -------------------------------------------------------------------------------- /include/utils/matvec.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // matvec.hpp: convenience aggregation header for all utilities for working with vectors ana matrices 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | #include "vector_utils.hpp" 8 | #include "print_utils.hpp" -------------------------------------------------------------------------------- /include/utils/print_utils.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // print_utils.hpp : include file containing templated utilities to work with vectors and matrices 3 | // 4 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 7 | 8 | 9 | // These functions print matrices and vectors in a nice format 10 | namespace sw { 11 | namespace hprblas { 12 | 13 | // printVector pretty prints a vector 14 | template 15 | void printVector(std::ostream& ostr, const std::string& name, const Vector& v) { 16 | size_t d = size(v); 17 | ostr << "Vector: " << name << " is of size " << d << " elements" << std::endl; 18 | std::streamsize old_prec = ostr.precision(); 19 | ostr << std::setprecision(17); 20 | for (auto&& element: v) std::cout << std::setw(20) << element << " "; 21 | ostr << std::setprecision(old_prec) << std::endl; 22 | } 23 | 24 | // printMatrix pretty prints a 2D dense matrix 25 | template 26 | void printMatrix(std::ostream& ostr, const std::string& name, const Matrix& M, int precision = 17, bool hex = false) { 27 | using namespace mtl; 28 | size_t d = num_rows(M); 29 | ostr << "Matrix: " << name << " is " << d << "x" << d << std::endl; 30 | std::streamsize old_prec = ostr.precision(); 31 | ostr << std::setprecision(precision); 32 | for (size_t i = 0; i 49 | void printMatrix(std::ostream& ostr, const std::string& name, const std::vector& M) { 50 | using namespace mtl; 51 | size_t d = size(M); 52 | ostr << "Matrix: " << name << " is " << d << "x" << d << std::endl; 53 | std::streamsize old_prec = ostr.precision(); 54 | ostr << std::setprecision(17); 55 | for (size_t i = 0; i 65 | void printSubMatrix(std::ostream& ostr, const std::string& name, const Matrix& A, unsigned ai, unsigned aj, unsigned blockHeight, unsigned blockWidth) { 66 | ostr << "Sub-Matrix (" << ai << "," << aj << ") of " << name << " of size " << blockHeight << "x" << blockWidth << std::endl; 67 | std::streamsize old_prec = ostr.precision(); 68 | ostr << std::setprecision(17); 69 | 70 | unsigned aRows = unsigned(mtl::mat::num_rows(A)); 71 | unsigned aCols = unsigned(mtl::mat::num_cols(A)); 72 | 73 | unsigned aRow = ai*blockHeight; 74 | unsigned aCol = aj*blockWidth; 75 | 76 | unsigned maxRow = (aRow + blockHeight < aRows) ? blockHeight : aRows - aRow; 77 | unsigned maxCol = (aCol + blockWidth < aCols) ? blockWidth : aCols - aCol; 78 | 79 | for (unsigned i = 0; i < maxRow; ++i) { 80 | for (unsigned j = 0; j < maxCol; ++j) { 81 | std::cout << std::setw(20) << A[aRow + i][aCol + j] << " "; 82 | } 83 | ostr << std::endl; 84 | } 85 | ostr << std::setprecision(old_prec); 86 | } 87 | 88 | } // namespace blas 89 | } // namespace sw 90 | -------------------------------------------------------------------------------- /include/utils/vector_utils.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // vector_utils.hpp : include file containing templated utilities to work with vectors and matrices 3 | // 4 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 5 | // 6 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 7 | 8 | namespace sw { 9 | namespace hprblas { 10 | 11 | // initialize a vector 12 | template 13 | void init(Vector& x, const Scalar& value) { 14 | for (size_t i = 0; i < x.size(); ++i) x[i] = value; 15 | } 16 | 17 | // generate random data vector 18 | template 19 | void randomVectorFill(size_t n, std::vector& vec) { 20 | for (size_t i = 0; i < n; i++) { 21 | int rnd1 = rand(); 22 | int rnd2 = rand(); 23 | double rnd = rnd1 / (double)rnd2; 24 | vec[i] = (element_T)rnd; 25 | } 26 | } 27 | 28 | // generate a vector of random permutations around 0.0 29 | // contraction is a right shift of the random variable causing smaller fluctuations 30 | // RAND_MAX is typically a 16bit number so can't contract more than 15 bits 31 | template 32 | void randomVectorFillAroundZeroEPS(size_t n, std::vector& vec, size_t contraction = 6) { 33 | for (size_t i = 0; i < n; i++) { 34 | int rnd1 = (rand() - (RAND_MAX >> 1)) >> contraction; 35 | double eps = rnd1 / (double)RAND_MAX; 36 | double v = eps; 37 | vec[i] = (element_T)v; 38 | } 39 | } 40 | 41 | // generate a vector of random permutations around 1.0 42 | // contraction is a right shift of the random variable causing smaller fluctuations 43 | // RAND_MAX is typically a 16bit number so can't contract more than 15 bits 44 | template 45 | void randomVectorFillAroundOneEPS(size_t n, std::vector& vec, size_t contraction = 6) { 46 | for (size_t i = 0; i < n; i++) { 47 | int rnd1 = (rand() - (RAND_MAX >> 1)) >> contraction; 48 | double eps = rnd1 / (double)RAND_MAX; 49 | double v = 1.0 + eps; 50 | vec[i] = (element_T)v; 51 | } 52 | } 53 | 54 | // print a sampling of the provided vector 55 | // if samples is set to 0, all elements of the vector are printed 56 | template 57 | void sampleVector(std::string vec_name, std::vector& vec, uint32_t start = 0, uint32_t incr = 1, uint32_t nrSamples = 0) { 58 | std::cout << "Vector sample is: " << '\n'; 59 | if (nrSamples) { 60 | uint32_t printed = 0; 61 | for (uint32_t i = start; i < vec.size(); i += incr) { 62 | if (printed < nrSamples) { 63 | printed++; 64 | std::cout << vec_name << "[" << std::setw(3) << i << "] = " << std::setprecision(15) << vec[i] << '\n'; 65 | } 66 | } 67 | } 68 | else { 69 | for (uint32_t i = start; i < vec.size(); i += incr) { 70 | std::cout << vec_name << "[" << std::setw(3) << i << "] = " << std::setprecision(15) << vec[i] << '\n'; 71 | } 72 | } 73 | std::cout << std::endl; 74 | } 75 | 76 | } // namespace blas 77 | } // namespace sw 78 | -------------------------------------------------------------------------------- /set_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # source this file 3 | 4 | echo "Source this file to set the built environment variables (MTL_DIR, UNIVERSAL_DIR, HPRBLAS_DIR)" 5 | 6 | export MTL_DIR=~/dev/clones/mtl4 7 | export UNIVERSAL_DIR=~/dev/clones/universal 8 | export HPRBLAS_DIR=~/dev/clones/hpr-blas 9 | -------------------------------------------------------------------------------- /solvers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "solvers" "Solvers" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /solvers/README.md: -------------------------------------------------------------------------------- 1 | # HPR decompositions and solvers 2 | 3 | Cholesky -> LLT 4 | LDLT 5 | GaussJordan 6 | LU 7 | SVD 8 | -------------------------------------------------------------------------------- /solvers/multi_precision.cpp: -------------------------------------------------------------------------------- 1 | // multi_precision.cpp : example program comparing float vs posit matrix inversion algorithms 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPR-BLAS project, which is released under an MIT Open Source license. 6 | 7 | // Boost arbitrary precision floats 8 | #include 9 | 10 | // configure the posit number system behavior 11 | #define POSIT_ROUNDING_ERROR_FREE_IO_FORMAT 0 12 | // configure the HPR-BLAS behavior 13 | #define HPRBLAS_TRACE_ROUNDING_EVENTS 0 14 | #include 15 | #include 16 | // matrix generators 17 | #include 18 | #include 19 | 20 | using namespace sw::universal; 21 | 22 | template 23 | void Equation() { 24 | Scalar a{ 1.0 }; 25 | Scalar b(2.0); 26 | Scalar c = a + b; 27 | std::cout << c << std::endl; 28 | } 29 | 30 | int main(int argc, char** argv) 31 | try { 32 | using namespace std; 33 | using namespace mtl; 34 | using namespace sw::hprblas; 35 | using namespace boost::multiprecision; 36 | 37 | using sp = boost::multiprecision::cpp_bin_float_single; 38 | using dp = boost::multiprecision::cpp_bin_float_double; 39 | using qp = boost::multiprecision::cpp_bin_float_quad; 40 | 41 | 42 | 43 | Equation(); 44 | 45 | return EXIT_SUCCESS; 46 | } 47 | catch (char const* msg) { 48 | std::cerr << msg << std::endl; 49 | return EXIT_FAILURE; 50 | } 51 | catch (const sw::universal::universal_arithmetic_exception& err) { 52 | std::cerr << "Uncaught universal arithmetic exception: " << err.what() << std::endl; 53 | return EXIT_FAILURE; 54 | } 55 | catch (const sw::universal::universal_internal_exception& err) { 56 | std::cerr << "Uncaught universal internal exception: " << err.what() << std::endl; 57 | return EXIT_FAILURE; 58 | } 59 | catch (std::runtime_error& err) { 60 | std::cerr << err.what() << std::endl; 61 | return EXIT_FAILURE; 62 | } 63 | catch (...) { 64 | std::cerr << "Caught unknown exception" << std::endl; 65 | return EXIT_FAILURE; 66 | } 67 | -------------------------------------------------------------------------------- /tests/posit/128bit_posit.cpp: -------------------------------------------------------------------------------- 1 | // 128bit_posit.cpp: Functionality tests for standard 128-bit posits 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include "common.hpp" 8 | // enable posit arithmetic exceptions 9 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 10 | #include 11 | #include "../test_helpers.hpp" 12 | #include "../posit_test_helpers.hpp" 13 | 14 | /* 15 | Standard posits with nbits = 128 have 4 exponent bits. 16 | */ 17 | 18 | int main(int argc, char** argv) 19 | try { 20 | using namespace std; 21 | using namespace sw::unum; 22 | 23 | const size_t RND_TEST_CASES = 1000; 24 | 25 | const size_t nbits = 128; 26 | const size_t es = 4; 27 | 28 | int nrOfFailedTestCases = 0; 29 | bool bReportIndividualTestCases = false; 30 | std::string tag = " posit<128,4>"; 31 | 32 | cout << "Standard posit<128,4> configuration tests" << endl; 33 | posit p; 34 | cout << dynamic_range(p) << endl << endl; 35 | 36 | cout << "Arithmetic tests " << RND_TEST_CASES << " randoms each" << endl; 37 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms<128, 4>(tag, bReportIndividualTestCases, OPCODE_ADD, RND_TEST_CASES), tag, "addition "); 38 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms<128, 4>(tag, bReportIndividualTestCases, OPCODE_SUB, RND_TEST_CASES), tag, "subtraction "); 39 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms<128, 4>(tag, bReportIndividualTestCases, OPCODE_MUL, RND_TEST_CASES), tag, "multiplication"); 40 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms<128, 4>(tag, bReportIndividualTestCases, OPCODE_DIV, RND_TEST_CASES), tag, "division "); 41 | 42 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 43 | } 44 | catch (char const* msg) { 45 | std::cerr << msg << std::endl; 46 | return EXIT_SUCCESS; //as we manually throwing the not supported yet it should not fall through the cracks EXIT_FAILURE; 47 | } 48 | catch (const posit_arithmetic_exception& err) { 49 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 50 | return EXIT_FAILURE; 51 | } 52 | catch (const quire_exception& err) { 53 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 54 | return EXIT_FAILURE; 55 | } 56 | catch (const posit_internal_exception& err) { 57 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 58 | return EXIT_FAILURE; 59 | } 60 | catch (const std::runtime_error& err) { 61 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 62 | return EXIT_FAILURE; 63 | } 64 | catch (...) { 65 | std::cerr << "Caught unknown exception" << std::endl; 66 | return EXIT_FAILURE; 67 | } -------------------------------------------------------------------------------- /tests/posit/48bit_posit.cpp: -------------------------------------------------------------------------------- 1 | // 48bit_posit.cpp: Functionality tests for extended standard 48-bit posits 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include "common.hpp" 8 | // enable posit arithmetic exceptions 9 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 10 | #include 11 | #include "../test_helpers.hpp" 12 | #include "../posit_test_helpers.hpp" 13 | 14 | /* 15 | Extended Standard posit with nbits = 48 have es = 2 exponent bits. 16 | */ 17 | 18 | int main(int argc, char** argv) 19 | try { 20 | using namespace std; 21 | using namespace sw::unum; 22 | 23 | const size_t RND_TEST_CASES = 150000; 24 | 25 | const size_t nbits = 48; 26 | const size_t es = 2; 27 | 28 | int nrOfFailedTestCases = 0; 29 | bool bReportIndividualTestCases = false; 30 | std::string tag = " posit<48,2>"; 31 | 32 | cout << "Extended Standard posit<48,2> configuration tests" << endl; 33 | 34 | posit p; 35 | cout << dynamic_range(p) << endl << endl; 36 | 37 | cout << "Arithmetic tests " << RND_TEST_CASES << " randoms each" << endl; 38 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_ADD, RND_TEST_CASES), tag, "addition "); 39 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_SUB, RND_TEST_CASES), tag, "subtraction "); 40 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_MUL, RND_TEST_CASES), tag, "multiplication"); 41 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_DIV, RND_TEST_CASES), tag, "division "); 42 | 43 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 44 | } 45 | catch (char const* msg) { 46 | std::cerr << msg << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | catch (const posit_arithmetic_exception& err) { 50 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 51 | return EXIT_FAILURE; 52 | } 53 | catch (const quire_exception& err) { 54 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 55 | return EXIT_FAILURE; 56 | } 57 | catch (const posit_internal_exception& err) { 58 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 59 | return EXIT_FAILURE; 60 | } 61 | catch (const std::runtime_error& err) { 62 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 63 | return EXIT_FAILURE; 64 | } 65 | catch (...) { 66 | std::cerr << "Caught unknown exception" << std::endl; 67 | return EXIT_FAILURE; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /tests/posit/64bit_posit.cpp: -------------------------------------------------------------------------------- 1 | // 64bit_posit.cpp: Functionality tests for standard 64-bit posits 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include "common.hpp" 8 | // enable posit arithmetic exceptions 9 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 10 | #include 11 | #include "../test_helpers.hpp" 12 | #include "../posit_test_helpers.hpp" 13 | 14 | /* 15 | Standard posit with nbits = 64 have es = 3 exponent bits. 16 | */ 17 | 18 | int main(int argc, char** argv) 19 | try { 20 | using namespace std; 21 | using namespace sw::unum; 22 | 23 | const size_t RND_TEST_CASES = 0; // 100000; 24 | 25 | const size_t nbits = 64; 26 | const size_t es = 3; 27 | 28 | int nrOfFailedTestCases = 0; 29 | bool bReportIndividualTestCases = false; 30 | std::string tag = " posit<64,3>"; 31 | 32 | cout << "Standard posit<64,3> configuration tests" << endl; 33 | 34 | posit p; 35 | cout << dynamic_range(p) << endl << endl; 36 | 37 | cout << "Arithmetic tests " << RND_TEST_CASES << " randoms each" << endl; 38 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_ADD, RND_TEST_CASES), tag, "addition "); 39 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_SUB, RND_TEST_CASES), tag, "subtraction "); 40 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_MUL, RND_TEST_CASES), tag, "multiplication"); 41 | nrOfFailedTestCases += ReportTestResult(VerifyThroughRandoms(tag, bReportIndividualTestCases, OPCODE_DIV, RND_TEST_CASES), tag, "division "); 42 | 43 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 44 | } 45 | catch (char const* msg) { 46 | std::cerr << msg << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | catch (const posit_arithmetic_exception& err) { 50 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 51 | return EXIT_FAILURE; 52 | } 53 | catch (const quire_exception& err) { 54 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 55 | return EXIT_FAILURE; 56 | } 57 | catch (const posit_internal_exception& err) { 58 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 59 | return EXIT_FAILURE; 60 | } 61 | catch (const std::runtime_error& err) { 62 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 63 | return EXIT_FAILURE; 64 | } 65 | catch (...) { 66 | std::cerr << "Caught unknown exception" << std::endl; 67 | return EXIT_FAILURE; 68 | } 69 | -------------------------------------------------------------------------------- /tests/posit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "qa" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /tests/posit/big_posits.cpp: -------------------------------------------------------------------------------- 1 | // big_posit.cpp: Functionality tests for big posits 2 | // 3 | // Copyright (C) 2017-2019 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | #include "common.hpp" 8 | 9 | #include 10 | // enable posit arithmetic exceptions 11 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 12 | // to capture all the possible bits, use 13 | #define POSIT_ROUNDING_ERROR_FREE_IO_FORMAT 1 14 | #include 15 | #include "../test_helpers.hpp" 16 | #include "../posit_test_helpers.hpp" 17 | 18 | /* 19 | experiments with big posits 20 | */ 21 | 22 | // Sample the conversion space around 1 when number of fraction bits 23 | // of a big posit is bigger than the number of bits in the input representation 24 | template 25 | void Sample(Ty value) { 26 | using namespace std; 27 | using namespace sw::unum; 28 | 29 | cout << typeid(value).name() << endl; 30 | cout << posit<54, 3>(value) << " " << double(posit<54, 3>(value)) << endl; 31 | cout << posit<56, 3>(value) << " " << double(posit<56, 3>(value)) << endl; 32 | cout << posit<58, 3>(value) << " " << double(posit<58, 3>(value)) << endl; 33 | cout << posit<60, 3>(value) << " " << double(posit<60, 3>(value)) << endl; 34 | cout << posit<62, 3>(value) << " " << double(posit<62, 3>(value)) << endl; 35 | cout << posit<64, 3>(value) << " " << double(posit<64, 3>(value)) << endl; 36 | cout << posit<66, 3>(value) << " " << double(posit<66, 3>(value)) << endl; 37 | cout << posit<67, 3>(value) << " " << double(posit<67, 3>(value)) << endl; 38 | cout << posit<68, 3>(value) << " " << double(posit<68, 3>(value)) << endl; 39 | cout << posit<69, 3>(value) << " " << double(posit<69, 3>(value)) << endl; 40 | cout << posit<70, 3>(value) << " " << double(posit<70, 3>(value)) << endl; 41 | cout << posit<71, 3>(value) << " " << double(posit<71, 3>(value)) << endl; 42 | cout << posit<72, 3>(value) << " " << double(posit<72, 3>(value)) << endl; 43 | cout << posit<80, 3>(value) << " " << double(posit<80, 3>(value)) << endl; 44 | } 45 | 46 | int main(int argc, char** argv) 47 | try { 48 | using namespace std; 49 | using namespace sw::unum; 50 | 51 | const size_t RND_TEST_CASES = 1000; 52 | 53 | const size_t nbits = 128; 54 | const size_t es = 4; 55 | 56 | int nrOfFailedTestCases = 0; 57 | bool bReportIndividualTestCases = false; 58 | std::string tag = " big posit conversion experiments"; 59 | 60 | posit<80, 3> p; 61 | p = 1ull; 62 | p = 2ull; 63 | 64 | Sample(1); 65 | Sample(2); 66 | Sample(1ull); 67 | Sample(2ull); 68 | Sample(1.0f); 69 | 70 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 71 | } 72 | catch (char const* msg) { 73 | std::cerr << msg << std::endl; 74 | return EXIT_SUCCESS; //as we manually throwing the not supported yet it should not fall through the cracks EXIT_FAILURE; 75 | } 76 | catch (const posit_arithmetic_exception& err) { 77 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 78 | return EXIT_FAILURE; 79 | } 80 | catch (const quire_exception& err) { 81 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 82 | return EXIT_FAILURE; 83 | } 84 | catch (const posit_internal_exception& err) { 85 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 86 | return EXIT_FAILURE; 87 | } 88 | catch (const std::runtime_error& err) { 89 | std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; 90 | return EXIT_FAILURE; 91 | } 92 | catch (...) { 93 | std::cerr << "Caught unknown exception" << std::endl; 94 | return EXIT_FAILURE; 95 | } -------------------------------------------------------------------------------- /tests/posit/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for common system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #ifdef WINDOWS 9 | // Including SDKDDKVer.h defines the highest available Windows platform. 10 | 11 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 12 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 13 | 14 | #include 15 | #endif // WINDOWS 16 | 17 | #include 18 | #include 19 | //#include 20 | #include // uint8_t, etc. 21 | #include // for frexp/frexpf and std::fma 22 | #include // feclearexcept/fetestexcept 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #if defined(__GNUC__) 30 | #if __GNUC__ < 5 31 | #define hexfloat scientific 32 | #define defaultfloat scientific 33 | #endif 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /tests/test_helpers.hpp: -------------------------------------------------------------------------------- 1 | // test_helpers.cpp : functions to aid in testing and test reporting 2 | // 3 | // Copyright (C) 2017 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 6 | 7 | 8 | // test reporting helper 9 | int ReportTestResult(int nrOfFailedTests, std::string description, std::string test_operation) 10 | { 11 | if (nrOfFailedTests > 0) { 12 | std::cout << description << " " << test_operation << " FAIL " << nrOfFailedTests << " failed test cases" << std::endl; 13 | } 14 | else { 15 | std::cout << description << " " << test_operation << " PASS" << std::endl; 16 | } 17 | return nrOfFailedTests; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /tests/valid_test_helpers.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // posit_test_helpers.cpp : functions to aid in testing and test reporting on posit types. 4 | // Needs to be included after posit type is declared. 5 | // 6 | // Copyright (C) 2017 Stillwater Supercomputing, Inc. 7 | // 8 | // This file is part of the universal numbers project, which is released under an MIT Open Source license. 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace sw { 16 | namespace unum { 17 | 18 | static constexpr unsigned FLOAT_TABLE_WIDTH = 15; 19 | 20 | 21 | } // namespace unum 22 | } // namespace sw 23 | 24 | -------------------------------------------------------------------------------- /tools/benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "benchmark" "Benchmarks" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /tools/benchmark/number_systems.cpp: -------------------------------------------------------------------------------- 1 | // number_systems.cpp: performance comparison between different number systems 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | #include 7 | //#define POSIT_FAST_SPECIALIZATION 1 8 | #define POSIT_FAST_POSIT_16_1 1 9 | #define POSIT_FAST_POSIT_32_2 1 10 | #include 11 | #include 12 | 13 | // define a true 256-bit IEEE floating point type 14 | constexpr size_t bits_in_octand = 113 + 128; 15 | using cpp_bin_float_octand = boost::multiprecision::number, boost::multiprecision::expression_template_option::et_off>; 16 | // define the floating point types (single, double, quad, octand) 17 | using sp = boost::multiprecision::cpp_bin_float_single; 18 | using dp = boost::multiprecision::cpp_bin_float_double; 19 | using qp = boost::multiprecision::cpp_bin_float_quad; 20 | using op = cpp_bin_float_octand; 21 | 22 | int main(int argc, char** argv) 23 | try { 24 | using namespace std; 25 | using namespace sw::universal; 26 | 27 | cout << "Arithmetic performance comparison" << endl; 28 | 29 | posit<16,1> p16; 30 | float f; 31 | sp boostsp; 32 | posit<32, 2> p32; 33 | 34 | OperatorPerformance report; 35 | GeneratePerformanceReport(f, report); 36 | cout << ReportPerformance(f, report) << endl; 37 | // GeneratePerformanceReport(boostsp, report); 38 | // cout << ReportPerformance(boostsp, report) << endl; 39 | GeneratePerformanceReport(p16, report); 40 | cout << ReportPerformance(p16, report) << endl; 41 | GeneratePerformanceReport(p32, report); 42 | cout << ReportPerformance(p32, report) << endl; 43 | 44 | return EXIT_SUCCESS; 45 | } 46 | catch (char const* msg) { 47 | std::cerr << msg << std::endl; 48 | return EXIT_FAILURE; 49 | } 50 | catch (const sw::universal::posit_arithmetic_exception& err) { 51 | std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (const sw::universal::quire_exception& err) { 55 | std::cerr << "Uncaught quire exception: " << err.what() << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | catch (const sw::universal::posit_internal_exception& err) { 59 | std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl; 60 | return EXIT_FAILURE; 61 | } 62 | catch (std::runtime_error& err) { 63 | std::cerr << err.what() << std::endl; 64 | return EXIT_FAILURE; 65 | } 66 | catch (...) { 67 | std::cerr << "Caught unknown exception" << std::endl; 68 | return EXIT_FAILURE; 69 | } 70 | -------------------------------------------------------------------------------- /tools/characterization/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all(false "app" "Tools/Characterization" ${SOURCES} ) 4 | -------------------------------------------------------------------------------- /tools/matrix_generation/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file (GLOB SOURCES "./*.cpp") 2 | 3 | compile_all("true" "matgen" "Tools/Matrix-generation" "${SOURCES}") 4 | -------------------------------------------------------------------------------- /tools/matrix_generation/asymmetric.cpp: -------------------------------------------------------------------------------- 1 | // asymmetric.cpp: asymmetric matrix generation 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 7 | #include 8 | // matrix generators 9 | #include "generators/matrix_generators.hpp" 10 | 11 | int main(int argc, char** argv) 12 | try { 13 | using namespace std; 14 | using namespace mtl; 15 | using namespace sw::universal; 16 | using namespace sw::hprblas; 17 | 18 | int nrOfFailedTestCases = 0; 19 | std::streamsize prec = std::cout.precision(); 20 | 21 | constexpr size_t nbits = 32; 22 | constexpr size_t es = 2; 23 | 24 | using Scalar = posit; 25 | // using Scalar = double; 26 | using Matrix = mtl::mat::dense2D< Scalar >; 27 | 28 | constexpr size_t N = 5; 29 | Matrix Q(N,N); 30 | 31 | uniform_random_orthogonal_Heiberger(Q); 32 | cout << Q << endl; 33 | 34 | Scalar pminpos(SpecificValue::minpos); 35 | cout << "minpos<32,2> = " << pminpos << endl; 36 | cout << "minpos<32,2> = " << setw(52) << setprecision(52) << std::fixed << pminpos << endl; 37 | 38 | cout << Q << endl; 39 | 40 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 41 | } 42 | catch (char const* msg) { 43 | std::cerr << "caught ad hoc exception: " << msg << std::endl; 44 | return EXIT_FAILURE; 45 | } 46 | catch (const sw::universal::posit_arithmetic_exception& err) { 47 | std::cerr << "uncaught posit arithmetic exception: " << err.what() << std::endl; 48 | return EXIT_FAILURE; 49 | } 50 | catch (mtl::runtime_error& err) { 51 | std::cerr << "caught MTL run-time exception: " << err.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | catch (...) { 55 | std::cerr << "caught unknown exception" << std::endl; 56 | return EXIT_FAILURE; 57 | } 58 | -------------------------------------------------------------------------------- /tools/matrix_generation/common.hpp: -------------------------------------------------------------------------------- 1 | // common.hpp : include file for standard system include files, 2 | #pragma once 3 | 4 | #include // uint8_t, etc. 5 | #include // for frexp/frexpf 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | 16 | #ifdef WINDOWS 17 | // Including SDKDDKVer.h defines the highest available Windows platform. 18 | 19 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 20 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 21 | 22 | #include 23 | #endif // WINDOWS 24 | -------------------------------------------------------------------------------- /tools/matrix_generation/symmetric.cpp: -------------------------------------------------------------------------------- 1 | // symmetric.cpp: symmetric matrix generation 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | #include 7 | // matrix generators 8 | #include 9 | 10 | int main(int argc, char** argv) 11 | try { 12 | using namespace std; 13 | using namespace mtl; 14 | using namespace sw::universal; 15 | using namespace sw::hprblas; 16 | 17 | int nrOfFailedTestCases = 0; 18 | 19 | constexpr size_t nbits = 16; 20 | constexpr size_t es = 1; 21 | 22 | using Matrix = mtl::mat::dense2D< posit >; 23 | 24 | constexpr size_t N = 10; 25 | Matrix A(N, N); 26 | 27 | // How do I make that matrix symmetric as type? 28 | uniform_rand_diagonally_dominant(A); 29 | 30 | cout << A << endl; 31 | 32 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 33 | } 34 | catch (char const* msg) { 35 | std::cerr << "caught ad hoc exception: " << msg << std::endl; 36 | return EXIT_FAILURE; 37 | } 38 | catch (mtl::runtime_error& err) { 39 | std::cerr << "caught MTL run-time exception: " << err.what() << std::endl; 40 | return EXIT_FAILURE; 41 | } 42 | catch (...) { 43 | std::cerr << "caught unknown exception" << std::endl; 44 | return EXIT_FAILURE; 45 | } 46 | -------------------------------------------------------------------------------- /tools/matrix_generation/test.cpp: -------------------------------------------------------------------------------- 1 | // test structure to collaborate with Simunova 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // Filename: eigenvalue_example.cpp (part of MTL4) 6 | 7 | #include 8 | // matrix generators 9 | #include 10 | #include 11 | 12 | using Matrix = mtl::mat::dense2D< double >; 13 | 14 | #ifdef MTL_MSVC_BUG_FIX 15 | int eigenvalue_example() { 16 | using namespace std; 17 | 18 | Matrix M1(3,3), M2(3,3), M3(3,3), M4(3,3); 19 | 20 | M1 = 2,0,0, 21 | 1,1,0, 22 | 0,1,3; //EWs: 1,2,3 23 | 24 | mtl::mat::eigenvalue_solver E1(M1); 25 | E1.setMaxIteration(10); 26 | E1.calc(); 27 | cout << "M1(setting the number of iterations): " 28 | << E1.get_eigenvalues() << "\n"; 29 | 30 | M2 = 1,0,0, 31 | 0,1,5, 32 | 0,-2,3; //EWs: 1,2+3i,2-3i 33 | 34 | mtl::mat::eigenvalue_solver E2(M2); 35 | E2.setTolerance(1.0e-10); 36 | E2.calc(); 37 | cout << "M2(providing tolerance): " 38 | << E2.get_eigenvalues() << "\n"; 39 | 40 | M3 = -261, 209, -49, 41 | -530, 422, -98, 42 | -800, 631, -144; //EWs: 3,4,10 43 | 44 | mtl::mat::eigenvalue_solver E3(M3); 45 | E3.setMaxIteration(10); 46 | E3.setTolerance(1.0e-10); 47 | E3.calc(); 48 | cout << "M3(providing both): " 49 | << E3.get_eigenvalues() << "\n"; 50 | 51 | M4 = 1,-3,3, 52 | 3,-5,3, 53 | 6,-6,4; //EWs: -2,-2,4 54 | 55 | mtl::mat::eigenvalue_solver E4(M4); 56 | E4.calc(); 57 | cout << "M4(with defaults): " 58 | << E4.get_eigenvalues() << "\n"; 59 | 60 | // Creating the solver implicitly 61 | cout << "M4(with defaults): " << eigenvalues(M4) << "\n"; 62 | } 63 | #endif // MTL_MSVC_BUG_FIX 64 | 65 | int main() { 66 | using namespace std; 67 | using namespace sw::universal; 68 | using namespace sw::hprblas; 69 | 70 | #ifdef MTL_MSVC_BUG_FIX 71 | eigenvalue_example(); 72 | #endif 73 | 74 | for (size_t N = 5; N < 51; N = N + 5) { 75 | Matrix A(N,N); 76 | uniform_rand_diagonally_dominant(A); 77 | cout << A << endl; 78 | // cout << eigenvalues(A) << endl; 79 | } 80 | 81 | { 82 | using Matrix = mtl::mat::dense2D< posit<16,1> >; 83 | Matrix M1(3, 3); 84 | 85 | M1 = 2, 0, 0, 86 | 1, 1, 0, 87 | 0, 1, 3; 88 | 89 | cout << "M1:\n" << M1 << endl; 90 | printMatrix(cout, "M1", M1, 0, true); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tools/matrix_generation/uniform_random.cpp: -------------------------------------------------------------------------------- 1 | // uniform_random.cpp: uniform random matrix generator test 2 | // 3 | // Copyright (C) 2017-2020 Stillwater Supercomputing, Inc. 4 | // 5 | // This file is part of the HPRBLAS project, which is released under an MIT Open Source license. 6 | #define POSIT_THROW_ARITHMETIC_EXCEPTION 1 7 | #include 8 | // matrix generators 9 | #include "generators/matrix_generators.hpp" 10 | 11 | int main(int argc, char** argv) 12 | try { 13 | using namespace std; 14 | using namespace mtl; 15 | using namespace sw::universal; 16 | using namespace sw::hprblas; 17 | 18 | int nrOfFailedTestCases = 0; 19 | std::streamsize prec = std::cout.precision(); 20 | 21 | constexpr size_t nbits = 32; 22 | constexpr size_t es = 2; 23 | 24 | using Scalar = posit; 25 | // using Scalar = double; 26 | using Matrix = mtl::mat::dense2D< Scalar >; 27 | 28 | constexpr size_t m = 5; 29 | constexpr size_t n = 4; 30 | Matrix A(m,n); 31 | 32 | uniform_rand(A); 33 | cout << "uniform random :\n" << A << endl; 34 | 35 | auto B = uniform_rand(m, n, -1.0, 1.0); 36 | cout << "uniform random(-1,1) :\n" << B << endl; 37 | 38 | uniform_rand_sorted(A); 39 | cout << "uniform random sorted :\n" << A << endl; 40 | 41 | return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 42 | } 43 | catch (char const* msg) { 44 | std::cerr << "caught ad hoc exception: " << msg << std::endl; 45 | return EXIT_FAILURE; 46 | } 47 | catch (const sw::universal::posit_arithmetic_exception& err) { 48 | std::cerr << "uncaught posit arithmetic exception: " << err.what() << std::endl; 49 | return EXIT_FAILURE; 50 | } 51 | catch (mtl::runtime_error& err) { 52 | std::cerr << "caught MTL run-time exception: " << err.what() << std::endl; 53 | return EXIT_FAILURE; 54 | } 55 | catch (...) { 56 | std::cerr << "caught unknown exception" << std::endl; 57 | return EXIT_FAILURE; 58 | } 59 | --------------------------------------------------------------------------------