NEURON
phase1.cpp
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 #include <cassert>
10 #include <mutex>
11 
14 #include "coreneuron/io/phase1.hpp"
16 
17 int (*nrn2core_get_dat1_)(int tid,
18  int& n_presyn,
19  int& n_netcon,
20  std::vector<int>& output_gid,
21  int*& netcon_srcgid,
22  std::vector<int>& netcon_negsrcgid_tid);
23 
24 namespace coreneuron {
26  assert(!F.fail());
27  int n_presyn = F.read_int(); /// Number of PreSyn-s in NrnThread nt
28  int n_netcon = F.read_int(); /// Number of NetCon-s in NrnThread nt
29 
30  this->output_gids = F.read_vector<int>(n_presyn);
31  this->netcon_srcgids = F.read_vector<int>(n_netcon);
32  // For file mode transfer, it is not allowed that negative gids exist
33  // in different threads. So this->netcon_tids remains clear.
34 
35  F.close();
36 }
37 
38 Phase1::Phase1(int thread_id) {
39  int* netcon_srcgid;
40  int n_presyn;
41  int n_netcon;
42 
43  // TODO : check error codes for NEURON - CoreNEURON communication
44  int valid = (*nrn2core_get_dat1_)(
45  thread_id, n_presyn, n_netcon, output_gids, netcon_srcgid, this->netcon_negsrcgid_tid);
46  if (!valid) {
47  return;
48  }
49  assert(output_gids.size() == n_presyn);
50  this->netcon_srcgids = std::vector<int>(netcon_srcgid, netcon_srcgid + n_netcon);
51  delete[] netcon_srcgid;
52 }
53 
55  nt.n_presyn = this->output_gids.size();
56  nt.n_netcon = this->netcon_srcgids.size();
57 
58  nrnthreads_netcon_srcgid[nt.id] = new int[nt.n_netcon];
59  std::copy(this->netcon_srcgids.begin(),
60  this->netcon_srcgids.end(),
62 
63  // netcon_negsrcgid_tid is empty if file transfer or single thread
65 
66  nt.netcons = new NetCon[nt.n_netcon];
67 
68  if (nt.n_presyn) {
70  nt.presyns = new PreSyn[nt.n_presyn];
71  }
72 
73  PreSyn* ps = nt.presyns;
74  /// go through all presyns
75  for (auto& gid: this->output_gids) {
76  if (gid == -1) {
77  ++ps;
78  continue;
79  }
80 
81  {
82  const std::lock_guard<OMP_Mutex> lock(mut);
83  // Note that the negative (type, index)
84  // coded information goes into the neg_gid2out[tid] hash table.
85  // See netpar.cpp for the netpar_tid_... function implementations.
86  // Both that table and the process wide gid2out table can be deleted
87  // before the end of setup
88 
89  /// Put gid into the gid2out hash table with correspondent output PreSyn
90  /// Or to the negative PreSyn map
91  if (gid >= 0) {
92  if (gid2in.find(gid) != gid2in.end()) {
93  auto const m = "gid=" + std::to_string(gid) +
94  " already exists as an input port";
95  hoc_execerror(m.c_str(),
96  "Setup all the output ports on this process before using them as "
97  "input ports.");
98  }
99  if (gid2out.find(gid) != gid2out.end()) {
100  auto const m = "gid=" + std::to_string(gid) +
101  " already exists on this process as an output port";
102  hoc_execerror(m.c_str(), nullptr);
103  }
104  ps->gid_ = gid;
105  ps->output_index_ = gid;
106  gid2out[gid] = ps;
107  } else {
108  nrn_assert(neg_gid2out[nt.id].find(gid) == neg_gid2out[nt.id].end());
109  ps->output_index_ = -1;
110  neg_gid2out[nt.id][gid] = ps;
111  }
112  } // end of the mutex
113 
114  ++ps;
115  }
116 }
117 
118 } // namespace coreneuron
std::vector< T > read_vector(size_t count)
bool fail() const
Is the file not open.
void close()
Close currently open file.
int read_int()
Parse a single integer entry.
std::vector< int > output_gids
Definition: phase1.hpp:27
std::vector< int > netcon_srcgids
Definition: phase1.hpp:28
void populate(NrnThread &nt, OMP_Mutex &mut)
Definition: phase1.cpp:54
std::vector< int > netcon_negsrcgid_tid
Definition: phase1.hpp:29
Phase1(FileHandler &F)
Definition: phase1.cpp:25
#define assert(ex)
Definition: hocassrt.h:24
static double valid(void *v)
Definition: linmod1.cpp:46
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
static OMP_Mutex mut
Definition: nrn_setup.cpp:154
void * ecalloc_align(size_t n, size_t size, size_t alignment)
std::map< int, InputPreSyn * > gid2in
Definition: nrn_setup.cpp:160
std::vector< std::map< int, PreSyn * > > neg_gid2out
Vector of maps for negative presyns.
Definition: nrn_setup.cpp:157
std::vector< std::vector< int > > nrnthreads_netcon_negsrcgid_tid
If a nrnthreads_netcon_srcgid is negative, need to determine the thread when in order to use the corr...
Definition: nrn_setup.cpp:170
std::vector< int * > nrnthreads_netcon_srcgid
Only for setup vector of netcon source gids.
Definition: nrn_setup.cpp:166
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
std::map< int, PreSyn * > gid2out
Maps for ouput and input presyns.
Definition: nrn_setup.cpp:159
std::string to_string(const T &obj)
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
Definition: nrn_assert.h:33
#define lock
int(* nrn2core_get_dat1_)(int tid, int &n_presyn, int &n_netcon, std::vector< int > &output_gid, int *&netcon_srcgid, std::vector< int > &netcon_negsrcgid_tid)
Definition: phase1.cpp:17
PreSynHelper * presyns_helper
Definition: multicore.hpp:84