Debian Codegraph @

examples/waveproducer.cc

raw
1 #include <mpi.h>
2 #include <cstdlib>
3 #include <cmath>
4 #include <music.hh>
5
6 #define TIMESTEP 0.001
7
8 MPI_Comm comm;
9 double* data;
10
11 int
12 main (int argc, char* argv[])
13 {
14 MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
15
16 int width = atoi (argv[1]); // command line arg gives width
17
18 MUSIC::ContOutputPort* wavedata =
19 setup->publishContOutput ("wavedata");
20
21 comm = setup->communicator ();
22 int nProcesses;
23 MPI_Comm_size (comm, &nProcesses); // how many processes are there?
24 int rank;
25 MPI_Comm_rank (comm, &rank); // which process am I?
26
27 // For clarity, assume that width is a multiple of n_processes
28 int nLocalVars = width / nProcesses;
29 data = new double[nLocalVars];
30 for (int i = 0; i < nLocalVars; ++i)
31 data[i] = 0.0;
32
33 // Declare what data we have to export
34 MUSIC::ArrayData dmap (data,
35 MPI_DOUBLE,
36 rank * nLocalVars,
37 nLocalVars);
38 wavedata->map (&dmap);
39
40 double stoptime;
41 setup->config ("stoptime", &stoptime);
42
43 MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, TIMESTEP);
44
45 for (; runtime->time () < stoptime; runtime->tick ())
46 {
47 if (rank == 0)
48 {
49 // Generate original data on master node
50 int i;
51 double time = runtime->time ();
52
53 for (i = 0; i < nLocalVars; ++i)
54 data[i] = sin (2 * M_PI * time * i);
55 }
56
57 // Broadcast these data out to all nodes
58 MPI_Bcast (data, nLocalVars, MPI_DOUBLE, 0, comm);
59 }
60
61 runtime->finalize ();
62
63 delete runtime;
64
65 return 0;
66 }