# GPU Puzzles P09 Shared memory indexing issue

**URL:** <https://forum.modular.com/t/gpu-puzzles-p09-shared-memory-indexing-issue/1699>\
**Category:** Standard Library\
**Tags:** gpu\
**Created:** [June 20, 2025, 9:58pm UTC](https://forum.modular.com/t/gpu-puzzles-p09-shared-memory-indexing-issue/1699 "2025-06-20T21:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Augment](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Augment](https://forum.modular.com/u/Augment)\
**Post date:** [June 20, 2025, 9:58pm UTC](https://forum.modular.com/t/gpu-puzzles-p09-shared-memory-indexing-issue/1699/1 "2025-06-20T21:58:26Z")

</div>

I am looking at GPU problem p09 with `LayoutTensor`, and I have this solution:

```mojo
fn pooling[
    layout: Layout
](
    output: LayoutTensor[mut=True, dtype, layout],
    a: LayoutTensor[mut=True, dtype, layout],
    size: Int,
):
    # Allocate shared memory using tensor builder
    shared = tb[dtype]().row_major[TPB]().shared().alloc()

    global_i = block_dim.x * block_idx.x + thread_idx.x
    local_i = thread_idx.x

    if global_i < size:
        shared[local_i] = a[global_i]

    barrier()

    if global_i < size:
        acc = Float32(0)
        for j in range(max(local_i-2, 0), local_i+1):
            acc += shared[j][0]

        output[global_i] = acc

```

For some reason, it seems to fail for `thread_idx = 1`

```plaintext
out: HostBuffer([0.0, 0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0])
expected: HostBuffer([0.0, 1.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0]) 
Unhandled exception caught during execution: At /home/arseni/repositories/mojo-gpu-puzzles/problems/p09/p09_layout_tensor.mojo:80:29: AssertionError: left == right comparison failed: left: 0.0 

```

I can see the suggested solution in the docs, but I want to understand why this does not work, or how you would debug something like this in Mojo/lldb. Is there some crux behind indexing into shared/SIMD memory? Or is it just some indexing issue?

---

<div class="post-metadata">

**Author:** ![steepcurve](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@steepcurve](https://forum.modular.com/u/steepcurve)\
**Post date:** [June 22, 2025, 12:43pm UTC](https://forum.modular.com/t/gpu-puzzles-p09-shared-memory-indexing-issue/1699/2 "2025-06-22T12:43:42Z")

</div>

`local_i` is `UInt`, so `local_i-2` may underflow.

---

<div class="post-metadata">

**Author:** ![Augment](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Augment](https://forum.modular.com/u/Augment)\
**Post date:** [June 27, 2025, 5:57pm UTC](https://forum.modular.com/t/gpu-puzzles-p09-shared-memory-indexing-issue/1699/3 "2025-06-27T17:57:15Z")

</div>

Youre right, wrapping the local\_i-2 in Int solves the problem, thank you.
