| 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 <string> |
| 25 |
#include <cstdlib> |
| 26 |
|
| 27 |
extern "C" { |
| 28 |
#include <unistd.h> |
| 29 |
#include <getopt.h> |
| 30 |
} |
| 31 |
|
| 32 |
#include <music.hh> |
| 33 |
|
| 34 |
#include "datafile.h" |
| 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: eventsource [OPTION...] N_UNITS PREFIX [SUFFIX]" << std::endl |
| 44 |
<< "`eventsource' reads spikes from a set of files with names PREFIX RANK SUFFIX" << std::endl |
| 45 |
<< "and propagates these spikes through a MUSIC output port." << std::endl << std:: endl |
| 46 |
<< " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl |
| 47 |
<< " -b, --maxbuffered TICKS maximal amount of data buffered" << std::endl |
| 48 |
<< " -m, --imaptype TYPE linear (default) or roundrobin" << std::endl |
| 49 |
<< " -i, --indextype TYPE global (default) or local" << std::endl |
| 50 |
<< " --out PORTNAME output port name (default: out)" << std::endl |
| 51 |
<< " -h, --help print this help message" << std::endl << std::endl |
| 52 |
<< "Report bugs to <[email protected]>." << std::endl; |
| 53 |
} |
| 54 |
exit (1); |
| 55 |
} |
| 56 |
|
| 57 |
string portName ("out"); |
| 58 |
int nUnits; |
| 59 |
double timestep = DEFAULT_TIMESTEP; |
| 60 |
int maxbuffered = 0; |
| 61 |
string imaptype = "linear"; |
| 62 |
string indextype = "global"; |
| 63 |
string prefix; |
| 64 |
string suffix = ".dat"; |
| 65 |
|
| 66 |
void |
| 67 |
getargs (int rank, int argc, char* argv[]) |
| 68 |
{ |
| 69 |
enum { OUT }; |
| 70 |
opterr = 0; // handle errors ourselves |
| 71 |
while (1) |
| 72 |
{ |
| 73 |
static struct option longOptions[] = |
| 74 |
{ |
| 75 |
{"timestep", required_argument, 0, 't'}, |
| 76 |
{"maxbuffered", required_argument, 0, 'b'}, |
| 77 |
{"imaptype", required_argument, 0, 'm'}, |
| 78 |
{"indextype", required_argument, 0, 'i'}, |
| 79 |
{"help", no_argument, 0, 'h'}, |
| 80 |
{"out", required_argument, 0, OUT}, |
| 81 |
{0, 0, 0, 0} |
| 82 |
}; |
| 83 |
/* `getopt_long' stores the option index here. */ |
| 84 |
int option_index = 0; |
| 85 |
|
| 86 |
// the + below tells getopt_long not to reorder argv |
| 87 |
int c = getopt_long (argc, argv, "+t:b:m:i:h", |
| 88 |
longOptions, &option_index); |
| 89 |
|
| 90 |
/* detect the end of the options */ |
| 91 |
if (c == -1) |
| 92 |
break; |
| 93 |
|
| 94 |
switch (c) |
| 95 |
{ |
| 96 |
case 't': |
| 97 |
timestep = atof (optarg); // NOTE: could do error checking |
| 98 |
continue; |
| 99 |
case 'b': |
| 100 |
maxbuffered = atoi (optarg); |
| 101 |
continue; |
| 102 |
case 'm': |
| 103 |
imaptype = optarg; |
| 104 |
if (imaptype != "linear" && imaptype != "roundrobin") |
| 105 |
{ |
| 106 |
usage (rank); |
| 107 |
abort (); |
| 108 |
} |
| 109 |
continue; |
| 110 |
case 'i': |
| 111 |
indextype = optarg; |
| 112 |
if (indextype != "global" && indextype != "local") |
| 113 |
{ |
| 114 |
usage (rank); |
| 115 |
abort (); |
| 116 |
} |
| 117 |
continue; |
| 118 |
case OUT: |
| 119 |
portName = optarg; |
| 120 |
continue; |
| 121 |
case '?': |
| 122 |
break; // ignore unknown options |
| 123 |
case 'h': |
| 124 |
usage (rank); |
| 125 |
|
| 126 |
default: |
| 127 |
abort (); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (argc < optind + 2 || argc > optind + 3) |
| 132 |
usage (rank); |
| 133 |
|
| 134 |
nUnits = atoi (argv[optind]); |
| 135 |
prefix = argv[optind + 1]; |
| 136 |
if (argc == optind + 3) |
| 137 |
suffix = argv[optind + 2]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
int |
| 142 |
main (int argc, char *argv[]) |
| 143 |
{ |
| 144 |
|
| 145 |
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv); |
| 146 |
|
| 147 |
MPI_Comm comm = setup->communicator (); |
| 148 |
int nProcesses; |
| 149 |
MPI_Comm_size (comm, &nProcesses); |
| 150 |
int rank; |
| 151 |
MPI_Comm_rank (comm, &rank); |
| 152 |
|
| 153 |
getargs (rank, argc, argv); |
| 154 |
|
| 155 |
MUSIC::EventOutputPort* out = setup->publishEventOutput (portName); |
| 156 |
if (!out->isConnected ()) |
| 157 |
{ |
| 158 |
if (rank == 0) |
| 159 |
std::cerr << "eventsource port is not connected" << std::endl; |
| 160 |
MPI_Abort (comm, 1); |
| 161 |
} |
| 162 |
|
| 163 |
MUSIC::Index::Type type; |
| 164 |
if (indextype == "global") |
| 165 |
type = MUSIC::Index::GLOBAL; |
| 166 |
else |
| 167 |
type = MUSIC::Index::LOCAL; |
| 168 |
|
| 169 |
if (imaptype == "linear") |
| 170 |
{ |
| 171 |
int nUnitsPerProcess = nUnits / nProcesses; |
| 172 |
int nLocalUnits = nUnitsPerProcess; |
| 173 |
int rest = nUnits % nProcesses; |
| 174 |
int firstId = nUnitsPerProcess * rank; |
| 175 |
if (rank < rest) |
| 176 |
{ |
| 177 |
firstId += rank; |
| 178 |
nLocalUnits += 1; |
| 179 |
} |
| 180 |
else |
| 181 |
firstId += rest; |
| 182 |
MUSIC::LinearIndex indices (firstId, nLocalUnits); |
| 183 |
|
| 184 |
if (maxbuffered > 0) |
| 185 |
out->map (&indices, type, maxbuffered); |
| 186 |
else |
| 187 |
out->map (&indices, type); |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
std::vector<MUSIC::GlobalIndex> v; |
| 192 |
for (int i = rank; i < nUnits; i += nProcesses) |
| 193 |
v.push_back (i); |
| 194 |
MUSIC::PermutationIndex indices (&v.front (), v.size ()); |
| 195 |
if (maxbuffered > 0) |
| 196 |
out->map (&indices, type, maxbuffered); |
| 197 |
else |
| 198 |
out->map (&indices, type); |
| 199 |
} |
| 200 |
|
| 201 |
double stoptime; |
| 202 |
setup->config ("stoptime", &stoptime); |
| 203 |
|
| 204 |
std::ostringstream spikefile; |
| 205 |
spikefile << prefix << rank << suffix; |
| 206 |
Datafile in (spikefile.str ()); |
| 207 |
if (!in) |
| 208 |
{ |
| 209 |
std::cerr << "eventsource: could not open " |
| 210 |
<< spikefile.str () << std::endl; |
| 211 |
abort (); |
| 212 |
} |
| 213 |
|
| 214 |
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep); |
| 215 |
|
| 216 |
in.skipHeader (); |
| 217 |
int id; |
| 218 |
double t; |
| 219 |
in >> t >> id; |
| 220 |
bool moreSpikes = !in.eof (); |
| 221 |
|
| 222 |
double time = runtime->time (); |
| 223 |
while (time < stoptime) |
| 224 |
{ |
| 225 |
double nextTime = time + timestep; |
| 226 |
while (moreSpikes && t < nextTime) |
| 227 |
{ |
| 228 |
// MUSIC_LOGR("event("<<id<<","<<t<<")"); |
| 229 |
out->insertEvent (t, MUSIC::GlobalIndex (id)); |
| 230 |
in >> t >> id; |
| 231 |
moreSpikes = !in.eof (); |
| 232 |
} |
| 233 |
// Make data available for other programs |
| 234 |
runtime->tick (); |
| 235 |
|
| 236 |
time = runtime->time (); |
| 237 |
} |
| 238 |
|
| 239 |
runtime->finalize (); |
| 240 |
|
| 241 |
delete runtime; |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |