@@ -244,6 +244,52 @@ public enum GPU {
244244 }
245245 }
246246
247+ /// Perform the block with a temporarily altered wired memory limit.
248+ ///
249+ /// Note: this manipulates a global value. Nested calls will work as expected but
250+ /// concurrent calls cannot.
251+ ///
252+ /// See also ``DeviceInfo/maxRecommendedWorkingSetSize``.
253+ ///
254+ /// - Parameters:
255+ /// - limit: new limit in bytes
256+ /// - body: block to perform
257+ public static func withWiredLimit< R> (
258+ _ limit: Int , _ body: ( ) throws -> R
259+ ) rethrows -> R {
260+ var current = 0
261+ mlx_set_wired_limit ( & current, limit)
262+ defer {
263+ var tmp = 0
264+ mlx_set_wired_limit ( & tmp, current)
265+ }
266+
267+ return try body ( )
268+ }
269+
270+ /// Perform the block with a temporarily altered wired memory limit.
271+ ///
272+ /// Note: this manipulates a global value. Nested calls will work as expected but
273+ /// concurrent calls cannot.
274+ ///
275+ /// See also ``DeviceInfo/maxRecommendedWorkingSetSize``.
276+ ///
277+ /// - Parameters:
278+ /// - limit: new limit in bytes
279+ /// - body: block to perform
280+ public static func withWiredLimit< R> (
281+ _ limit: Int , _ body: ( ) async throws -> R
282+ ) async rethrows -> R {
283+ var current = 0
284+ mlx_set_wired_limit ( & current, limit)
285+ defer {
286+ var tmp = 0
287+ mlx_set_wired_limit ( & tmp, current)
288+ }
289+
290+ return try await body ( )
291+ }
292+
247293 /// Cause all cached metal buffers to be deallocated.
248294 public static func clearCache( ) {
249295 mlx_clear_cache ( )
0 commit comments