From 921aaa8a59e60ca54d13b6998ab78d801f78c5e3 Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Wed, 5 Nov 2025 09:38:38 -0700 Subject: [PATCH 1/2] start to update old unsafe pointer usage --- problems/p01/p01.mojo | 4 ++-- problems/p02/p02.mojo | 7 +++---- problems/p03/p03.mojo | 5 ++--- problems/p04/p04.mojo | 5 ++--- problems/p05/p05.mojo | 7 +++---- problems/p06/p06.mojo | 5 ++--- problems/p07/p07.mojo | 5 ++--- problems/p08/p08.mojo | 6 +++--- problems/p08/p08_layout_tensor.mojo | 1 - problems/p09/p09.mojo | 5 +++-- problems/p11/p11.mojo | 6 +++--- problems/p12/p12.mojo | 8 ++++---- problems/p17/op/conv1d.mojo | 1 - problems/p19/op/attention.mojo | 1 - solutions/p01/p01.mojo | 4 ++-- solutions/p02/p02.mojo | 7 +++---- solutions/p03/p03.mojo | 5 ++--- solutions/p04/p04.mojo | 5 ++--- solutions/p05/p05.mojo | 7 +++---- solutions/p06/p06.mojo | 5 ++--- solutions/p07/p07.mojo | 5 ++--- solutions/p08/p08.mojo | 6 +++--- solutions/p08/p08_layout_tensor.mojo | 1 - solutions/p11/p11.mojo | 6 +++--- solutions/p12/p12.mojo | 8 ++++---- solutions/p19/op/attention.mojo | 1 - 26 files changed, 55 insertions(+), 71 deletions(-) diff --git a/problems/p01/p01.mojo b/problems/p01/p01.mojo index bda50bdc..066da9eb 100644 --- a/problems/p01/p01.mojo +++ b/problems/p01/p01.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,7 +10,8 @@ alias dtype = DType.float32 fn add_10( - output: UnsafePointer[Scalar[dtype]], a: UnsafePointer[Scalar[dtype]] + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], ): i = thread_idx.x # FILL ME IN (roughly 1 line) diff --git a/problems/p02/p02.mojo b/problems/p02/p02.mojo index be6b8353..edb40318 100644 --- a/problems/p02/p02.mojo +++ b/problems/p02/p02.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,9 +10,9 @@ alias dtype = DType.float32 fn add( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], ): i = thread_idx.x # FILL ME IN (roughly 1 line) diff --git a/problems/p03/p03.mojo b/problems/p03/p03.mojo index 474489c5..e123f89c 100644 --- a/problems/p03/p03.mojo +++ b/problems/p03/p03.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 fn add_10_guard( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): i = thread_idx.x diff --git a/problems/p04/p04.mojo b/problems/p04/p04.mojo index 2a954400..e7f85ce3 100644 --- a/problems/p04/p04.mojo +++ b/problems/p04/p04.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 fn add_10_2d( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = thread_idx.y diff --git a/problems/p05/p05.mojo b/problems/p05/p05.mojo index 37e8aa83..51154b06 100644 --- a/problems/p05/p05.mojo +++ b/problems/p05/p05.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext, HostBuffer from testing import assert_equal @@ -11,9 +10,9 @@ alias dtype = DType.float32 fn broadcast_add( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = thread_idx.y diff --git a/problems/p06/p06.mojo b/problems/p06/p06.mojo index c679b21a..6b75cbc8 100644 --- a/problems/p06/p06.mojo +++ b/problems/p06/p06.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 fn add_10_blocks( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): i = block_dim.x * block_idx.x + thread_idx.x diff --git a/problems/p07/p07.mojo b/problems/p07/p07.mojo index 09db5cd1..146bee1f 100644 --- a/problems/p07/p07.mojo +++ b/problems/p07/p07.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 fn add_10_blocks_2d( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = block_dim.y * block_idx.y + thread_idx.y diff --git a/problems/p08/p08.mojo b/problems/p08/p08.mojo index dd74f555..b1ba25dc 100644 --- a/problems/p08/p08.mojo +++ b/problems/p08/p08.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,8 +14,8 @@ alias dtype = DType.float32 fn add_10_shared( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): shared = stack_allocation[ diff --git a/problems/p08/p08_layout_tensor.mojo b/problems/p08/p08_layout_tensor.mojo index a6fce741..311863ed 100644 --- a/problems/p08/p08_layout_tensor.mojo +++ b/problems/p08/p08_layout_tensor.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace diff --git a/problems/p09/p09.mojo b/problems/p09/p09.mojo index 5df6563d..70572e57 100644 --- a/problems/p09/p09.mojo +++ b/problems/p09/p09.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -17,7 +16,8 @@ alias ITER = 2 # ANCHOR: first_crash fn add_10( - output: UnsafePointer[Scalar[dtype]], a: UnsafePointer[Scalar[dtype]] + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]] ): i = thread_idx.x output[i] = a[i] + 10.0 @@ -105,6 +105,7 @@ def main(): print() with DeviceContext() as ctx: + # TODO: fix this input_ptr = UnsafePointer[Scalar[dtype]]() result_buf = ctx.enqueue_create_buffer[dtype](SIZE).enqueue_fill(0) diff --git a/problems/p11/p11.mojo b/problems/p11/p11.mojo index 62d3c6d1..c0f0e55c 100644 --- a/problems/p11/p11.mojo +++ b/problems/p11/p11.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,8 +14,8 @@ alias dtype = DType.float32 fn pooling( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): shared = stack_allocation[ diff --git a/problems/p12/p12.mojo b/problems/p12/p12.mojo index 690230cc..1e9e12c0 100644 --- a/problems/p12/p12.mojo +++ b/problems/p12/p12.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,9 +14,9 @@ alias dtype = DType.float32 fn dot_product( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): # FILL ME IN (roughly 13 lines) diff --git a/problems/p17/op/conv1d.mojo b/problems/p17/op/conv1d.mojo index c038eb01..511dc079 100644 --- a/problems/p17/op/conv1d.mojo +++ b/problems/p17/op/conv1d.mojo @@ -68,7 +68,6 @@ fn conv1d_kernel[ import compiler from runtime.asyncrt import DeviceContextPtr from tensor import InputTensor, OutputTensor -from memory import UnsafePointer from gpu.host import DeviceBuffer diff --git a/problems/p19/op/attention.mojo b/problems/p19/op/attention.mojo index 0c0485be..3b24c3c0 100644 --- a/problems/p19/op/attention.mojo +++ b/problems/p19/op/attention.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext, HostBuffer, DeviceBuffer from gpu.memory import AddressSpace, async_copy_wait_all diff --git a/solutions/p01/p01.mojo b/solutions/p01/p01.mojo index a769de40..bc08d725 100644 --- a/solutions/p01/p01.mojo +++ b/solutions/p01/p01.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,7 +10,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_solution fn add_10( - output: UnsafePointer[Scalar[dtype]], a: UnsafePointer[Scalar[dtype]] + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]] ): i = thread_idx.x output[i] = a[i] + 10.0 diff --git a/solutions/p02/p02.mojo b/solutions/p02/p02.mojo index 02d1a858..df8a95fa 100644 --- a/solutions/p02/p02.mojo +++ b/solutions/p02/p02.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,9 +10,9 @@ alias dtype = DType.float32 # ANCHOR: add_solution fn add( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], ): i = thread_idx.x output[i] = a[i] + b[i] diff --git a/solutions/p03/p03.mojo b/solutions/p03/p03.mojo index 1282610f..0f4196a2 100644 --- a/solutions/p03/p03.mojo +++ b/solutions/p03/p03.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_guard_solution fn add_10_guard( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): i = thread_idx.x diff --git a/solutions/p04/p04.mojo b/solutions/p04/p04.mojo index cb71c88a..50946361 100644 --- a/solutions/p04/p04.mojo +++ b/solutions/p04/p04.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_2d_solution fn add_10_2d( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = thread_idx.y diff --git a/solutions/p05/p05.mojo b/solutions/p05/p05.mojo index aa59180d..9e595eba 100644 --- a/solutions/p05/p05.mojo +++ b/solutions/p05/p05.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_dim, block_idx from gpu.host import DeviceContext, HostBuffer from testing import assert_equal @@ -11,9 +10,9 @@ alias dtype = DType.float32 # ANCHOR: broadcast_add_solution fn broadcast_add( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = thread_idx.y diff --git a/solutions/p06/p06.mojo b/solutions/p06/p06.mojo index 84823c3b..5941b637 100644 --- a/solutions/p06/p06.mojo +++ b/solutions/p06/p06.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_blocks_solution fn add_10_blocks( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): i = block_dim.x * block_idx.x + thread_idx.x diff --git a/solutions/p07/p07.mojo b/solutions/p07/p07.mojo index 0c86ec6e..d9cd5f86 100644 --- a/solutions/p07/p07.mojo +++ b/solutions/p07/p07.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim from gpu.host import DeviceContext from testing import assert_equal @@ -11,8 +10,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_blocks_2d_solution fn add_10_blocks_2d( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): row = block_dim.y * block_idx.y + thread_idx.y diff --git a/solutions/p08/p08.mojo b/solutions/p08/p08.mojo index cd62ffd0..e6ec65b0 100644 --- a/solutions/p08/p08.mojo +++ b/solutions/p08/p08.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,8 +14,8 @@ alias dtype = DType.float32 # ANCHOR: add_10_shared_solution fn add_10_shared( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): shared = stack_allocation[ diff --git a/solutions/p08/p08_layout_tensor.mojo b/solutions/p08/p08_layout_tensor.mojo index 8b46e8c0..9d9277bc 100644 --- a/solutions/p08/p08_layout_tensor.mojo +++ b/solutions/p08/p08_layout_tensor.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace diff --git a/solutions/p11/p11.mojo b/solutions/p11/p11.mojo index ad84d2fd..3c48767c 100644 --- a/solutions/p11/p11.mojo +++ b/solutions/p11/p11.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,8 +14,8 @@ alias dtype = DType.float32 # ANCHOR: pooling_solution fn pooling( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): shared = stack_allocation[ diff --git a/solutions/p12/p12.mojo b/solutions/p12/p12.mojo index b8fa0d28..7cf5b579 100644 --- a/solutions/p12/p12.mojo +++ b/solutions/p12/p12.mojo @@ -1,4 +1,4 @@ -from memory import UnsafePointer, stack_allocation +from memory import stack_allocation from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext from gpu.memory import AddressSpace @@ -14,9 +14,9 @@ alias dtype = DType.float32 # ANCHOR: dot_product_solution fn dot_product( - output: UnsafePointer[Scalar[dtype]], - a: UnsafePointer[Scalar[dtype]], - b: UnsafePointer[Scalar[dtype]], + output: UnsafeMutPointer[Scalar[dtype]], + a: UnsafeImmutPointer[Scalar[dtype]], + b: UnsafeImmutPointer[Scalar[dtype]], size: Int, ): shared = stack_allocation[ diff --git a/solutions/p19/op/attention.mojo b/solutions/p19/op/attention.mojo index 5b54c3a5..e6663346 100644 --- a/solutions/p19/op/attention.mojo +++ b/solutions/p19/op/attention.mojo @@ -1,4 +1,3 @@ -from memory import UnsafePointer from gpu import thread_idx, block_idx, block_dim, barrier from gpu.host import DeviceContext, HostBuffer, DeviceBuffer from gpu.memory import AddressSpace, async_copy_wait_all From 2c4b933c5a5d163eb2ac77932ce2498522ddf53a Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Wed, 5 Nov 2025 09:51:06 -0700 Subject: [PATCH 2/2] fix formatting --- problems/p09/p09.mojo | 2 +- solutions/p01/p01.mojo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/p09/p09.mojo b/problems/p09/p09.mojo index 70572e57..c6875658 100644 --- a/problems/p09/p09.mojo +++ b/problems/p09/p09.mojo @@ -17,7 +17,7 @@ alias ITER = 2 # ANCHOR: first_crash fn add_10( output: UnsafeMutPointer[Scalar[dtype]], - a: UnsafeImmutPointer[Scalar[dtype]] + a: UnsafeImmutPointer[Scalar[dtype]], ): i = thread_idx.x output[i] = a[i] + 10.0 diff --git a/solutions/p01/p01.mojo b/solutions/p01/p01.mojo index bc08d725..2661e316 100644 --- a/solutions/p01/p01.mojo +++ b/solutions/p01/p01.mojo @@ -11,7 +11,7 @@ alias dtype = DType.float32 # ANCHOR: add_10_solution fn add_10( output: UnsafeMutPointer[Scalar[dtype]], - a: UnsafeImmutPointer[Scalar[dtype]] + a: UnsafeImmutPointer[Scalar[dtype]], ): i = thread_idx.x output[i] = a[i] + 10.0