[pypy-commit] stmgc c7: add parallel version of n-queens

Remi Meier noreply at buildbot.pypy.org
Wed Feb 5 13:47:30 CET 2014


Author: Remi Meier
Branch: c7
Changeset: r703:852f9a4772d4
Date: 2014-02-05 13:02 +0100
http://bitbucket.org/pypy/stmgc/changeset/852f9a4772d4/

Log:	add parallel version of n-queens

diff --git a/duhton/demo/nqueens.duh b/duhton/demo/nqueens.duh
--- a/duhton/demo/nqueens.duh
+++ b/duhton/demo/nqueens.duh
@@ -3,13 +3,33 @@
 
 
 
-(setq count (container 0))
 
 (defun abs (i)
   (if (<= 0 i)
       i
     (- 0 i)))
 
+(defun clean_list (n)
+  (setq i n)
+  (setq res (list))
+  (while (> i 0)
+    (append res 0)
+    (setq i (- i 1))
+    )
+  res
+  )
+
+(defun copy_list (xs)
+  (setq res (list))
+  (setq idx 0)
+  (while (< idx (len xs))
+    (append res (get xs idx))
+    (setq idx (+ idx 1))
+    )
+  res
+  )
+
+
 (defun attacks (hist col i j)
   (|| (== (get hist j) i)
       (== (abs (- (get hist j) i))
@@ -38,11 +58,11 @@
     )
   )
 
-(defun solve (n col hist)
+(defun solve (n col hist count)
   (if (== col n)
       (progn
         (set count (+ (get count) 1))
-        (print_solution hist n)
+        ;; (print_solution hist n)
         )
 
     ;; else
@@ -57,7 +77,7 @@
       (if (>= j col)
           (progn
             (set hist col i)
-            (solve n (+ col 1) hist)
+            (solve n (+ col 1) hist count)
             ))
       
       (setq i (+ i 1))
@@ -66,20 +86,63 @@
   )
 
 
+(defun solve_parallel (n col hist count)
+  (if (== col n)
+      (progn
+        (set count (+ (get count) 1))
+        ;; (print_solution hist n)
+        )
 
-(defun clean_list (n)
-  (setq i n)
-  (setq res (list))
-  (while (> i 0)
-    (append res 0)
-    (setq i (- i 1))
+    ;; else
+    (setq i 0)
+    (setq transaction-limit 1)
+    (if (== col transaction-limit)
+        (setq counts (list)))
+    
+    (while (< i n)
+      (setq j 0)
+      (while (&& (< j col)
+                 (not (attacks hist col i j)))
+        (setq j (+ j 1))
+        )
+
+      (if (>= j col)
+          (progn
+            (set hist col i)
+            (if (== col transaction-limit)
+                (progn
+                  (setq new_cont (container 0))
+                  (append counts new_cont)
+                  (transaction solve n (+ col 1) (copy_list hist) new_cont)
+                  )
+              (solve_parallel n (+ col 1) hist count)
+              )
+            )
+        )
+      ;; iterator
+      (setq i (+ i 1))
+      )
+    
+    (if (== col transaction-limit)
+        (progn
+          (run-transactions)
+          (setq i 0)
+          (while (< i (len counts))
+            (set count (+ (get count) (get (get counts i))))
+            (setq i (+ i 1))
+            )
+          )
+      )
     )
-  res
   )
 
 
 
-(setq n 8)
-(solve n 0 (clean_list n))
+
+
+(setq count (container 0))
+
+(setq n 11)
+(solve_parallel n 0 (clean_list n) count)
 (print (quote solutions:) (get count))
 


More information about the pypy-commit mailing list