https://mooseframework.inl.gov
KokkosMemoryPool.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 #include "KokkosHeader.h"
13 #include "KokkosArray.h"
14 
15 #include "MooseApp.h"
16 
17 namespace Moose::Kokkos
18 {
19 
28 template <typename T>
30 {
31 public:
37  KOKKOS_FUNCTION MemoryChunk(const ::Kokkos::MemoryPool<MemSpace> & pool, dof_id_type size)
38  : _pool(pool), _size(size)
39  {
40  auto ptr = pool.allocate(size);
41 
42  KOKKOS_ASSERT(ptr);
43 
44  _ptr = static_cast<T *>(ptr);
45  }
49  KOKKOS_FUNCTION ~MemoryChunk() { _pool.deallocate(_ptr, _size); }
54  KOKKOS_FUNCTION T * get() const { return _ptr; }
55 
56 private:
60  const ::Kokkos::MemoryPool<MemSpace> & _pool;
68  T * _ptr;
69 };
70 
76 {
77 public:
83  MemoryPool(dof_id_type size, unsigned int ways);
84 
92  template <typename T>
93  KOKKOS_FUNCTION MemoryChunk<T> allocate(dof_id_type idx, unsigned int size) const
94  {
95  auto pool = idx % _pools.size();
96 
97  return MemoryChunk<T>(_pools[pool], size * sizeof(T));
98  }
99 
100 private:
105 };
106 
112 {
113 public:
118  MemoryPoolHolder(const MooseApp & app) : _app(app), _pool(_app.getKokkosMemoryPool()) {}
123  : _app(holder._app), _pool(_app.getKokkosMemoryPool())
124  {
125  }
126 
127 protected:
132  KOKKOS_FUNCTION const MemoryPool & kokkosMemoryPool() const { return _pool; }
133 
134 private:
138  const MooseApp & _app;
143 };
144 
145 } // namespace Moose::Kokkos
const MooseApp & _app
Reference of the MOOSE app.
The Kokkos array class.
Definition: KokkosArray.h:64
T * _ptr
Pointer to the memory chunk.
A temporary object returned by the Kokkos memory pool during memory chunk allocation.
KOKKOS_FUNCTION MemoryChunk(const ::Kokkos::MemoryPool< MemSpace > &pool, dof_id_type size)
Constructor.
KOKKOS_FUNCTION const MemoryPool & kokkosMemoryPool() const
Get the const reference of the Kokkos memory pool.
The Kokkos interface that holds the Kokkos memory pool.
Base class for MOOSE-based applications.
Definition: MooseApp.h:108
MemoryPoolHolder(const MemoryPoolHolder &holder)
Copy constructor.
The Kokkos class that manages memory pool for dynamically-sized temporary arrays in Kokkos parallel f...
MemoryPool _pool
Device copy of the Kokkos memory pool.
Array<::Kokkos::MemoryPool< MemSpace > > _pools
Kokkos memory pools.
KOKKOS_FUNCTION MemoryChunk< T > allocate(dof_id_type idx, unsigned int size) const
Allocate a memory chunk The returned object should be alive while the memory chunk is used...
const ::Kokkos::MemoryPool< MemSpace > & _pool
Reference of the Kokkos memory pool.
KOKKOS_FUNCTION ~MemoryChunk()
Destructor.
MemoryPool(dof_id_type size, unsigned int ways)
Constructor.
MemoryPoolHolder(const MooseApp &app)
Constructor.
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
uint8_t dof_id_type
const dof_id_type _size
Memory chunk size in the number of bytes.