Define a simple function copying the contents of one array to another:
function copy_array!(array_t::AbstractArray, array_s::AbstractArray)
array_t .= array_s
return nothing
end
The following function performs the above function with and without @spawn
and compares the results:
using Base.Threads: @spawn
using LinearAlgebra: norm
function test_spawned_copy()
N = 1000
A = rand(N);
B = zeros(N);
C = zeros(N);
copy_array!(B, A)
@spawn copy_array!(C, A)
println("‖A‖ = $(norm(A))\n‖B‖ = $(norm(B))\n‖C‖ = $(norm(C))")
end
Executing the above function, we realize that copy_array!
is not effective with @spawn
:
julia> test_spawned_copy()
‖A‖ = 18.176417571506622
‖B‖ = 18.176417571506622
‖C‖ = 0.0 # assignment in array C didn't occur
However, if the contents of the above test function are executed in REPL, copy_array!
produces the same results with and without @spawn
:
julia> N = 1000;
julia> A = rand(N);
julia> B = zeros(N);
julia> C = zeros(N);
julia> copy_array!(B, A);
julia> @spawn copy_array!(C, A);
julia> println("‖A‖ = $(norm(A))\n‖B‖ = $(norm(B))\n‖C‖ = $(norm(C))");
‖A‖ = 18.054455128272014
‖B‖ = 18.054455128272014
‖C‖ = 18.054455128272014 # assignment in array C did occur
Is this an expected result? Here is the Julia version:
julia> VERSION
v"1.5.4-pre.0"