Skip to content

Commit 0fae2fb

Browse files
committed
add docs
1 parent c6d53e4 commit 0fae2fb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/src/usage/kernel.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ Additional notes:
8484
- Kernels must always return nothing
8585
- Kernels are asynchronous. To synchronize, use the `Metal.@sync` macro.
8686

87+
## Printing
88+
89+
When debugging, it's not uncommon to want to print some values. This is achieved with `@mtlprintf`:
90+
91+
```julia
92+
function gpu_add2_print!(y, x)
93+
index = thread_position_in_grid_1d()
94+
@mtlprintf("thread %d", index)
95+
@inbounds y[i] += x[i]
96+
return nothing
97+
end
98+
99+
A = Metal.ones(Float32, 8);
100+
B = Metal.rand(Float32, 8);
101+
102+
@metal threads=length(A) gpu_add2_print!(A, B)
103+
```
104+
105+
`@mtlprintf` is supported on macOS 15 and later. `@mtlprintf` support most of the format specifiers that `printf`
106+
supports in C with the following exceptions:
107+
- `%n` and `%s` conversion specifiers are not supported
108+
- Default argument promotion applies to arguments of half type which promote to the `double` type
109+
- The format string must be a string literal
110+
111+
Metal places output from `@mtlprintf` into a log buffer. The system only removes the messages from the log buffer when the command buffer completes. When the log buffer becomes full, the system drops all subsequent messages.
112+
113+
See also: `@mtlprint`, `@mtlprintln` and `@mtlshow`
114+
87115
## Other Helpful Links
88116

89117
[Metal Shading Language Specification](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)

0 commit comments

Comments
 (0)