File: eventselect.cc

package info (click to toggle)
music 1.2.1-0.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,596 kB
  • sloc: cpp: 26,805; sh: 5,674; python: 510; makefile: 406; ansic: 224
file content (248 lines) | stat: -rw-r--r-- 5,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  This file is part of MUSIC.
 *  Copyright (C) 2008, 2009, 2022 INCF
 *
 *  MUSIC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MUSIC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Leave as first include---required by BG/L
#include <mpi.h>

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>

extern "C" {
#include <unistd.h>
#include <getopt.h>
}

#include <music.hh>

const double DEFAULT_TIMESTEP = 1e-2;

void
usage (int rank)
{
  if (rank == 0)
    {
      std::cerr << "Usage: eventselect [OPTION...] N_UNITS UNITS" << std::endl
		<< "`eventselect' receives events from an input port of width N_UNITS" << std::endl
		<< "and sends events for the subset of id:s specified in the file UNITS" << std::endl << std:: endl
		<< "  -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
		<< "  -h, --help              print this help message" << std::endl << std::endl
		<< "Report bugs to <music-bugs@incf.org>." << std::endl;
    }
  exit (1);
}

std::vector<MUSIC::Event> eventBuffer;

class MyEventHandlerGlobal : public MUSIC::EventHandlerGlobalIndex {
public:
  void operator () (double t, MUSIC::GlobalIndex id)
  {
    eventBuffer.push_back (MUSIC::Event (t, id));
  }
};

int nUnits;
double timestep = DEFAULT_TIMESTEP;
string units;

void
getargs (int rank, int argc, char* argv[])
{
  opterr = 0; // handle errors ourselves
  while (1)
    {
      static struct option longOptions[] =
	{
	  {"timestep",  required_argument, 0, 't'},
	  {"help",      no_argument,       0, 'h'},
	  {0, 0, 0, 0}
	};
      /* `getopt_long' stores the option index here. */
      int option_index = 0;

      // the + below tells getopt_long not to reorder argv
      int c = getopt_long (argc, argv, "+t:m:i:h", longOptions, &option_index);

      /* detect the end of the options */
      if (c == -1)
	break;

      switch (c)
	{
	case 't':
	  timestep = atof (optarg); // NOTE: some error checking would be good!
	  continue;
	case '?':
	  break; // ignore unknown options
	case 'h':
	  usage (rank);

	default:
	  abort ();
	}
    }

  if (argc != optind + 2)
    usage (rank);

  nUnits = atoi (argv[optind]);
  units = argv[optind + 1];
}

void
mapOutput (MUSIC::EventOutputPort* out,
	   int nProcesses,
	   int rank,
	   std::string fname)
{
  std::ifstream unitfile (fname.c_str ());
  if (!unitfile)
    {
      std::cerr << "eventselect: could not open "
		<< fname << " for writing" << std::endl;
      abort ();      
    }

  // First count the integers
  int nUnits = 0;
  int id;
  while (unitfile >> id)
    ++nUnits;
  unitfile.close ();
  int nUnitsPerProcess = nUnits / nProcesses;
  int nLocalUnits = nUnitsPerProcess;
  int rest = nUnits % nProcesses;
  int firstId = nUnitsPerProcess * rank;
  if (rank < rest)
    {
      firstId += rank;
      nLocalUnits += 1;
    }
  else
    firstId += rest;
  // Now read in our selection
  nUnits = 0;
  unitfile.open (fname.c_str ());
  while (nUnits < firstId)
    {
      unitfile >> id;
      ++nUnits;
    }
  nUnits = 0;
  std::vector<MUSIC::GlobalIndex> units;
  while (nUnits < nLocalUnits)
    {
      unitfile >> id;
      units.push_back (id);
      ++nUnits;
    }
  unitfile.close ();
  MUSIC::PermutationIndex indices (&units.front (), units.size ());

  out->map (&indices, MUSIC::Index::GLOBAL);
}

void
mapInput (MUSIC::EventInputPort* in,
	  int nProcesses,
	  int rank,
	  MyEventHandlerGlobal& evhandler)
{
  int nUnitsPerProcess = nUnits / nProcesses;
  int nLocalUnits = nUnitsPerProcess;
  int rest = nUnits % nProcesses;
  int firstId = nUnitsPerProcess * rank;
  if (rank < rest)
    {
      firstId += rank;
      nLocalUnits += 1;
    }
  else
    firstId += rest;
  MUSIC::LinearIndex indices (firstId, nLocalUnits);

  in->map (&indices, &evhandler, 0.0);
}

int
main (int argc, char *argv[])
{
  MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
  
  MPI_Comm comm = setup->communicator ();
  int nProcesses;
  MPI_Comm_size (comm, &nProcesses);
  int rank;
  MPI_Comm_rank (comm, &rank);
  
  getargs (rank, argc, argv);

  MUSIC::EventInputPort* in = setup->publishEventInput ("in");
  if (!in->isConnected ())
    {
      if (rank == 0)
	std::cerr << "eventselect port in is not connected" << std::endl;
      MPI_Abort (comm, 1);
    }

  MUSIC::EventOutputPort* out = setup->publishEventOutput ("out");
  if (!out->isConnected ())
    {
      if (rank == 0)
	std::cerr << "eventselect port out is not connected" << std::endl;
      MPI_Abort (comm, 1);
    }

  mapOutput (out, nProcesses, rank, units);

  MyEventHandlerGlobal evhandlerGlobal;

  mapInput (in, nProcesses, rank, evhandlerGlobal);
  
  double stoptime;
  setup->config ("stoptime", &stoptime);

  MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);

  double time = runtime->time ();
  while (time < stoptime)
    {
      eventBuffer.clear ();
      // Retrieve data
      runtime->tick ();
      
      sort (eventBuffer.begin (), eventBuffer.end ());
      for (std::vector<MUSIC::Event>::iterator i = eventBuffer.begin ();
	   i != eventBuffer.end ();
	   ++i)
	// Send data (NOTE: assumes that non-mapped id:s will be discarded)
	out->insertEvent (i->t, MUSIC::GlobalIndex (i->id));

      time = runtime->time ();
    }

  runtime->finalize ();

  delete runtime;

  return 0;
}