如何在Julia中同时写入数组的多个索引? [英] How to write to multiple indices of an array at the same time in Julia?
本文介绍了如何在Julia中同时写入数组的多个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Julia中看到类似的内容:
I would like to see something like this working in Julia:
using Distributed
addprocs(4)
@everywhere arr = Array{Int}(undef, 10)
for i = 1:10
@spawn arr[i] = i
end
执行此操作的正确方法是什么?
What is the proper way of doing this?
推荐答案
您可以通过以下方式并行化该过程.
You have the following ways to parallelize the process.
-
线程(需要设置
JULIA_NUM_THREADS
系统变量)
arr = Array{Int}(undef, 10)
Threads.@threads for i = 1:10
arr[i] = i
end
SharedArrays
using Distributed, SharedArrays
addprocs(4)
arr = SharedVector{Int}(10)
@sync @distributed for i in 1:10
arr[i] = i
end
请注意,常见的错误是忘记将@sync
放在没有聚合器功能的@distributed
之前(请参见最后一个示例).
Note that a common error is to forget to place @sync
before @distributed
that does not have an aggregator function (see the last example).
汇总您的分布式计算结果
Aggregate the results of your distributed computation
using Distributed
addprocs(4)
arr = @distributed (append!) for i in 1:10
[i]
end
这篇关于如何在Julia中同时写入数组的多个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文