Behavior of threads

Something like this:

using ChunkSplitters

struct A{T}
    output_big_array::T
end
# some function that operates on the array contained in a, given
# a parameter x and an input array v
function (a::A)(x,v) 
    a.output_big_array .= a.output_big_array .+ x * v
    return a
end

# function that updates the array contained in A
function f!(input_big_array, a) 
    a(2.0, input_big_array)
end

input_big_array = ones(1000)
output_big_array = zeros(1000)

function foo(input_big_array, output_big_array; nchunks = Threads.nthreads())
    # create a structure for each chunk, with a slice of the array; r is a range
    a_chunks = [ A(@view(output_big_array[r])) for (r,_) in chunks(output_big_array,nchunks) ]
    Threads.@threads for (r, ichunk) in chunks(input_big_array, nchunks)
        f!(@view(input_big_array[r]), a_chunks[ichunk])
    end 
    # create the final structure
    a = A(output_big_array)
    return a
end

1 Like