crabmeat 1.0
Loading...
Searching...
No Matches
kspatial.h
1
11
12#ifndef CBM_KSPATIAL_H_
13#define CBM_KSPATIAL_H_
14
15#include "crabmeat-common.h"
16#include "cbm_servreg.h"
17
18#include "kernel/kbase.h"
19#include "kernel/kmount.h"
20
21#include "kernel/kfactory.h"
22#include "kernel/kfactorystore.h"
23
24#include "string/cbm_string.h"
25#include "time/cbm_time.h"
26
27/* input classes */
28#include "input/setup/setup.h"
29namespace cbm {
30typedef source_descriptor_t kdesc_t;
31typedef ldndc::setup::input_class_setup_t setup_t;
32}
33
34#include "containers/cbm_set.h"
35#include <list>
36#include <map>
37
38struct lmessage_t;
39struct lreply_t;
40
41namespace cbm {
42
43/* knode flags */
44#define IsCreated 0x0001
45#define IsInitialized 0x0002
46#define IsRegisteredPorts 0x0004
47#define IsUnregisteredPorts 0x0008
48#define IsFinalized 0x0010
49#define IsDestroyed 0x0020
50#define IsBad 0x0100
51
52struct knode_t
53{
54 kernel_t * kernel;
55 io_kcomm_t * iokcomm;
56
57 char const * kcfg;
58
59 /* knode flags (see above) */
60 int flags;
61
62 /* tree */
63 knode_t * parent;
64 knode_t * sibling;
65 knode_t * child;
66
67 /* linked list*/
68 knode_t * next;
69};
70struct kqueue_data_t
71{
72 cbm::string_t kname;
73
74 lid_t kid;
75 cbm::string_t ksource;
76
77 kernel_t * kclient;
78 cbm::string_t kclient_uri;
79 cbm::mountargs_t mountargs;
80};
81struct kqueue_t
82{
83 ~kqueue_t()
84 { this->clear(); }
85
86 bool is_empty() const
87 { return this->m_queue.empty(); }
88 size_t size() const
89 { return this->m_queue.size(); }
90
91 lerr_t push_back( kqueue_data_t & _qe)
92 {
93 cbm::mountargs_t & args = _qe.mountargs;
94 if ( args.cfg)
95 {
96 args.cfg = cbm::strdup( args.cfg);
97 if ( !args.cfg)
98 { return LDNDC_ERR_NOMEM; }
99 }
100 this->m_queue.push_back( _qe);
101 return LDNDC_ERR_OK;
102 }
103 void pop_front()
104 {
105 kqueue_data_t * qe = this->front();
106 if ( qe && qe->mountargs.cfg)
107 { cbm::strfree( qe->mountargs.cfg); }
108 this->m_queue.pop_front();
109 }
110 kqueue_data_t * front()
111 { return this->is_empty() ? NULL : &this->m_queue.front(); }
112 kqueue_data_t const * front() const
113 { return this->is_empty() ? NULL : &this->m_queue.front(); }
114
115 void clear()
116 {
117 while ( !this->is_empty())
118 { this->pop_front(); }
119 this->m_queue.clear();
120 }
121private:
122 typedef std::list< kqueue_data_t > _kqueue_t;
123 _kqueue_t m_queue;
124};
125
126struct CBM_API uri_generate_t
127{
128 static cbm::uri_t generate( kqueue_data_t const *,
129 kernel_t const * /*parent*/);
130};
131
132/* Stores pointers to all kernels in domain including
133 * the root kernel itself (which is a bit of a pain..).
134 * By linking kernels (parent,sibling,child) according
135 * to mount request we obtain a hierarchical tree
136 * structure representing the domain.
137 */
138#define KNodeMemoryReserveCount 32
139class CBM_API knodes_t
140{
141public:
142 knodes_t();
143 ~knodes_t();
144
145 size_t size() const
146 { return CBM_SetSize( this->m_nodes); }
147
148 knode_t * insert_node( knode_t * /*parent*/, knode_t *);
149 knode_t * remove_node( knode_t *, cbm::string_t const & /*uri*/); /*NOTE only removes leaf nodes*/
150
151 class const_iterator
152 {
153 public:
154 const_iterator();
155 const_iterator( CBM_Set const &);
156 const_iterator & operator++()
157 {
158 if ( !CBM_SetNodeIteratorIsEnd( this->m_ni))
159 { this->m_ni = CBM_SetFindNext( this->m_ni); }
160 return *this;
161 }
162 knode_t const * operator*()
163 { return static_cast< knode_t * >( this->m_ni.value); }
164 bool operator==( const_iterator const & _rhs) const
165 { return this->m_ni.value == _rhs.m_ni.value; }
166 bool operator!=( const_iterator const & _rhs) const
167 { return !this->operator==( _rhs); }
168 private:
169 CBM_SetNodeIterator m_ni;
170 };
171 const_iterator cbegin() const
172 { return const_iterator( this->m_nodes); }
173 const_iterator cend() const
174 { return const_iterator(); }
175
176 class iterator
177 {
178 public:
179 iterator();
180 iterator( CBM_Set &);
181 iterator & operator++()
182 {
183 if ( !CBM_SetNodeIteratorIsEnd( this->m_ni))
184 { this->m_ni = CBM_SetFindNext( this->m_ni); }
185 return *this;
186 }
187 knode_t * operator*()
188 { return static_cast< knode_t * >( this->m_ni.value); }
189 bool operator==( iterator const & _rhs) const
190 { return this->m_ni.value == _rhs.m_ni.value; }
191 bool operator!=( iterator const & _rhs) const
192 { return !this->operator==( _rhs); }
193 private:
194 CBM_SetNodeIterator m_ni;
195 };
196 iterator begin()
197 { return iterator( this->m_nodes); }
198 iterator end()
199 { return iterator(); }
200
201 knode_t * find_node( cbm::string_t const &);
202 knode_t const * find_node( cbm::string_t const &) const;
203
204private:
205 CBM_Set m_nodes;
206};
207
208class CBM_API kspatial_t : public kernel_t
209{
210 CBM_KERNEL_OBJECT(kspatial_t,__mounter__)
211 public:
212 kspatial_t();
213 ~kspatial_t();
214
215 bool is_valid_simulation() const
216 { return this->number_of_alive_kernels() > 0; }
217 bool is_complete_simulation() const
218 { return this->number_of_valid_kernels()
219 == this->number_of_alive_kernels(); }
220
221 size_t number_of_kernels() const;
222 size_t number_of_alive_kernels() const;
223 size_t number_of_valid_kernels() const;
224
229 kernel_t const * kernel_by_uri( uri_t const &) const;
230 kernel_t * kernel_by_uri( uri_t const &);
231
232 knodes_t & get_knodes()
233 { return this->m_knodes; }
234 knodes_t const & get_knodes() const
235 { return this->m_knodes; }
236
237 public:
238 void set_modelcompositor( char const * /*name*/);
239
240 public:
241 lerr_t configure( RunLevelArgs *);
242
243 lerr_t initialize( RunLevelArgs *);
244 lerr_t initialize_domain( kdesc_t const *, size_t);
245
246 lerr_t read( RunLevelArgs *);
247 lerr_t finalize( RunLevelArgs *);
248
249 protected:
250 kqueue_t m_kqueue;
251 /* input / output interface */
252 io_kcomm_t * m_iokcomm;
253
254 /* container holding pointers to models */
255 knodes_t m_knodes;
256
257 /* service registry */
258 cbm::service_registry_t m_servreg;
259
260 /* model compositor kernel to use */
261 cbm::string_t m_modelcompositor;
262
263 /* store next available rank for kernel class */
264 std::map< std::string, int > m_ranks;
265
266 public:
267 lerr_t enqueue_for_mount( cbm::string_t const & /*kernel name*/,
268 lid_t const & /*id*/, cbm::string_t /*source*/,
269 kernel_t * /*client*/, cbm::mountargs_t const * /*data*/);
270 lerr_t enqueue_for_umount(
271 cbm::string_t const & /*uri*/, kernel_t * /*client*/);
272
273 cbm::services_t resolve_services( cbm::entities_t const &,
274 cbm::services_t const &, int * /*rc*/);
275
276 public:
277 /* deactivate kernel and decrease number of active kernels */
278 void deactivate_kernel( knode_t & /*non-null kernel*/);
279 /* deactivate all kernels: this is used to terminate simulation
280 * in a sane way .. e.g., in openmp builds we cannot simply break
281 * from the time loop, so we fake an empty simulation
282 */
283 void deactivate();
284
285 private:
286 lerr_t kernels_create();
287 lerr_t kernel_create( kqueue_data_t const *, knode_t *);
288
289 lerr_t kernels_registerports();
290 lerr_t kernels_initialize();
291 lerr_t kernels_finalize();
292 lerr_t kernels_unregisterports();
293
294 lerr_t kernels_destroy();
295
296 private:
297 lerr_t m_regrid();
298 lerr_t m_kernelsrunlevel( RunLevel, int /*knode flag*/, int /*call order*/);
299 lerr_t m_kernelsrunlevel( RunLevel, int /*knode flag*/,
300 knode_t * /*head*/, knode_t ** /*recall head*/);
301
302 private:
303 /* hide these buggers for now */
304 kspatial_t( kspatial_t const &);
305 kspatial_t & operator=( kspatial_t const &);
306};
307
308} /* namespace cbm */
309
310#endif /* !CBM_KSPATIAL_H_ */
311
The "Service Registry" holds information about available services (e.g., models, readers,...
Definition Lresources.h:51