# Loop Vectorization syntax very limiting

**URL:** <https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387>\
**Category:** General Usage\
**Created:** [October 25, 2023, 4:23pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387 "2023-10-25T16:23:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [October 25, 2023, 4:23pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/1 "2023-10-25T16:23:33Z")

</div>

I’m using LoopVectorization to speed up some function to great effect. However, I’m finding the syntax it allows to be quite limiting. I’m loping over a sparse matrix like this to gather an energy function. It works great and is fast, but the syntax is very ugly. Moreover, I would like to hold the sparse matrix in some struct and call everything on the containing struct.

The following works:

```julia
function getdE(gstate, gadj::SparseMatrixCSC, idx)
    efac = 0f0
    @turbo check_empty = true for ptr in nzrange(gadj, idx)
        efac += -gstate[gadj.rowval[ptr]]*gadj.nzval[ptr]
    end
    return efac
end

```

But with this I get errors:

```julia
@inline conn_range(adj::SparseMatrixCSC, idx) = nzrange(adj, idx)
@inline getweight(adj::SparseMatrixCSC, ptr) = adj.nzval[ptr]
@inline conn_idx(adj::SparseMatrixCSC, ptr) = adj.rowval[ptr]

function getdE(gstate, gadj::SparseMatrixCSC, idx)
    efac = 0f0
    @turbo check_empty = true for ptr in conn_range(gadj, idx)
        weight = getweight(gadj, ptr)
        cidx = conn_idx(gadj, ptr)
        efac += -gstate[cidx]*weight
    end
    return efac
end

```

Which normally should compile to the same code, I’m getting the warning that check args failed and it uses @inbounds @fastmath as fallback. Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 25, 2023, 4:34pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/2 "2023-10-25T16:34:52Z")

</div>

If you want to have super fast code you will always have to sacrifice some beauty and readability… In general, what you have to sacrifice is quite small when using Julia, compared to other languages.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 25, 2023, 4:43pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/3 "2023-10-25T16:43:44Z")

</div>

This specific problem is because of how LoopVectorization.jl is implemented.  
Maybe the replacement will be ready in a year, which won’t have these limitations.

---

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [October 25, 2023, 5:24pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/4 "2023-10-25T17:24:40Z")

</div>

I agree that in general, performant code will be less readable. In this case however, since I just want to inline the functions anyway, I’m basically just creating an alias which normally is completely compiled away. Couldn’t the macro carry out the inlining somehow?

---

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [October 25, 2023, 6:09pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/5 "2023-10-25T18:09:28Z")

</div>

I’ve seen you are working on LoopModels, it looks awesome, and thanks for all the effort.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 25, 2023, 6:17pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/6 "2023-10-25T18:17:47Z")

</div>

> [@f.ij](#):
>
> `efac += -gstate[cidx]*weight`

Nothing to do with Loopvectorization, but did you know that you can write `x -= a` instead of `x += -a`?

There’s also `/=` and `*=`, etc.

---

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [October 25, 2023, 6:19pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/7 "2023-10-25T18:19:52Z")

</div>

I’m aware! The full expression in my code comes from an energy function, where this term has a minus sign, so IMO it’s a bit more readable like this and, moreover, it’s not always the only term that is added (and the others might not have minus signs)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 25, 2023, 8:05pm UTC](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/8 "2023-10-25T20:05:50Z")

</div>

> [@f.ij](#):
>
> Couldn’t the macro carry out the inlining somehow?

Macros don’t work on that level. Otherwise they wouldn’t be called macros, I think. EDIT: I see this was already explained in a new thread.
