# Initializing @cuStaticSharedMem array?

**URL:** <https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861>\
**Category:** GPU\
**Created:** [May 12, 2018, 7:39pm UTC](https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861 "2018-05-12T19:39:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fghorow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fghorow/32/4132_2.png) [@fghorow](https://discourse.julialang.org/u/fghorow)\
**Post date:** [May 12, 2018, 7:39pm UTC](https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861/1 "2018-05-12T19:39:40Z")

</div>

I’m both a Julia and a CUDAnative newbie. CUDAnative is attracting me from my native numpy environment! 😉 This is probably a FAQ:

I have several arrays I want to live in Shared Memory. (They are constant throughout my computation, if that happens to matter.) I have found the @cuStaticSharedMem macro, and tried to initialize it on the host in (what to me is) the obvious way:

```julia
d_bc1d = @cuStaticSharedMem(Float32,size(bound_coords_1d))
d_bc1d .= bound_coords_1d

```

This causes the IJulia kernel to die, when executed from a Jupyter notebook.

What is the proper idiom for accomplishing this task?

Many thanks in advance for any help!!!

---

<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:** [May 12, 2018, 7:50pm UTC](https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861/2 "2018-05-12T19:50:50Z")

</div>

Shared memory is not to be initialized on the host, but only on the device. See these examples: [https://devblogs.nvidia.com/using-shared-memory-cuda-cc/](https://devblogs.nvidia.com/using-shared-memory-cuda-cc/)  
This means the `@cuStaticsharedMem` macro doesn’t work in host context, and can only live in device code.

Also, if those arrays are going to be constant, shared memory is _probably_ not what you’re looking for. GPUs have caches where loaded values end up, so you probably don’t need to do anything. If the arrays are small, constant memory could help, or texture memory otherwise, but neither are currently supported by CUDAnative. Shared memory is used when eg. communicating intermediate computations to other threads outside of the warp.

For future reference, please provide full examples. That makes it much easier to provide help.

---

<div class="post-metadata">

**Author:** ![fghorow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fghorow/32/4132_2.png) [@fghorow](https://discourse.julialang.org/u/fghorow)\
**Post date:** [May 12, 2018, 8:06pm UTC](https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861/3 "2018-05-12T20:06:24Z")

</div>

OK, thanks @maleadt !

It’s a steep learning curve, and your help is much appreciated!

---

<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:** [May 12, 2018, 8:13pm UTC](https://discourse.julialang.org/t/initializing-custaticsharedmem-array/10861/4 "2018-05-12T20:13:52Z")

</div>

Yeah, CUDAnative is pretty low-level. For an actual example of shared memory, see eg. the reduce example: [https://github.com/JuliaGPU/CUDAnative.jl/blob/master/examples/reduce/reduce.jl#L31](https://github.com/JuliaGPU/CUDAnative.jl/blob/master/examples/reduce/reduce.jl#L31)  
Implementations like this are supposed to end up in CuArrays/GPUarrays where they are much user friendlier to use. So depending on your use case you might want to have a look at those packages instead. But if you do need custom kernels, feel free to post any problems you have here.
