Debian Codegraph @

testsuite/sanitytests/constsource.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;
37
38 void
39 usage (int rank)
40 {
41 if (rank == 0)
42 {
43 std::cerr << "Usage: constsource [OPTION...]" << std::endl
44 << "`constsource' sends out constant values" << 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 << " -h, --help print this help message" << std::endl << std::endl
48 << "Report bugs to <[email protected]>." << std::endl;
49 }
50 exit (1);
51 }
52
53 double timestep = DEFAULT_TIMESTEP;
54 int localwidth = 1;
55
56 void
57 getargs (int rank, int argc, char* argv[])
58 {
59 opterr = 0; // handle errors ourselves
60 while (1)
61 {
62 static struct option longOptions[] =
63 {
64 {"timestep", required_argument, 0, 't'},
65 {"help", no_argument, 0, 'h'},
66 {0, 0, 0, 0}
67 };
68 /* `getopt_long' stores the option index here. */
69 int option_index = 0;
70
71 // the + below tells getopt_long not to reorder argv
72 int c = getopt_long (argc, argv, "+t:n:h",
73 longOptions, &option_index);
74
75 /* detect the end of the options */
76 if (c == -1)
77 break;
78
79 switch (c)
80 {
81 case 't':
82 timestep = atof (optarg);
83 continue;
84 case '?':
85 break; // ignore unknown options
86 case 'h':
87 usage (rank);
88
89 default:
90 abort ();
91 }
92 }
93
94 if (argc < optind + 0 || argc > optind + 0)
95 usage (rank);
96 }
97
98 int
99 main (int argc, char *argv[])
100 {
101 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
102
103 MPI_Comm comm = setup->communicator ();
104 int nProcesses;
105 MPI_Comm_size (comm, &nProcesses);
106 int rank;
107 MPI_Comm_rank (comm, &rank);
108
109 getargs (rank, argc, argv);
110
111 MUSIC::ContOutputPort* out = setup->publishContOutput ("contdata");
112 if (!out->isConnected ())
113 {
114 if (rank == 0)
115 std::cerr << "constsource port is not connected" << std::endl;
116 MPI_Abort (comm, 1);
117 }
118
119 int totalWidth = out->width ();
120 int localWidth = (totalWidth-1) / nProcesses + 1;
121 int myWidth = localWidth;
122 if (rank == nProcesses - 1) // Last processor
123 myWidth = totalWidth - (nProcesses-1) * localWidth;
124
125 dataarray = new double[myWidth];
126
127 for (int i = 0; i < myWidth; ++i)
128 dataarray[i] = rank * localWidth + i;
129
130
131 MUSIC::ArrayData dmap (dataarray,
132 MPI_DOUBLE,
133 rank * localWidth,
134 myWidth);
135
136 out->map (&dmap);
137
138
139 double stoptime;
140 setup->config ("stoptime", &stoptime);
141
142 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
143
144
145 double time = runtime->time ();
146 while (time < stoptime)
147 {
148 runtime->tick ();
149
150 time = runtime->time ();
151 }
152
153 runtime->finalize ();
154
155 delete runtime;
156
157 return 0;
158 }