@@ -93,31 +93,42 @@ function _threadsfor(iter,lbody)
93
93
end
94
94
95
95
"""
96
- Threads.@threads
96
+ Threads.@threads [schedule] for ... end
97
97
98
- A macro to parallelize a for-loop to run with multiple threads. This spawns [`nthreads()`](@ref)
99
- number of threads, splits the iteration space amongst them, and iterates in parallel.
100
- A barrier is placed at the end of the loop which waits for all the threads to finish
101
- execution, and the loop returns.
98
+ A macro to parallelize a `for` loop to run with multiple threads. Splits the iteration
99
+ space among multiple tasks and runs those tasks on threads according to a scheduling
100
+ policy.
101
+ A barrier is placed at the end of the loop which waits for all tasks to finish
102
+ execution.
103
+
104
+ The `schedule` argument can be used to request a particular scheduling policy.
105
+ The only currently supported value is `static`, which creates one task per thread
106
+ and divides the iterations equally among them.
107
+ The default schedule (used when no `schedule` argument is present) is subject to change.
108
+
109
+ !!! compat "Julia 1.5"
110
+ The `schedule` argument is available as of Julia 1.5.
102
111
"""
103
112
macro threads (args... )
104
113
na = length (args)
105
- if na != 1
114
+ if na == 2
115
+ sched, ex = args
116
+ elseif na == 1
117
+ sched = :static
118
+ ex = args[1 ]
119
+ else
106
120
throw (ArgumentError (" wrong number of arguments in @threads" ))
107
121
end
108
- ex = args[1 ]
109
- if ! isa (ex, Expr)
110
- throw (ArgumentError (" need an expression argument to @threads" ))
122
+ if ! (isa (ex, Expr) && ex. head === :for )
123
+ throw (ArgumentError (" @threads requires a `for` loop expression" ))
111
124
end
112
- if ex. head === :for
113
- if ex. args[1 ] isa Expr && ex. args[1 ]. head === :(= )
114
- return _threadsfor (ex. args[1 ], ex. args[2 ])
115
- else
116
- throw (ArgumentError (" nested outer loops are not currently supported by @threads" ))
117
- end
118
- else
119
- throw (ArgumentError (" unrecognized argument to @threads" ))
125
+ if sched != :static
126
+ throw (ArgumentError (" unsupported schedule argument in @threads" ))
127
+ end
128
+ if ! (ex. args[1 ] isa Expr && ex. args[1 ]. head === :(= ))
129
+ throw (ArgumentError (" nested outer loops are not currently supported by @threads" ))
120
130
end
131
+ return _threadsfor (ex. args[1 ], ex. args[2 ])
121
132
end
122
133
123
134
"""
0 commit comments