Debian Codegraph @

testsuite/sanitytests/clocksource.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 <string>
25 #include <cstdlib>
26
27 extern "C" {
28 #include <unistd.h>
29 #include <getopt.h>
30 }
31
32 #include <music.hh>
33
34 const double DEFAULT_TIMESTEP = 1e-2;
35
36 double dataarray[1];
37
38 void
39 usage (int rank)
40 {
41 if (rank == 0)
42 {
43 std::cerr << "Usage: clocksource [OPTION...]" << std::endl
44 << "`clocksource' sends out the current time" << std::endl
45 << "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
57
58 void
59 getargs (int rank, int argc, char* argv[])
60 {
61 opterr = 0; // handle errors ourselves
62 while (1)
63 {
64 static struct option longOptions[] =
65 {
66 {"timestep", required_argument, 0, 't'},
67 {"maxbuffered", required_argument, 0, 'b'},
68 {"help", no_argument, 0, 'h'},
69 {0, 0, 0, 0}
70 };
71 /* `getopt_long' stores the option index here. */
72 int option_index = 0;
73
74 // the + below tells getopt_long not to reorder argv
75 int c = getopt_long (argc, argv, "+t:b:h",
76 longOptions, &option_index);
77
78 /* detect the end of the options */
79 if (c == -1)
80 break;
81
82 switch (c)
83 {
84 case 't':
85 timestep = atof (optarg);
86 continue;
87 case 'b':
88 maxbuffered = atoi (optarg);
89 continue;
90 case '?':
91 break; // ignore unknown options
92 case 'h':
93 usage (rank);
94
95 default:
96 abort ();
97 }
98 }
99
100 if (argc < optind + 0 || argc > optind + 0)
101 usage (rank);
102 }
103
104 int
105 main (int argc, char *argv[])
106 {
107 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
108
109 MPI_Comm comm = setup->communicator ();
110 int rank;
111 MPI_Comm_rank (comm, &rank);
112
113 getargs (rank, argc, argv);
114
115 MUSIC::ContOutputPort* out = setup->publishContOutput ("clock");
116 if (!out->isConnected ())
117 {
118 if (rank == 0)
119 std::cerr << "clocksource port is not connected" << std::endl;
120 MPI_Abort (comm, 1);
121 }
122
123
124 MUSIC::ArrayData dmap (dataarray,
125 MPI_DOUBLE,
126 0,
127 1);
128
129 if (maxbuffered > 0)
130 out->map (&dmap, maxbuffered);
131 else
132 out->map (&dmap);
133
134
135 double stoptime;
136 setup->config ("stoptime", &stoptime);
137
138 dataarray[0] = 0.0;
139
140 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
141
142
143 for (; runtime->time () < stoptime; runtime->tick ())
144 {
145 // Use current time (valid at next tick) at exported data
146 dataarray[0] = runtime->time () + timestep;
147 }
148
149 runtime->finalize ();
150
151 delete runtime;
152
153 return 0;
154 }