Repository navigation
Expand file tree
/
Copy pathp10.mojo
More file actions
163 lines (132 loc) · 5.18 KB
/
Copy pathp10.mojo
File metadata and controls
163 lines (132 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
from max.gpu import thread_idx, block_dim, block_idx
from max.gpu.sync import barrier
from max.gpu.host import DeviceContext
from layout import TileTensor, TensorEngine
from layout.layout import row_major
from layout.tile_tensor import stack_allocation
from std.testing import assert_equal
from std.sys import argv
# ANCHOR: shared_memory_race
comptime SIZE = 2
comptime BLOCKS_PER_GRID = 1
comptime THREADS_PER_BLOCK = (3, 3)
comptime dtype = DType.float32
comptime layout = row_major[SIZE, SIZE]()
comptime LayoutType = type_of(layout)
def shared_memory_race[
Engine: TensorEngine,
](
output: TileTensor[
mut=True, dtype, LayoutType, MutAnyOrigin, Engine=Engine
],
a: TileTensor[mut=False, dtype, LayoutType, ImmutAnyOrigin, Engine=Engine],
size_dev: Int32,
) where (Engine.element_size == 1):
var size = Int(size_dev)
var row = thread_idx.y
var col = thread_idx.x
var shared_sum = stack_allocation[dtype=dtype, address_space=.SHARED](
row_major[1]()
)
if row < size and col < size:
shared_sum[0] += rebind[Scalar[dtype]](a[row, col])
barrier()
if row < size and col < size:
output[row, col] = shared_sum[0]
# ANCHOR_END: shared_memory_race
# ANCHOR: add_10_2d_no_guard
def add_10_2d[
Engine: TensorEngine,
](
output: TileTensor[
mut=True, dtype, LayoutType, MutAnyOrigin, Engine=Engine
],
a: TileTensor[mut=False, dtype, LayoutType, ImmutAnyOrigin, Engine=Engine],
size_dev: Int32,
) where (Engine.element_size == 1):
var size = Int(size_dev)
_ = size
var row = thread_idx.y
var col = thread_idx.x
output[row, col] = a[row, col] + 10.0
# ANCHOR_END: add_10_2d_no_guard
def main() raises:
if len(argv()) != 2:
print(
"Expected one command-line argument: '--memory-bug' or"
" '--race-condition'"
)
return
var flag = argv()[1]
with DeviceContext() as ctx:
var out_buf = ctx.enqueue_create_buffer[dtype](SIZE * SIZE)
out_buf.enqueue_fill(0)
var out_tensor = TileTensor(out_buf, layout)
print("out shape:", out_tensor.dim[0](), "x", out_tensor.dim[1]())
var expected = ctx.enqueue_create_host_buffer[dtype](SIZE * SIZE)
expected.enqueue_fill(0)
var a = ctx.enqueue_create_buffer[dtype](SIZE * SIZE)
a.enqueue_fill(0)
with a.map_to_host() as a_host:
for i in range(SIZE * SIZE):
a_host[i] = Scalar[dtype](i)
var a_tensor = TileTensor(a, layout)
if flag == "--memory-bug":
print("Running memory bug example (bounds checking issue)...")
# Fill expected values directly since it's a HostBuffer
for i in range(SIZE * SIZE):
expected[i] = Scalar[dtype](i + 10)
ctx.enqueue_function[add_10_2d[out_tensor.Engine]](
out_tensor,
a_tensor,
Int32(SIZE),
grid_dim=BLOCKS_PER_GRID,
block_dim=THREADS_PER_BLOCK,
)
ctx.synchronize()
with out_buf.map_to_host() as out_buf_host:
print("out:", out_buf_host)
print("expected:", expected)
for i in range(SIZE * SIZE):
assert_equal(out_buf_host[i], expected[i])
print("Memory bug test: passed")
print("Puzzle 10 complete ✅")
elif flag == "--race-condition":
print("Running race condition example...")
var total_sum = Scalar[dtype](0.0)
with a.map_to_host() as a_host:
for i in range(SIZE * SIZE):
total_sum += a_host[i] # Sum: 0 + 1 + 2 + 3 = 6
# All positions should contain the total sum
for i in range(SIZE * SIZE):
expected[i] = total_sum
ctx.enqueue_function[shared_memory_race[out_tensor.Engine]](
out_tensor,
a_tensor,
Int32(SIZE),
grid_dim=BLOCKS_PER_GRID,
block_dim=THREADS_PER_BLOCK,
)
ctx.synchronize()
with out_buf.map_to_host() as out_buf_host:
print("out:", out_buf_host)
print("expected:", expected)
for i in range(SIZE * SIZE):
assert_equal(out_buf_host[i], expected[i])
print("Race condition test: passed")
print("Puzzle 10 complete ✅")
else:
print("Unknown flag:", flag)
print("Available flags: --memory-bug, --race-condition")