NEURON
bbssrvmpi.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <nrnmpi.h>
3 #ifdef NRNMPI // to end of file
4 #include <stdio.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include "bbssrv2mpi.h"
9 #include "bbssrv.h"
10 
11 #define debug 0
12 
13 #define POLLCNT 300
14 extern int bbs_poll_;
15 void bbs_handle();
16 extern double hoc_cross_x_;
17 
18 static int bbs_poll_cnt_;
19 static int bbs_msg_cnt_;
20 
21 void bbs_handle() {
23  bbs_poll_ = POLLCNT;
24  } else {
25  bbs_poll_ = -1;
26  return;
27  }
28  ++bbs_poll_cnt_;
30 }
31 
33  if (nrnmpi_numprocs_bbs > 1) {
34  bbs_poll_ = POLLCNT;
35  }
36 }
37 void BBSDirectServer::done() {
38  return;
39  printf("bbs_msg_cnt_=%d bbs_poll_cnt_=%d bbs_poll_=%d\n",
40  bbs_msg_cnt_,
41  bbs_poll_cnt_,
42  ((bbs_poll_ < 0) ? -bbs_poll_ : bbs_poll_));
43 }
44 
46  int size;
47  int tag;
48  int source;
49  nrnmpi_probe(&size, &tag, &source);
50  handle1(size, tag, source);
51 }
52 
54  int size;
55  int tag;
56  int source;
57  if (nrnmpi_iprobe(&size, &tag, &source) != 0) {
58  do {
59  handle1(size, tag, source);
60  } while (nrnmpi_iprobe(&size, &tag, &source) != 0);
61  }
62 }
63 
64 void BBSDirectServer::handle1(int size, int tag, int cid) {
65  bbsmpibuf* recv;
66  bbsmpibuf* send;
67  char* key;
68  int index;
69  send = nullptr;
70  recv = nrnmpi_newbuf(size);
71  nrnmpi_ref(recv);
72  tag = nrnmpi_bbsrecv(cid, recv);
73  ++bbs_msg_cnt_;
74  if (size > 0) {
75  nrnmpi_upkbegin(recv);
76  }
77  switch (tag) {
78  case POST_TODO:
79  index = nrnmpi_getid(recv); // the parent index
80 #if debug
81  printf("handle POST_TODO from %x when cross=%g\n", cid, hoc_cross_x_);
82 #endif
84  break;
85  case POST_RESULT:
86  index = nrnmpi_getid(recv);
87 #if debug
88  printf("handle POST_RESULT %d from %x when cross=%g\n", index, cid, hoc_cross_x_);
89 #endif
91  break;
92  case POST:
93  key = nrnmpi_getkey(recv);
94 #if debug
95  printf("handle POST %s from %x when cross=%g\n", key, cid, hoc_cross_x_);
96 #endif
98  break;
99  case LOOK:
100  key = nrnmpi_getkey(recv);
101 #if debug
102  printf("handle LOOK %s from %x when cross=%g\n", key, cid, hoc_cross_x_);
103 #endif
104  if (BBSDirectServer::server_->look(key, &send)) {
105  nrnmpi_bbssend(cid, LOOK_YES, send);
106  nrnmpi_unref(send);
107  } else {
108  nrnmpi_bbssend(cid, LOOK_NO, nullptr);
109  }
110  break;
111  case LOOK_TAKE:
112  key = nrnmpi_getkey(recv);
113 #if debug
114  printf("handle LOOK_TAKE %s from %x when cross=%g\n", key, cid, hoc_cross_x_);
115 #endif
116  if (BBSDirectServer::server_->look_take(key, &send)) {
117 #if debug
118  printf("handle sending back something\n");
119 #endif
120  nrnmpi_bbssend(cid, LOOK_TAKE_YES, send);
121  nrnmpi_unref(send);
122  } else {
123  nrnmpi_bbssend(cid, LOOK_TAKE_NO, nullptr);
124  }
125  break;
126  case TAKE:
127  key = nrnmpi_getkey(recv);
128 #if debug
129  printf("handle TAKE %s from %x when cross=%g\n", key, cid, hoc_cross_x_);
130 #endif
131  if (BBSDirectServer::server_->look_take(key, &send)) {
132 #if debug
133  printf("handle sending back something\n");
134 #endif
135  nrnmpi_bbssend(cid, TAKE, send);
136  nrnmpi_unref(send);
137  } else {
138 #if debug
139  printf("handle put_pending %s for %d\n", key, cid);
140 #endif
142  }
143  break;
144  case LOOK_TAKE_TODO:
145 #if debug
146  printf("handle LOOK_TAKE_TODO for cid=%x\n", cid);
147 #endif
149 #if debug
150  printf("handle sending back id=%d\n", index);
151 #endif
152  nrnmpi_bbssend(cid, index + 1, send);
153  if (index) {
154  nrnmpi_unref(send);
155  }
156  break;
157  case LOOK_TAKE_RESULT:
158  index = nrnmpi_getid(recv);
159 #if debug
160  printf("handle LOOK_TAKE_RESULT for %x pid=%d\n", cid, index);
161 #endif
163 #if debug
164  printf("handle sending back id=%d\n", index);
165 #endif
166  nrnmpi_bbssend(cid, index + 1, send);
167  if (index) {
168  nrnmpi_unref(send);
169  }
170  break;
171  case TAKE_TODO:
172 #if debug
173  printf("handle TAKE_TODO for %x\n", cid);
174 #endif
176 #if debug
177  printf("handle sent back a context\n");
178 #endif
179  break;
180  }
182  if (index) {
183 #if debug
184  printf("handle sending back id=%d\n", index);
185 #endif
186  nrnmpi_bbssend(cid, index + 1, send);
187  nrnmpi_unref(send);
188  } else {
189 #if debug
190  printf("handle add_looking_todo\n");
191 #endif
193  }
194  break;
195  case HELLO:
196 #if debug
197  printf("handle HELLO from %x when cross=%g\n", cid, hoc_cross_x_);
198 #endif
199  nrnmpi_pkbegin(recv);
200  nrnmpi_enddata(recv);
201  nrnmpi_bbssend(cid, HELLO, recv);
202  break;
203  default:
204  printf("unknown message\n");
205  break;
206  }
207  nrnmpi_unref(recv);
208 }
209 #else
210 void bbs_handle() {}
211 #endif // NRNMPI
#define POST_TODO
Definition: bbssrv.h:13
#define LOOK_TAKE_RESULT
Definition: bbssrv.h:16
#define LOOK_TAKE_NO
Definition: bbssrv.h:11
#define TAKE
Definition: bbssrv.h:7
#define TAKE_TODO
Definition: bbssrv.h:17
#define LOOK_TAKE
Definition: bbssrv.h:6
#define LOOK
Definition: bbssrv.h:5
#define LOOK_NO
Definition: bbssrv.h:9
#define POST_RESULT
Definition: bbssrv.h:14
#define POST
Definition: bbssrv.h:4
#define HELLO
Definition: bbssrv.h:12
#define LOOK_YES
Definition: bbssrv.h:8
#define LOOK_TAKE_TODO
Definition: bbssrv.h:15
#define LOOK_TAKE_YES
Definition: bbssrv.h:10
void bbs_handle()
Definition: bbssrvmpi.cpp:210
int look_take_result(int parentid)
void add_looking_todo(int cid)
bool look(const char *key)
bool look_take(const char *key)
static void handle()
void post_todo(int parentid, int cid)
void post(const char *key)
void put_pending(const char *key, int cid)
static void handle1()
static BBSDirectServer * server_
Definition: bbslsrv2.h:17
bool send_context(int cid)
int remaining_context_cnt_
Definition: bbslsrv2.h:36
void post_result(int id)
static void handle_block()
int bbs_poll_
Definition: code.cpp:32
#define key
Definition: tqueue.hpp:45
double hoc_cross_x_
Definition: hoc_init.cpp:224
printf
Definition: extdef.h:5
short index
Definition: cabvars.h:11
int nrnmpi_numprocs_bbs