https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
17namespace Moose::Kokkos
18{
19
28template <typename T>
30{
31public:
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
56private:
60 const ::Kokkos::MemoryPool<MemSpace> & _pool;
64 const dof_id_type _size;
68 T * _ptr;
69};
70
76{
77public:
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
100private:
105};
106
112{
113public:
118 MemoryPoolHolder(const MooseApp & app) : _app(app), _pool(_app.getKokkosMemoryPool()) {}
123 : _app(holder._app), _pool(_app.getKokkosMemoryPool())
124 {
125 }
126
127protected:
132 KOKKOS_FUNCTION const MemoryPool & kokkosMemoryPool() const { return _pool; }
133
134private:
138 const MooseApp & _app;
143};
144
145} // namespace Moose::Kokkos
Base class for MOOSE-based applications.
Definition MooseApp.h:110
KOKKOS_FUNCTION index_type size() const
Get the total array size.
The Kokkos array class.
A temporary object returned by the Kokkos memory pool during memory chunk allocation.
KOKKOS_FUNCTION ~MemoryChunk()
Destructor.
T * _ptr
Pointer to the memory chunk.
KOKKOS_FUNCTION MemoryChunk(const ::Kokkos::MemoryPool< MemSpace > &pool, dof_id_type size)
Constructor.
const ::Kokkos::MemoryPool< MemSpace > & _pool
Reference of the Kokkos memory pool.
const dof_id_type _size
Memory chunk size in the number of bytes.
KOKKOS_FUNCTION T * get() const
Get the pointer to the chunk.
The Kokkos interface that holds the Kokkos memory pool.
const MooseApp & _app
Reference of the MOOSE app.
MemoryPool _pool
Device copy of the Kokkos memory pool.
MemoryPoolHolder(const MooseApp &app)
Constructor.
MemoryPoolHolder(const MemoryPoolHolder &holder)
Copy constructor.
KOKKOS_FUNCTION const MemoryPool & kokkosMemoryPool() const
Get the const reference of the Kokkos memory pool.
The Kokkos class that manages memory pool for dynamically-sized temporary arrays in Kokkos parallel f...
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.
MemoryPool(dof_id_type size, unsigned int ways)
Constructor.
Array<::Kokkos::MemoryPool< MemSpace > > _pools
Kokkos memory pools.