# Question about Numba CUDA Matrix Multiply example

**URL:** <https://numba.discourse.group/t/question-about-numba-cuda-matrix-multiply-example/2512>\
**Category:** Community Support\
**Created:** [April 12, 2024, 8:14pm UTC](https://numba.discourse.group/t/question-about-numba-cuda-matrix-multiply-example/2512 "2024-04-12T20:14:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgyurgyik](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/cgyurgyik/32/1045_2.png) [@cgyurgyik](https://numba.discourse.group/u/cgyurgyik)\
**Post date:** [April 12, 2024, 8:14pm UTC](https://numba.discourse.group/t/question-about-numba-cuda-matrix-multiply-example/2512/1 "2024-04-12T20:14:40Z")

</div>

from: [Examples — Numba 0.50.1 documentation](https://numba.pydata.org/numba-doc/latest/cuda/examples.html#matrix-multiplication)

```auto
from numba import cuda, float32

# Controls threads per block and shared memory usage.
# The computation will be done on blocks of TPBxTPB elements.
TPB = 16

@cuda.jit
def fast_matmul(A, B, C):
    # Define an array in the shared memory
    # The size and type of the arrays must be known at compile time
    sA = cuda.shared.array(shape=(TPB, TPB), dtype=float32)
    sB = cuda.shared.array(shape=(TPB, TPB), dtype=float32)

    x, y = cuda.grid(2)

    tx = cuda.threadIdx.x
    ty = cuda.threadIdx.y
    bpg = cuda.gridDim.x # blocks per grid

    if x >= C.shape[0] and y >= C.shape[1]:
        # Quit if (x, y) is outside of valid C boundary
        return

    # Each thread computes one element in the result matrix.
    # The dot product is chunked into dot products of TPB-long vectors.
    tmp = 0.
    for i in range(bpg):
        # Preload data into shared memory
        sA[tx, ty] = A[x, ty + i * TPB]
        sB[tx, ty] = B[tx + i * TPB, y]

        # Wait until all threads finish preloading
        cuda.syncthreads()

        # Computes partial product on the shared memory
        for j in range(TPB):
            tmp += sA[tx, j] * sB[j, ty]

        # Wait until all threads finish computing
        cuda.syncthreads()

    C[x, y] = tmp

```

Shouldn’t this:

```auto
    if x >= C.shape[0] and y >= C.shape[1]:
        # Quit if (x, y) is outside of valid C boundary
        return

```

be:

```auto
    if x >= C.shape[0] or y >= C.shape[1]:
        # Quit if (x, y) is outside of valid C boundary
        return

```

---

<div class="post-metadata">

**Author:** ![cgyurgyik](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/cgyurgyik/32/1045_2.png) [@cgyurgyik](https://numba.discourse.group/u/cgyurgyik)\
**Post date:** [April 12, 2024, 8:17pm UTC](https://numba.discourse.group/t/question-about-numba-cuda-matrix-multiply-example/2512/2 "2024-04-12T20:17:54Z")

</div>

Ah, nevermind - this is old documentation. Sorry!
