diff --git a/CMakeLists.txt b/CMakeLists.txt index 029ea5fdaed2af2fd6325b10d21d64f9fc7bd5db..ed5d6751e582a55ffa238c030118b074c2122a91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,10 @@ include_directories(${PASTIS_SOURCE_DIR}/packages/CLI11/include) add_subdirectory(utils) include_directories(utils) +# Pastis algebra +#add_subdirectory(algebra) +include_directories(algebra) + # Pastis experimental add_subdirectory(experimental) include_directories(experimental) diff --git a/algebra/TinyVector.hpp b/algebra/TinyVector.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4dec9f1a80c2e0d3bfb81158352eab80e60005d4 --- /dev/null +++ b/algebra/TinyVector.hpp @@ -0,0 +1,151 @@ +#ifndef TINY_VECTOR_HPP +#define TINY_VECTOR_HPP + +#include <cassert> +#include <iostream> + +template <size_t N, typename T=double> +class TinyVector +{ +private: + T m_values[N]; + static_assert((N>0),"TinyVector size must be strictly positive"); + +public: + KOKKOS_INLINE_FUNCTION + T operator,(const TinyVector& v) + { + T t = m_values[0]*v.m_values[0]; + for (size_t i=1; i<N; ++i) { + t += m_values[i]*v.m_values[i]; + } + return std::move(t); + } + + KOKKOS_INLINE_FUNCTION + friend TinyVector operator*(const T& t, const TinyVector& v) + { + TinyVector tv; + for (size_t i=0; i<N; ++i) { + tv.m_values[i] = t * v.m_values[i]; + } + return std::move(tv); + } + + KOKKOS_INLINE_FUNCTION + friend std::ostream& operator<<(std::ostream& os, const TinyVector& v) + { + os << '(' << v.m_values[0]; + for (size_t i=1; i<N; ++i) { + os << ',' << v.m_values[i]; + } + os << ')'; + return os; + } + + KOKKOS_INLINE_FUNCTION + TinyVector operator+(const TinyVector& v) const + { + TinyVector sum; + for (size_t i=0; i<N; ++i) { + sum[i] = m_values[i]+v.m_values[i]; + } + return std::move(sum); + } + + KOKKOS_INLINE_FUNCTION + TinyVector operator-(const TinyVector& v) const + { + TinyVector difference; + for (size_t i=0; i<N; ++i) { + difference[i] = m_values[i]-v.m_values[i]; + } + return std::move(difference); + } + + KOKKOS_INLINE_FUNCTION + TinyVector& operator+=(const TinyVector& v) + { + for (size_t i=0; i<N; ++i) { + m_values[i] += v.m_values[i]; + } + return *this; + } + + KOKKOS_INLINE_FUNCTION + TinyVector& operator-=(const TinyVector& v) + { + for (size_t i=0; i<N; ++i) { + m_values[i] -= v.m_values[i]; + } + return *this; + } + + KOKKOS_INLINE_FUNCTION + TinyVector& operator=(const T& t) + { + for (size_t i=0; i<N; ++i) { + m_values[i] = t; + } + return *this; + } + + KOKKOS_INLINE_FUNCTION + T& operator[](const size_t& i) + { + assert(i<N); + return m_values[i]; + } + + KOKKOS_INLINE_FUNCTION + const T& operator[](const size_t& i) const + { + assert(i<N); + return m_values[i]; + } + + KOKKOS_INLINE_FUNCTION + const TinyVector& operator=(const TinyVector& v) + { + for (size_t i=0; i<N; ++i) { + m_values[i] = v.m_values[i]; + } + return *this; + } + + KOKKOS_INLINE_FUNCTION + TinyVector& operator=(TinyVector&& v) = default; + + KOKKOS_INLINE_FUNCTION + TinyVector() + { + ; + } + + KOKKOS_INLINE_FUNCTION + TinyVector(const T& t) + { + for (size_t i=0; i<N; ++i) { + m_values[i] = t; + } + } + + KOKKOS_INLINE_FUNCTION + TinyVector(const TinyVector& v) + { + for (size_t i=0; i<N; ++i) { + m_values[i] = v.m_values[i]; + } + } + + KOKKOS_INLINE_FUNCTION + TinyVector(TinyVector&& v) = default; + + KOKKOS_INLINE_FUNCTION + ~TinyVector() + { + ; + } +}; + +#endif // TINYVECTOR_HPP diff --git a/main.cpp b/main.cpp index fb203f33487c79a86fdaef0480cdfc3dd7b42585..f2a8f173cdc0bb527e731c54cd5072a4abac99bb 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,8 @@ #include <AcousticSolver.hpp> #include <AcousticSolverTest.hpp> +#include <TinyVector.hpp> + #include <CLI/CLI.hpp> #include <cassert> #include <limits> @@ -107,6 +109,12 @@ int main(int argc, char *argv[]) method_cost_map["AcousticSolver"] = timer.seconds(); } + Kokkos::View<TinyVector<2,double>*[2]> test("test", 10); + constexpr size_t N = 3; + std::cout << "sizeof(TinyVector<" << N << ",double>)=" << sizeof(TinyVector<N,double>) << std::endl; + std::cout << "sizeof(double)=" << sizeof(double) << std::endl; + + Kokkos::finalize(); std::cout << "----------------------\n"; @@ -125,5 +133,13 @@ int main(int argc, char *argv[]) << rang::style::reset << '\n'; } + TinyVector<2> x=1; + TinyVector<2> y=2; + + std::cout << x << "-" << y << "=" << x-y << std::endl; + std::cout << x << "+" << y << "=" << x+y << std::endl; + std::cout << "3*" << x << '=' << 3*x << std::endl; + x[1]=3; y[1]=0.1; + std::cout << "< " << x << " | " << y << " > = " << (x,y) << std::endl; return 0; }