LCOV - code coverage report
Current view: top level - tarch/la - Vector.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 37 86.5 %
Date: 2026-02-16 14:39:39 Functions: 12 18 66.7 %

          Line data    Source code
       1             : // Copyright (C) 2015 Technische Universitaet Muenchen
       2             : // This file is part of the Mamico project. For conditions of distribution
       3             : // and use, please see the copyright notice in Mamico's main folder, or at
       4             : // www5.in.tum.de/mamico
       5             : #ifndef _TARCH_LA_VECTOR_H_
       6             : #define _TARCH_LA_VECTOR_H_
       7             : #include "tarch/TarchDefinitions.h"
       8             : #include <cmath>
       9             : #include <iostream>
      10             : #include <type_traits>
      11             : #include <Kokkos_Core.hpp>
      12             : 
      13             : namespace tarch {
      14             : namespace la {
      15             : template <int size, class T> class Vector;
      16             : }
      17             : } // namespace tarch
      18             : 
      19             : /**     light-weight implementation of a vector class.
      20             :  *  @tparam size Size
      21             :  *  @tparam T Data type
      22             :  *  @author Philipp Neumann
      23             :  */
      24             : 
      25        3024 : template <int size, class T> class tarch::la::Vector {
      26             : private:
      27             :   T _entries[size];
      28             : 
      29             : public:
      30    10771654 :   KOKKOS_FUNCTION Vector() {}
      31             :   /** @brief init. vector with a scalar value.
      32             :    *    @param t scalar value for the initialization
      33             :    */
      34             : 
      35     3522802 :   KOKKOS_FUNCTION Vector(const T& t) {
      36    14206138 :     for (int i = 0; i < size; i++) {
      37    10741558 :       _entries[i] = t;
      38             :     }
      39             :   }
      40             :   /** @brief special constructor for 2D left empty for general purpose vector
      41             :    *    @param t0 scalar value for the initialization
      42             :    *    @param t1 scalar value for the initialization
      43             :    */
      44         224 :   KOKKOS_FUNCTION Vector(const T& t0, const T& t1) {
      45             :     static_assert(size == 2, "ERROR Vector(t0,t1) only valid for 2D vectors!");
      46         224 :     _entries[0] = t0;
      47         224 :     _entries[1] = t1;
      48             :   }
      49             :   /** @brief special constructor for 3D; left empty for general purpose vector
      50             :    *    @param t0 scalar value for the initialization
      51             :    *    @param t1 scalar value for the initialization
      52             :    *    @param t2 scalar value for the initialization
      53             :    */
      54     4303236 :   KOKKOS_FUNCTION Vector(const T& t0, const T& t1, const T& t2) {
      55             :     static_assert(size == 3, "ERROR Vector(t0,t1,t2) only valid for 3D vectors!");
      56     4303244 :     _entries[0] = t0;
      57     4303244 :     _entries[1] = t1;
      58     4276015 :     _entries[2] = t2;
      59             :   }
      60             :   /** @brief constructor init. vector from vector
      61             :    *    @param v Vector for the initialization
      62             :    */
      63    80309052 :   KOKKOS_FUNCTION Vector(const Vector<size, T>& v) {
      64   321279612 :     for (int i = 0; i < size; i++) {
      65   240970560 :       _entries[i] = v[i];
      66             :     }
      67    80309052 :   }
      68             :   /** @brief assigns a value to all vector entries.
      69             :    *    @param t scalar value for the assignment
      70             :    */
      71             :   KOKKOS_FUNCTION void assign(const T& t) {
      72             :     for (int i = 0; i < size; i++) {
      73             :       _entries[i] = t;
      74             :     }
      75             :   }
      76             :   /** @brief operator overloading for vector assignment
      77             :    *    @param v Vector for the assignment
      78             :    */
      79     4539348 :   KOKKOS_FUNCTION Vector<size, T>& operator=(const Vector<size, T>& v) {
      80    18157328 :     for (int i = 0; i < size; i++) {
      81    13617980 :       _entries[i] = v[i];
      82             :     }
      83     4539348 :     return *this;
      84             :   }
      85             :   /** @brief operator overloading; access to vector entries; both () and [] are
      86             :    * allowed
      87             :    *    @param i index
      88             :    */
      89    35212676 :   KOKKOS_FUNCTION T& operator[](int i) {
      90             : #if (TARCH_DEBUG == TARCH_YES)
      91    10590086 :     if (i < 0 || i > size - 1) {
      92             :       Kokkos::abort("ERROR Vector T& operator[]: i out of range!\n");
      93             :     }
      94             : #endif
      95       97796 :     return _entries[i];
      96             :   }
      97             :   /** @brief operator overloading; access to vector entries; both () and [] are
      98             :    * allowed; !!! Attention: const
      99             :    *    @param i index
     100             :    */
     101    68298978 :   KOKKOS_FUNCTION const T& operator[](int i) const {
     102             : #if (TARCH_DEBUG == TARCH_YES)
     103   290233018 :     if (i < 0 || i > size - 1) {
     104             :       Kokkos::abort("ERROR Vector const T& operator[]: i out of range!\n");
     105             :     }
     106             : #endif
     107       38124 :     return _entries[i];
     108             :   }
     109             :   /** @brief operator overloading; add a vector to this existing one (this)
     110             :    *    @param v vector that has to be added
     111             :    */
     112           0 :   KOKKOS_FUNCTION Vector<size, T>& operator+=(const Vector<size, T>& v) {
     113           0 :     for (int i = 0; i < size; i++) {
     114           0 :       _entries[i] += v[i];
     115             :     }
     116           0 :     return *this;
     117             :   }
     118             :   /** @brief operator overloading; subtracts a vector from the existing one
     119             :    * (this)
     120             :    *    @param v vector that has to be subtracted
     121             :    */
     122       40432 :   KOKKOS_FUNCTION Vector<size, T>& operator-=(const Vector<size, T>& v) {
     123      161728 :     for (int i = 0; i < size; i++) {
     124      121296 :       _entries[i] -= v[i];
     125             :     }
     126       40432 :     return *this;
     127             :   }
     128             : 
     129             :   /** cast Vector<size, T> to Vector<size, convert_to_T> */
     130           0 :   template <class convert_to_T> explicit KOKKOS_FUNCTION operator Vector<size, convert_to_T>() const {
     131             :     Vector<size, convert_to_T> ans;
     132      103392 :     for (unsigned int i = 0; i < size; i++) {
     133       77544 :       ans[i] = static_cast<convert_to_T>(_entries[i]);
     134             :     }
     135             :     return ans;
     136             :   }
     137             : };
     138             : 
     139             : #include "VectorOperations.cpph"
     140             : #endif // _TARCH_LA_VECTOR_H_

Generated by: LCOV version 1.14