Julia中嵌套循环的并行处理

Julia中嵌套循环的并行处理,julia,Julia,我试图实现嵌套循环的并行处理。但是,我得到以下语法错误 我正在修改这里的示例() 这很有效 for N in 1:5:20, H in 1:5:20 println("The N of this iteration in $N, $H") end 这是语法错误 using Distributed @distributed for N in 1:5:20, H in 1:5:20 println("The N of th

我试图实现嵌套循环的并行处理。但是,我得到以下语法错误

我正在修改这里的示例()

这很有效

for N in 1:5:20, H in 1:5:20
           println("The N of this iteration in $N, $H")
end
这是语法错误

using Distributed

@distributed for N in 1:5:20, H in 1:5:20
           println("The N of this iteration in $N, $H")
       end


分布式
宏只支持对一个参数进行迭代。因此,您可以:

@distributed for (N,H) in collect(Iterators.product(1:5:20,1:5:20))
    println("The N of this iteration in $N, $H")
end
当然,另一种选择是使用嵌套循环。在这种情况下,只有一个循环是分布式的,另一个选择(对于单节点,多核并行化)是多线程。由于较低的开销和共享内存,这通常更容易/更有效(请注意,Julia中没有类似pythongil的东西)。 相应的Julia命令是
线程。@Threads
(在for循环前面)或
线程。@spawn
用于生成并行执行的任务

编辑:添加了示例

Threads.@threads for (N,H) in collect(Iterators.product(1:5:20,1:5:20))
    println("The N of this iteration in $N, $H")
end
第2例:

f(x) = x^2
futures = [Threads.@spawn f(x) for x=1:10]
fetch.(futures)

你能提供一个可复制的例子吗?上面增加了一些例子