Debian Codegraph @

utils/eventsink.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009, 2018, 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 <fstream>
25 #include <string>
26 #include <cstdlib>
27 #include <vector>
28
29 extern "C" {
30 #include <unistd.h>
31 #include <getopt.h>
32 }
33
34 #include <music.hh>
35
36 const double DEFAULT_TIMESTEP = 1e-2;
37
38 void
39 usage (int rank)
40 {
41 if (rank == 0)
42 {
43 std::cerr << "Usage: eventsink [OPTION...] N_UNITS PREFIX [SUFFIX]" << std::endl
44 << "`eventsink' receives spikes through a MUSIC input port" << std::endl
45 << "and writes these to a set of files with names PREFIX RANK SUFFIX" << std::endl << std:: endl
46 << " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
47 << " -m, --imaptype TYPE linear (default) or roundrobin" << std::endl
48 << " -i, --indextype TYPE global (default) or local" << std::endl
49 << " --in PORTNAME input port name (default: in)" << std::endl
50 << " -h, --help print this help message" << std::endl << std::endl
51 << "Report bugs to <[email protected]>." << std::endl;
52 }
53 exit (1);
54 }
55
56 std::vector<MUSIC::Event> eventBuffer;
57
58 class MyEventHandlerGlobal : public MUSIC::EventHandlerGlobalIndex {
59 public:
60 void operator () (double t, MUSIC::GlobalIndex id)
61 {
62 eventBuffer.push_back (MUSIC::Event (t, id));
63 }
64 };
65
66 class MyEventHandlerLocal: public MUSIC::EventHandlerLocalIndex {
67 public:
68 void operator () (double t, MUSIC::LocalIndex id)
69 {
70 eventBuffer.push_back (MUSIC::Event (t, id));
71 }
72 };
73
74 string portName ("in");
75 int nUnits;
76 double timestep = DEFAULT_TIMESTEP;
77 string imaptype = "linear";
78 string indextype = "global";
79 string prefix;
80 string suffix = ".dat";
81 bool useBarrier = false;
82
83 void
84 getargs (int rank, int argc, char* argv[])
85 {
86 enum { IN };
87 opterr = 0; // handle errors ourselves
88 while (1)
89 {
90 static struct option longOptions[] =
91 {
92 {"timestep", required_argument, 0, 't'},
93 {"imaptype", required_argument, 0, 'm'},
94 {"indextype", required_argument, 0, 'i'},
95 {"help", no_argument, 0, 'h'},
96 {"in", required_argument, 0, IN},
97 {"adapter", no_argument, 0, 'z'},
98 {0, 0, 0, 0}
99 };
100 /* `getopt_long' stores the option index here. */
101 int option_index = 0;
102
103 // the + below tells getopt_long not to reorder argv
104 int c = getopt_long (argc, argv, "+t:m:i:hz", longOptions, &option_index);
105
106 /* detect the end of the options */
107 if (c == -1)
108 break;
109
110 switch (c)
111 {
112 case 't':
113 timestep = atof (optarg); // NOTE: could do error checking
114 continue;
115 case 'm':
116 imaptype = optarg;
117 if (imaptype != "linear" && imaptype != "roundrobin")
118 {
119 usage (rank);
120 abort ();
121 }
122 continue;
123 case 'i':
124 indextype = optarg;
125 if (indextype != "global" && indextype != "local")
126 {
127 usage (rank);
128 abort ();
129 }
130 continue;
131 case IN:
132 portName = optarg;
133 continue;
134 case 'z':
135 useBarrier = true;
136 continue;
137 case '?':
138 break; // ignore unknown options
139 case 'h':
140 usage (rank);
141
142 default:
143 abort ();
144 }
145 }
146
147 if (argc < optind + 2 || argc > optind + 3)
148 usage (rank);
149
150 nUnits = atoi (argv[optind]);
151 prefix = argv[optind + 1];
152 if (argc == optind + 3)
153 suffix = argv[optind + 2];
154 }
155
156 int
157 main (int argc, char *argv[])
158 {
159 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
160
161 MPI_Comm comm = setup->communicator ();
162 int nProcesses;
163 MPI_Comm_size (comm, &nProcesses);
164 int rank;
165 MPI_Comm_rank (comm, &rank);
166
167 getargs (rank, argc, argv);
168
169 MUSIC::EventInputPort* in = setup->publishEventInput (portName);
170 if (!in->isConnected ())
171 {
172 if (rank == 0)
173 std::cerr << "eventsink port is not connected" << std::endl;
174 MPI_Abort (comm, 1);
175 }
176
177 std::ostringstream spikefile;
178 spikefile << prefix << rank << suffix;
179 std::ofstream out (spikefile.str ().c_str ());
180 if (!out)
181 {
182 std::cerr << "eventsink: could not open "
183 << spikefile.str () << " for writing" << std::endl;
184 abort ();
185 }
186
187 MyEventHandlerGlobal evhandlerGlobal;
188 MyEventHandlerLocal evhandlerLocal;
189
190 if (imaptype == "linear")
191 {
192 int nUnitsPerProcess = nUnits / nProcesses;
193 int nLocalUnits = nUnitsPerProcess;
194 int rest = nUnits % nProcesses;
195 int firstId = nUnitsPerProcess * rank;
196 if (rank < rest)
197 {
198 firstId += rank;
199 nLocalUnits += 1;
200 }
201 else
202 firstId += rest;
203 MUSIC::LinearIndex indices (firstId, nLocalUnits);
204
205 if (indextype == "global")
206 in->map (&indices, &evhandlerGlobal, 0.0);
207 else
208 in->map (&indices, &evhandlerLocal, 0.0);
209 }
210 else
211 {
212 std::vector<MUSIC::GlobalIndex> v;
213 for (int i = rank; i < nUnits; i += nProcesses)
214 v.push_back (i);
215 MUSIC::PermutationIndex indices (&v.front (), v.size ());
216
217 if (indextype == "global")
218 in->map (&indices, &evhandlerGlobal, 0.0);
219 else
220 in->map (&indices, &evhandlerLocal, 0.0);
221 }
222
223 double stoptime;
224 setup->config ("stoptime", &stoptime);
225
226 if (useBarrier)
227 MPI_Barrier (MPI_COMM_WORLD);
228
229 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
230
231 double time = runtime->time ();
232 while (time < stoptime)
233 {
234 eventBuffer.clear ();
235 // Retrieve data from other program
236 runtime->tick ();
237
238 sort (eventBuffer.begin (), eventBuffer.end ());
239 for (std::vector<MUSIC::Event>::iterator i = eventBuffer.begin ();
240 i != eventBuffer.end ();
241 ++i)
242 out << i->t << '\t' << i->id << std::endl;
243
244 time = runtime->time ();
245 }
246 out.close ();
247
248 runtime->finalize ();
249
250 delete runtime;
251
252 return 0;
253 }