Debian Codegraph @

testsuite/sanitytests/contdelay.cc

raw
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: contdelay [OPTION...]" << std::endl
44 << "`contdelay' receives data a MUSIC input port" << std::endl
45 << "and sends them out after a delay" << 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 << " -h, --help print this help message" << std::endl << std::endl
50 << "Report bugs to <[email protected]>." << std::endl;
51 }
52 exit (1);
53 }
54
55 std::vector<MUSIC::Event> eventBuffer;
56 std::vector<MUSIC::Event> overflowBuffer;
57
58 double timestep = DEFAULT_TIMESTEP;
59 double delay = 0.0;
60 int label = -1;
61 int maxbuffered = 0;
62
63
64 void
65 getargs (int rank, int argc, char* argv[])
66 {
67 opterr = 0; // handle errors ourselves
68 while (1)
69 {
70 static struct option longOptions[] =
71 {
72 {"timestep", required_argument, 0, 't'},
73 {"delay", required_argument, 0, 'd'},
74 {"label", required_argument, 0, 'L'},
75 {"maxbuffer", required_argument, 0, 'b'},
76 {"help", no_argument, 0, 'h'},
77 {0, 0, 0, 0}
78 };
79 /* `getopt_long' stores the option index here. */
80 int option_index = 0;
81
82 // the + below tells getopt_long not to reorder argv
83 int c = getopt_long (argc, argv, "+t:d:L:b:h", longOptions, &option_index);
84
85 /* detect the end of the options */
86 if (c == -1)
87 break;
88
89 switch (c)
90 {
91 case 't':
92 timestep = atof (optarg);
93 continue;
94 case 'd':
95 delay = atof(optarg);
96 continue;
97 case 'L':
98 label = atoi(optarg);
99 continue;
100 case 'b':
101 maxbuffered = atoi(optarg);
102 continue;
103 case '?':
104 break; // ignore unknown options
105 case 'h':
106 usage (rank);
107
108 default:
109 abort ();
110 }
111 }
112
113 if (argc < optind || argc > optind)
114 usage (rank);
115 }
116
117 double inData = 0.0;
118 double outData = 0.0;
119 double auxData = 0.0;
120
121
122 int
123 main (int argc, char *argv[])
124 {
125 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
126
127 MPI_Comm comm = setup->communicator ();
128 int rank;
129 MPI_Comm_rank (comm, &rank);
130
131 getargs (rank, argc, argv);
132
133 MUSIC::ContInputPort* in = setup->publishContInput ("in");
134 if (!in->isConnected ())
135 {
136 if (rank == 0)
137 std::cerr << "contdelay input port is not connected" << std::endl;
138 MPI_Abort (comm, 1);
139 }
140
141 MUSIC::ArrayData inMap (&inData,
142 MPI_DOUBLE,
143 0,
144 1);
145 in->map (&inMap, delay, maxbuffered);
146
147
148 MUSIC::ContOutputPort* out = setup->publishContOutput ("out");
149 if (!out->isConnected ())
150 {
151 if (rank == 0)
152 std::cerr << "contdelay output port is not connected" << std::endl;
153 MPI_Abort (comm, 1);
154 }
155
156 MUSIC::ArrayData outMap (&outData,
157 MPI_DOUBLE,
158 0,
159 1);
160 out->map (&outMap, maxbuffered);
161
162
163 // Optional extra input port
164 MUSIC::ContInputPort* aux = setup->publishContInput ("aux");
165
166 if (aux->isConnected ())
167 {
168 MUSIC::ArrayData auxMap (&auxData,
169 MPI_DOUBLE,
170 0,
171 1);
172 aux->map (&auxMap, delay, maxbuffered);
173 }
174
175 double stoptime;
176 setup->config ("stoptime", &stoptime);
177
178 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
179
180 for (; runtime->time () < stoptime; runtime->tick ())
181 {
182 outData = inData + auxData;
183 }
184
185 runtime->finalize ();
186
187 delete runtime;
188
189 return 0;
190 }