ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
comin_state.cpp
Go to the documentation of this file.
1
12
13#include "comin_state.hpp"
14#include "comin_logging.hpp"
15
16#include <algorithm>
17#include <cstring>
18
19// Global state variable.
20t_comin_state comin::state;
21
22extern "C" {
23
24 void comin_state_set_num_plugins(int num_plugins) {
25 comin::state.num_plugins = num_plugins;
26 }
27
28 int comin_state_get_num_plugins() { return comin::state.num_plugins; }
29
30 void comin_state_set_error_code(int errcode) {
31 comin::state.errcode = errcode;
32 }
33
34 int comin_state_get_error_code() { return comin::state.errcode; }
35
36 void comin_state_set_lstdout(bool lstdout) { comin::state.lstdout = lstdout; }
37
38 bool comin_state_get_lstdout() { return comin::state.lstdout; }
39
41 comin::state.output_unit = unit;
42 }
43
44 int comin_state_get_output_unit() { return comin::state.output_unit; }
45
46 t_comin_var_map* comin_state_get_var_list() {
47 return &comin::state.comin_var_list;
48 }
49
50 t_comin_var_request_list* comin_state_get_var_request_list() {
51 return &comin::state.comin_var_request_list;
52 }
53
54 // sets verbosity level, related to ICON's `msg_level`.
55 // 0 = silent
57 comin::state.comin_iverbosity = level;
58 }
59
60 // get verbosity level
62 return comin::state.comin_iverbosity;
63 }
64
65 // Access information on the current entry point being processed by ComIn.
66 // @ingroup fortran_interface
67 t_comin_entry_point comin_current_get_ep() { return comin::state.current_ep; }
68
69 // Access information on the current domain id being processed by ComIn.
70 // @ingroup fortran_interface
71 int comin_current_get_domain_id() { return comin::state.current_domain_id; }
72
73 // Access information on the current simulation date-time stamp (ISO 8601)
74 // @ingroup fortran_interface
76 return comin::state.current_datetime.c_str();
77 }
78
79 void comin_current_set_datetime(const char* datetime_c) {
80 std::string datetime{datetime_c};
81
82 if (datetime.size() > COMIN_MAX_DATETIME_STR_LEN) {
83 comin_error_set(COMIN_ERROR_FATAL);
84 } else {
85 comin::state.current_datetime = std::move(datetime);
86 }
87 }
88
89 void comin_current_set_plugin_id(int plugin_id) {
90 comin::state.current_plugin_id = plugin_id;
91 }
92
93 int comin_current_get_plugin_id() { return comin::state.current_plugin_id; }
94
95 bool comin_state_is_primary_done() { return comin::state.l_primary_done; }
96
97 void comin_state_set_primary_done() { comin::state.l_primary_done = true; }
98
99 void
100 comin_state_set_sync_halo_fct(t_comin_var_sync_halo_fct_ptr sync_halo_fct) {
101 comin::state.sync_halo_fct = sync_halo_fct;
102 }
103
105 t_comin_var_sync_device_fct_ptr sync_device_fct) {
106 comin::state.sync_device_fct = sync_device_fct;
107 }
108
109 // Synchronize all variables requested for halo region synchronization.
110 void comin_sync_vars_for_halo_region(t_comin_entry_point ep, int plugin_id,
111 int rw_flag) {
112 using comin::state;
113 auto& context =
114 state.comin_var_list_context[std::make_tuple(ep, plugin_id)];
115 for (const t_comin_var_context& item : context) {
116 if ((item.access_flag & rw_flag) &&
117 (item.access_flag & COMIN_FLAG_SYNC_HALO)) {
118 if (state.sync_halo_fct == nullptr) {
119 comin_error_set(COMIN_ERROR_VAR_SYNC_HALO_NOT_ASSOCIATED);
120 return;
121 }
122 state.sync_halo_fct(item.var_item->descriptor.id, item.var_item->cptr,
123 item.var_item->type_id, item.var_item->array_shape,
124 item.var_item->dim_semantics);
125 }
126 }
127 }
128
129 // Check if any variable was requested with device access at non-ported
130 // entrypoints (`lacc == .false.`).
131 void comin_check_var_no_device(t_comin_entry_point ep, int plugin_id) {
132 auto& context =
133 comin::state.comin_var_list_context[std::make_tuple(ep, plugin_id)];
134 for (const t_comin_var_context& item : context) {
135 if (item.access_flag & COMIN_FLAG_DEVICE) {
136 const char* ep_name = comin_callback_get_ep_name(ep);
137 comin::logging::message(
138 1, "WARNING: Device access at a non-ported entrypoint (", ep_name,
139 ") requested.");
140 }
141 }
142 }
143
144 // Synchronize all variables requested for device access.
145 void comin_sync_vars_for_device(t_comin_entry_point ep, int plugin_id,
146 int rw_flag) {
147 using comin::state;
148 auto& context = state.comin_var_list_context[{ep, plugin_id}];
149
150 for (const t_comin_var_context& item : context) {
151 if ((item.access_flag & rw_flag) &&
152 (item.access_flag & COMIN_FLAG_SYNC_HALO)) {
153 if (state.sync_device_fct == nullptr) {
154 comin_error_set(COMIN_ERROR_VAR_SYNC_DEVICE_MEM_NOT_ASSOCIATED);
155 return;
156 }
157 state.sync_device_fct(item.var_item->cptr, item.var_item->device_ptr,
158 item.var_item->type_id,
159 item.var_item->array_shape,
160 rw_flag == COMIN_FLAG_WRITE);
161 }
162 }
163 }
165 t_comin_glb2loc_index_lookup_fct_ptr fct) {
166 comin::state.comin_descrdata_fct_glb2loc_cell = fct;
167 if (!comin::state.comin_descrdata_fct_glb2loc_cell) {
168 comin_error_set(COMIN_ERROR_DESCRDATA_SET_FCT_GLB2LOC);
169 return;
170 }
171 }
173 t_comin_glb2loc_index_lookup_fct_ptr fct) {
174 comin::state.comin_descrdata_fct_glb2loc_edge = fct;
175 if (!comin::state.comin_descrdata_fct_glb2loc_edge) {
176 comin_error_set(COMIN_ERROR_DESCRDATA_SET_FCT_GLB2LOC);
177 return;
178 }
179 }
181 t_comin_glb2loc_index_lookup_fct_ptr fct) {
182 comin::state.comin_descrdata_fct_glb2loc_vert = fct;
183 if (!comin::state.comin_descrdata_fct_glb2loc_vert) {
184 comin_error_set(COMIN_ERROR_DESCRDATA_SET_FCT_GLB2LOC);
185 return;
186 }
187 }
188
190 return comin::state.comin_descrdata_fct_glb2loc_cell(jg, glb);
191 }
193 return comin::state.comin_descrdata_fct_glb2loc_edge(jg, glb);
194 }
196 return comin::state.comin_descrdata_fct_glb2loc_vert(jg, glb);
197 }
198
199 void
200 comin_state_set_host_errhandler_fct(t_comin_host_errhandler_fct_ptr fct) {
201 comin::state.comin_host_errhandler_fct = fct;
202 }
203
205 return comin::state.comin_host_errhandler_fct != nullptr;
206 }
207
209 auto& timesteplength = comin::state.comin_descrdata_timesteplength;
210 if (timesteplength.size() < (size_t)jg) {
211 timesteplength.resize(jg);
212 }
213 timesteplength[jg - 1] = dt;
214 }
215
217 auto& timesteplength = comin::state.comin_descrdata_timesteplength;
218 if (timesteplength.size() < (size_t)jg) {
219 comin_error_set(COMIN_ERROR_DESCRDATA_TIMESTEPLENGTH_NOT_SET);
220 return 0.0;
221 }
222 return timesteplength[jg - 1];
223 }
224
225 void comin_state_set_simulation_interval(const char* exp_start,
226 const char* exp_stop,
227 const char* run_start,
228 const char* run_stop) {
229 comin::state.comin_descrdata_simulation_interval.exp_start = exp_start;
230 comin::state.comin_descrdata_simulation_interval.exp_stop = exp_stop;
231 comin::state.comin_descrdata_simulation_interval.run_start = run_start;
232 comin::state.comin_descrdata_simulation_interval.run_stop = run_stop;
233 }
234
236 return comin::state.comin_descrdata_simulation_interval.exp_start.c_str();
237 }
238
240 return comin::state.comin_descrdata_simulation_interval.exp_stop.c_str();
241 }
242
244 return comin::state.comin_descrdata_simulation_interval.run_start.c_str();
245 }
246
248 return comin::state.comin_descrdata_simulation_interval.run_stop.c_str();
249 }
250}
const char * comin_callback_get_ep_name(t_comin_entry_point iep)
void comin_error_set(t_comin_error_code error_code)
int comin_state_get_output_unit()
void comin_state_set_simulation_interval(const char *exp_start, const char *exp_stop, const char *run_start, const char *run_stop)
const char * comin_descrdata_get_simulation_interval_exp_start()
int comin_state_get_error_code()
t_comin_var_map * comin_state_get_var_list()
const char * comin_descrdata_get_simulation_interval_exp_stop()
bool comin_state_is_primary_done()
int comin_current_get_plugin_id()
int comin_setup_get_verbosity_level()
int comin_descrdata_index_lookup_glb2loc_cell(int jg, int glb)
void comin_state_set_primary_done()
const char * comin_current_get_datetime()
void comin_current_set_plugin_id(int plugin_id)
void comin_current_set_datetime(const char *datetime_c)
void comin_state_set_sync_device_fct(t_comin_var_sync_device_fct_ptr sync_device_fct)
void comin_check_var_no_device(t_comin_entry_point ep, int plugin_id)
void comin_state_set_output_unit(int unit)
void comin_setup_set_verbosity_level(int level)
bool comin_state_is_host_errhandler_set()
int comin_descrdata_index_lookup_glb2loc_vert(int jg, int glb)
void comin_descrdata_set_fct_glb2loc_cell(t_comin_glb2loc_index_lookup_fct_ptr fct)
void comin_state_set_lstdout(bool lstdout)
const char * comin_descrdata_get_simulation_interval_run_start()
t_comin_entry_point comin_current_get_ep()
bool comin_state_get_lstdout()
void comin_state_set_error_code(int errcode)
const char * comin_descrdata_get_simulation_interval_run_stop()
int comin_descrdata_index_lookup_glb2loc_edge(int jg, int glb)
void comin_descrdata_set_fct_glb2loc_edge(t_comin_glb2loc_index_lookup_fct_ptr fct)
void comin_sync_vars_for_device(t_comin_entry_point ep, int plugin_id, int rw_flag)
void comin_state_set_sync_halo_fct(t_comin_var_sync_halo_fct_ptr sync_halo_fct)
int comin_current_get_domain_id()
void comin_state_set_num_plugins(int num_plugins)
void comin_descrdata_set_fct_glb2loc_vert(t_comin_glb2loc_index_lookup_fct_ptr fct)
void comin_state_set_host_errhandler_fct(t_comin_host_errhandler_fct_ptr fct)
t_comin_var_request_list * comin_state_get_var_request_list()
int comin_state_get_num_plugins()
void comin_descrdata_set_timesteplength(int jg, double dt)
double comin_descrdata_get_timesteplength(int jg)
void comin_sync_vars_for_halo_region(t_comin_entry_point ep, int plugin_id, int rw_flag)
const int jg