This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathregister_mech.cpp
More file actions
436 lines (383 loc) · 13.7 KB
/
register_mech.cpp
File metadata and controls
436 lines (383 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
# =============================================================================
# Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
#
# See top-level LICENSE file for details.
# =============================================================================.
*/
#include <cstring>
#include "coreneuron/nrnconf.h"
#include "coreneuron/sim/multicore.hpp"
#include "coreneuron/membrane_definitions.h"
#include "coreneuron/mechanism/eion.hpp"
#include "coreneuron/mechanism/mech_mapping.hpp"
#include "coreneuron/mechanism/membfunc.hpp"
#include "coreneuron/coreneuron.hpp"
#include "coreneuron/utils/nrnoc_aux.hpp"
namespace coreneuron {
int secondorder = 0;
nrn_pragma_omp(declare target)
double t, dt, celsius, pi;
nrn_pragma_omp(end declare target)
int rev_dt;
using Pfrv = void (*)();
static void ion_write_depend(int type, int etype);
void hoc_reg_bbcore_read(int type, bbcore_read_t f) {
if (type == -1) {
return;
}
corenrn.get_bbcore_read()[type] = f;
}
void hoc_reg_bbcore_write(int type, bbcore_write_t f) {
if (type == -1) {
return;
}
corenrn.get_bbcore_write()[type] = f;
}
void add_nrn_has_net_event(int type) {
if (type == -1) {
return;
}
corenrn.get_has_net_event().push_back(type);
}
/* values are type numbers of mechanisms which have FOR_NETCONS statement */
int nrn_fornetcon_cnt_; /* how many models have a FOR_NETCONS statement */
int* nrn_fornetcon_type_; /* what are the type numbers */
int* nrn_fornetcon_index_; /* what is the index into the ppvar array */
void add_nrn_fornetcons(int type, int indx) {
if (type == -1)
return;
int i = nrn_fornetcon_cnt_++;
nrn_fornetcon_type_ = (int*) erealloc(nrn_fornetcon_type_, (i + 1) * sizeof(int));
nrn_fornetcon_index_ = (int*) erealloc(nrn_fornetcon_index_, (i + 1) * sizeof(int));
nrn_fornetcon_type_[i] = type;
nrn_fornetcon_index_[i] = indx;
}
void add_nrn_artcell(int type, int qi) {
if (type == -1) {
return;
}
corenrn.get_is_artificial()[type] = 1;
corenrn.get_artcell_qindex()[type] = qi;
}
void set_pnt_receive(int type,
pnt_receive_t pnt_receive,
pnt_receive_t pnt_receive_init,
short size) {
if (type == -1) {
return;
}
corenrn.get_pnt_receive()[type] = pnt_receive;
corenrn.get_pnt_receive_init()[type] = pnt_receive_init;
corenrn.get_pnt_receive_size()[type] = size;
}
void alloc_mech(int memb_func_size_) {
corenrn.get_memb_funcs().resize(memb_func_size_);
corenrn.get_pnt_map().resize(memb_func_size_);
corenrn.get_pnt_receive().resize(memb_func_size_);
corenrn.get_pnt_receive_init().resize(memb_func_size_);
corenrn.get_pnt_receive_size().resize(memb_func_size_);
corenrn.get_watch_check().resize(memb_func_size_);
corenrn.get_is_artificial().resize(memb_func_size_, false);
corenrn.get_artcell_qindex().resize(memb_func_size_);
corenrn.get_prop_param_size().resize(memb_func_size_);
corenrn.get_prop_dparam_size().resize(memb_func_size_);
corenrn.get_mech_data_layout().resize(memb_func_size_, 1);
corenrn.get_bbcore_read().resize(memb_func_size_);
corenrn.get_bbcore_write().resize(memb_func_size_);
}
void initnrn() {
secondorder = DEF_secondorder; /* >0 means crank-nicolson. 2 means currents
adjusted to t+dt/2 */
t = 0.; /* msec */
dt = DEF_dt; /* msec */
rev_dt = (int) (DEF_rev_dt); /* 1/msec */
celsius = DEF_celsius; /* degrees celsius */
}
/* if vectorized then thread_data_size added to it */
int register_mech(const char** m,
mod_alloc_t alloc,
mod_f_t cur,
mod_f_t jacob,
mod_f_t stat,
mod_f_t initialize,
int /* nrnpointerindex */,
int vectorized) {
auto& memb_func = corenrn.get_memb_funcs();
int type = nrn_get_mechtype(m[1]);
// No mechanism in the .dat files
if (type == -1)
return type;
assert(type);
#ifdef DEBUG
printf("register_mech %s %d\n", m[1], type);
#endif
if (memb_func[type].sym) {
assert(strcmp(memb_func[type].sym, m[1]) == 0);
} else {
memb_func[type].sym = (char*) emalloc(strlen(m[1]) + 1);
strcpy(memb_func[type].sym, m[1]);
}
memb_func[type].current = cur;
memb_func[type].jacob = jacob;
memb_func[type].alloc = alloc;
memb_func[type].state = stat;
memb_func[type].initialize = initialize;
memb_func[type].constructor = nullptr;
memb_func[type].destructor = nullptr;
#if VECTORIZE
memb_func[type].vectorized = vectorized ? 1 : 0;
memb_func[type].thread_size_ = vectorized ? (vectorized - 1) : 0;
memb_func[type].thread_mem_init_ = nullptr;
memb_func[type].thread_cleanup_ = nullptr;
memb_func[type].thread_table_check_ = nullptr;
memb_func[type].is_point = 0;
memb_func[type].setdata_ = nullptr;
memb_func[type].dparam_semantics = nullptr;
#endif
register_all_variables_offsets(type, &m[2]);
return type;
}
void nrn_writes_conc(int type, int /* unused */) {
static int lastion = EXTRACELL + 1;
if (type == -1)
return;
#if CORENRN_DEBUG
printf("%s reordered from %d to %d\n", corenrn.get_memb_func(type).sym, type, lastion);
#endif
if (nrn_is_ion(type)) {
++lastion;
}
}
void _nrn_layout_reg(int type, int layout) {
corenrn.get_mech_data_layout()[type] = layout;
}
void hoc_register_net_receive_buffering(NetBufReceive_t f, int type) {
corenrn.get_net_buf_receive().emplace_back(f, type);
}
void hoc_register_net_send_buffering(int type) {
corenrn.get_net_buf_send_type().push_back(type);
}
void hoc_register_watch_check(nrn_watch_check_t nwc, int type) {
corenrn.get_watch_check()[type] = nwc;
}
void hoc_register_prop_size(int type, int psize, int dpsize) {
if (type == -1)
return;
int pold = corenrn.get_prop_param_size()[type];
int dpold = corenrn.get_prop_dparam_size()[type];
if (psize != pold || dpsize != dpold) {
corenrn.get_different_mechanism_type().push_back(type);
}
corenrn.get_prop_param_size()[type] = psize;
corenrn.get_prop_dparam_size()[type] = dpsize;
if (dpsize) {
corenrn.get_memb_func(type).dparam_semantics = (int*) ecalloc(dpsize, sizeof(int));
}
}
void hoc_register_dparam_semantics(int type, int ix, const char* name) {
/* needed for SoA to possibly reorder name_ion and some "pointer" pointers. */
/* only interested in area, iontype, cvode_ieq,
netsend, pointer, pntproc, bbcorepointer, watch, diam, fornetcon
xx_ion and #xx_ion which will get
a semantics value of -1, -2, -3,
-4, -5, -6, -7, -8, -9, -10,
type, and type+1000 respectively
*/
auto& memb_func = corenrn.get_memb_funcs();
if (strcmp(name, "area") == 0) {
memb_func[type].dparam_semantics[ix] = -1;
} else if (strcmp(name, "iontype") == 0) {
memb_func[type].dparam_semantics[ix] = -2;
} else if (strcmp(name, "cvodeieq") == 0) {
memb_func[type].dparam_semantics[ix] = -3;
} else if (strcmp(name, "netsend") == 0) {
memb_func[type].dparam_semantics[ix] = -4;
} else if (strcmp(name, "pointer") == 0) {
memb_func[type].dparam_semantics[ix] = -5;
} else if (strcmp(name, "pntproc") == 0) {
memb_func[type].dparam_semantics[ix] = -6;
} else if (strcmp(name, "bbcorepointer") == 0) {
memb_func[type].dparam_semantics[ix] = -7;
} else if (strcmp(name, "watch") == 0) {
memb_func[type].dparam_semantics[ix] = -8;
} else if (strcmp(name, "diam") == 0) {
memb_func[type].dparam_semantics[ix] = -9;
} else if (strcmp(name, "fornetcon") == 0) {
memb_func[type].dparam_semantics[ix] = -10;
} else {
int i = name[0] == '#' ? 1 : 0;
int etype = nrn_get_mechtype(name + i);
memb_func[type].dparam_semantics[ix] = etype + i * 1000;
/* note that if style is needed (i==1), then we are writing a concentration */
if (i) {
ion_write_depend(type, etype);
}
}
#if CORENRN_DEBUG
printf("dparam semantics %s ix=%d %s %d\n",
memb_func[type].sym,
ix,
name,
memb_func[type].dparam_semantics[ix]);
#endif
}
/* only ion type ion_write_depend_ are non-nullptr */
/* and those are array of integers with first integer being array size */
/* and remaining size-1 integers containing the mechanism types that write concentrations to that
* ion */
static void ion_write_depend(int type, int etype) {
auto& memb_func = corenrn.get_memb_funcs();
auto& ion_write_depend_ = corenrn.get_ion_write_dependency();
if (ion_write_depend_.size() < memb_func.size()) {
ion_write_depend_.resize(memb_func.size());
}
int size = !ion_write_depend_[etype].empty() ? ion_write_depend_[etype][0] + 1 : 2;
ion_write_depend_[etype].resize(size, 0);
ion_write_depend_[etype][0] = size;
ion_write_depend_[etype][size - 1] = type;
}
static int depend_append(int idep, int* dependencies, int deptype, int type) {
/* append only if not already in dependencies and != type*/
bool add = true;
if (deptype == type) {
return idep;
}
for (int i = 0; i < idep; ++i) {
if (deptype == dependencies[i]) {
add = false;
break;
}
}
if (add) {
dependencies[idep++] = deptype;
}
return idep;
}
/* return list of types that this type depends on (10 should be more than enough) */
/* dependencies must be an array that is large enough to hold that array */
/* number of dependencies is returned */
int nrn_mech_depend(int type, int* dependencies) {
int dpsize = corenrn.get_prop_dparam_size()[type];
int* ds = corenrn.get_memb_func(type).dparam_semantics;
int idep = 0;
if (ds)
for (int i = 0; i < dpsize; ++i) {
if (ds[i] > 0 && ds[i] < 1000) {
int deptype = ds[i];
int idepnew = depend_append(idep, dependencies, deptype, type);
if ((idepnew > idep) && !corenrn.get_ion_write_dependency().empty() &&
!corenrn.get_ion_write_dependency()[deptype].empty()) {
auto& iwd = corenrn.get_ion_write_dependency()[deptype];
int size = iwd[0];
for (int j = 1; j < size; ++j) {
idepnew = depend_append(idepnew, dependencies, iwd[j], type);
}
}
idep = idepnew;
}
}
return idep;
}
void register_constructor(mod_f_t c) {
corenrn.get_memb_funcs().back().constructor = c;
}
void register_destructor(mod_f_t d) {
corenrn.get_memb_funcs().back().destructor = d;
}
int point_reg_helper(const Symbol* s2) {
static int next_pointtype = 1; /* starts at 1 since 0 means not point in pnt_map */
int type = nrn_get_mechtype(s2);
// No mechanism in the .dat files
if (type == -1)
return type;
corenrn.get_pnt_map()[type] = next_pointtype++;
corenrn.get_memb_func(type).is_point = 1;
return corenrn.get_pnt_map()[type];
}
int point_register_mech(const char** m,
mod_alloc_t alloc,
mod_f_t cur,
mod_f_t jacob,
mod_f_t stat,
mod_f_t initialize,
int nrnpointerindex,
mod_f_t constructor,
mod_f_t destructor,
int vectorized) {
const Symbol* s = m[1];
register_mech(m, alloc, cur, jacob, stat, initialize, nrnpointerindex, vectorized);
register_constructor(constructor);
register_destructor(destructor);
return point_reg_helper(s);
}
void _modl_cleanup() {}
int state_discon_allowed_;
int state_discon_flag_ = 0;
void state_discontinuity(int /* i */, double* pd, double d) {
if (state_discon_allowed_ && state_discon_flag_ == 0) {
*pd = d;
/*printf("state_discontinuity t=%g pd=%lx d=%g\n", t, (long)pd, d);*/
}
}
void hoc_reg_ba(int mt, mod_f_t f, int type) {
if (type == -1)
return;
switch (type) { /* see bablk in src/nmodl/nocpout.c */
case 11:
type = BEFORE_BREAKPOINT;
break;
case 22:
type = AFTER_SOLVE;
break;
case 13:
type = BEFORE_INITIAL;
break;
case 23:
type = AFTER_INITIAL;
break;
case 14:
type = BEFORE_STEP;
break;
default:
printf("before-after processing type %d for %s not implemented\n",
type,
corenrn.get_memb_func(mt).sym);
nrn_exit(1);
}
auto bam = (BAMech*) emalloc(sizeof(BAMech));
bam->f = f;
bam->type = mt;
bam->next = corenrn.get_bamech()[type];
corenrn.get_bamech()[type] = bam;
}
void _nrn_thread_reg0(int i, void (*f)(ThreadDatum*)) {
if (i == -1)
return;
corenrn.get_memb_func(i).thread_cleanup_ = f;
}
void _nrn_thread_reg1(int i, void (*f)(ThreadDatum*)) {
if (i == -1)
return;
corenrn.get_memb_func(i).thread_mem_init_ = f;
}
void _nrn_thread_table_reg(int i,
void (*f)(int, int, double*, Datum*, ThreadDatum*, NrnThread*, int)) {
if (i == -1)
return;
corenrn.get_memb_func(i).thread_table_check_ = f;
}
void _nrn_setdata_reg(int i, void (*call)(double*, Datum*)) {
if (i == -1)
return;
corenrn.get_memb_func(i).setdata_ = call;
}
Memb_func::~Memb_func() {
if (sym != nullptr) {
free(sym);
}
if (dparam_semantics != nullptr) {
free(dparam_semantics);
}
}
} // namespace coreneuron