Debian Codegraph @

utils/eventselect.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: eventselect [OPTION...] N_UNITS UNITS" << std::endl
44 << "`eventselect' receives events from an input port of width N_UNITS" << std::endl
45 << "and sends events for the subset of id:s specified in the file UNITS" << std::endl << std:: endl
46 << " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
47 << " -h, --help print this help message" << std::endl << std::endl
48 << "Report bugs to <[email protected]>." << std::endl;
49 }
50 exit (1);
51 }
52
53 std::vector<MUSIC::Event> eventBuffer;
54
55 class MyEventHandlerGlobal : public MUSIC::EventHandlerGlobalIndex {
56 public:
57 void operator () (double t, MUSIC::GlobalIndex id)
58 {
59 eventBuffer.push_back (MUSIC::Event (t, id));
60 }
61 };
62
63 int nUnits;
64 double timestep = DEFAULT_TIMESTEP;
65 string units;
66
67 void
68 getargs (int rank, int argc, char* argv[])
69 {
70 opterr = 0; // handle errors ourselves
71 while (1)
72 {
73 static struct option longOptions[] =
74 {
75 {"timestep", required_argument, 0, 't'},
76 {"help", no_argument, 0, 'h'},
77 {0, 0, 0, 0}
78 };
79 /* `getopt_long' stores the option index here. */
80 int option_index = 0;
81
82 // the + below tells getopt_long not to reorder argv
83 int c = getopt_long (argc, argv, "+t:m:i:h", longOptions, &option_index);
84
85 /* detect the end of the options */
86 if (c == -1)
87 break;
88
89 switch (c)
90 {
91 case 't':
92 timestep = atof (optarg); // NOTE: some error checking would be good!
93 continue;
94 case '?':
95 break; // ignore unknown options
96 case 'h':
97 usage (rank);
98
99 default:
100 abort ();
101 }
102 }
103
104 if (argc != optind + 2)
105 usage (rank);
106
107 nUnits = atoi (argv[optind]);
108 units = argv[optind + 1];
109 }
110
111 void
112 mapOutput (MUSIC::EventOutputPort* out,
113 int nProcesses,
114 int rank,
115 std::string fname)
116 {
117 std::ifstream unitfile (fname.c_str ());
118 if (!unitfile)
119 {
120 std::cerr << "eventselect: could not open "
121 << fname << " for writing" << std::endl;
122 abort ();
123 }
124
125 // First count the integers
126 int nUnits = 0;
127 int id;
128 while (unitfile >> id)
129 ++nUnits;
130 unitfile.close ();
131 int nUnitsPerProcess = nUnits / nProcesses;
132 int nLocalUnits = nUnitsPerProcess;
133 int rest = nUnits % nProcesses;
134 int firstId = nUnitsPerProcess * rank;
135 if (rank < rest)
136 {
137 firstId += rank;
138 nLocalUnits += 1;
139 }
140 else
141 firstId += rest;
142 // Now read in our selection
143 nUnits = 0;
144 unitfile.open (fname.c_str ());
145 while (nUnits < firstId)
146 {
147 unitfile >> id;
148 ++nUnits;
149 }
150 nUnits = 0;
151 std::vector<MUSIC::GlobalIndex> units;
152 while (nUnits < nLocalUnits)
153 {
154 unitfile >> id;
155 units.push_back (id);
156 ++nUnits;
157 }
158 unitfile.close ();
159 MUSIC::PermutationIndex indices (&units.front (), units.size ());
160
161 out->map (&indices, MUSIC::Index::GLOBAL);
162 }
163
164 void
165 mapInput (MUSIC::EventInputPort* in,
166 int nProcesses,
167 int rank,
168 MyEventHandlerGlobal& evhandler)
169 {
170 int nUnitsPerProcess = nUnits / nProcesses;
171 int nLocalUnits = nUnitsPerProcess;
172 int rest = nUnits % nProcesses;
173 int firstId = nUnitsPerProcess * rank;
174 if (rank < rest)
175 {
176 firstId += rank;
177 nLocalUnits += 1;
178 }
179 else
180 firstId += rest;
181 MUSIC::LinearIndex indices (firstId, nLocalUnits);
182
183 in->map (&indices, &evhandler, 0.0);
184 }
185
186 int
187 main (int argc, char *argv[])
188 {
189 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
190
191 MPI_Comm comm = setup->communicator ();
192 int nProcesses;
193 MPI_Comm_size (comm, &nProcesses);
194 int rank;
195 MPI_Comm_rank (comm, &rank);
196
197 getargs (rank, argc, argv);
198
199 MUSIC::EventInputPort* in = setup->publishEventInput ("in");
200 if (!in->isConnected ())
201 {
202 if (rank == 0)
203 std::cerr << "eventselect port in is not connected" << std::endl;
204 MPI_Abort (comm, 1);
205 }
206
207 MUSIC::EventOutputPort* out = setup->publishEventOutput ("out");
208 if (!out->isConnected ())
209 {
210 if (rank == 0)
211 std::cerr << "eventselect port out is not connected" << std::endl;
212 MPI_Abort (comm, 1);
213 }
214
215 mapOutput (out, nProcesses, rank, units);
216
217 MyEventHandlerGlobal evhandlerGlobal;
218
219 mapInput (in, nProcesses, rank, evhandlerGlobal);
220
221 double stoptime;
222 setup->config ("stoptime", &stoptime);
223
224 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
225
226 double time = runtime->time ();
227 while (time < stoptime)
228 {
229 eventBuffer.clear ();
230 // Retrieve data
231 runtime->tick ();
232
233 sort (eventBuffer.begin (), eventBuffer.end ());
234 for (std::vector<MUSIC::Event>::iterator i = eventBuffer.begin ();
235 i != eventBuffer.end ();
236 ++i)
237 // Send data (NOTE: assumes that non-mapped id:s will be discarded)
238 out->insertEvent (i->t, MUSIC::GlobalIndex (i->id));
239
240 time = runtime->time ();
241 }
242
243 runtime->finalize ();
244
245 delete runtime;
246
247 return 0;
248 }