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