i create countProcessors() (CP) threads each one of them processes a chunk of
ch.len/CP size
proc mt_pi(n: int): float =
proc term(i,n:int, ch:var seq[float]) =
let
size = ch.len
chunk_sz = size div n
rfrom = i * chunk_sz
rto = if (i+1) * chunk_sz > size: size else: (i+1) * chunk_sz
for index in rfrom..<rto: # process in this thread a chunk
ch[index] = 4 * math.pow(-1, index.float) / (2*index.float + 1)
let nth = countProcessors()
var ch = newSeq[float](n+1)
parallel:
for k in 0..nth:
spawn term(k, nth, ch)
for k in 0..ch.high: result += ch[k]
Run