summary refs log tree commit diff stats
path: root/tests/parallel/tpi.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/parallel/tpi.nim')
-rw-r--r--tests/parallel/tpi.nim26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/parallel/tpi.nim b/tests/parallel/tpi.nim
new file mode 100644
index 000000000..dcb9b8fc5
--- /dev/null
+++ b/tests/parallel/tpi.nim
@@ -0,0 +1,26 @@
+discard """
+  output: '''3.141792613595791
+3.141792613595791'''
+"""
+
+import strutils, math, threadpool
+
+proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
+
+proc piU(n: int): float =
+  var ch = newSeq[FlowVar[float]](n+1)
+  for k in 0..n:
+    ch[k] = spawn term(float(k))
+  for k in 0..n:
+    result += ^ch[k]
+
+proc piS(n: int): float =
+  var ch = newSeq[float](n+1)
+  parallel:
+    for k in 0..ch.high:
+      ch[k] = spawn term(float(k))
+  for k in 0..ch.high:
+    result += ch[k]
+
+echo formatFloat(piU(5000))
+echo formatFloat(piS(5000))