You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/usage/kernel.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,34 @@ Additional notes:
84
84
- Kernels must always return nothing
85
85
- Kernels are asynchronous. To synchronize, use the `Metal.@sync` macro.
86
86
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
+
functiongpu_add2_print!(y, x)
93
+
index =thread_position_in_grid_1d()
94
+
@mtlprintf("thread %d", index)
95
+
@inbounds y[i] += x[i]
96
+
returnnothing
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
+
87
115
## Other Helpful Links
88
116
89
117
[Metal Shading Language Specification](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)
0 commit comments