Debian Codegraph @

pymusic/examples/testsource.py

raw
1 #!/usr/bin/env python3
2
3 import music
4 from itertools import groupby, takewhile
5 import sys
6
7 setup = music.Setup()
8 stoptime = setup.config("stoptime")
9 timestep = setup.config("timestep")
10 buf = setup.config("buffer")
11 events = setup.config("events")
12
13 comm = setup.comm
14 rank = comm.Get_rank()
15 size = comm.Get_size()
16
17 out = setup.publishEventOutput("out")
18
19 width = out.width()
20 local = width // size
21 rest = width % size
22 firstId = rank * local
23
24 # distribute the rest:
25 firstId += min(rank, rest)
26 if rank < rest: local += 1
27
28 index = music.Index.LOCAL
29
30 if index == music.Index.LOCAL:
31 def mapout(i): return i
32 else:
33 def mapout(i): return i + firstId
34
35 out.map(index,
36 base=firstId,
37 size=local,
38 maxBuffered=buf)
39
40 eventgen = ((mapout(i % local), i * stoptime / events)
41 for i in range(events)) # index, time
42 steps = groupby(eventgen, lambda i_t: int(i_t[1]/timestep))
43 def step(): return next(steps, (None, None))
44
45 runtime = setup.runtime(timestep)
46 times = takewhile(lambda t: t < stoptime, runtime)
47 nextStep, nextEvents = step()
48 for t in times:
49 if int(t/timestep) != nextStep: continue
50
51 for i, when in nextEvents:
52 sys.stderr.write("Insert rank {}: val={}, type={}, ind={}\n".
53 format(rank, when, i, music.Index.tostr(index)))
54 out.insertEvent(when, i, index)
55
56 nextStep, nextEvents = step()
57
58 runtime.finalize()