Skip to content

Instantly share code, notes, and snippets.

@geetduggal
Last active August 29, 2015 13:56
Show Gist options
  • Save geetduggal/9290100 to your computer and use it in GitHub Desktop.
Save geetduggal/9290100 to your computer and use it in GitHub Desktop.
# Here is a fun language thing I made for parallel for loops. The syntax is:
# parallelFor(sequence, i, numProcs):
# do something with sequence[i]
# Which gets translated by this code:
template parallelFor*[T](data:openarray[T], i:expr, numProcs:int, body : stmt) : stmt {.immediate.} =
let numOpsPerThread = (data.len/numProcs).toInt
proc fe(j:int) {.thread.} =
for q in 0 .. numOpsPerThread-1:
let i = j*numOpsPerThread+q
body
var thr: array[0..numProcs-1, TThread[int]]
for j in 0 .. numProcs-1:
createThread(thr[j], fe, j)
joinThreads(thr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment