# Segfault with SIMD.jl and BenchmarkTools.jl

**URL:** https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352
**Category:** Performance
**Tags:** package
**Created:** [August 9, 2019, 1:31pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352 "2019-08-09T13:31:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [August 9, 2019, 1:31pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/1 "2019-08-09T13:31:51Z")

</div>

Hi,  
I try to learn how to use SIMD.jl and the following snippet produces a segmentation fault when I use @btime.

```julia
using BenchmarkTools
using SIMD

const SBS=8

function microaxpy_simd!(ys::Vector{T}, a::T, xs::Vector{T}) where {T}
    @assert length(ys) == length(xs)
    lane = VecRange{SBS}(0)
    ys[lane+1] += a*xs[lane+1]
end

function test()
   x=ones(SBS)
   y=ones(SBS)
   a=2.0

   microaxpy_simd!(y,a,x) #Seems OK
   @show y
   # @btime microaxpy_simd!($y,$a,$x) #produce s segfault
end

```

Any hints ?

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [August 9, 2019, 2:24pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/2 "2019-08-09T14:24:25Z")

</div>

> [@LaurentPlagne](#):
>
> lane = VecRange{SBS}(0)

Shouldn’t this be `VecRange{SBS}(1)`, in accordance with

> <https://github.com/eschnett/SIMD.jl/blob/6497df2700ecb48d5bbf8bd49ddedf9fc03ed629/src/SIMD.jl#L1845-L1858>

?

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [August 9, 2019, 3:10pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/3 "2019-08-09T15:10:04Z")

</div>

Thank you for the suggestion but when I try

```julia
lane = VecRange{SBS}(1)
ys[lane+0] += a*xs[lane+0]

```

I have the same segfault crash as with

```julia
lane = VecRange{SBS}(0)
ys[lane+1] += a*xs[lane+1]

```

which I copied from the SIMD.jl markdown README (I could not find a more detailed doc).  
What is strange is that if you try the top MWE, the result `y` looks fine (`[3.0,...3.0]`).

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [August 9, 2019, 7:56pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/4 "2019-08-09T19:56:45Z")

</div>

Ah OK, yeah. I think it might have to do with returning the `Vec{8, Float64}` from `microaxpy_simd!`. If you return `ys` or `nothing`, it’s fine. Not sure why.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [August 9, 2019, 10:16pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/5 "2019-08-09T22:16:58Z")

</div>

Argh, not the first time that I forget that returning nothing must be explicit 😊

Thank you very much for your help !

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [August 9, 2019, 11:49pm UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/6 "2019-08-09T23:49:06Z")

</div>

Still, if anyone knows why returning a `SIMD.Vec` (a glorified `NTuple` of `Core.VecElement`s) causes a segfault, I’d like to know.

---

<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: [August 10, 2019, 1:59am UTC](https://discourse.julialang.org/t/segfault-with-simd-jl-and-benchmarktools-jl/27352/7 "2019-08-10T01:59:08Z")

</div>

[Here](https://github.com/JuliaLang/julia/issues/30426) is a GitHub issue (from last December).

If you return a `NTuple{W,Core.VecElement{T}}` wrapped in a struct (eg, a `SIMD.Vec` or a tuple), you’re likely to get a segfault.
