from moedebug import *
from moenp import *
from moebez import *
+from schequeue import ScheduledTask
from math import atan2, atan, sqrt
counter = 0
def _dbg(oc, s):
- dbg('OC#%04d %s' % (oc._counter, s))
+ dbg('%s %s' % (oc._desc, s))
def __init__(oc, cp, nt):
- oc._counter = OptimisedCurve.counter
+ oc._desc = 'OC#%04d' % OptimisedCurve.counter
OptimisedCurve.counter += 1
+
oc._dbg('cp= ' + ' '.join(map(vec2dbg, cp)))
db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
findcurve_epsilon = 0.01
+ oc.sched = ScheduledTask(oc._await_subproc, oc._desc)
+ oc.sched.pre_spawn()
+
cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
oc._dbg('STARTING FINDCURVE %s' % cl)
subproc = subprocess.Popen(
oc.subproc = subproc
oc.nt = nt
+ oc.sched.post_spawn()
#oc._await_subproc()
def _await_subproc(oc):
assert(subproc.returncode == 0)
oc.subproc = None
+ oc.sched.post_reap()
+
oc._result = np.reshape(findcurve_result, (-1,3), 'C')
oc._dbg(repr(oc._result))
--- /dev/null
+
+from __future__ import print_function
+
+import multiprocessing
+
+from moedebug import *
+
+class ScheduledTask():
+ avail = multiprocessing.cpu_count()
+ running = [ ]
+ factor = 0.74
+
+ @staticmethod
+ def tidy_queue():
+ ScheduledTask.running = [
+ st
+ for st in ScheduledTask.running
+ if st.running
+ ]
+
+ @staticmethod
+ def want(weight):
+ while True:
+ ScheduledTask.tidy_queue()
+ running_weight = sum(map((lambda st: st.weight), ScheduledTask.running))
+ if ScheduledTask.avail - running_weight >= weight:
+ return
+ if not ScheduledTask.running:
+ return
+ other = ScheduledTask.running[0]
+ other._dbg('run_now!')
+ other.run_now()
+ assert(not other.running)
+
+ def _dbg(st, s):
+ dbg('ST %s %s' % (st.desc, s))
+
+ def __init__(st, run_now, desc, weight=1.):
+ st.run_now = run_now
+ st.weight = weight * ScheduledTask.factor
+ st.running = False
+ st.desc = desc
+ st._dbg('create')
+
+ def pre_spawn(st):
+ st._dbg('pre_spawn')
+ assert(not st.running)
+ ScheduledTask.want(st.weight)
+
+ def post_spawn(st):
+ st._dbg('post_spawn')
+ st.running = True
+ ScheduledTask.running.append(st)
+ ScheduledTask.want(0)
+
+ def post_reap(st):
+ st._dbg('post_reap')
+ st.running = False