Debian Codegraph @

utils/eventcounter.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009, 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: eventcounter [OPTION...] N_UNITS PREFIX [SUFFIX]" << std::endl
44 << "`eventcounter' receives spikes through a MUSIC input port, counts" << std::endl
45 << "all spikes for each index and writes the frequencies to a set of files" << std::endl
46 << "with names PREFIX RANK SUFFIX" << std::endl << std:: endl
47 << " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
48 << " -b, --maxbuffered TICKS maximal amount of data buffered" << std::endl
49 << " -m, --imaptype TYPE linear (default) or roundrobin" << 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<int> counters;
57
58 class MyEventHandlerLocal: public MUSIC::EventHandlerLocalIndex {
59 public:
60 void operator () (double t, MUSIC::LocalIndex id)
61 {
62 ++counters[id];
63 }
64 };
65
66 int nUnits;
67 double timestep = DEFAULT_TIMESTEP;
68 int maxbuffered = 0;
69 string imaptype = "linear";
70 string prefix;
71 string suffix = ".dat";
72
73 void
74 getargs (int rank, int argc, char* argv[])
75 {
76 opterr = 0; // handle errors ourselves
77 while (1)
78 {
79 static struct option longOptions[] =
80 {
81 {"timestep", required_argument, 0, 't'},
82 {"maxbuffered", required_argument, 0, 'b'},
83 {"imaptype", required_argument, 0, 'm'},
84 {"help", no_argument, 0, 'h'},
85 {0, 0, 0, 0}
86 };
87 /* `getopt_long' stores the option index here. */
88 int option_index = 0;
89
90 // the + below tells getopt_long not to reorder argv
91 int c = getopt_long (argc, argv, "+t:b:m:h", longOptions, &option_index);
92
93 /* detect the end of the options */
94 if (c == -1)
95 break;
96
97 switch (c)
98 {
99 case 't':
100 timestep = atof (optarg); // NOTE: could do error checking
101 continue;
102 case 'b':
103 maxbuffered = atoi (optarg);
104 continue;
105 case 'm':
106 imaptype = optarg;
107 if (imaptype != "linear" && imaptype != "roundrobin")
108 {
109 usage (rank);
110 abort ();
111 }
112 continue;
113 case '?':
114 break; // ignore unknown options
115 case 'h':
116 usage (rank);
117
118 default:
119 abort ();
120 }
121 }
122
123 if (argc < optind + 2 || argc > optind + 3)
124 usage (rank);
125
126 nUnits = atoi (argv[optind]);
127 prefix = argv[optind + 1];
128 if (argc == optind + 3)
129 suffix = argv[optind + 2];
130 }
131
132 int
133 main (int argc, char *argv[])
134 {
135 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
136
137 MPI_Comm comm = setup->communicator ();
138 int nProcesses;
139 MPI_Comm_size (comm, &nProcesses);
140 int rank;
141 MPI_Comm_rank (comm, &rank);
142
143 getargs (rank, argc, argv);
144
145 MUSIC::EventInputPort* in = setup->publishEventInput ("in");
146 if (!in->isConnected ())
147 {
148 if (rank == 0)
149 std::cerr << "eventcounter port is not connected" << std::endl;
150 MPI_Abort (comm, 1);
151 }
152
153 std::ostringstream spikefile;
154 spikefile << prefix << rank << suffix;
155 std::ofstream out (spikefile.str ().c_str ());
156 if (!out)
157 {
158 std::cerr << "eventcounter: could not open "
159 << spikefile.str () << " for writing" << std::endl;
160 abort ();
161 }
162
163 MyEventHandlerLocal evhandlerLocal;
164
165 if (imaptype == "linear")
166 {
167 int nUnitsPerProcess = nUnits / nProcesses;
168 int nLocalUnits = nUnitsPerProcess;
169 int rest = nUnits % nProcesses;
170 int firstId = nUnitsPerProcess * rank;
171 if (rank < rest)
172 {
173 firstId += rank;
174 nLocalUnits += 1;
175 }
176 else
177 firstId += rest;
178 MUSIC::LinearIndex indices (firstId, nLocalUnits);
179
180 if (maxbuffered > 0)
181 in->map (&indices, &evhandlerLocal, 0.0, maxbuffered);
182 else
183 in->map (&indices, &evhandlerLocal, 0.0);
184 }
185 else
186 {
187 std::vector<MUSIC::GlobalIndex> v;
188 for (int i = rank; i < nUnits; i += nProcesses)
189 v.push_back (i);
190 MUSIC::PermutationIndex indices (&v.front (), v.size ());
191
192 if (maxbuffered > 0)
193 in->map (&indices, &evhandlerLocal, 0.0, maxbuffered);
194 else
195 in->map (&indices, &evhandlerLocal, 0.0);
196 }
197
198 counters.resize (nUnits);
199 for (int i = 0; i < nUnits; ++i)
200 counters[i] = 0;
201
202 double stoptime;
203 setup->config ("stoptime", &stoptime);
204
205 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
206
207 double time = runtime->time ();
208 while (time < stoptime)
209 {
210 // Retrieve data from other program
211 runtime->tick ();
212
213 time = runtime->time ();
214 }
215 for (std::vector<int>::iterator i = counters.begin ();
216 i != counters.end ();
217 ++i)
218 out << static_cast<double> (*i) / stoptime << std::endl;
219 out.close ();
220
221 runtime->finalize ();
222
223 delete runtime;
224
225 return 0;
226 }