| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 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 <fstream> |
| 24 |
#include <sstream> |
| 25 |
#include <string> |
| 26 |
#include <cstdlib> |
| 27 |
#include <cstring> |
| 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: messagesource [OPTION...] PREFIX [SUFFIX]" << std::endl |
| 44 |
<< "`messagesource' reads messages from a set of files with names PREFIX RANK SUFFIX" << std::endl |
| 45 |
<< "and propagates these messages 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 |
<< " -h, --help print this help message" << std::endl << std::endl |
| 49 |
<< "Report bugs to <[email protected]>." << std::endl; |
| 50 |
} |
| 51 |
exit (1); |
| 52 |
} |
| 53 |
|
| 54 |
double timestep = DEFAULT_TIMESTEP; |
| 55 |
int maxbuffered = 0; |
| 56 |
string prefix; |
| 57 |
string suffix = ".dat"; |
| 58 |
|
| 59 |
void |
| 60 |
getargs (int rank, int argc, char* argv[]) |
| 61 |
{ |
| 62 |
opterr = 0; // handle errors ourselves |
| 63 |
while (1) |
| 64 |
{ |
| 65 |
static struct option longOptions[] = |
| 66 |
{ |
| 67 |
{"timestep", required_argument, 0, 't'}, |
| 68 |
{"maxbuffered", required_argument, 0, 'b'}, |
| 69 |
{"help", no_argument, 0, 'h'}, |
| 70 |
{0, 0, 0, 0} |
| 71 |
}; |
| 72 |
/* `getopt_long' stores the option index here. */ |
| 73 |
int option_index = 0; |
| 74 |
|
| 75 |
// the + below tells getopt_long not to reorder argv |
| 76 |
int c = getopt_long (argc, argv, "+t:b:h", |
| 77 |
longOptions, &option_index); |
| 78 |
|
| 79 |
/* detect the end of the options */ |
| 80 |
if (c == -1) |
| 81 |
break; |
| 82 |
|
| 83 |
switch (c) |
| 84 |
{ |
| 85 |
case 't': |
| 86 |
timestep = atof (optarg); // NOTE: could do error checking |
| 87 |
continue; |
| 88 |
case 'b': |
| 89 |
maxbuffered = atoi (optarg); |
| 90 |
continue; |
| 91 |
case '?': |
| 92 |
break; // ignore unknown options |
| 93 |
case 'h': |
| 94 |
usage (rank); |
| 95 |
|
| 96 |
default: |
| 97 |
abort (); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (argc < optind + 1 || argc > optind + 2) |
| 102 |
usage (rank); |
| 103 |
|
| 104 |
prefix = argv[optind]; |
| 105 |
if (argc == optind + 2) |
| 106 |
suffix = argv[optind + 1]; |
| 107 |
} |
| 108 |
|
| 109 |
int |
| 110 |
main (int argc, char *argv[]) |
| 111 |
{ |
| 112 |
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv); |
| 113 |
|
| 114 |
MPI_Comm comm = setup->communicator (); |
| 115 |
int rank; |
| 116 |
MPI_Comm_rank (comm, &rank); |
| 117 |
|
| 118 |
getargs (rank, argc, argv); |
| 119 |
|
| 120 |
MUSIC::MessageOutputPort* out = setup->publishMessageOutput ("out"); |
| 121 |
if (!out->isConnected ()) |
| 122 |
{ |
| 123 |
if (rank == 0) |
| 124 |
std::cerr << "messagesource port is not connected" << std::endl; |
| 125 |
MPI_Abort (comm, 1); |
| 126 |
} |
| 127 |
|
| 128 |
if (maxbuffered > 0) |
| 129 |
out->map (maxbuffered); |
| 130 |
else |
| 131 |
out->map (); |
| 132 |
|
| 133 |
double stoptime; |
| 134 |
setup->config ("stoptime", &stoptime); |
| 135 |
|
| 136 |
std::ostringstream messagefile; |
| 137 |
messagefile << prefix << rank << suffix; |
| 138 |
std::ifstream in (messagefile.str ().c_str ()); |
| 139 |
if (!in) |
| 140 |
{ |
| 141 |
std::cerr << "messagesource: could not open " |
| 142 |
<< messagefile.str () << std::endl; |
| 143 |
abort (); |
| 144 |
} |
| 145 |
|
| 146 |
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep); |
| 147 |
|
| 148 |
double t; |
| 149 |
char msg[80]; |
| 150 |
in >> t; |
| 151 |
in.ignore (); // ignore one whitespace character |
| 152 |
in.get (msg, 80); |
| 153 |
bool moreMessages = !in.eof (); |
| 154 |
|
| 155 |
double time = runtime->time (); |
| 156 |
while (time < stoptime) |
| 157 |
{ |
| 158 |
double nextTime = time + timestep; |
| 159 |
while (moreMessages && t < nextTime) |
| 160 |
{ |
| 161 |
out->insertMessage (t, msg, strlen (msg)); |
| 162 |
in >> t; |
| 163 |
in.ignore (); // ignore one whitespace character |
| 164 |
in.get (msg, 80); |
| 165 |
moreMessages = !in.eof (); |
| 166 |
} |
| 167 |
// Make data available for other programs |
| 168 |
runtime->tick (); |
| 169 |
|
| 170 |
time = runtime->time (); |
| 171 |
} |
| 172 |
|
| 173 |
runtime->finalize (); |
| 174 |
|
| 175 |
delete runtime; |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |