import os,strutils
    proc fib(n:int):int =
      if n < 2: n
      else:
        fib(n-1) + fib(n-2)
    
    when defined(asyncthreadpool):
      const threadtype = "asyncthreadpool"
      import asyncthreadpool,asyncdispatch
      let t = newThreadPool()
      template mySpawn(x:untyped):untyped = t.spawn x
      template mySync(x:untyped):untyped = waitFor x
    elif defined(taskpools):
      const threadtype = "taskpools"
      import taskpools,cpuinfo
      let t = Taskpool.new(countProcessors()*2)
      template mySpawn(x:untyped):untyped = t.spawn x
      template mySync(x:untyped):untyped = sync x
    elif defined(weave):
      const threadtype = "weave"
      import weave
      init(Weave)
      template mySpawn(x:untyped):untyped = spawn x
      template mySync(x:untyped):untyped = sync x
    elif defined(stdlib):
      const threadtype = "stdlib"
      template mySpawn(x:untyped):untyped = spawn x
      template mySync(x:untyped):untyped = ^x
    else:
      const threadtype = "serial"
      template mySpawn(x:untyped):untyped = x
      template mySync(x:untyped):untyped = x
    
    
    proc fib2(n:int):int =
      if n > 20:
        let
          n1 = mySpawn fib2(n-1)
          n2 = fib2(n-2)
        mySync(n1) + n2
      else:
        fib(n)
    
    
    proc main() =
      let n = commandLineParams()[0].parseInt
      echo threadtype," fib(",n,") is",fib2(n)
      when defined(weave):
        exit(Weave)
    main()
    
    
    Run

note that i've changed your example slightly so that tasks themselves spawn 
other tasks recursively.

results (nim 1.6.8 -d:release -d:lto --threads:on)
    

  * serial: 2.75 s
  * asyncthreadpool: i could get it to work for your original example, but not 
for the recursive spawning, i get "ValueError: no handles or timers registered 
in dispatch" or "OSError: Too many open files" i'm sure theres some solution 
involving `newDispatcher` but life is too short
  * stdlib:blocks forever for n >32ish, but not always. using `preferSpawn` 
helps a bit but not really
  * taskpools: 1.5s
  * weave: 1.2s


on devel:
    

  * fib2 needs to be marked {.gcsafe.}


    

  * serial 7.0s
  * taskpools 3.6s
  * weave 3.6s
  * using `--mm:refc` gives the previous speeds


Reply via email to