| 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: eventdelay [OPTION...]" << std::endl |
| 44 |
<< "`eventdelay' receives spikes through a MUSIC input port" << std::endl |
| 45 |
<< "and relays them to an output port" << std::endl << std:: endl |
| 46 |
<< " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl |
| 47 |
<< " -d, --delay SECS amount of delay" << std::endl |
| 48 |
<< " -b, --maxbuffer TICKS maximal buffer" << std::endl |
| 49 |
<< " -L, --label LABEL log events using LABEL" << 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 |
std::vector<MUSIC::Event> overflowBuffer; |
| 58 |
|
| 59 |
double timestep = DEFAULT_TIMESTEP; |
| 60 |
double delay = 0.0; |
| 61 |
int label = -1; |
| 62 |
int maxbuffered = 0; |
| 63 |
|
| 64 |
#ifdef MUSIC_LOCAL |
| 65 |
class MyEventHandler: public MUSIC::EventHandlerLocalIndex { |
| 66 |
public: |
| 67 |
void operator () (double t, MUSIC::LocalIndex id) |
| 68 |
{ |
| 69 |
eventBuffer.push_back (MUSIC::Event (t + delay, id)); |
| 70 |
if (label != -1) |
| 71 |
std::cout << label << ":Got(" << id << |
| 72 |
", " << t + delay << ")" << std::endl; |
| 73 |
} |
| 74 |
}; |
| 75 |
#else |
| 76 |
class MyEventHandler: public MUSIC::EventHandlerGlobalIndex { |
| 77 |
public: |
| 78 |
void operator () (double t, MUSIC::GlobalIndex id) |
| 79 |
{ |
| 80 |
eventBuffer.push_back (MUSIC::Event (t + delay, id)); |
| 81 |
if (label != -1) |
| 82 |
std::cout << label << ":Got(" << id << |
| 83 |
", " << t + delay << ")" << std::endl; |
| 84 |
} |
| 85 |
}; |
| 86 |
#endif |
| 87 |
|
| 88 |
void |
| 89 |
getargs (int rank, int argc, char* argv[]) |
| 90 |
{ |
| 91 |
opterr = 0; // handle errors ourselves |
| 92 |
while (1) |
| 93 |
{ |
| 94 |
static struct option longOptions[] = |
| 95 |
{ |
| 96 |
{"timestep", required_argument, 0, 't'}, |
| 97 |
{"delay", required_argument, 0, 'd'}, |
| 98 |
{"label", required_argument, 0, 'L'}, |
| 99 |
{"maxbuffer", required_argument, 0, 'b'}, |
| 100 |
{"help", no_argument, 0, 'h'}, |
| 101 |
{0, 0, 0, 0} |
| 102 |
}; |
| 103 |
/* `getopt_long' stores the option index here. */ |
| 104 |
int option_index = 0; |
| 105 |
|
| 106 |
// the + below tells getopt_long not to reorder argv |
| 107 |
int c = getopt_long (argc, argv, "+t:d:L:b:h", longOptions, &option_index); |
| 108 |
|
| 109 |
/* detect the end of the options */ |
| 110 |
if (c == -1) |
| 111 |
break; |
| 112 |
|
| 113 |
switch (c) |
| 114 |
{ |
| 115 |
case 't': |
| 116 |
timestep = atof (optarg); |
| 117 |
continue; |
| 118 |
case 'd': |
| 119 |
delay = atof(optarg); |
| 120 |
continue; |
| 121 |
case 'L': |
| 122 |
label = atoi(optarg); |
| 123 |
continue; |
| 124 |
case 'b': |
| 125 |
maxbuffered = atoi(optarg); |
| 126 |
continue; |
| 127 |
case '?': |
| 128 |
break; // ignore unknown options |
| 129 |
case 'h': |
| 130 |
usage (rank); |
| 131 |
|
| 132 |
default: |
| 133 |
abort (); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (argc < optind || argc > optind) |
| 138 |
usage (rank); |
| 139 |
} |
| 140 |
|
| 141 |
int |
| 142 |
main (int argc, char *argv[]) |
| 143 |
{ |
| 144 |
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv); |
| 145 |
|
| 146 |
MPI_Comm comm = setup->communicator (); |
| 147 |
int nProcesses; |
| 148 |
MPI_Comm_size (comm, &nProcesses); |
| 149 |
int rank; |
| 150 |
MPI_Comm_rank (comm, &rank); |
| 151 |
|
| 152 |
getargs (rank, argc, argv); |
| 153 |
|
| 154 |
MUSIC::EventInputPort* in = setup->publishEventInput ("in"); |
| 155 |
if (!in->isConnected ()) |
| 156 |
{ |
| 157 |
if (rank == 0) |
| 158 |
std::cerr << "eventdelay input port is not connected" << std::endl; |
| 159 |
MPI_Abort (comm, 1); |
| 160 |
} |
| 161 |
|
| 162 |
MUSIC::EventOutputPort* out = setup->publishEventOutput ("out"); |
| 163 |
if (!out->isConnected ()) |
| 164 |
{ |
| 165 |
if (rank == 0) |
| 166 |
std::cerr << "eventdelay output port is not connected" << std::endl; |
| 167 |
MPI_Abort (comm, 1); |
| 168 |
} |
| 169 |
|
| 170 |
// Optional extra input port |
| 171 |
MUSIC::EventInputPort* aux = setup->publishEventInput ("aux"); |
| 172 |
|
| 173 |
int width = in->width (); |
| 174 |
|
| 175 |
MyEventHandler evhandler; |
| 176 |
|
| 177 |
int localWidth = width / nProcesses; |
| 178 |
int myWidth = localWidth; |
| 179 |
|
| 180 |
if (rank == nProcesses-1) |
| 181 |
{ |
| 182 |
myWidth = localWidth; // FIXME |
| 183 |
} |
| 184 |
|
| 185 |
MUSIC::LinearIndex indices (rank*localWidth, myWidth); |
| 186 |
|
| 187 |
if (maxbuffered) |
| 188 |
in->map (&indices, &evhandler, delay, maxbuffered); |
| 189 |
else |
| 190 |
in->map (&indices, &evhandler, delay); |
| 191 |
|
| 192 |
#ifdef MUSIC_LOCAL |
| 193 |
out->map (&indices, MUSIC::Index::LOCAL); |
| 194 |
#else |
| 195 |
out->map (&indices, MUSIC::Index::GLOBAL); |
| 196 |
#endif |
| 197 |
if (aux->isConnected ()) |
| 198 |
{ |
| 199 |
if (maxbuffered) |
| 200 |
aux->map (&indices, &evhandler, 0.0, maxbuffered); |
| 201 |
else |
| 202 |
aux->map (&indices, &evhandler, 0.0); |
| 203 |
} |
| 204 |
double stoptime; |
| 205 |
setup->config ("stoptime", &stoptime); |
| 206 |
|
| 207 |
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep); |
| 208 |
|
| 209 |
for (; runtime->time () < stoptime; runtime->tick ()) |
| 210 |
{ |
| 211 |
sort (eventBuffer.begin (), eventBuffer.end ()); |
| 212 |
for (std::vector<MUSIC::Event>::iterator i = eventBuffer.begin (); |
| 213 |
i != eventBuffer.end (); |
| 214 |
++i) |
| 215 |
{ |
| 216 |
if (i->t < runtime->time () + timestep) |
| 217 |
{ |
| 218 |
if (label != -1) |
| 219 |
std::cout << label << ":Sent(" << i->id << ", " |
| 220 |
<< i->t << " @" << runtime->time () |
| 221 |
<< ")" << std::endl; |
| 222 |
#ifdef MUSIC_LOCAL |
| 223 |
out->insertEvent (i->t, MUSIC::LocalIndex (i->id)); |
| 224 |
#else |
| 225 |
out->insertEvent (i->t, MUSIC::GlobalIndex (i->id)); |
| 226 |
#endif |
| 227 |
} |
| 228 |
else |
| 229 |
overflowBuffer.push_back (*i); |
| 230 |
} |
| 231 |
eventBuffer.clear (); |
| 232 |
|
| 233 |
for (std::vector<MUSIC::Event>::iterator i = overflowBuffer.begin (); |
| 234 |
i != overflowBuffer.end (); |
| 235 |
++i) |
| 236 |
eventBuffer.push_back (*i); |
| 237 |
overflowBuffer.clear (); |
| 238 |
} |
| 239 |
|
| 240 |
runtime->finalize (); |
| 241 |
|
| 242 |
delete runtime; |
| 243 |
|
| 244 |
return 0; |
| 245 |
} |