# Julia: How to set and use CUDA/Host Global memory

**URL:** <https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247>\
**Category:** GPU\
**Created:** [January 8, 2022, 4:48pm UTC](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247 "2022-01-08T16:48:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johntrinder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johntrinder/32/31783_2.png) [@johntrinder](https://discourse.julialang.org/u/johntrinder)\
**Post date:** [January 8, 2022, 4:48pm UTC](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247/1 "2022-01-08T16:48:15Z")

</div>

I want to poll a global flag inside of a kernel in CUDA so as to shut down the kernel gracefully via the Host.

1. How do I set up a Global flag in the host (I have tried abort=CuArray([false]) )
2. Do I pass ‘abort’ as an arg to the kernel function? and can I check it’s value there?
3. While the kernel is running (takes about 3 seconds) without sync: after 0.5 sec I set ‘abort’ to true in the host-side, but this has no affect - the kernel carries on running to the 3 second completion.

`a = cu([0]; unified=true)`

```julia
function kernel(b)
    b[1] > 0 && (@cuprintln("ABORTED"); return nothing)
    for _ in 1:200000
        a = sqrt(2)
    end
    return nothing
end

```

```julia
println("START")
fill!(a, 0)
@cuda threads=16 blocks=1 kernel(a)
fill!(a, 1) # has no effect on the kernel
println("DONE")

```

I’m doing something basically wrong here. Will someone put me right please?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 10, 2022, 6:40am UTC](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247/2 "2022-01-10T06:40:17Z")

</div>

That won’t work like that; the `fill!` uses an API call which is executed in-order, and thus waits for the kernel to complete. Either perform that call on a different stream (using `CUDA.stream!`, or from a different task), or use an allocation that doesn’t require. Typically that’s a device-mapped host allocation, using `Mem.alloc(Mem.Host, sizeof(Int), Mem.HOSTALLOC_DEVICEMAP)` (then wrapped to an `Array` using `unsafe_wrap`). See the `exception_flag` in CUDA.jl for an example use of this.

---

<div class="post-metadata">

**Author:** ![johntrinder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johntrinder/32/31783_2.png) [@johntrinder](https://discourse.julialang.org/u/johntrinder)\
**Post date:** [January 10, 2022, 5:38pm UTC](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247/3 "2022-01-10T17:38:35Z")

</div>

Thanks for that.  
I’m a newbie at CUDA.jl although I read up on CUDA several years ago.  
How would I use CUDA.stream! ?  
Would it be  
`CUDA.stream!(CuStream()) do; fill!(a,1);end`

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 10, 2022, 5:42pm UTC](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/74247/4 "2022-01-10T17:42:35Z")

</div>

Something like that should work, yes. You can also use tasks as shown here: [CUDA.jl 3.0 ⋅ JuliaGPU](https://juliagpu.org/post/2021-04-09-cuda_3.0/)

But if you just want to set a single flag, using a device-mapped host allocation is probably a better choice (also guarantees that the GPU will read the flag as soon as its set on the CPU).
