StaticVector Index Increase

I want to increase the static array Aat address iby x. If it were a mutable array, I would just do it A[i] += x. But since this is a StaticArray, I need to create a new one. However, if I’m a new size A, then I would do something like

A = A + @SVector [0,0,x]

and have a branch for everyone i. But in this case, SVectoris the user, so I only know in advance using type information. I would prefer that my core logic is not just a generated function for this, so I was hoping this would be a simple solution, or maybe an auxiliary function would be needed for this @generated.

Note that this problem is equivalent to creating @SVectorwith a value xin the location i, but otherwise it is zero. If there is an easy way to do this, then my problem is also resolved.

+4
source share
2 answers

It is a little difficult to get all the necessary values ​​for this problem available at compile time. Now I have:

@generated updateindex(s::SVector{L,T},j::Type{Val{I}},v) where {L,T,I} = 
  Expr(:call, :(SVector{L,T}), (ifelse(i==I, :(s[$i]+v), :(s[$i])) for i=1:L)...)

or just set the coordinate:

@generated setindex(s::SVector{L,T},j::Type{Val{I}},v) where {L,T,I} = 
  Expr(:call, :(SVector{L,T}), (ifelse(i==I, :v, :(s[$i])) for i=1:L)...)

And it can be used as:

julia> Z = @SVector [1,1,1,1,1];

julia> updateindex(Z,Val{3},4)
5-element SVector{5,Int64}:
 1
 1
 5
 1
 1

And compared as:

julia> using BenchmarkTools

julia> @btime updateindex($Z,Val{3},4);
  2.032 ns (0 allocations: 0 bytes)

Minimum Code:

julia> @code_native updateindex(Z,Val{3},4)
    .text
Filename: REPL[13]
    pushq   %rbp
    movq    %rsp, %rbp
Source line: 1
    vmovups (%rsi), %xmm0
    addq    16(%rsi), %rcx
    movq    24(%rsi), %rax
    movq    32(%rsi), %rdx
    vmovups %xmm0, (%rdi)
    movq    %rcx, 16(%rdi)
    movq    %rax, 24(%rdi)
    movq    %rdx, 32(%rdi)
    movq    %rdi, %rax
    popq    %rbp
    retq
    nopl    (%rax)

Does this solve the puzzle?

By the way, if there are ways to rewrite this in a more readable form, I will be glad to see in the comments (and update the answer accordingly).

UPDATE

Chris's comment correctly noted that a version with a non value type can be executed j:

@generated setindex(s::SVector{L,T},j,v) where {L,T} =
  Expr(:call, :(SVector{L,T}), (:(ifelse($i==j, v, s[$i])) for i=1:L)...)

- ( , , , ):

julia> setindex(Z,4,3)
5-element SVector{5,Int64}:
 1
 1
 1
 3
 1

julia> @code_native setindex(Z,4,3)
    .text
Filename: REPL[15]
    pushq   %rbp
    movq    %rsp, %rbp
Source line: 1
    cmpq    $1, %rdx
    movq    (%rsi), %r8
    cmoveq  %rcx, %r8
    cmpq    $2, %rdx
    movq    8(%rsi), %r9
    cmoveq  %rcx, %r9
    cmpq    $3, %rdx
    movq    16(%rsi), %r10
    cmoveq  %rcx, %r10
    cmpq    $4, %rdx
    movq    24(%rsi), %rax
    cmoveq  %rcx, %rax
    cmpq    $5, %rdx
    cmovneq 32(%rsi), %rcx
    movq    %r8, (%rdi)
    movq    %r9, 8(%rdi)
    movq    %r10, 16(%rdi)
    movq    %rax, 24(%rdi)
    movq    %rcx, 32(%rdi)
    movq    %rdi, %rax
    popq    %rbp
    retq
    nopw    %cs:(%rax,%rax)
+2

julia> k = 4 4

julia> @SVector [i == k? 1.0 : 0 for i in 1:5] 5-element SVector{10,Float64}: 0.0 0.0 0.0 1.0 0.0

, StaticArrays.jl README.

, Julia , :

  • , .

julia> function increment_value(A::SVector{L,T},x,k) where {L,T} _A = [i == k ? x : zero(x) for i in 1:L] A+_A end

julia> A = @SVector [0, 0, 0, 0, 10] 5-element SVector{5,Int64}: 0 0 0 0 10

julia> increment_value(A,5,2) 5-element SVector{5,Int64}: 0 5 0 0 10

, ifelse:

`julia > , StaticArrays, BenchmarkTools

julia> function increment_value(A :: SVector{L,T}, x,k) where {T,L} SVector(ntuple(i->ifelse(i == k, A[i]+x, A[i]), Val{L})) end increment_value (generic function with 1 method)

julia> a = @SVector [ 1, 2, 3, 4, 5] 5-element SVector{5,Int64}: 1 2 3 4 5

julia> @benchmark increment_value($a,$3,$5) BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0

minimum time: 3.178 ns (0.00% GC) median time: 3.285 ns (0.00% GC) mean time: 3.293 ns (0.00% GC) maximum time: 13.620 ns (0.00% GC) samples: 10000 evals/sample: 1000

+4

Source: https://habr.com/ru/post/1690084/


All Articles