# Working with shared memory as one or more variables, what is a good approach?

**URL:** <https://discourse.julialang.org/t/working-with-shared-memory-as-one-or-more-variables-what-is-a-good-approach/93758>\
**Category:** GPU\
**Created:** [January 30, 2023, 9:51am UTC](https://discourse.julialang.org/t/working-with-shared-memory-as-one-or-more-variables-what-is-a-good-approach/93758 "2023-01-30T09:51:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcsj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcsj/32/18526_2.png) [@bcsj](https://discourse.julialang.org/u/bcsj)\
**Post date:** [January 30, 2023, 9:51am UTC](https://discourse.julialang.org/t/working-with-shared-memory-as-one-or-more-variables-what-is-a-good-approach/93758/1 "2023-01-30T09:51:25Z")

</div>

I’m somewhat new to writing gpu code, but I am working on a somewhat complicated kernel function, which involves a number of steps in which shared memory is used to speed up the computations.

To be efficient I reuse the preallocated shared memory in the different parts of the code. In some parts however, it would be beneficial for the shared memory to be tied to only one large array, e.g.

```julia
# case 1
shared = @cuDynamicSharedMem(T, 2 * size)

```

In other parts it would be convenient if the shared memory was partitioned to different variables, e.g.

```julia
# case 2
shA = @cuDynamicSharedMem(T, size)
shB = @cuDynamicSharedMem(T, size, sizeof(shA))

```

So I had the following idea: Allocate as in `case 1`, but then create some auxiliary variables which points into the memory. My thought was to do something like this:

```julia
struct Partition{T,N}
    S::T
    i::Int
    Partition{N}(S::T, i::Int) = new{T, N}(S, i)
end
getindex(P::Partition{T,N}, i::Int) = P.S[P.i * N + i]

```

and use it like:

```julia
# e.g size = 2^10
shared = @cuDynamicSharedMem(T, 2 * size)
shA = Partition{size}(shared, 0)
shB = Partition{size}(shared, 1)
# shA[i], shB[i] ( = shared[i], shared[size+i] )

```

**My questions are now** :

- Is this idea reasonable?
- Is there a better way to do something like this? - something built-in available?
- Is there some performance aspect I’m neglecting where I might be stepping on my own toes by doing this?

---

<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 30, 2023, 1:07pm UTC](https://discourse.julialang.org/t/working-with-shared-memory-as-one-or-more-variables-what-is-a-good-approach/93758/2 "2023-01-30T13:07:05Z")

</div>

> [@bcsj](#):
>
> Is there a better way to do something like this? - something built-in available?

`view`? [https://github.com/JuliaGPU/CUDA.jl/blob/7681e085bde038730e11a7ff123937ccb325e910/test/device/intrinsics/memory.jl#L167-L200](https://github.com/JuliaGPU/CUDA.jl/blob/7681e085bde038730e11a7ff123937ccb325e910/test/device/intrinsics/memory.jl#L167-L200)

---

<div class="post-metadata">

**Author:** ![bcsj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcsj/32/18526_2.png) [@bcsj](https://discourse.julialang.org/u/bcsj)\
**Post date:** [January 30, 2023, 1:14pm UTC](https://discourse.julialang.org/t/working-with-shared-memory-as-one-or-more-variables-what-is-a-good-approach/93758/3 "2023-01-30T13:14:29Z")

</div>

Now I just feel embarrassed. 😶
