https://mooseframework.inl.gov
KokkosThread.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #ifdef MOOSE_KOKKOS_SCOPE
13 #include "KokkosHeader.h"
14 #endif
15 
16 #include "MooseUtils.h"
17 #include "Conversion.h"
18 
19 namespace Moose::Kokkos
20 {
21 
22 using ThreadID = MOOSE_KOKKOS_INDEX_TYPE;
23 
28 template <typename thread_id_type = ThreadID, unsigned int max_dimension = 4>
29 class Thread
30 {
31  static_assert(std::is_integral_v<thread_id_type>,
32  "Kokkos thread index type must be an integral type");
33  static_assert(std::is_unsigned_v<thread_id_type>, "Kokkos thread index type must be unsigned");
34  static_assert(!std::is_same_v<bool, thread_id_type>, "Kokkos thread index type must not be bool");
35 
36 public:
37  using id_type = thread_id_type;
38 
39 #ifdef MOOSE_KOKKOS_SCOPE
40 
44  template <typename... size_type>
45  void resize(size_type... sizes);
50  KOKKOS_FUNCTION thread_id_type size() const { return _size; }
56  KOKKOS_FUNCTION thread_id_type size(const unsigned int dim) const
57  {
58  KOKKOS_ASSERT(dim < _dim);
59 
60  return _dims[dim];
61  }
68  KOKKOS_FUNCTION thread_id_type operator()(const thread_id_type tid, const unsigned int dim) const
69  {
70  KOKKOS_ASSERT(dim < _dim);
71 
72  return (tid / _strides[dim]) % _dims[dim];
73  }
74 #endif
75 
76 protected:
80  thread_id_type _size = 0;
84  unsigned int _dim = 0;
88  thread_id_type _dims[max_dimension];
92  thread_id_type _strides[max_dimension];
93 };
94 
95 #ifdef MOOSE_KOKKOS_SCOPE
96 template <typename thread_id_type, unsigned int max_dimension>
97 template <typename... size_type>
98 void
100 {
101  static_assert((std::is_convertible<size_type, thread_id_type>::value && ...),
102  "All arguments must be convertible to thread_id_type");
103  static_assert(sizeof...(sizes) <= max_dimension, "Number of arguments exceeds maximum dimension");
104 
105  std::vector<thread_id_type> dims;
106  (dims.push_back(sizes), ...);
107 
108  uint64_t size = 1;
109  _dim = dims.size();
110 
111  for (unsigned int dim = 0; dim < _dim; ++dim)
112  {
113  _dims[dim] = dims[dim];
114  _strides[dim] = dim ? _strides[dim - 1] * dims[dim - 1] : 1;
115  size *= dims[dim];
116  }
117 
119  mooseError("Kokkos thread error: the dimensions provided (",
120  Moose::stringify(dims),
121  ") has the total size of ",
122  size,
123  " which exceeds the limit of ",
124  MooseUtils::prettyCppType<thread_id_type>(),
125  ".");
126 
127  _size = size;
128 }
129 #endif
130 
131 } // namespace Moose::Kokkos
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
KOKKOS_FUNCTION thread_id_type size(const unsigned int dim) const
Get the size of each dimension.
Definition: KokkosThread.h:56
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
auto max(const L &left, const R &right)
thread_id_type _dims[max_dimension]
Thread pool size of each dimension.
Definition: KokkosThread.h:88
MOOSE_KOKKOS_INDEX_TYPE ThreadID
Definition: KokkosThread.h:22
void resize(size_type... sizes)
Set the thread pool size and dimension.
Definition: KokkosThread.h:99
KOKKOS_FUNCTION thread_id_type operator()(const thread_id_type tid, const unsigned int dim) const
Get the multi-dimensional thread index of a dimension given a one-dimensional thread index...
Definition: KokkosThread.h:68
thread_id_type _strides[max_dimension]
Thread pool stride of each dimension.
Definition: KokkosThread.h:92
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
thread_id_type _size
Total thread pool size.
Definition: KokkosThread.h:80
thread_id_type id_type
Definition: KokkosThread.h:37
unsigned int _dim
Thread pool dimension.
Definition: KokkosThread.h:84
KOKKOS_FUNCTION thread_id_type size() const
Get the total thread pool size.
Definition: KokkosThread.h:50
The Kokkos thread object that aids in converting the one-dimensional thread index into multi-dimensio...
Definition: KokkosThread.h:29