I have copied the example (computing Pi) from the experimental features document, but I have replaced a simple float variable by an object which contains a seq[float] variable, since this comes closer to my real application.
I don't understand the error message
Error: type mismatch: got 'FlowVar[seq[float]]' for 'spawn term(k), nimCreateFlowVar' but expected 'seq[float]'
Here is the code
# nim c --threads:on -r ParPiObj.nim
type
Coeff = object
c : seq[float]
# Compute PI in an inefficient way
import strutils, math, threadpool
# {.experimental: "parallel".}
proc term(k: int): seq[float] =
result= @[(if (k and 1) == 0 : 1.0 else: -1.0) * 4.0/(float(k+k) + 1.0)]
proc pi(n: int): float =
var ch = newSeq[Coeff](n+1)
{.push experimental: "parallel".}
parallel:
for k in 0..ch.high:
ch[k].c = spawn term(k)
#[ gives Error: type mismatch: got 'FlowVar[seq[float]]'
for 'spawn term(k), nimCreateFlowVar' but expected 'seq[float]'
]#
for k in 0..ch.high:
result += ch[k].c
{.pop.}
echo formatFloat(pi(5000))
Many thanks for some hints or pointers to additional documentation, Helmut
Many, many thanks for this in in-detail explanation. Please add (something like) this to the documentation of the parallel clause. I myself have to digest the FlowVar construct first.
Is there a typo in your third version? It doesn't compile here (development Nim) The line in error is following one
ch[k] = spawn term(k) # cannot create a flowVar of type: Coeff