Add specific elements of a CUDA matrix

You can take the sum of a view with indices:

function fitness(adjacencyMatrix::Union{Matrix{T}, CuArray},
                 position::Union{Vector, CuArray}) where T <: Real
    head = view(position, 1:length(position)-1)
    tail = view(position, 2:length(position))
    idx = CartesianIndex.(head, tail)
    sum(view(adjacencyMatrix, idx))
end

That’s slightly faster :slight_smile: 100us instead of 270ms on my system.

4 Likes