Debian Codegraph @

utils/contsink.cc

raw
1 #include <mpi.h>
2 #include <music.hh>
3 #include <iostream>
4 #include <sstream>
5 #include <string>
6 #include <cstdlib>
7
8 extern "C" {
9 #include <unistd.h>
10 #include <getopt.h>
11 }
12
13 #define DEFAULT_TIMESTEP 1e-2
14
15 double *data;
16
17
18 void
19 usage (int rank)
20 {
21 if (rank == 0)
22 {
23 std::cerr << "Usage: contsink [OPTION...]" << std::endl
24 << "`contsink' receives continuous data" << std::endl
25 << "through a MUSIC input port." << std::endl << std:: endl
26 << " -t, --timestep TIMESTEP time between tick() calls (default " << DEFAULT_TIMESTEP << " s)" << std::endl
27 << " -d, --delay SECS delay until data should arrive" << std::endl
28 << " -i, --interpolate no interpolation" << std::endl
29 << " -h, --help print this help message" << std::endl << std::endl
30 << "Report bugs to <[email protected]>." << std::endl;
31 }
32 exit (1);
33 }
34
35 double timestep = DEFAULT_TIMESTEP;
36 double delay = 0.0;
37 bool interpolate = true;
38
39 void
40 getargs (int rank, int argc, char* argv[])
41 {
42 opterr = 0; // handle errors ourselves
43 while (1)
44 {
45 static struct option longOptions[] =
46 {
47 {"timestep", required_argument, 0, 't'},
48 {"delay", required_argument, 0, 'd'},
49 {"interpolate", no_argument, 0, 'i'},
50 {"help", no_argument, 0, 'h'},
51 {0, 0, 0, 0}
52 };
53 /* `getopt_long' stores the option index here. */
54 int option_index = 0;
55
56 // the + below tells getopt_long not to reorder argv
57 int c = getopt_long (argc, argv, "+t:d:ih",
58 longOptions, &option_index);
59
60 /* detect the end of the options */
61 if (c == -1)
62 break;
63
64 switch (c)
65 {
66 case 't':
67 timestep = atof (optarg);
68 continue;
69 case 'd':
70 delay = atof (optarg);
71 continue;
72 case 'i':
73 interpolate = false;
74 continue;
75 case '?':
76 break; // ignore unknown options
77 case 'h':
78 usage (rank);
79
80 default:
81 abort ();
82 }
83 }
84
85 if (argc < optind + 0 || argc > optind + 0)
86 usage (rank);
87 }
88
89 int
90 main (int argc, char* argv[])
91 {
92 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
93
94 MUSIC::ContInputPort* contdata = setup->publishContInput ("contdata");
95
96 MPI_Comm comm = setup->communicator ();
97 int nProcesses;
98 MPI_Comm_size (comm, &nProcesses);
99 int rank;
100 MPI_Comm_rank (comm, &rank);
101
102 getargs (rank, argc, argv);
103
104 int totalWidth = contdata->width ();
105 int localWidth = (totalWidth-1) / nProcesses + 1;
106 int myWidth = localWidth;
107 if (rank == nProcesses - 1) // Last processor
108 myWidth = totalWidth - (nProcesses-1) * localWidth;
109
110 data = new double[myWidth];
111
112 // Declare where in memory to put data
113 MUSIC::ArrayData dmap (data,
114 MPI_DOUBLE,
115 rank * localWidth,
116 myWidth);
117 contdata->map (&dmap, delay, interpolate);
118
119 double stoptime;
120 setup->config ("stoptime", &stoptime);
121
122 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, timestep);
123
124
125 for (; runtime->time () < stoptime; runtime->tick ())
126 {
127 for (int i = 0; i < myWidth; ++i)
128 std::cout << data[i] << " ";
129 std::cout << "on " << rank << " @" << runtime->time () << std::endl;
130 }
131
132 runtime->finalize ();
133
134 delete runtime;
135
136 return 0;
137 }