ARTS 2.5.11 (git: 725533f0)
workspace_ng.cc
Go to the documentation of this file.
1
8#include "workspace_ng.h"
9#include "debug.h"
10#include "tokval.h"
12#include <memory>
13
14#include "agenda_class.h"
15#include "global_data.h"
17#include "wsv_aux.h"
18
19namespace global_data {
21} // namespace global_data
22
24
25Index Workspace::add_wsv(const WsvRecord &wsv) {
26 wsv_data_ptr->push_back(wsv);
27 WsvMap_ptr->operator[](wsv.Name()) = wsv_data_ptr->nelem() - 1;
28 ws.emplace_back();
29 return wsv_data_ptr->nelem() - 1;
30}
31
32void Workspace::set_empty(Index i) {
33 if (ws[i].size()) {
34 ws[i].pop();
35 emplace(i);
36 }
37}
38
39void Workspace::duplicate(Index i) {
40 static const auto agenda_index = global_data::WsvGroupMap.at("Agenda");
41
43
44 if (ws[i].size()) {
45 wsvs.wsv = workspace_memory_handler.duplicate((*wsv_data_ptr)[i].Group(),
46 ws[i].top().wsv);
47 wsvs.initialized = true;
48 } else {
49 if ((*wsv_data_ptr)[i].Group() == agenda_index) {
50 wsvs.wsv = std::make_shared<Agenda>(*this);
51 } else {
52 wsvs.wsv = workspace_memory_handler.allocate((*wsv_data_ptr)[i].Group());
53 }
54 wsvs.initialized = false;
55 }
56 ws[i].push(std::move(wsvs));
57}
58
60 : std::enable_shared_from_this<Workspace>(),
61 ws(workspace.ws.nelem()),
62 wsv_data_ptr(workspace.wsv_data_ptr),
63 WsvMap_ptr(workspace.WsvMap_ptr),
64 original_workspace(workspace.original_workspace) {
65 for (Index i = 0; i < workspace.ws.nelem(); i++) {
66 if (workspace.ws[i].size() && workspace.ws[i].top().wsv) {
68 wsvs.wsv = workspace.ws[i].top().wsv;
69 wsvs.initialized = workspace.ws[i].top().initialized;
70 ws[i].push(std::move(wsvs));
71 }
72 }
73}
74
76 for (Index i=0; i<nelem(); i++) {
77 if (is_initialized(i)) {
78 auto group = wsv_data_ptr->at(i).Group();
79
80 if (group == WorkspaceGroupIndexValue<Agenda>) {
81 Agenda& ag = *static_cast<Agenda*>((*this)[i].get());
82 ag.set_workspace(*this);
83 } else if (group == WorkspaceGroupIndexValue<ArrayOfAgenda>) {
84 for (auto& ag: *static_cast<ArrayOfAgenda*>((*this)[i].get())) {
85 ag.set_workspace(*this);
86 }
87 }
88 }
89 }
90}
91
92void Workspace::pop(Index i) {
93 ws[i].pop(); }
94
95void Workspace::swap(Workspace &other) noexcept {
96 ws.swap(other.ws);
97 wsv_data_ptr.swap(other.wsv_data_ptr);
98 WsvMap_ptr.swap(other.WsvMap_ptr);
99 std::swap(original_workspace, other.original_workspace);
100
101 // Must also claim Agenda ownership
102 claim_agenda_ownership();
103 other.claim_agenda_ownership();
104}
105
106bool Workspace::is_initialized(Index i) const {
107 return ws[i].size() and ws[i].top().initialized;
108}
109
110Index Workspace::depth(Index i) const { return static_cast<Index>(ws[i].size()); }
111
112void Workspace::emplace(Index i) {
113 static const auto agenda_index = global_data::WsvGroupMap.at("Agenda");
114
115 if ((*wsv_data_ptr)[i].Group() == agenda_index) {
116 ws[i].emplace(
117 WorkspaceVariableStruct{std::make_shared<Agenda>(*this), false});
118 } else {
119 ws[i].emplace(WorkspaceVariableStruct{
120 workspace_memory_handler.allocate((*wsv_data_ptr)[i].Group()), false});
121 }
122}
123
124std::shared_ptr<void> Workspace::operator[](Index i) {
125 if (ws[i].size() == 0) emplace(i);
126 ws[i].top().initialized = true;
127 return ws[i].top().wsv;
128}
129
131 : ws(global_data::wsv_data.nelem()),
132 wsv_data_ptr(std::make_shared<Array<WsvRecord>>(global_data::wsv_data)),
133 WsvMap_ptr(std::make_shared<unordered_map<String, Index>>()),
134 original_workspace(this) {
135 for(auto x : global_data::WsvMap) WsvMap_ptr -> emplace(x);
136 ARTS_ASSERT(wsv_data_ptr -> size() == WsvMap_ptr->size())
137
138 for (Index i = 0; i < (*wsv_data_ptr).nelem(); i++) {
139 if ((*wsv_data_ptr)[i].has_defaults()) {
140 push_move(i, (*wsv_data_ptr)[i].get_copy());
141 }
142 }
143}
144
145std::shared_ptr<Workspace> Workspace::deepcopy() {
146 auto mout = Workspace{};
147 auto out = std::make_shared<Workspace>(std::move(mout));
148 out->wsv_data_ptr = std::make_shared<Workspace::wsv_data_type>(
149 *wsv_data_ptr);
150 out->WsvMap_ptr = std::make_shared<Workspace::WsvMap_type>(
151 *WsvMap_ptr);
152 out->ws.resize(nelem());
153
154 for (Index i = 0; i < out->nelem(); i++) {
155 auto &wsv_data = out->wsv_data_ptr->operator[](i);
156
157 if (depth(i) > 0) {
158 // Set the WSV by copying the top value
159 out->ws[i].emplace(WorkspaceVariableStruct{
160 workspace_memory_handler.duplicate(
161 wsv_data_ptr->operator[](i).Group(), ws[i].top().wsv),
162 is_initialized(i)});
163
164 // Copy the agenda to the new workspace
165 if (wsv_data.Group() == WorkspaceGroupIndexValue<Agenda>) {
166 Agenda *ag = static_cast<Agenda *>(out->operator[](i).get());
167 *ag = ag->deepcopy_if(*out);
168 } else if (wsv_data.Group() == WorkspaceGroupIndexValue<ArrayOfAgenda>) {
169 for (auto &a :
170 *static_cast<ArrayOfAgenda *>(out->operator[](i).get())) {
171 a = a.deepcopy_if(*out);
172 }
173 }
174 }
175
176 // If we have any default agenda types, we must copy them to the new workspace as well
177 if (wsv_data.has_defaults()) {
178 if (wsv_data.Group() == WorkspaceGroupIndexValue<Agenda>) {
179 wsv_data.update_default_value(
180 Agenda(wsv_data.default_value()).deepcopy_if(*out));
181 }
182 if (wsv_data.Group() == WorkspaceGroupIndexValue<ArrayOfAgenda>) {
183 wsv_data.update_default_value(
184 deepcopy_if(*out, wsv_data.default_value()));
185 }
186 }
187 }
188
189 return out;
190}
191
194 out.reserve(inds.nelem());
195 for (auto ind : inds) out.push_back(wsv_data_ptr->at(ind));
196 return out;
197}
198
200 ArrayOfIndex out;
201 out.reserve(wsv_data.nelem());
202 for (auto &wsv : wsv_data) {
203 Index &pos = out.emplace_back();
204
205 if (auto ptr = WsvMap_ptr->find(wsv.Name()); ptr == WsvMap_ptr->end()) {
206 pos = add_wsv(wsv);
207 } else {
208 pos = ptr->second;
209 }
210
211 auto &existing_wsv = wsv_data_ptr->at(pos);
212
213 ARTS_USER_ERROR_IF(existing_wsv.Group() not_eq wsv.Group(),
214 "Mismatching groups")
215 }
216 return out;
217}
218
219std::shared_ptr<Workspace> Workspace::create() {
220 auto mout = Workspace{};
221 return std::make_shared<Workspace>(std::move(mout));
222}
223
224std::shared_ptr<Workspace> Workspace::shallowcopy() const {
225 auto mout = Workspace{*this};
226 return std::make_shared<Workspace>(std::move(mout));
227}
ArrayOfAgenda deepcopy_if(Workspace &ws, const ArrayOfAgenda &agendas)
Same as Agenda member method but for an entire array.
Declarations for agendas.
The Agenda class.
void set_workspace(Workspace &x)
Agenda deepcopy_if(Workspace &) const
Creates a deep copy of the agenda if necessary (i.e., different workspace)!
This can be used to make arrays out of anything.
Definition array.h:31
Index nelem() const ARTS_NOEXCEPT
Definition array.h:75
Handling of workspace memory.
std::shared_ptr< void > allocate(Index group_index)
Allocate workspace WSV of given group.
std::shared_ptr< void > duplicate(Index group_index, const std::shared_ptr< void > &ptr)
Duplicate workspace variable of given group.
Workspace class.
std::shared_ptr< wsv_data_type > wsv_data_ptr
void swap(Workspace &other) noexcept
Swap with another workspace.
Index add_wsv(const WsvRecord &wsv)
Add a new variable to this workspace.
static std::shared_ptr< Workspace > create()
Creates a new Workspace, it has to be created as a shared pointer.
void emplace(Index)
std::shared_ptr< Workspace > shallowcopy() const
Shallow copy of a Workspace, it has to be created as a shared pointer.
void pop(Index i)
Remove the topmost WSV from its stack.
void claim_agenda_ownership()
T * get(const char *name)
Retrieve a value ptr if it exist (FIXME: C++20 allows const char* as template argument)
bool is_initialized(Index i) const
Checks existence of the given WSV.
wsv_data_type wsvs(const ArrayOfIndex &) const
void duplicate(Index i)
Duplicate WSV.
Index nelem() const
Get the number of workspace variables.
Array< WorkspaceVariable > ws
Workspace variable container.
Workspace()
Construct a new workspace.
std::shared_ptr< Workspace > deepcopy()
Gets a full copy that owns all the data (only gets the top of the stack)
std::shared_ptr< WsvMap_type > WsvMap_ptr
Index depth(Index i) const
Return scoping level of the given WSV.
void set_empty(Index i)
Delete WSV.
std::shared_ptr< void > operator[](Index i)
Retrieve a pointer to the given WSV.
void push_move(Index i, std::shared_ptr< T > &&wsv_ptr)
Move a WSV onto its stack.
This class contains all static information for one workspace variable.
Definition wsv_aux.h:41
const String & Name() const
Name of this workspace variable.
Definition wsv_aux.h:75
Helper macros for debugging.
#define ARTS_ASSERT(condition,...)
Definition debug.h:86
#define ARTS_USER_ERROR_IF(condition,...)
Definition debug.h:137
WorkspaceMemoryHandler workspace_memory_handler
The workspace memory handler Defined in workspace_ng.cc.
std::map< String, Index > WsvMap
Definition workspace.cc:24
const map< String, Index > WsvGroupMap
The map associated with wsv_groups.
Definition groups.cc:24
#define a
The WorkspaceMemoryHandler.
This file contains the Workspace class.
Auxiliary header stuff related to workspace variable groups.