Debian Codegraph @

testsuite/sanitytests/multiport.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009, 2019, 2022 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Leave as first include---required by BG/L
20 #include <mpi.h>
21
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <cstdlib>
26 #include <cmath>
27
28 extern "C" {
29 #include <unistd.h>
30 #include <getopt.h>
31 }
32
33 #include <music.hh>
34 #include <music/error.hh>
35
36 const double DEFAULT_TIMESTEP = 1e-2;
37 const double DEFAULT_FREQUENCY = 10.0; // Hz
38
39 void
40 usage (int rank)
41 {
42 if (rank == 0)
43 {
44 std::cerr << "Usage: multiport [OPTION...]" << std::endl
45 << "`eventgenerator' generates spikes from a Poisson distribution." << std::endl << std:: endl
46 << " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
47 << " -b, --maxbuffered TICKS maximal amount of data buffered" << std::endl
48 << " -f, --frequency FREQ average frequency (default " << DEFAULT_FREQUENCY << " Hz)" << std::endl
49 << " -m, --imaptype TYPE linear (default) or roundrobin" << std::endl
50 << " -i, --indextype TYPE global (default) or local" << std::endl
51 << " --out NAME:WIDTH name and width of an output port" << std::endl
52 << " --in NAME:WIDTH name and width of input port" << std::endl
53 << " -h, --help print this help message" << std::endl << std::endl
54 << "Report bugs to <[email protected]>." << std::endl;
55 }
56 exit (1);
57 }
58
59 double timestep = DEFAULT_TIMESTEP;
60 int maxbuffered = 0;
61 double freq = DEFAULT_FREQUENCY;
62 string imaptype = "linear";
63 string indextype = "global";
64
65 double
66 negexp (double m)
67 {
68 return - m * log (drand48 ());
69 }
70
71 class Port {
72 protected:
73 MUSIC::Setup* setup_;
74 std::string name_;
75 int width_;
76
77 public:
78 Port (MUSIC::Setup* setup, std::string name, int width)
79 : setup_ (setup), name_ (name), width_ (width) { }
80 };
81
82 class OutputPort : public Port {
83 MUSIC::EventOutputPort* port_;
84 MUSIC::Index::Type type;
85 std::vector<MUSIC::GlobalIndex> ids;
86 std::vector<double> nextSpike;
87 double m;
88
89 public:
90 OutputPort (MUSIC::Setup* setup, std::string name, int width)
91 : Port (setup, name, width) { }
92
93 void publish ()
94 {
95 port_ = setup_->publishEventOutput (name_);
96 if (!port_->isConnected ())
97 {
98 if (MUSIC::mpi_get_rank (setup_->communicator ()) == 0)
99 std::cerr << "multiport port is not connected" << std::endl;
100 MPI_Abort (setup_->communicator (), 1);
101 }
102 }
103
104 void map (int maxbuffered, std::string imaptype, std::string indextype)
105 {
106 MPI_Comm comm = setup_->communicator ();
107 int rank = MUSIC::mpi_get_rank (comm);
108 int nProcesses = MUSIC::mpi_get_comm_size (comm);
109
110 if (indextype == "global")
111 type = MUSIC::Index::GLOBAL;
112 else
113 type = MUSIC::Index::LOCAL;
114
115 if (imaptype == "linear")
116 {
117 int nUnitsPerProcess = width_ / nProcesses;
118 int nLocalUnits = nUnitsPerProcess;
119 int rest = width_ % nProcesses;
120 int firstId = nUnitsPerProcess * rank;
121 if (rank < rest)
122 {
123 firstId += rank;
124 nLocalUnits += 1;
125 }
126 else
127 firstId += rest;
128 for (int i = 0; i < nLocalUnits; ++i)
129 ids.push_back (firstId + i);
130 MUSIC::LinearIndex indices (firstId, nLocalUnits);
131
132 if (maxbuffered > 0)
133 port_->map (&indices, type, maxbuffered);
134 else
135 port_->map (&indices, type);
136 }
137 else
138 {
139 for (int i = rank; i < width_; i += nProcesses)
140 ids.push_back (i);
141 MUSIC::PermutationIndex indices (&ids.front (), ids.size ());
142 if (maxbuffered > 0)
143 port_->map (&indices, type, maxbuffered);
144 else
145 port_->map (&indices, type);
146 }
147 }
148
149 void init (double freq)
150 {
151 m = 1.0 / freq;
152 for (unsigned int i = 0; i < ids.size (); ++i)
153 nextSpike.push_back (negexp (m));
154 }
155
156 void tick (double nextTime)
157 {
158 if (type == MUSIC::Index::GLOBAL)
159 {
160 for (unsigned int i = 0; i < ids.size (); ++i)
161 while (nextSpike[i] < nextTime)
162 {
163 port_->insertEvent (nextSpike[i],
164 MUSIC::GlobalIndex (ids[i]));
165 nextSpike[i] += negexp (m);
166 }
167 }
168 else
169 {
170 for (unsigned int i = 0; i < ids.size (); ++i)
171 while (nextSpike[i] < nextTime)
172 {
173 port_->insertEvent (nextSpike[i],
174 MUSIC::LocalIndex (i));
175 nextSpike[i] += negexp (m);
176 }
177 }
178 }
179 };
180
181 class MyEventHandlerGlobal : public MUSIC::EventHandlerGlobalIndex {
182 public:
183 void operator () (double t, MUSIC::GlobalIndex id)
184 {
185 //eventBuffer.push_back (MUSIC::Event (t, id));
186 }
187 };
188
189 class MyEventHandlerLocal: public MUSIC::EventHandlerLocalIndex {
190 public:
191 void operator () (double t, MUSIC::LocalIndex id)
192 {
193 //eventBuffer.push_back (MUSIC::Event (t, id));
194 }
195 };
196
197 class InputPort : public Port {
198 MUSIC::EventInputPort* port_;
199 MyEventHandlerGlobal evhandlerGlobal;
200 MyEventHandlerLocal evhandlerLocal;
201
202 public:
203 InputPort (MUSIC::Setup* setup, std::string name, int width)
204 : Port (setup, name, width) { }
205
206 void publish ()
207 {
208 port_ = setup_->publishEventInput (name_);
209 if (!port_->isConnected ())
210 {
211 if (MUSIC::mpi_get_rank (setup_->communicator ()) == 0)
212 std::cerr << "multiport port is not connected" << std::endl;
213 MPI_Abort (setup_->communicator (), 1);
214 }
215 }
216
217 void map (std::string imaptype, std::string indextype)
218 {
219 MPI_Comm comm = setup_->communicator ();
220 int rank = MUSIC::mpi_get_rank (comm);
221 int nProcesses = MUSIC::mpi_get_comm_size (comm);
222
223 if (imaptype == "linear")
224 {
225 int nUnitsPerProcess = width_ / nProcesses;
226 int nLocalUnits = nUnitsPerProcess;
227 int rest = width_ % nProcesses;
228 int firstId = nUnitsPerProcess * rank;
229 if (rank < rest)
230 {
231 firstId += rank;
232 nLocalUnits += 1;
233 }
234 else
235 firstId += rest;
236 MUSIC::LinearIndex indices (firstId, nLocalUnits);
237
238 if (indextype == "global")
239 port_->map (&indices, &evhandlerGlobal, 0.1);
240 else
241 port_->map (&indices, &evhandlerLocal, 0.0);
242 }
243 else
244 {
245 std::vector<MUSIC::GlobalIndex> v;
246 for (int i = rank; i < width_; i += nProcesses)
247 v.push_back (i);
248 MUSIC::PermutationIndex indices (&v.front (), v.size ());
249
250 if (indextype == "global")
251 port_->map (&indices, &evhandlerGlobal, 0.0);
252 else
253 port_->map (&indices, &evhandlerLocal, 0.0);
254 }
255 }
256 };
257
258 std::vector<OutputPort*> outputPort;
259
260 void
261 makeOutput (MUSIC::Setup* setup, std::string name, int width)
262 {
263 outputPort.push_back (new OutputPort (setup, name, width));
264 }
265
266 std::vector<InputPort*> inputPort;
267
268 void
269 makeInput (MUSIC::Setup* setup, std::string name, int width)
270 {
271 inputPort.push_back (new InputPort (setup, name, width));
272 }
273
274 void
275 parsePort (MUSIC::Setup* setup,
276 std::string s,
277 void (*makePort) (MUSIC::Setup* setup, std::string name, int width))
278 {
279 std::istringstream is (s);
280 char name[80];
281 if (!is.getline (name, 80, ':'))
282 MUSIC::error ("couldn't parse port name");
283 int width;
284 if (!(is >> width))
285 MUSIC::error ("couldn't parse port width");
286 makePort (setup, name, width);
287 }
288
289 void
290 getargs (MUSIC::Setup* setup, int argc, char* argv[])
291 {
292 int rank = MUSIC::mpi_get_rank (setup->communicator ());
293
294 enum { OUT, IN };
295 opterr = 0; // handle errors ourselves
296 while (1)
297 {
298 static struct option longOptions[] =
299 {
300 {"timestep", required_argument, 0, 't'},
301 {"maxbuffered", required_argument, 0, 'b'},
302 {"frequency", required_argument, 0, 'f'},
303 {"imaptype", required_argument, 0, 'm'},
304 {"indextype", required_argument, 0, 'i'},
305 {"help", no_argument, 0, 'h'},
306 {"out", required_argument, 0, OUT},
307 {"in", required_argument, 0, IN},
308 {0, 0, 0, 0}
309 };
310 /* `getopt_long' stores the option index here. */
311 int option_index = 0;
312
313 // the + below tells getopt_long not to reorder argv
314 int c = getopt_long (argc, argv, "+t:b:f:m:i:h", longOptions, &option_index);
315
316 /* detect the end of the options */
317 if (c == -1)
318 break;
319
320 switch (c)
321 {
322 case 't':
323 timestep = atof (optarg); // NOTE: could do error checking
324 continue;
325 case 'b':
326 maxbuffered = atoi (optarg);
327 continue;
328 case 'f':
329 freq = atof (optarg); // NOTE: error checking here as well
330 continue;
331 case 'm':
332 imaptype = optarg;
333 if (imaptype != "linear" && imaptype != "roundrobin")
334 {
335 usage (rank);
336 abort ();
337 }
338 continue;
339 case 'i':
340 indextype = optarg;
341 if (indextype != "global" && indextype != "local")
342 {
343 usage (rank);
344 abort ();
345 }
346 continue;
347 case OUT:
348 parsePort (setup, optarg, makeOutput);
349 continue;
350 case IN:
351 parsePort (setup, optarg, makeInput);
352 continue;
353 case '?':
354 break; // ignore unknown options
355 case 'h':
356 usage (rank);
357
358 default:
359 abort ();
360 }
361 }
362
363 if (argc != optind)
364 usage (rank);
365 }
366
367 #include <cassert>
368
369 void
370 errhandler (MPI_Comm* comm, int* error, ...)
371 {
372 assert (0);
373 }
374
375 int
376 main (int argc, char *argv[])
377 {
378 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
379 MPI_Errhandler errh;
380 #if MUSIC_HAVE_MPI_COMM_CREATE_ERRHANDLER
381 MPI_Comm_create_errhandler (errhandler, &errh);
382 #else
383 MPI_Errhandler_create (errhandler, &errh);
384 #endif
385 MPI_Comm_set_errhandler (MPI_COMM_WORLD, errh);
386
387 MPI_Comm comm = setup->communicator ();
388 int rank = MUSIC::mpi_get_rank (comm);
389
390 getargs (setup, argc, argv);
391
392 for (std::vector<InputPort*>::iterator i = inputPort.begin ();
393 i != inputPort.end ();
394 ++i)
395 (*i)->publish ();
396
397 for (std::vector<OutputPort*>::iterator i = outputPort.begin ();
398 i != outputPort.end ();
399 ++i)
400 (*i)->publish ();
401
402 for (std::vector<InputPort*>::iterator i = inputPort.begin ();
403 i != inputPort.end ();
404 ++i)
405 (*i)->map (imaptype, indextype);
406
407 for (std::vector<OutputPort*>::iterator i = outputPort.begin ();
408 i != outputPort.end ();
409 ++i)
410 (*i)->map (maxbuffered, imaptype, indextype);
411
412 double stoptime;
413 setup->config ("stoptime", &stoptime);
414
415 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
416
417 srand48 (rank); // Use different seeds
418
419 for (std::vector<OutputPort*>::iterator i = outputPort.begin ();
420 i != outputPort.end ();
421 ++i)
422 (*i)->init (freq);
423
424 double time = runtime->time ();
425 while (time < stoptime)
426 {
427 double nextTime = time + timestep;
428
429 for (std::vector<OutputPort*>::iterator i = outputPort.begin ();
430 i != outputPort.end ();
431 ++i)
432 (*i)->tick (nextTime);
433
434 // Make data available for other programs
435 runtime->tick ();
436
437 time = runtime->time ();
438 }
439
440 runtime->finalize ();
441
442 delete runtime;
443
444 return 0;
445 }