MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
dtensor.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_DTENSOR
13 #define MFEM_DTENSOR
14 
15 #include "../general/backends.hpp"
16 
17 namespace mfem
18 {
19 
20 /// A Class to compute the real index from the multi-indices of a tensor
21 template <int N, int Dim, typename T, typename... Args>
22 class TensorInd
23 {
24 public:
25  MFEM_HOST_DEVICE
26  static inline int result(const int* sizes, T first, Args... args)
27  {
28 #if !(defined(MFEM_USE_CUDA) || defined(MFEM_USE_HIP))
29  MFEM_ASSERT(first<sizes[N-1],"Trying to access out of boundary.");
30 #endif
31  return first + sizes[N - 1] * TensorInd < N + 1, Dim, Args... >
32  ::result(sizes, args...);
33  }
34 };
35 
36 // Terminal case
37 template <int Dim, typename T, typename... Args>
38 class TensorInd<Dim, Dim, T, Args...>
39 {
40 public:
41  MFEM_HOST_DEVICE
42  static inline int result(const int* sizes, T first, Args... args)
43  {
44 #if !(defined(MFEM_USE_CUDA) || defined(MFEM_USE_HIP))
45  MFEM_ASSERT(first<sizes[Dim-1],"Trying to access out of boundary.");
46 #endif
47  return first;
48  }
49 };
50 
51 
52 /// A class to initialize the size of a Tensor
53 template <int N, int Dim, typename T, typename... Args>
54 class Init
55 {
56 public:
57  static inline int result(int* sizes, T first, Args... args)
58  {
59  sizes[N - 1] = first;
60  return first * Init < N + 1, Dim, Args... >::result(sizes, args...);
61  }
62 };
63 
64 // Terminal case
65 template <int Dim, typename T, typename... Args>
66 class Init<Dim, Dim, T, Args...>
67 {
68 public:
69  static inline int result(int* sizes, T first, Args... args)
70  {
71  sizes[Dim - 1] = first;
72  return first;
73  }
74 };
75 
76 
77 /// A basic generic Tensor class, appropriate for use on the GPU
78 template<int Dim, typename Scalar = double>
80 {
81 protected:
82  int capacity;
83  Scalar *data;
84  int sizes[Dim];
85 
86 public:
87  /// Default constructor
88  DeviceTensor() = delete;
89 
90  /// Constructor to initialize a tensor from the Scalar array _data
91  template <typename... Args>
92  DeviceTensor(Scalar* _data, Args... args)
93  {
94  static_assert(sizeof...(args) == Dim, "Wrong number of arguments");
95  // Initialize sizes, and compute the number of values
96  const long int nb = Init<1, Dim, Args...>::result(sizes, args...);
97  capacity = nb;
98  data = (capacity > 0) ? _data : NULL;
99  }
100 
101  /// Copy constructor
102  MFEM_HOST_DEVICE DeviceTensor(const DeviceTensor& t)
103  {
104  capacity = t.capacity;
105  for (int i = 0; i < Dim; ++i)
106  {
107  sizes[i] = t.sizes[i];
108  }
109  data = t.data;
110  }
111 
112  /// Conversion to `Scalar *`.
113  inline operator Scalar *() const { return data; }
114 
115  /// Const accessor for the data
116  template <typename... Args> MFEM_HOST_DEVICE inline
117  Scalar& operator()(Args... args) const
118  {
119  static_assert(sizeof...(args) == Dim, "Wrong number of arguments");
120  return data[ TensorInd<1, Dim, Args...>::result(sizes, args...) ];
121  }
122 
123  /// Subscript operator where the tensor is viewed as a 1D array.
124  MFEM_HOST_DEVICE inline Scalar& operator[](int i) const
125  {
126  return data[i];
127  }
128 };
129 
130 
131 /** @brief Wrap a pointer as a DeviceTensor with automatically deduced template
132  parameters */
133 template <typename T, typename... Dims>
134 inline DeviceTensor<sizeof...(Dims),T> Reshape(T *ptr, Dims... dims)
135 {
136  return DeviceTensor<sizeof...(Dims),T>(ptr, dims...);
137 }
138 
139 
143 
144 } // mfem namespace
145 
146 #endif // MFEM_DTENSOR
static MFEM_HOST_DEVICE int result(const int *sizes, T first, Args...args)
Definition: dtensor.hpp:42
DeviceTensor()=delete
Default constructor.
MFEM_HOST_DEVICE DeviceTensor(const DeviceTensor &t)
Copy constructor.
Definition: dtensor.hpp:102
DeviceTensor(Scalar *_data, Args...args)
Constructor to initialize a tensor from the Scalar array _data.
Definition: dtensor.hpp:92
MFEM_HOST_DEVICE Scalar & operator()(Args...args) const
Const accessor for the data.
Definition: dtensor.hpp:117
DeviceTensor< 2, double > DeviceMatrix
Definition: dtensor.hpp:142
DeviceTensor< sizeof...(Dims), T > Reshape(T *ptr, Dims...dims)
Wrap a pointer as a DeviceTensor with automatically deduced template parameters.
Definition: dtensor.hpp:134
DeviceTensor< 1, int > DeviceArray
Definition: dtensor.hpp:140
static MFEM_HOST_DEVICE int result(const int *sizes, T first, Args...args)
Definition: dtensor.hpp:26
A basic generic Tensor class, appropriate for use on the GPU.
Definition: dtensor.hpp:79
A class to initialize the size of a Tensor.
Definition: dtensor.hpp:54
static int result(int *sizes, T first, Args...args)
Definition: dtensor.hpp:57
DeviceTensor< 1, double > DeviceVector
Definition: dtensor.hpp:141
A Class to compute the real index from the multi-indices of a tensor.
Definition: dtensor.hpp:22
int sizes[Dim]
Definition: dtensor.hpp:84
static int result(int *sizes, T first, Args...args)
Definition: dtensor.hpp:69
MFEM_HOST_DEVICE Scalar & operator[](int i) const
Subscript operator where the tensor is viewed as a 1D array.
Definition: dtensor.hpp:124