CoreNEURON
multicore.hpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================.
7 */
8 
9 #pragma once
10 
11 #include "coreneuron/nrnconf.h"
14 #include "coreneuron/mpi/nrnmpi.h"
17 #include <vector>
18 #include <memory>
19 
20 namespace coreneuron {
21 class NetCon;
22 class PreSyn;
23 
24 extern bool use_solve_interleave;
25 
26 /*
27  Point_process._presyn, used only if its NET_RECEIVE sends a net_event, is
28  eliminated. Needed only by net_event function. Replaced by
29  PreSyn* = nt->presyns + nt->pnt2presyn_ix[pnttype2presyn[pnt->_type]][pnt->_i_instance];
30 */
31 
32 struct NrnThreadMembList { /* patterned after CvMembList in cvodeobj.h */
35  int index;
36  int* dependencies; /* list of mechanism types that this mechanism depends on*/
37  int ndependencies; /* for scheduling we need to know the dependency count */
38 };
40  int mech_id,
41  Memb_func& memb_func,
42  int& shadow_rhs_cnt,
43  const std::vector<int>& mech_types,
44  const std::vector<int>& nodecounts);
45 
47  Memb_list* ml; /* an item in the NrnThreadMembList */
50 };
51 
52 struct NrnFastImem {
53  double* nrn_sav_rhs;
54  double* nrn_sav_d;
55 };
56 
58  void** vpr; /* PlayRecord Objects known by NEURON */
59  double** scatter; /* if bsize == 0, each time step */
60  double** varrays; /* if bsize > 0, the Vector data pointers. */
61  double** gather; /* pointers to values that get scattered to NEURON */
62  int n_pr; /* number of PlayRecord instances */
63  int n_trajec; /* number of trajectories requested */
64  int bsize; /* buffer size of the Vector data */
65  int vsize; /* number of elements in varrays so far */
66 };
67 
68 /* for OpenACC, in order to avoid an error while update PreSyn, with virtual base
69  * class, we are adding helper with flag variable which could be updated on GPU
70  */
71 struct PreSynHelper {
72  int flag_;
73 };
74 
75 struct NrnThread: public MemoryManaged {
76  double _t = 0;
77  double _dt = -1e9;
78  double cj = 0.0;
79 
80  NrnThreadMembList* tml = nullptr;
81  Memb_list** _ml_list = nullptr;
82  Point_process* pntprocs = nullptr; // synapses and artificial cells with and without gid
83  PreSyn* presyns = nullptr; // all the output PreSyn with and without gid
85  int** pnt2presyn_ix = nullptr; // eliminates Point_process._presyn used only by net_event
86  // sender.
87  NetCon* netcons = nullptr;
88  double* weights = nullptr; // size n_weight. NetCon.weight_ points into this array.
89 
90  int n_pntproc = 0;
91  int n_weight = 0;
92  int n_netcon = 0;
93  int n_input_presyn = 0;
94  int n_presyn = 0; // only for model_size
95  int n_real_output = 0; // for checking their thresholds.
96 
97  int ncell = 0; /* analogous to old rootnodecount */
98  int end = 0; /* 1 + position of last in v_node array. Now v_node_count. */
99  int id = 0; /* this is nrn_threads[id] */
100  int _stop_stepping = 0;
101  int n_vecplay = 0; /* number of instances of VecPlayContinuous */
102 
103  size_t _ndata = 0;
104  size_t _nvdata = 0;
105  size_t _nidata = 0; /* sizes */
106  double* _data = nullptr; /* all the other double* and Datum to doubles point into here*/
107  int* _idata = nullptr; /* all the Datum to ints index into here */
108  void** _vdata = nullptr; /* all the Datum to pointers index into here */
109  void** _vecplay = nullptr; /* array of instances of VecPlayContinuous */
110 
111  double* _actual_rhs = nullptr;
112  double* _actual_d = nullptr;
113  double* _actual_a = nullptr;
114  double* _actual_b = nullptr;
115  double* _actual_v = nullptr;
116  double* _actual_area = nullptr;
117  double* _actual_diam = nullptr; /* nullptr if no mechanism has dparam with diam semantics */
118  double* _shadow_rhs = nullptr; /* Not pointer into _data. Avoid race for multiple POINT_PROCESS
119  in same compartment */
120  double* _shadow_d = nullptr; /* Not pointer into _data. Avoid race for multiple POINT_PROCESS in
121  same compartment */
122 
123  /* Fast membrane current calculation struct */
125 
126  int* _v_parent_index = nullptr;
127  int* _permute = nullptr;
128  char* _sp13mat = nullptr; /* handle to general sparse matrix */
129  Memb_list* _ecell_memb_list = nullptr; /* normally nullptr */
130 
131  double _ctime = 0.0; /* computation time in seconds (using nrnmpi_wtime) */
132 
133  NrnThreadBAList* tbl[BEFORE_AFTER_SIZE]; /* wasteful since almost all empty */
134 
135  int shadow_rhs_cnt = 0; /* added to facilitate the NrnThread transfer to GPU */
136  int compute_gpu = 0; /* define whether to compute with gpus */
137  int stream_id = 0; /* define where the kernel will be launched on GPU stream */
140  int* _net_send_buffer = nullptr;
141 
142  int* _watch_types = nullptr; /* nullptr or 0 terminated array of integers */
143  void* mapping = nullptr; /* section to segment mapping information */
144  std::unique_ptr<SummationReportMapping> summation_report_handler_; /* report to ALU (values of
145  the current summation */
146  TrajectoryRequests* trajec_requests = nullptr; /* per time step values returned to NEURON */
147 
148  /* Needed in case there are FOR_NETCON statements in use. */
149  std::size_t _fornetcon_perm_indices_size{}; /* length of _fornetcon_perm_indices */
150  size_t* _fornetcon_perm_indices{}; /* displacement like list of indices */
151  std::size_t _fornetcon_weight_perm_size{}; /* length of _fornetcon_weight_perm */
152  size_t* _fornetcon_weight_perm{}; /* permutation indices into weight */
153 
154  std::vector<int> _pnt_offset; /* for SelfEvent queue transfer */
155 };
156 
157 extern void nrn_threads_create(int n);
158 extern int nrn_nthread;
159 extern NrnThread* nrn_threads;
160 template <typename F, typename... Args>
161 void nrn_multithread_job(F&& job, Args&&... args) {
162  int i;
163  // clang-format off
164 
165  #pragma omp parallel for private(i) shared(nrn_threads, job, nrn_nthread, \
166  nrnmpi_myid) schedule(static, 1)
167  // FIXME: multiple forwarding of the same arguments...
168  for (i = 0; i < nrn_nthread; ++i) {
169  job(nrn_threads + i, std::forward<Args>(args)...);
170  }
171  // clang-format on
172 }
173 
174 extern void nrn_thread_table_check(void);
175 
176 extern void nrn_threads_free(void);
177 
178 extern bool _nrn_skip_initmodel;
179 
180 
181 extern void dt2thread(double);
182 extern void clear_event_queue(void);
183 extern void nrn_ba(NrnThread*, int);
184 extern void* nrn_fixed_step_lastpart(NrnThread*);
185 extern void nrn_solve_minimal(NrnThread*);
186 extern void nrncore2nrn_send_init();
187 extern void* setup_tree_matrix_minimal(NrnThread*);
188 extern void nrncore2nrn_send_values(NrnThread*);
189 extern void nrn_fixed_step_group_minimal(int total_sim_steps);
190 extern void nrn_fixed_single_steps_minimal(int total_sim_steps, double tstop);
191 extern void nrn_fixed_step_minimal(void);
192 extern void nrn_finitialize(int setv, double v);
193 extern void direct_mode_initialize();
194 extern void nrn_mk_table_check(void);
195 extern void nonvint(NrnThread* _nt);
196 extern void update(NrnThread*);
197 
198 constexpr int at_time(NrnThread* nt, double te) {
199  double x = te - 1e-11;
200  if (x <= nt->_t && x > (nt->_t - nt->_dt)) {
201  return 1;
202  }
203  return 0;
204 }
205 } // namespace coreneuron
coreneuron::NrnThread::netcons
NetCon * netcons
Definition: multicore.hpp:87
coreneuron::NrnThread::n_real_output
int n_real_output
Definition: multicore.hpp:95
coreneuron::NrnThread::nrn_fast_imem
NrnFastImem * nrn_fast_imem
Definition: multicore.hpp:124
coreneuron::NrnThreadMembList::dependencies
int * dependencies
Definition: multicore.hpp:36
membfunc.hpp
coreneuron::NrnThread::_vdata
void ** _vdata
Definition: multicore.hpp:108
coreneuron::nrn_nthread
int nrn_nthread
Definition: multicore.cpp:55
coreneuron::nrncore2nrn_send_init
void nrncore2nrn_send_init()
Definition: fadvance_core.cpp:267
coreneuron::NrnThread::_shadow_d
double * _shadow_d
Definition: multicore.hpp:120
coreneuron::nrn_fixed_single_steps_minimal
void nrn_fixed_single_steps_minimal(int total_sim_steps, double tstop)
--> Coreneuron
Definition: fadvance_core.cpp:128
coreneuron::TrajectoryRequests::vsize
int vsize
Definition: multicore.hpp:65
coreneuron::NrnThread::_fornetcon_weight_perm
size_t * _fornetcon_weight_perm
Definition: multicore.hpp:152
coreneuron::NrnThread::_net_send_buffer_cnt
int _net_send_buffer_cnt
Definition: multicore.hpp:139
coreneuron::Point_process
Definition: mechanism.hpp:35
coreneuron::dt2thread
void dt2thread(double adt)
Definition: fadvance_core.cpp:70
coreneuron::NrnThread::_t
double _t
Definition: multicore.hpp:76
coreneuron::NrnFastImem::nrn_sav_d
double * nrn_sav_d
Definition: multicore.hpp:54
coreneuron::PreSynHelper
Definition: multicore.hpp:71
coreneuron::NrnThread::_net_send_buffer_size
int _net_send_buffer_size
Definition: multicore.hpp:138
coreneuron::nrn_ba
void nrn_ba(NrnThread *nt, int bat)
Definition: fadvance_core.cpp:258
MemoryManaged
for gpu builds with unified memory support
Definition: memory.h:172
coreneuron::NrnThread::presyns
PreSyn * presyns
Definition: multicore.hpp:83
coreneuron::TrajectoryRequests::n_trajec
int n_trajec
Definition: multicore.hpp:63
coreneuron::NrnThread::presyns_helper
PreSynHelper * presyns_helper
Definition: multicore.hpp:84
coreneuron::setup_tree_matrix_minimal
void * setup_tree_matrix_minimal(NrnThread *)
Definition: treeset_core.cpp:178
coreneuron::nrn_threads_free
void nrn_threads_free()
Definition: multicore.cpp:125
coreneuron::NrnThread::cj
double cj
Definition: multicore.hpp:78
coreneuron::NrnThread::_watch_types
int * _watch_types
Definition: multicore.hpp:142
coreneuron::NrnThreadMembList::ndependencies
int ndependencies
Definition: multicore.hpp:37
coreneuron::nrn_solve_minimal
void nrn_solve_minimal(NrnThread *)
Definition: solve_core.cpp:18
coreneuron::Memb_list
Definition: mechanism.hpp:131
coreneuron::nrn_threads_create
void nrn_threads_create(int n)
Definition: multicore.cpp:102
coreneuron::NrnThread::tml
NrnThreadMembList * tml
Definition: multicore.hpp:80
coreneuron::NrnThread::_fornetcon_weight_perm_size
std::size_t _fornetcon_weight_perm_size
Definition: multicore.hpp:151
coreneuron::NrnThreadMembList::index
int index
Definition: multicore.hpp:35
coreneuron::NrnThread::_actual_rhs
double * _actual_rhs
Definition: multicore.hpp:111
coreneuron::NrnThread::compute_gpu
int compute_gpu
Definition: multicore.hpp:136
coreneuron::direct_mode_initialize
void direct_mode_initialize()
All state from NEURON necessary to continue a run.
Definition: nrn2core_data_init.cpp:50
coreneuron::NrnThread::_pnt_offset
std::vector< int > _pnt_offset
Definition: multicore.hpp:154
coreneuron::NrnThreadBAList
Definition: multicore.hpp:46
coreneuron::nrn_mk_table_check
void nrn_mk_table_check()
Definition: multicore.cpp:133
coreneuron::NrnThread::_actual_a
double * _actual_a
Definition: multicore.hpp:113
coreneuron::NrnThread::_actual_diam
double * _actual_diam
Definition: multicore.hpp:117
coreneuron::NrnThread::_nvdata
size_t _nvdata
Definition: multicore.hpp:104
coreneuron::PreSynHelper::flag_
int flag_
Definition: multicore.hpp:72
coreneuron::NrnThread::_sp13mat
char * _sp13mat
Definition: multicore.hpp:128
coreneuron
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
Definition: corenrn_parameters.cpp:12
coreneuron::NrnThread::trajec_requests
TrajectoryRequests * trajec_requests
Definition: multicore.hpp:146
coreneuron::nrn_fixed_step_minimal
void nrn_fixed_step_minimal()
Definition: fadvance_core.cpp:91
coreneuron::NrnThread::n_weight
int n_weight
Definition: multicore.hpp:91
coreneuron::NrnThread::n_pntproc
int n_pntproc
Definition: multicore.hpp:90
coreneuron::NrnThread::tbl
NrnThreadBAList * tbl[BEFORE_AFTER_SIZE]
Definition: multicore.hpp:133
coreneuron::NrnThread::_ecell_memb_list
Memb_list * _ecell_memb_list
Definition: multicore.hpp:129
coreneuron::NrnThread::_v_parent_index
int * _v_parent_index
Definition: multicore.hpp:126
coreneuron::i
int i
Definition: cellorder.cpp:485
coreneuron::NrnThread::_dt
double _dt
Definition: multicore.hpp:77
coreneuron::NrnThread::_ml_list
Memb_list ** _ml_list
Definition: multicore.hpp:81
coreneuron::update
void update(NrnThread *_nt)
Definition: fadvance_core.cpp:201
coreneuron::PreSyn
Definition: netcon.hpp:104
coreneuron::NrnThread::_idata
int * _idata
Definition: multicore.hpp:107
coreneuron::nrn_multithread_job
void nrn_multithread_job(F &&job, Args &&... args)
Definition: multicore.hpp:161
coreneuron::at_time
constexpr int at_time(NrnThread *nt, double te)
Definition: multicore.hpp:198
coreneuron::NrnThread::shadow_rhs_cnt
int shadow_rhs_cnt
Definition: multicore.hpp:135
coreneuron::_nrn_skip_initmodel
bool _nrn_skip_initmodel
Definition: finitialize.cpp:19
coreneuron::TrajectoryRequests::scatter
double ** scatter
Definition: multicore.hpp:59
coreneuron::NetCon
Definition: netcon.hpp:47
coreneuron::NrnThread::_stop_stepping
int _stop_stepping
Definition: multicore.hpp:100
coreneuron::NrnThread::_net_send_buffer
int * _net_send_buffer
Definition: multicore.hpp:140
nrnmpi.hpp
coreneuron::NrnThread::n_presyn
int n_presyn
Definition: multicore.hpp:94
coreneuron::NrnThread::_ndata
size_t _ndata
Definition: multicore.hpp:103
coreneuron::NrnThread
Definition: multicore.hpp:75
coreneuron::NrnThreadMembList
Definition: multicore.hpp:32
coreneuron::Memb_func
Definition: membfunc.hpp:32
coreneuron::TrajectoryRequests::varrays
double ** varrays
Definition: multicore.hpp:60
coreneuron::NrnThreadBAList::next
NrnThreadBAList * next
Definition: multicore.hpp:49
coreneuron::NrnThread::_fornetcon_perm_indices_size
std::size_t _fornetcon_perm_indices_size
Definition: multicore.hpp:149
coreneuron::create_tml
NrnThreadMembList * create_tml(NrnThread &nt, int mech_id, Memb_func &memb_func, int &shadow_rhs_cnt, const std::vector< int > &mech_types, const std::vector< int > &nodecounts)
Definition: multicore.cpp:64
coreneuron::NrnThread::_shadow_rhs
double * _shadow_rhs
Definition: multicore.hpp:118
coreneuron::nrncore2nrn_send_values
void nrncore2nrn_send_values(NrnThread *nth)
Definition: fadvance_core.cpp:284
coreneuron::NrnThread::stream_id
int stream_id
Definition: multicore.hpp:137
coreneuron::TrajectoryRequests::vpr
void ** vpr
Definition: multicore.hpp:58
coreneuron::NrnThread::_data
double * _data
Definition: multicore.hpp:106
coreneuron::nrn_threads
NrnThread * nrn_threads
Definition: multicore.cpp:56
coreneuron::TrajectoryRequests
Definition: multicore.hpp:57
coreneuron::NrnThreadBAList::ml
Memb_list * ml
Definition: multicore.hpp:47
coreneuron::NrnThread::pnt2presyn_ix
int ** pnt2presyn_ix
Definition: multicore.hpp:85
coreneuron::NrnThread::_actual_d
double * _actual_d
Definition: multicore.hpp:112
coreneuron::NrnThread::_nidata
size_t _nidata
Definition: multicore.hpp:105
nrnconf.h
coreneuron::NrnThread::_fornetcon_perm_indices
size_t * _fornetcon_perm_indices
Definition: multicore.hpp:150
coreneuron::NrnThread::_vecplay
void ** _vecplay
Definition: multicore.hpp:109
coreneuron::nrn_fixed_step_group_minimal
void nrn_fixed_step_group_minimal(int total_sim_steps)
Definition: fadvance_core.cpp:148
nrnreport.hpp
coreneuron::NrnThreadMembList::next
NrnThreadMembList * next
Definition: multicore.hpp:33
coreneuron::TrajectoryRequests::n_pr
int n_pr
Definition: multicore.hpp:62
coreneuron::TrajectoryRequests::bsize
int bsize
Definition: multicore.hpp:64
coreneuron::NrnThread::weights
double * weights
Definition: multicore.hpp:88
coreneuron::NrnThread::_ctime
double _ctime
Definition: multicore.hpp:131
BEFORE_AFTER_SIZE
#define BEFORE_AFTER_SIZE
Definition: membfunc.hpp:72
coreneuron::BAMech
Definition: membfunc.hpp:73
coreneuron::NrnFastImem::nrn_sav_rhs
double * nrn_sav_rhs
Definition: multicore.hpp:53
coreneuron::use_solve_interleave
bool use_solve_interleave
Definition: solve_core.cpp:13
coreneuron::NrnThread::pntprocs
Point_process * pntprocs
Definition: multicore.hpp:82
coreneuron::NrnThread::_actual_v
double * _actual_v
Definition: multicore.hpp:115
v
#define v
Definition: md1redef.h:11
coreneuron::NrnThread::mapping
void * mapping
Definition: multicore.hpp:143
coreneuron::NrnFastImem
Definition: multicore.hpp:52
coreneuron::nonvint
void nonvint(NrnThread *_nt)
Definition: fadvance_core.cpp:232
coreneuron::nrn_fixed_step_lastpart
void * nrn_fixed_step_lastpart(NrnThread *nth)
Definition: fadvance_core.cpp:376
coreneuron::NrnThread::n_netcon
int n_netcon
Definition: multicore.hpp:92
coreneuron::NrnThread::summation_report_handler_
std::unique_ptr< SummationReportMapping > summation_report_handler_
Definition: multicore.hpp:144
coreneuron::NrnThread::n_input_presyn
int n_input_presyn
Definition: multicore.hpp:93
coreneuron::NrnThread::_actual_b
double * _actual_b
Definition: multicore.hpp:114
coreneuron::NrnThreadBAList::bam
BAMech * bam
Definition: multicore.hpp:48
coreneuron::TrajectoryRequests::gather
double ** gather
Definition: multicore.hpp:61
coreneuron::NrnThread::end
int end
Definition: multicore.hpp:98
coreneuron::NrnThread::_actual_area
double * _actual_area
Definition: multicore.hpp:116
coreneuron::nrn_thread_table_check
void nrn_thread_table_check()
Definition: multicore.cpp:168
coreneuron::NrnThreadMembList::ml
Memb_list * ml
Definition: multicore.hpp:34
memory.h
nrnmpi.h
coreneuron::NrnThread::ncell
int ncell
Definition: multicore.hpp:97
coreneuron::NrnThread::_permute
int * _permute
Definition: multicore.hpp:127
coreneuron::nrn_finitialize
void nrn_finitialize(int setv, double v)
Definition: finitialize.cpp:40
coreneuron::clear_event_queue
void clear_event_queue()
Definition: cvodestb.cpp:47
coreneuron::NrnThread::n_vecplay
int n_vecplay
Definition: multicore.hpp:101