Merge branch 'new_spawn' of https://github.com/Araq/Nimrod into new_spawn

This commit is contained in:
Araq 2014-06-01 22:00:06 +02:00
commit be229163af
38 changed files with 1923 additions and 159 deletions

View file

@ -0,0 +1,21 @@
discard """
outputsub: "EVEN 28"
"""
import threadpool
proc odd(a: int) = echo "ODD ", a
proc even(a: int) = echo "EVEN ", a
proc main() =
var a: array[0..30, int]
for i in low(a)..high(a): a[i] = i
parallel:
var i = 0
while i <= 29:
spawn even(a[i])
spawn odd(a[i+1])
inc i, 2
# is correct here
main()

View file

@ -0,0 +1,33 @@
discard """
output: '''0
1
2
3
4
5
6
7
8'''
sortoutput: true
"""
import threadpool
proc f(a: openArray[int]) =
for x in a: echo x
proc f(a: int) = echo a
proc main() =
var a: array[0..9, int] = [0,1,2,3,4,5,6,7,8,9]
parallel:
spawn f(a[0..2])
#spawn f(a[16..30])
var i = 3
while i <= 8:
spawn f(a[i])
spawn f(a[i+1])
inc i, 2
# is correct here
main()

View file

@ -0,0 +1,17 @@
discard """
output: '''foobarfoobarbazbearbazbear'''
cmd: "nimrod $target --threads:on $options $file"
"""
import threadpool
proc computeSomething(a, b: string): string = a & b & a & b
proc main =
let fvA = spawn computeSomething("foo", "bar")
let fvB = spawn computeSomething("baz", "bear")
echo(^fvA, ^fvB)
main()
sync()

View file

@ -0,0 +1,25 @@
discard """
output: '''3
4
5
6
7'''
sortoutput: true
"""
import threadpool, os
proc p(x: int) =
os.sleep(100 - x*10)
echo x
proc testFor(a, b: int; foo: var openArray[int]) =
parallel:
for i in max(a, 0) .. min(b, foo.high):
spawn p(foo[i])
var arr = [0, 1, 2, 3, 4, 5, 6, 7]
testFor(3, 10, arr)

View file

@ -0,0 +1,25 @@
discard """
errormsg: "can prove: i + 1 > 30"
line: 21
"""
import threadpool
proc f(a: openArray[int]) =
for x in a: echo x
proc f(a: int) = echo a
proc main() =
var a: array[0..30, int]
parallel:
spawn f(a[0..15])
spawn f(a[16..30])
var i = 0
while i <= 30:
spawn f(a[i])
spawn f(a[i+1])
inc i
#inc i # inc i, 2 would be correct here
main()

View file

@ -0,0 +1,26 @@
discard """
errormsg: "invalid usage of counter after increment"
line: 21
"""
import threadpool
proc f(a: openArray[int]) =
for x in a: echo x
proc f(a: int) = echo a
proc main() =
var a: array[0..30, int]
parallel:
spawn f(a[0..15])
spawn f(a[16..30])
var i = 0
while i <= 30:
inc i
spawn f(a[i])
inc i
#spawn f(a[i+1])
#inc i # inc i, 2 would be correct here
main()

View file

@ -0,0 +1,25 @@
discard """
errormsg: "cannot prove (i)..(i) disjoint from (i + 1)..(i + 1)"
line: 20
"""
import threadpool
proc f(a: openArray[int]) =
for x in a: echo x
proc f(a: int) = echo a
proc main() =
var a: array[0..30, int]
parallel:
#spawn f(a[0..15])
#spawn f(a[16..30])
var i = 0
while i <= 29:
spawn f(a[i])
spawn f(a[i+1])
inc i
#inc i # inc i, 2 would be correct here
main()

26
tests/parallel/tpi.nim Normal file
View file

@ -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[Promise[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))

View file

@ -4,20 +4,22 @@ discard """
cmd: "nimrod $target --threads:on $options $file"
"""
import threadpool
var
x, y = 0
proc p1 =
for i in 0 .. 1_000_000:
for i in 0 .. 10_000:
discard
inc x
atomicInc x
proc p2 =
for i in 0 .. 1_000_000:
for i in 0 .. 10_000:
discard
inc y, 2
atomicInc y, 2
for i in 0.. 3:
spawn(p1())

View file

@ -0,0 +1,9 @@
discard """
line: 9
errormsg: "'spawn' takes a call expression"
cmd: "nimrod $target --threads:on $options $file"
"""
import threadpool
let foo = spawn(1)

View file

@ -1,7 +0,0 @@
discard """
line: 7
errormsg: "'spawn' takes a call expression of type void"
cmd: "nimrod $target --threads:on $options $file"
"""
spawn(1)

View file

@ -46,7 +46,7 @@ type
msg*: string
ccodeCheck*: string
err*: TResultEnum
substr*: bool
substr*, sortoutput*: bool
targets*: set[TTarget]
const
@ -113,6 +113,8 @@ proc parseSpec*(filename: string): TSpec =
result.action = actionRun
result.outp = e.value
result.substr = true
of "sortoutput":
result.sortoutput = parseCfgBool(e.value)
of "exitcode":
discard parseInt(e.value, result.exitCode)
of "msg":

View file

@ -11,7 +11,8 @@
import
parseutils, strutils, pegs, os, osproc, streams, parsecfg, json,
marshal, backend, parseopt, specs, htmlgen, browsers, terminal
marshal, backend, parseopt, specs, htmlgen, browsers, terminal,
algorithm
const
resultsFile = "testresults.html"
@ -150,6 +151,11 @@ proc codegenCheck(test: TTest, check: string, given: var TSpec) =
except EIO:
given.err = reCodeNotFound
proc makeDeterministic(s: string): string =
var x = splitLines(s)
sort(x, system.cmp)
result = join(x, "\n")
proc testSpec(r: var TResults, test: TTest) =
# major entry point for a single test
let tname = test.name.addFileExt(".nim")
@ -191,8 +197,10 @@ proc testSpec(r: var TResults, test: TTest) =
r.addResult(test, "exitcode: " & $expected.exitCode,
"exitcode: " & $exitCode, reExitCodesDiffer)
else:
if strip(buf.string) != strip(expected.outp):
if not (expected.substr and expected.outp in buf.string):
var bufB = strip(buf.string)
if expected.sortoutput: bufB = makeDeterministic(bufB)
if bufB != strip(expected.outp):
if not (expected.substr and expected.outp in bufB):
given.err = reOutputsDiffer
if given.err == reSuccess:
codeGenCheck(test, expected.ccodeCheck, given)