# Non-temporal memory IO

**URL:** <https://discourse.julialang.org/t/non-temporal-memory-io/20605>\
**Category:** General Usage\
**Created:** [February 9, 2019, 4:01pm UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605 "2019-02-09T16:01:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [February 9, 2019, 4:01pm UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/1 "2019-02-09T16:01:30Z")

</div>

I have just recently learned about non-temporal memory reading and writing (reading [this](https://lwn.net/Articles/250967/)), and I’m trying to experiment with it. I already have a basic merge-sort algorithm implemented using SIMD.jl. How could I replace the `vload` and `vstore` functions with a non-temporal version using intrinsics directly? Or even better, would it be simple to add this as a feature in that library? I might try to do it if it seems like a good idea.

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [February 9, 2019, 7:14pm UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/2 "2019-02-09T19:14:01Z")

</div>

Here is my first experiment with SIMD.jl, comments are appreciated. [SIMD based merge sort in Julia · GitHub](https://gist.github.com/nlw0/a684f82c5c3c0f0c725f9e853e5a2fc7)

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [February 9, 2019, 9:37pm UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/3 "2019-02-09T21:37:53Z")

</div>

I tried to pass a nontemporal argument to `load` and `store` using `llvmcall`, following the code from SIMD.jl as a reference. The function works, except I never get a non-temporal instruction on the machine code, and even on the LLVM code the flag vanishes. What could be the reason for that?

```julia
function vstorent(x::Vec{4,Float64}, aa::Ptr{Float64})
    Base.llvmcall("
            %ptr = inttoptr i64 %1 to <4 x double>*
            store <4 x double> %0, <4 x double>* %ptr, align 8, !nontemporal !{ i32 1 }
            ret void"
                  , Cvoid, Tuple{NTuple{4,VecElement{Float64}}, Ptr{Float64}}, x.elts, aa)
end
qq = randn(100);
@code_llvm vstorent(Vec((100.0,100.0,100.0,100.0)), pointer(qq, 1))

; @ REPL[17]:2 within `vstorent'
define void @julia_vstorent_12380({ <4 x double> } addrspace(11)* nocapture nonnull readonly dereferenceable(32), i64) {
top:
; ┌ @ sysimg.jl:18 within `getproperty'
   %2 = getelementptr inbounds { <4 x double> }, { <4 x double> } addrspace(11)* %0, i64 0, i32 0
; └
  %3 = load <4 x double>, <4 x double> addrspace(11)* %2, align 16
  %ptr.i = inttoptr i64 %1 to <4 x double>*
  store <4 x double> %3, <4 x double>* %ptr.i, align 8
  ret void
}

```

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [February 11, 2019, 8:00am UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/4 "2019-02-11T08:00:12Z")

</div>

This ended up working. Either rebuilding Julia, updating LLVM or just using the -C flag did it. Even created this PR [Added support for non-temporal memory writing by nlw0 · Pull Request #46 · eschnett/SIMD.jl · GitHub](https://github.com/eschnett/SIMD.jl/pull/46)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 12, 2019, 6:20am UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/5 "2019-02-12T06:20:33Z")

</div>

Thanks a lot for posting this! I’ve been wanting to try it for sometimes.

I tried SIMD.jl master and your benchmark and I could reproduce the speedup. But it looks like `@code_llvm` output still hides `!nontemporal` metadata? Did you figure out why? I’m wondering if it could be a bug in IR printer or something.

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [February 12, 2019, 8:08am UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/6 "2019-02-12T08:08:44Z")

</div>

Yes, I noticed the same thing: it’s on @code\_native but not on @code\_llvm! I have no clue, I didn’t get into how any of that works…

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 13, 2019, 4:35am UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/7 "2019-02-13T04:35:13Z")

</div>

I posted an issue just in case it’s a bug [https://github.com/JuliaLang/julia/issues/31056](https://github.com/JuliaLang/julia/issues/31056)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 13, 2019, 7:40am UTC](https://discourse.julialang.org/t/non-temporal-memory-io/20605/8 "2019-02-13T07:40:10Z")

</div>

Oops. The answer was to use `@code_llvm raw=true ...`: [https://github.com/JuliaLang/julia/issues/31056#issuecomment-463083357](https://github.com/JuliaLang/julia/issues/31056#issuecomment-463083357)
