Skip to content

Commit 8df8edf

Browse files
committed
update docs
1 parent 4d6867b commit 8df8edf

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎docs/src/man/backends.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,14 @@ In particular, this means that the backend will be selected **first**, while onl
129129
```@docs
130130
TensorOperations.DefaultAllocator
131131
TensorOperations.ManualAllocator
132+
TensorOperations.BufferAllocator
132133
```
133134

134135
By default, the `DefaultAllocator` is used, which uses Julia's built-in memory management system.
135136
Optionally, it can be useful to use the `ManualAllocator`, as the manual memory management reduces the pressure on the garbage collector.
136137
In particular in multi-threaded applications, this can sometimes lead to a significant performance improvement.
138+
On the other hand, for often-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 for the requested temporaries.
137140

138141
Finally, users can also opt to use the `Bumper.jl` system, which pre-allocates a slab of memory that can be re-used afterwards.
139142
This is available through a package extension for `Bumper`.
@@ -165,4 +168,18 @@ TensorOperations.CUDAAllocator
165168

166169
Users can also define their own allocators, to facilitate experimentation with new implementations.
167170
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 tensor is temporary (will not persist outside the `@tensor` block) or permanent. 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 every `tensoralloc` call that has the temporary flag will be accompanied by exactly one matching call to `tensorfree!`, as soon as the temporary object is no longer needed.
178+
179+
For allocators that manage reusable buffers or maintain state across multiple contractions, the following helper methods can be useful:
180+
181+
* [`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.
182+
* [`allocator_reset!`](@ref): Restore the allocator to a previously saved checkpoint, effectively releasing all allocations made since the checkpoint was taken.
183+
184+
Here we are guaranteeing that every created checkpoint will be restored, and all temporary allocations that are inclosed within this scope will no longer be accessed.
185+
Additionally, if multiple checkpoints are created, they will be restored in the reverse order of how they were created.

‎docs/src/man/interface.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ TensorOperations.tensoralloc_add
9494
TensorOperations.tensoralloc_contract
9595
```
9696

97+
For allocators that manage reusable buffers or maintain state across multiple tensor
98+
operations, the following helper functions provide an interface for managing allocation
99+
regions:
100+
101+
```@docs
102+
TensorOperations.allocator_checkpoint!
103+
TensorOperations.allocator_reset!
104+
```
105+
106+
These can be used to save the current allocator state before a region of code and restore it
107+
afterwards, where it is guaranteed that all temporary objects allocated within that region
108+
are released. This is particularly useful for nested tensor operations or when repeatedly
109+
evaluating the same tensor contraction with an allocator like
110+
[`TensorOperations.BufferAllocator`](@ref).
111+
97112
## Utility
98113

99114
Some of the optional keywords for `@tensor` can be accessed only after implementing the

0 commit comments

Comments
 (0)