Last active
August 29, 2015 13:56
-
-
Save geetduggal/9290100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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