From e19d5bf49ae114fa925082e4987f2930ccb829b1 Mon Sep 17 00:00:00 2001 From: Tom P Date: Wed, 12 Sep 2007 00:50:55 -0400 Subject: [PATCH] reduced the hulking fn a little --- partest.py | 2 +- search.py | 42 +++++++++++++++--------------------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/partest.py b/partest.py index db8afdb..2468438 100755 --- a/partest.py +++ b/partest.py @@ -27,7 +27,7 @@ def main(Max = 32): splits = 1 else: splits = 0 - x = alphaBeta(pos, d, top = True, splits = splits) + x = alphaBeta(pos.copy(), d, top = True, splits = splits) print " ", removePV(x) print " ", algebraicForPV(x['pv']) diff --git a/search.py b/search.py index 5039245..fa49f71 100644 --- a/search.py +++ b/search.py @@ -52,7 +52,6 @@ def alphaBetaInC(pos, depth, return dict(value = value, pv = pv, nodeCount = counter.value) -# XXX Fold into parallel function def alphaBeta(pos, depth, alpha = -99999, beta = 99999, splits = 0, stopper = None, top = False, parentProc = None): a = time.time() @@ -77,9 +76,7 @@ def alphaBetaParallel(pos, depth, alpha, beta, splits, stopper, parentProc = Non return alphaBetaInC(pos, 0, alpha, beta, stopper) running = [] - results = Queue.Queue() - for k in kids: k.done = False - + results = Queue.Queue() nodeCount = 1 bestPV = [] @@ -89,10 +86,9 @@ def alphaBetaParallel(pos, depth, alpha, beta, splits, stopper, parentProc = Non kids[kids.index(favorite)] = None q.put(favorite) at_a_time = 1 - else: - at_a_time = 2 - if splits <= 0: - return alphaBetaInC(pos, depth, alpha, beta, stopper) + elif splits <= 0: + return alphaBetaInC(pos, depth, alpha, beta, stopper) + else: at_a_time = 2 for k in kids: if k is not None: q.put(k) @@ -117,14 +113,10 @@ def alphaBetaParallel(pos, depth, alpha, beta, splits, stopper, parentProc = Non running.append(proc) proc.position, proc.alpha = next, alpha proc.start() - anotherOneStarted() - except Queue.Empty: - pass + except Queue.Empty: pass try: resChild, resProc, result = results.get(True, .05) - if hasattr(resProc, 'defunct'): - continue try: rfq += 1 if splits > 0: at_a_time = 2 @@ -138,23 +130,18 @@ def alphaBetaParallel(pos, depth, alpha, beta, splits, stopper, parentProc = Non for proc in running: if proc.position == resChild: - proc.stop(); proc.defunct = True + proc.stop() running.remove(proc) if value >= beta: - alpha = beta - break + alpha = beta; break elif value > alpha: alpha = value bestPV = [pos] + result['pv'] # evaluate other pos (if there is one) with this alpha for proc in running: - # put that pos at the front of the queue - newQ = Queue.Queue() - while not q.empty(): newQ.put(q.get()) - q.put(proc.position) - while not newQ.empty(): q.put(newQ.get()) + insertAtFront(proc.position, q) except KeyboardInterrupt: for kid in running: kid.stop() raise @@ -162,10 +149,11 @@ def alphaBetaParallel(pos, depth, alpha, beta, splits, stopper, parentProc = Non for kid in running: kid.stop() return dict(value = alpha, nodeCount = nodeCount, pv = bestPV) -threadsStarted = 0 -def anotherOneStarted(): - global threadsStarted - threadsStarted += 1 - if threadsStarted % 1000 == 0: - print "********* Started %s threads" % threadsStarted + +def insertAtFront(element, queue): + newQ = Queue.Queue() + while not queue.empty(): newQ.put(queue.get()) + queue.put(element) + while not newQ.empty(): q.put(newQ.get()) + -- 2.11.4.GIT