我想在Julia中使用共享内存多线程。正如Threads。@ threads宏所做的那样,我可以使用ccall(:jl_threading_run ...)来执行此操作。尽管我的代码现在可以并行运行,但我没有达到我期望的加速。
以下代码旨在作为我所采用的方法和所遇到的性能问题的一个最小示例:[编辑:稍后请参见更多最小示例]
nthreads = Threads.nthreads()
test_size = 1000000
println("STARTED with ", nthreads, " thread(s) and test size of ", test_size, ".")
# Something to be processed:
objects = rand(test_size)
# Somewhere for our results
results = zeros(nthreads)
counts = zeros(nthreads)
# A function to do some work.
function worker_fn()
work_idx = 1
my_result = results[Threads.threadid()]
while work_idx > 0
my_result += objects[work_idx]
work_idx += nthreads
if work_idx > test_size
break
end
counts[Threads.threadid()] += 1
end
end
# Call …Run Code Online (Sandbox Code Playgroud)