Mostly using Ruby currently, with no support for real parallel compuiting unfortunately, this topic is for me one of the most interesting onces.
I recently found some nice examples for Rust, http://doc.rust-lang.org/0.11.0/guide-tasks.html including data exchange between threads and that Future concept for delayed results.
For Nimrod, I found an example at Rosetta-Code (OpenMP iterator, thread-pool and low level threads I think) but no discussion about advantages and disadvantages. And, I have not found some examples about data exchange between threads. I know there is a Channel module, and there seems to be also something like futures, but it is difficult to guess how to use that.
Interesting!
I tested it just for fun with devel branch some days ago. While the pi() example from spawn.txt already works fine, an modified example does not:
import threadpool
Type
TIntSeq = seq[int]
proc t(): TIntSeq =
result = @[1]
proc p(): int =
var a: TIntSeq
parallel:
a = spawn t()
result = 0
nimrod c --threads:on test.nim Error: internal error: (filename: compiler/lowerings.nim, line: 413)
And when we forget compiling with --threads:on
nimrod c --threads:on test.nim lib/pure/concurrency/threadpool.nim(64, 25) Error: undeclared identifier: 'fence'
Is there a general restriction for seq return types and parallel, or will this be fixed in 0.96? Or are I am doing it just wrong?
Is there a general restriction for seq return types and parallel, or will this be fixed in 0.96?
Will be fixed / implemented for 0.9.6. However it's an open question if the return type should be FlowVar[T] (which is how it needs to be implemented) or T (which makes it easier to use but requires more codegen effort) within a parallel section. For now I prefer FlowVar[T] but this is hairy for generic code.
May I ask about the state of parallel processing?
Yesterday I installed git bigbreak, but I discovered that the example above still does not compile -- something wrong with FlowVar...
From the latest spawn documentation it was my hope that it may work already.
My real goal was to make a parallel version of the Convex_Hull code from http://forum.nimrod-lang.org/t/483 -- upper and lover hull processed in parallel. But one problem seems to be that spawn does not work with procs which returns sequences. (For fixed size arrays it may work.) But for convex hull the result has dynamic size. May there exist a trick, i.e. put result on stack or heap, use references... Or will there be more support in Nim 1.0?
I believe you want something like this:
import threadpool
type
TIntSeq = seq[int]
proc t(): TIntSeq =
result = @[123,456,789]
proc p(): int =
var a: FlowVar[TIntSeq]
parallel:
a = spawn t()
result = (^a)[2]
echo p()
Thanks -- compiles and works fine with Nim git bigbreak :-)
I read about the term FlowVar and the ^ operator somewhere, but was not able to put it together. Will do more testing soon...