forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_addition.mojo
More file actions
executable file
·78 lines (64 loc) · 2.8 KB
/
Copy pathvector_addition.mojo
File metadata and controls
executable file
·78 lines (64 loc) · 2.8 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
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, 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 math import ceildiv
from sys import has_amd_gpu_accelerator, has_nvidia_gpu_accelerator
from gpu import global_idx
from gpu.host import DeviceContext
from layout import Layout, LayoutTensor
alias float_dtype = DType.float32
alias VECTOR_WIDTH = 10
alias BLOCK_SIZE = 5
alias layout = Layout.row_major(VECTOR_WIDTH)
def main():
constrained[
has_nvidia_gpu_accelerator() or has_amd_gpu_accelerator(),
"This example requires a supported GPU",
]()
# Get context for the attached GPU
var ctx = DeviceContext()
# Allocate data on the GPU address space
var lhs_buffer = ctx.enqueue_create_buffer[float_dtype](VECTOR_WIDTH)
var rhs_buffer = ctx.enqueue_create_buffer[float_dtype](VECTOR_WIDTH)
var out_buffer = ctx.enqueue_create_buffer[float_dtype](VECTOR_WIDTH)
# Fill in values across the entire width
_ = lhs_buffer.enqueue_fill(1.25)
_ = rhs_buffer.enqueue_fill(2.5)
# Wrap the device buffers in tensors
var lhs_tensor = LayoutTensor[float_dtype, layout](lhs_buffer)
var rhs_tensor = LayoutTensor[float_dtype, layout](rhs_buffer)
var out_tensor = LayoutTensor[float_dtype, layout](out_buffer)
# Calculate the number of blocks needed to cover the vector
var grid_dim = ceildiv(VECTOR_WIDTH, BLOCK_SIZE)
# Launch the vector_addition function as a GPU kernel
ctx.enqueue_function[vector_addition](
lhs_tensor,
rhs_tensor,
out_tensor,
VECTOR_WIDTH,
grid_dim=grid_dim,
block_dim=BLOCK_SIZE,
)
# Map to host so that values can be printed from the CPU
with out_buffer.map_to_host() as host_buffer:
var host_tensor = LayoutTensor[float_dtype, layout](host_buffer)
print("Resulting vector:", host_tensor)
fn vector_addition(
lhs_tensor: LayoutTensor[float_dtype, layout, MutableAnyOrigin],
rhs_tensor: LayoutTensor[float_dtype, layout, MutableAnyOrigin],
out_tensor: LayoutTensor[float_dtype, layout, MutableAnyOrigin],
size: Int,
):
"""The calculation to perform across the vector on the GPU."""
var global_tid = global_idx.x
if global_tid < size:
out_tensor[global_tid] = lhs_tensor[global_tid] + rhs_tensor[global_tid]