276
276
threadmap(f, A)
277
277
threadmap(f, A, B)
278
278
279
- Simple version of `map` using a `Threads.@threads` loop;
280
- only for vectors & really at most two of them, of nonzero length,
279
+ Simple version of `map` using a `Threads.@threads` loop,
280
+ or `Threads.@spawn` on Julia >= 1.3.
281
+
282
+ Only for vectors & really at most two of them, of nonzero length,
281
283
with all outputs having the same type.
282
284
"""
283
285
function threadmap (f:: Function , vw:: AbstractVector... )
@@ -286,14 +288,32 @@ function threadmap(f::Function, vw::AbstractVector...)
286
288
out1 = f (first .(vw)... )
287
289
_threadmap (out1, f, vw... )
288
290
end
289
- # NB barrier
290
- function _threadmap (out1, f, vw... )
291
- out = Vector {typeof(out1)} (undef, length (first (vw)))
292
- out[1 ] = out1
293
- Threads. @threads for i= 2 : length (first (vw))
294
- @inbounds out[i] = f (getindex .(vw, i)... )
291
+ # NB function barrier. Plus two versions:
292
+ @static if VERSION >= v " 1.3"
293
+
294
+ function _threadmap (out1, f, vw... )
295
+ out = Vector {typeof(out1)} (undef, length (first (vw)))
296
+ out[1 ] = out1
297
+ Threads. @threads for i in 2 : length (first (vw))
298
+ @inbounds out[i] = f (getindex .(vw, i)... )
299
+ end
300
+ out
301
+ end
302
+
303
+ else
304
+
305
+ function _threadmap (out1, f, vw... )
306
+ ell = length (first (vw))
307
+ out = Vector {typeof(out1)} (undef, ell)
308
+ out[1 ] = out1
309
+ Base. @sync for is in Iterators. partition (2 : ell, div (ell, Threads. nthreads ()))
310
+ Threads. @spawn for i in is
311
+ @inbounds out[i] = f (getindex .(vw, i)... )
312
+ end
313
+ end
314
+ out
295
315
end
296
- out
316
+
297
317
end
298
318
299
319
# Collect generators to allow indexing
0 commit comments