ImpactX
Loading...
Searching...
No Matches
dynamicdata.H
Go to the documentation of this file.
1/* Copyright 2022-2026 The Regents of the University of California, through Lawrence
2 * Berkeley National Laboratory (subject to receipt of any required
3 * approvals from the U.S. Dept. of Energy). All rights reserved.
4 *
5 * This file is part of ImpactX.
6 *
7 * Authors: Axel Huebl
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_ELEMENTS_MIXIN_DYNAMICDATA_H
11#define IMPACTX_ELEMENTS_MIXIN_DYNAMICDATA_H
12
13#include "impactx_export.H"
14
15#include <AMReX_Gpu.H>
16
17#include <map>
18#include <memory>
19#include <stdexcept>
20#include <string>
21#include <type_traits>
22#include <vector>
23
24
26{
27
48 template<typename T>
50 {
51 using DataType = T;
52
53 IMPACTX_EXPORT static int& next_id () {
54 static int id = 0;
55 return id;
56 }
57
58 IMPACTX_EXPORT static std::map<int, std::shared_ptr<T>>& registry () {
59 static std::map<int, std::shared_ptr<T>> reg;
60 return reg;
61 }
62
64 static int allocate_id () { return next_id()++; }
65
72 static std::shared_ptr<T> const &
73 get (int id)
74 {
75 auto& reg = registry();
76 auto it = reg.find(id);
77 if (it == reg.end() || !it->second)
78 throw std::runtime_error(
79 "GPUDataRegistry::get failed for id=" + std::to_string(id));
80 return it->second;
81 }
82
88 static void store (int id, std::shared_ptr<T> data)
89 {
90 registry()[id] = std::move(data);
91 }
92
99 template<typename... Args>
100 static T& emplace (int id, Args&&... args)
101 {
102 auto ptr = std::shared_ptr<T>(new T{std::forward<Args>(args)...}); // NOLINT(modernize-make-shared)
103 registry()[id] = ptr;
104 return *ptr;
105 }
106
108 static void clear ()
109 {
110 registry().clear();
111 next_id() = 0;
112 }
113 };
114
115} // namespace impactx::elements::mixin
116
117
130#define IMPACTX_GPUDATA_EXTERN(ElementType) \
131 extern template struct impactx::elements::mixin::GPUDataRegistry< \
132 ElementType::DynamicData::DataType>;
133
141#define IMPACTX_GPUDATA_INSTANTIATE(ElementType) \
142 template struct impactx::elements::mixin::GPUDataRegistry< \
143 ElementType::DynamicData::DataType>;
144
145#endif // IMPACTX_ELEMENTS_MIXIN_DYNAMICDATA_H
Definition alignment.H:23
static void store(int id, std::shared_ptr< T > data)
Definition dynamicdata.H:88
static IMPACTX_EXPORT int & next_id()
Definition dynamicdata.H:53
static std::shared_ptr< T > const & get(int id)
Definition dynamicdata.H:73
static int allocate_id()
Definition dynamicdata.H:64
static T & emplace(int id, Args &&... args)
Definition dynamicdata.H:100
CFbendCoefficients DataType
Definition dynamicdata.H:51
static IMPACTX_EXPORT std::map< int, std::shared_ptr< T > > & registry()
Definition dynamicdata.H:58
static void clear()
Definition dynamicdata.H:108