Skip to content

JeffreySarnoff/AlignedAllocs.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

274 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AlignedAllocs.jl

Stable Dev     Aqua QA

All allocations use the new Memory infrastructure.

Features

  • memalign and memalign_clear allocate aligned Vector{T} storage, either uninitialised or zeroed.
  • memaligned and memaligned_clear reshape aligned buffers into multi-dimensional arrays without copying.
  • memalign_fixed and memalign_clear_fixed build aligned FixedSizeArrays.jl containers directly.
  • alignment(::AbstractArray) reports the effective pointer alignment at runtime.
  • Cache-line detection with a safe fallback keeps defaults portable across platforms.
  • Precompilation via PrecompileTools minimises package load time.

Installation

pkg> add AlignedAllocs

Julia 1.11 or newer is required.

Quick Start

julia> using AlignedAllocs

# Cache-line aligned Float32 buffer
julia> xs = memalign(Float32, 256)
256-element Vector{Float32}:
 0.0
 0.0
 #= output truncated =#

# Explicit alignment with zero-initialisation
julia> ys = memalign_clear(UInt16, 128, 256)

# Multi-dimensional array with aligned storage
julia> az = memaligned(Float32, 32, 8; align=128)
32x8 Matrix{Float32}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 #= output truncated =#

# Fixed-size array backed by aligned storage
julia> using FixedSizeArrays
julia> fs = memalign_fixed(Float32, 16, 4; align=128)
16x4 FixedSizeArrays.FixedSizeMatrix{Float32}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 #= output truncated =#

# Inspect alignment guarantee
julia> alignment(az)
128

See the User Guide for workflow examples and the API Reference for detailed signatures.

Alignment Guarantees

  • Alignments must be powers of two >= 16 bytes; invalid inputs throw ArgumentError.
  • On POSIX systems vectors own the memory returned by posix_memalign.
  • On Windows vectors register a finalizer that calls _aligned_free when the array is collected.
  • memalign_clear preserves the vector while zeroing memory via Base.memset.

Development

Run the test suite with:

julia --project=. -e "using Pkg; Pkg.test()"

Documentation is built with Documenter.jl. See docs/ for build scripts and source pages.

License

MIT License (c) 2025 Jeffrey Sarnoff.

About

lightweight cross-platform allocation of aligned memory for an n-Vector{T} where isbitstype(T)

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages