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/man/backends.md
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,11 +129,14 @@ In particular, this means that the backend will be selected **first**, while onl
129
129
```@docs
130
130
TensorOperations.DefaultAllocator
131
131
TensorOperations.ManualAllocator
132
+
TensorOperations.BufferAllocator
132
133
```
133
134
134
135
By default, the `DefaultAllocator` is used, which uses Julia's built-in memory management system.
135
136
Optionally, it can be useful to use the `ManualAllocator`, as the manual memory management reduces the pressure on the garbage collector.
136
137
In particular in multi-threaded applications, this can sometimes lead to a significant performance improvement.
138
+
On the other hand, for repeated (but thread-safe!) `@tensor` calls, the `BufferAllocator` is a lightweight slab allocator that pre-allocates a buffer for temporaries, falling back to Julia's default if needed.
139
+
Upon repeated use it will automatically resize the buffer to accommodate the requested temporaries, avoiding repeated reallocation.
137
140
138
141
Finally, users can also opt to use the `Bumper.jl` system, which pre-allocates a slab of memory that can be re-used afterwards.
139
142
This is available through a package extension for `Bumper`.
Users can also define their own allocators, to facilitate experimentation with new implementations.
167
170
Here, no restriction is made on the type of the allocator, and any object can be passed as an allocator.
168
-
The required implemented methods are [`tensoralloc`](@ref) and [`tensorfree!`](@ref).
171
+
172
+
The core methods that can be customized for an allocator are:
173
+
174
+
*[`tensoralloc`](@ref): Allocate a tensor of a given type and structure. This method receives a flag indicating whether the output lives only within an `@tensor` block or persists outside of it. Temporary tensors can be allocated from internal buffers or pools, while permanent tensors should use a standard allocation strategy.
175
+
*[`tensorfree!`](@ref): Explicitly free a tensor, if applicable. For custom allocators that manage internal pools or buffers, this can be used to track when temporaries are no longer needed.
176
+
177
+
Here we are guaranteeing that the code generated by `@tensor` will contain exactly one matching call to `tensorfree!` for every `tensoralloc` call that has the temporary flag, as soon as the temporary object is no longer needed.
178
+
179
+
!!! warning
180
+
Special care should be taken when using these functions directly, as it is then up to the user to ensure every allocated temporary is freed appropriately.
181
+
Invalid usage such as not freeing a tensor, freeing it twice, or freeing an tensor that was not allocated as temporary is considered undefined behavior,
182
+
and can lead to memory leaks and/or segmentation faults.
183
+
184
+
185
+
For allocators that manage reusable buffers or maintain state across multiple contractions, the following helper methods can be useful to indicate that it is safe to free all objects that were allocated after a given checkpoint.
186
+
187
+
*[`allocator_checkpoint!`](@ref): Save the current state of the allocator (e.g., the current offset in a buffer). This can be called before a sequence of tensor operations to capture the allocation state.
188
+
*[`allocator_reset!`](@ref): Restore the allocator to a previously saved checkpoint, effectively releasing all allocations made since the checkpoint was taken.
189
+
190
+
Here we are guaranteeing that every created checkpoint will be restored, and all temporary allocations that are enclosed within this scope will no longer be accessed.
191
+
Additionally, if multiple checkpoints are created, they will be restored in a first-in-last-out order.
0 commit comments