| 1 |
#include <mpi.h> |
| 2 |
#include <cstdlib> |
| 3 |
#include <iostream> |
| 4 |
#include <music.hh> |
| 5 |
|
| 6 |
extern "C" { |
| 7 |
#include <unistd.h> |
| 8 |
#include <getopt.h> |
| 9 |
} |
| 10 |
|
| 11 |
const double DEFAULT_TIMESTEP = 0.01; |
| 12 |
|
| 13 |
MPI_Comm comm; |
| 14 |
|
| 15 |
void |
| 16 |
usage (int rank) |
| 17 |
{ |
| 18 |
if (rank == 0) |
| 19 |
{ |
| 20 |
std::cerr << "Usage: eventlogger [OPTION...]" << std::endl |
| 21 |
<< "`eventlogger' logs spikes from a MUSIC port." << std::endl << std::endl |
| 22 |
<< " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl |
| 23 |
<< " -l, --acclatency LATENCY acceptable data latency (s)" << std::endl |
| 24 |
<< " -b, --maxbuffered TICKS maximal amount of data buffered" << std::endl |
| 25 |
<< " -m, --imaptype TYPE linear (default) or roundrobin" << std::endl |
| 26 |
<< " -i, --indextype TYPE global (default) or local" << std::endl |
| 27 |
<< " -a, --all each receiver rank reports all spikes" << std::endl |
| 28 |
<< " --in PORTNAME input port name (default: in)" << std::endl |
| 29 |
<< " --message-in PORTNAME message port name (default: message)" << std::endl |
| 30 |
<< " -h, --help print this help message" << std::endl << std::endl |
| 31 |
<< "Report bugs to <[email protected]>." << std::endl; |
| 32 |
} |
| 33 |
exit (1); |
| 34 |
} |
| 35 |
|
| 36 |
double apptime; |
| 37 |
|
| 38 |
class MyEventHandlerGlobal : public MUSIC::EventHandlerGlobalIndex { |
| 39 |
int rank; |
| 40 |
public: |
| 41 |
MyEventHandlerGlobal (int rank_) : rank (rank_) { } |
| 42 |
void operator () (double t, MUSIC::GlobalIndex id) |
| 43 |
{ |
| 44 |
// For now: just print out incoming events |
| 45 |
std::cout << "Rank " << rank |
| 46 |
<< ": Event (" << id << ", " << t |
| 47 |
<< ") detected at " << apptime << std::endl; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
class MyEventHandlerLocal: public MUSIC::EventHandlerLocalIndex { |
| 52 |
int rank; |
| 53 |
public: |
| 54 |
MyEventHandlerLocal (int rank_) : rank (rank_) { } |
| 55 |
void operator () (double t, MUSIC::LocalIndex id) |
| 56 |
{ |
| 57 |
// For now: just print out incoming events |
| 58 |
std::cout << "Rank " << rank |
| 59 |
<< ": Event (" << id << ", " << t |
| 60 |
<< ") detected at " << apptime << std::endl; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
class MyMessageHandler : public MUSIC::MessageHandler { |
| 65 |
int rank; |
| 66 |
public: |
| 67 |
MyMessageHandler (int rank_) : rank (rank_) { } |
| 68 |
void operator () (double t, void* msg, size_t size) |
| 69 |
{ |
| 70 |
// Print out incoming messages |
| 71 |
std::string message (static_cast<char*> (msg), size); |
| 72 |
std::cout << "Rank " << rank |
| 73 |
<< ": Message (" << t << ", " << message |
| 74 |
<< ") detected at " << apptime << std::endl; |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
string portName ("in"); |
| 79 |
string messagePortName ("message"); |
| 80 |
double timestep = DEFAULT_TIMESTEP; |
| 81 |
double latency = 0.0; |
| 82 |
int maxbuffered = 0; |
| 83 |
std::string imaptype = "linear"; |
| 84 |
std::string indextype = "global"; |
| 85 |
bool all = false; |
| 86 |
bool useBarrier = false; |
| 87 |
|
| 88 |
int |
| 89 |
main (int argc, char* argv[]) |
| 90 |
{ |
| 91 |
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv); |
| 92 |
comm = setup->communicator (); |
| 93 |
int nProcesses; |
| 94 |
MPI_Comm_size (comm, &nProcesses); // how many processes are there? |
| 95 |
int rank; |
| 96 |
MPI_Comm_rank (comm, &rank); // which process am I? |
| 97 |
|
| 98 |
enum { IN, MESSAGE_IN }; |
| 99 |
opterr = 0; // handle errors ourselves |
| 100 |
while (1) |
| 101 |
{ |
| 102 |
static struct option longOptions[] = |
| 103 |
{ |
| 104 |
{"timestep", required_argument, 0, 't'}, |
| 105 |
{"acclatency", required_argument, 0, 'l'}, |
| 106 |
{"maxbuffered", required_argument, 0, 'b'}, |
| 107 |
{"imaptype", required_argument, 0, 'm'}, |
| 108 |
{"indextype", required_argument, 0, 'i'}, |
| 109 |
{"all", no_argument, 0, 'a'}, |
| 110 |
{"adapter", no_argument, 0, 'z'}, |
| 111 |
{"help", no_argument, 0, 'h'}, |
| 112 |
{"in", required_argument, 0, IN}, |
| 113 |
{"message-in", required_argument, 0, MESSAGE_IN}, |
| 114 |
{0, 0, 0, 0} |
| 115 |
}; |
| 116 |
//`getopt_long' stores the option index here. |
| 117 |
int optionIndex = 0; |
| 118 |
|
| 119 |
// the + below tells getopt_long not to reorder argv |
| 120 |
int c = getopt_long (argc, argv, "+t:l:b:m:i:azh", longOptions, &optionIndex); |
| 121 |
|
| 122 |
// detect the end of the options |
| 123 |
if (c == -1) |
| 124 |
break; |
| 125 |
|
| 126 |
switch (c) |
| 127 |
{ |
| 128 |
case 't': |
| 129 |
timestep = atof (optarg); // NOTE: could add error checking |
| 130 |
continue; |
| 131 |
case 'l': |
| 132 |
latency = atof (optarg); |
| 133 |
continue; |
| 134 |
case 'b': |
| 135 |
maxbuffered = atoi (optarg); |
| 136 |
continue; |
| 137 |
case 'm': |
| 138 |
imaptype = optarg; |
| 139 |
if (imaptype != "linear" && imaptype != "roundrobin") |
| 140 |
{ |
| 141 |
usage (rank); |
| 142 |
abort (); |
| 143 |
} |
| 144 |
continue; |
| 145 |
case 'i': |
| 146 |
indextype = optarg; |
| 147 |
if (indextype != "global" && indextype != "local") |
| 148 |
{ |
| 149 |
usage (rank); |
| 150 |
abort (); |
| 151 |
} |
| 152 |
continue; |
| 153 |
case 'a': |
| 154 |
all = true; |
| 155 |
continue; |
| 156 |
case 'z': |
| 157 |
useBarrier = true; |
| 158 |
continue; |
| 159 |
case IN: |
| 160 |
portName = optarg; |
| 161 |
continue; |
| 162 |
case MESSAGE_IN: |
| 163 |
messagePortName = optarg; |
| 164 |
continue; |
| 165 |
case '?': |
| 166 |
break; // ignore unknown options |
| 167 |
case 'h': |
| 168 |
usage (rank); |
| 169 |
|
| 170 |
default: |
| 171 |
abort (); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// Port publishing |
| 176 |
MUSIC::EventInputPort* evport = setup->publishEventInput (portName); |
| 177 |
MUSIC::MessageInputPort* msgport = setup->publishMessageInput (messagePortName); |
| 178 |
if (!evport->isConnected () && !msgport->isConnected ()) |
| 179 |
{ |
| 180 |
if (rank == 0) |
| 181 |
std::cerr << "eventlogger: no connected input ports" << std::endl; |
| 182 |
MPI_Abort (comm, 1); |
| 183 |
} |
| 184 |
|
| 185 |
// Port mapping |
| 186 |
|
| 187 |
MyEventHandlerGlobal evhandlerGlobal (rank); |
| 188 |
MyEventHandlerLocal evhandlerLocal (rank); |
| 189 |
|
| 190 |
if (evport->isConnected ()) |
| 191 |
{ |
| 192 |
// Split the width among the available processes |
| 193 |
int width = 0; |
| 194 |
if (evport->hasWidth ()) |
| 195 |
width = evport->width (); |
| 196 |
else |
| 197 |
{ |
| 198 |
std::cerr << "port width not specified in Configuration file" |
| 199 |
<< std::endl; |
| 200 |
MPI_Abort (comm, 1); |
| 201 |
} |
| 202 |
|
| 203 |
if (imaptype == "linear") |
| 204 |
{ |
| 205 |
int nLocal = width / nProcesses; |
| 206 |
int rest = width % nProcesses; |
| 207 |
int firstId = nLocal * rank; |
| 208 |
if (rank < rest) |
| 209 |
{ |
| 210 |
firstId += rank; |
| 211 |
nLocal += 1; |
| 212 |
} |
| 213 |
else |
| 214 |
firstId += rest; |
| 215 |
MUSIC::LinearIndex indexmap (all ? 0 : firstId, |
| 216 |
all ? width : nLocal); |
| 217 |
|
| 218 |
if (indextype == "global") |
| 219 |
if (maxbuffered > 0) |
| 220 |
evport->map (&indexmap, &evhandlerGlobal, latency, maxbuffered); |
| 221 |
else |
| 222 |
evport->map (&indexmap, &evhandlerGlobal, latency); |
| 223 |
else |
| 224 |
if (maxbuffered > 0) |
| 225 |
evport->map (&indexmap, &evhandlerLocal, latency, maxbuffered); |
| 226 |
else |
| 227 |
evport->map (&indexmap, &evhandlerLocal, latency); |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
std::vector<MUSIC::GlobalIndex> v; |
| 232 |
for (int i = all ? 0 : rank; i < width; i += all ? 1 : nProcesses) |
| 233 |
v.push_back (i); |
| 234 |
MUSIC::PermutationIndex indexmap (&v.front (), v.size ()); |
| 235 |
|
| 236 |
if (indextype == "global") |
| 237 |
if (maxbuffered > 0) |
| 238 |
evport->map (&indexmap, &evhandlerGlobal, latency, maxbuffered); |
| 239 |
else |
| 240 |
evport->map (&indexmap, &evhandlerGlobal, latency); |
| 241 |
else |
| 242 |
if (maxbuffered > 0) |
| 243 |
evport->map (&indexmap, &evhandlerLocal, latency, maxbuffered); |
| 244 |
else |
| 245 |
evport->map (&indexmap, &evhandlerLocal, latency); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Messages |
| 250 |
MyMessageHandler msgHandler (rank); |
| 251 |
if (msgport->isConnected ()) |
| 252 |
{ |
| 253 |
if (maxbuffered > 0) |
| 254 |
msgport->map (&msgHandler, latency, maxbuffered); |
| 255 |
else |
| 256 |
msgport->map (&msgHandler, latency); |
| 257 |
} |
| 258 |
|
| 259 |
double stoptime; |
| 260 |
setup->config ("stoptime", &stoptime); |
| 261 |
|
| 262 |
if (useBarrier) |
| 263 |
MPI_Barrier (MPI_COMM_WORLD); |
| 264 |
|
| 265 |
// Run |
| 266 |
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep); |
| 267 |
|
| 268 |
apptime = runtime->time (); |
| 269 |
while (apptime < stoptime) |
| 270 |
{ |
| 271 |
// Retrieve data from other program |
| 272 |
runtime->tick (); |
| 273 |
|
| 274 |
apptime = runtime->time (); |
| 275 |
} |
| 276 |
runtime->finalize (); |
| 277 |
|
| 278 |
delete runtime; |
| 279 |
|
| 280 |
return 0; |
| 281 |
} |