Skip to content

Commit

Permalink
Openmp parallel iterator improvements (#9493)
Browse files Browse the repository at this point in the history
* More flexibility in OpenMP pragma
* Use static to constrain to compile-time annotation string
* Update changelog with OpenMP change
  • Loading branch information
mratsim authored and Araq committed Oct 25, 2018
1 parent c844a91 commit 5b97762
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
use `editdistance.editDistance` or `editdistance.editDistanceAscii`
instead.

- The OpenMP parallel iterator \``||`\` now supports any `#pragma omp directives`
and not just `#pragma omp parallel for`. See [OpenMP documentation](https://www.openmp.org/wp-content/uploads/OpenMP-4.5-1115-CPP-web.pdf).

The default annotation is `parallel for`, if you used OpenMP without annotation
the change is transparent, if you used annotations you will have to prefix
your previous annotations with `parallel for`.

#### Breaking changes in the standard library

Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ proc genParForStmt(p: BProc, t: PNode) =
initLocExpr(p, call.sons[1], rangeA)
initLocExpr(p, call.sons[2], rangeB)

lineF(p, cpsStmts, "#pragma omp parallel for $4$n" &
lineF(p, cpsStmts, "#pragma omp $4$n" &
"for ($1 = $2; $1 <= $3; ++$1)",
[forLoopVar.loc.rdLoc,
rangeA.rdLoc, rangeB.rdLoc,
Expand Down
5 changes: 0 additions & 5 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2041,11 +2041,6 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result = c.graph.emptyNode
of mOmpParFor:
checkMinSonsLen(n, 3, c.config)
if n.sonsLen == 4:
let annotationStr = getConstExpr(c.module, semExpr(c, n[^1]), c.graph)
if annotationStr == nil or annotationStr.kind notin nkStrKinds:
localError(c.config, result[^1].info,
"The annotation string for `||` must be known at compile time")
result = semDirectOp(c, n, flags)
else:
result = semDirectOp(c, n, flags)
Expand Down
8 changes: 6 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2186,10 +2186,14 @@ else:
inc(res)


iterator `||`*[S, T](a: S, b: T, annotation=""): T {.
iterator `||`*[S, T](a: S, b: T, annotation: static string = "parallel for"): T {.
inline, magic: "OmpParFor", sideEffect.} =
## parallel loop iterator. Same as `..` but the loop may run in parallel.
## OpenMP parallel loop iterator. Same as `..` but the loop may run in parallel.
## `annotation` is an additional annotation for the code generator to use.
## The default annotation is `parallel for`.
## Please refer to the `OpenMP Syntax Reference<https://www.openmp.org/wp-content/uploads/OpenMP-4.5-1115-CPP-web.pdf>`_
## for further information.
##
## Note that the compiler maps that to
## the ``#pragma omp parallel for`` construct of `OpenMP`:idx: and as
## such isn't aware of the parallelism in your code! Be careful! Later
Expand Down

0 comments on commit 5b97762

Please sign in to comment.