Skip to content

Commit

Permalink
Merge pull request #113 from m3g/conditional_alloc_check
Browse files Browse the repository at this point in the history
Conditional alloc check
  • Loading branch information
lmiq authored Feb 1, 2025
2 parents d3de215 + 4d95338 commit ced6b79
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/Downgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
strategy:
matrix:
version:
- '1.6'
- '^1.6'
- '1.9'
- '^1.9'
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
Expand Down
42 changes: 40 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,52 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.9'
- 'lts'
- 'pre'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
exclude:
- version: '1.6'
- version: '1.9'
os: macOS-latest
build_is_production_build:
- true
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
- uses: actions/cache@v3
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
env:
BUILD_IS_PRODUCTION_BUILD: ${{ matrix.build_is_production_build }}
with:
coverage: false
tests-with-coverage:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- 'lts'
- 'pre'
os:
- ubuntu-latest
build_is_production_build:
- false
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand All @@ -39,6 +75,8 @@ jobs:
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
env:
BUILD_IS_PRODUCTION_BUILD: false
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
with:
Expand Down
19 changes: 10 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ Compat = "4.14.0"
DocStringExtensions = "0.9"
Documenter = "1.2.1"
ForwardDiff = "0.10.13"
LinearAlgebra = "1.6"
LinearAlgebra = "1.9"
Measurements = "2.11"
NearestNeighbors = "0.4.16"
PDBTools = "1.1"
PDBTools = "2"
Parameters = "0.12"
PrecompileTools = "1"
PrecompileTools = "1.2.1"
ProgressMeter = "1.6"
Random = "1.6"
Random = "1.9"
Setfield = "0.7, 0.8, 0.9, 1"
StaticArrays = "1.6"
Test = "1.6"
TestItemRunner = "0.2"
TestItems = "0.1, 1"
Test = "1.9"
TestItemRunner = "1"
TestItems = "1"
Unitful = "1.19"
julia = "1.6"
julia = "1.9"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand All @@ -53,6 +53,7 @@ PDBTools = "e29189f1-7114-4dbd-93d0-c5673a921a58"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

[targets]
test = ["Test", "TestItemRunner", "Chemfiles", "NearestNeighbors", "ForwardDiff", "Unitful", "Measurements", "BenchmarkTools", "Aqua", "Documenter", "PDBTools"]
test = ["Test", "TestItemRunner", "Chemfiles", "NearestNeighbors", "ForwardDiff", "Unitful", "Measurements", "BenchmarkTools", "Aqua", "Documenter", "PDBTools", "TestItems"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ USER GUIDE: <br>

## Installation

Download and install Julia for your platform from [this http url](https://julialang.org/downloads/). Version 1.6 or greater is required.
Download and install Julia for your platform from [this http url](https://julialang.org/downloads/). Version 1.9 or greater is required.

Install it as usual for registered Julia packages:

Expand Down
11 changes: 6 additions & 5 deletions src/Box.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,24 +468,25 @@ end
@test list == Tuple{Int64,Int64,Float64}[]
end

@testitem "Stable Box update" begin
@testitem "Stable Box update" setup=[AllocTest] begin
using CellListMap
using StaticArrays
using BenchmarkTools
using LinearAlgebra: diag
using .AllocTest: Allocs

# update with tuples
box = Box([1, 1, 1], 0.1)
a = @ballocated CellListMap.update_box($box; unitcell=(2, 2, 2), cutoff=0.2) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
new_box = CellListMap.update_box(box; unitcell=(2, 2, 2), cutoff=0.2)
@test new_box.cutoff == 0.2
@test new_box.input_unit_cell.matrix == [2 0 0; 0 2 0; 0 0 2]

# update with SVector
box = Box([1, 1, 1], 0.1)
a = @ballocated CellListMap.update_box($box; unitcell=SVector(2, 2, 2), cutoff=0.2) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
new_box = CellListMap.update_box(box; unitcell=(2, 2, 2), cutoff=0.2)
@test new_box.cutoff == 0.2
@test new_box.input_unit_cell.matrix == [2 0 0; 0 2 0; 0 0 2]
Expand All @@ -495,7 +496,7 @@ end
box = Box(limits(x), 0.1)
new_x = rand(SVector{3,Float64}, 1500)
a = @ballocated CellListMap.update_box($box; unitcell=$(limits(new_x)), cutoff=0.2) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
new_box = CellListMap.update_box(box; unitcell=limits(new_x), cutoff=0.2)
@test new_box.cutoff == 0.2
@test diag(new_box.input_unit_cell.matrix) == limits(new_x).limits .+ 2.1 * 0.2
Expand All @@ -504,7 +505,7 @@ end
box = Box([1 0 0; 0 1 0; 0 0 1], 0.1)
new_matrix = SMatrix{3,3,Float64,9}(2, 0, 0, 0, 2, 0, 0, 0, 2)
a = @ballocated CellListMap.update_box($box; unitcell=$new_matrix, cutoff=0.2) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
new_box = CellListMap.update_box(box; unitcell=new_matrix, cutoff=0.2)
@test new_box.cutoff == 0.2
@test new_box.input_unit_cell.matrix == [2 0 0; 0 2 0; 0 0 2]
Expand Down
2 changes: 1 addition & 1 deletion src/CellListMap.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CellListMap

using DocStringExtensions: TYPEDEF, TYPEDFIELDS
using TestItems: @testitem
using TestItems: @testitem, @testmodule
using Compat: @compat
using ProgressMeter: Progress, next!
using Parameters: @unpack, @with_kw
Expand Down
5 changes: 3 additions & 2 deletions src/CellOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,17 @@ function _minmax(x::AbstractVector{<:AbstractVector})
return SVector(xmin), SVector(xmax)
end

@testitem "_minmax" begin
@testitem "_minmax" setup=[AllocTest] begin
using BenchmarkTools
using StaticArrays
import CellListMap: _minmax
using .AllocTest: Allocs
x = [[0.0, 0.5, 1.0], [0.5, 1.0, 0.0], [1.0, 0.0, 0.5]]
@test _minmax(x) === (SVector(0.0, 0.0, 0.0), SVector(1.0, 1.0, 1.0))
x = [SVector(0.0, 0.5, 1.0), SVector(0.5, 1.0, 0.0), SVector(1.0, 0.0, 0.5)]
@test _minmax(x) === (SVector(0.0, 0.0, 0.0), SVector(1.0, 1.0, 1.0))
a = @ballocated _minmax($x) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
end

"""
Expand Down
35 changes: 19 additions & 16 deletions src/ParticleSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -757,23 +757,24 @@ function update_unitcell!(sys, unitcell)
return sys
end

@testitem "update_unitcell!" begin
@testitem "update_unitcell!" setup=[AllocTest] begin
using BenchmarkTools
using LinearAlgebra: diag
using StaticArrays
using CellListMap
using .AllocTest: Allocs
x = rand(SVector{3,Float64}, 1000)
sys1 = ParticleSystem(xpositions=x, unitcell=[1, 1, 1], cutoff=0.1, output=0.0)
update_unitcell!(sys1, SVector(2, 2, 2))
@test diag(sys1.unitcell) == [2, 2, 2]
a = @ballocated update_unitcell!($sys1, SVector(2, 2, 2)) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
y = rand(SVector{3,Float64}, 1000)
sys2 = ParticleSystem(xpositions=x, ypositions=y, unitcell=[1, 1, 1], cutoff=0.1, output=0.0)
update_unitcell!(sys2, SVector(2, 2, 2))
@test diag(sys2.unitcell) == [2, 2, 2]
a = @ballocated update_unitcell!($sys2, SVector(2, 2, 2)) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
# Test throwing error on updating non-periodic unit cells
sys = ParticleSystem(xpositions=x, cutoff=0.1, output=0.0)
@test_throws ArgumentError update_unitcell!(sys, [1, 1, 1])
Expand Down Expand Up @@ -838,23 +839,24 @@ function update_cutoff!(sys::ParticleSystem2, cutoff)
return sys
end

@testitem "update_cutoff!" begin
@testitem "update_cutoff!" setup=[AllocTest] begin
using BenchmarkTools
using StaticArrays
using CellListMap
using PDBTools
using .AllocTest: Allocs
x = rand(SVector{3,Float64}, 1000)
sys1 = ParticleSystem(xpositions=x, unitcell=[1, 1, 1], cutoff=0.1, output=0.0)
update_cutoff!(sys1, 0.2)
@test sys1.cutoff == 0.2
a = @ballocated update_cutoff!($sys1, 0.1) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
y = rand(SVector{3,Float64}, 1000)
sys2 = ParticleSystem(xpositions=x, ypositions=y, unitcell=[1, 1, 1], cutoff=0.1, output=0.0)
update_cutoff!(sys2, 0.2)
@test sys2.cutoff == 0.2
a = @ballocated update_cutoff!($sys2, 0.1) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)

# Update cutoff of non-periodic systems
x = coor(readPDB(CellListMap.argon_pdb_file))
Expand All @@ -864,14 +866,14 @@ end
update_cutoff!(sys1, 10.0)
@test sys1.unitcell [39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99] atol = 1e-2
a = @ballocated update_cutoff!($sys1, 8.0) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
sys2 = ParticleSystem(xpositions=x[1:50], ypositions=x[51:100], cutoff=8.0, output=0.0)
@test unitcelltype(sys2) == NonPeriodicCell
@test sys2.unitcell [35.63 0.0 0.0; 0.0 35.76 0.0; 0.0 0.0 35.79] atol = 1e-2
update_cutoff!(sys2, 10.0)
@test sys2.unitcell [39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99] atol = 1e-2
a = @ballocated update_cutoff!($sys2, 8.0) evals = 1 samples = 1
@test a == 0
@test a == Allocs(0)
end

#
Expand Down Expand Up @@ -946,44 +948,45 @@ function UpdateParticleSystem!(sys::ParticleSystem2, update_lists::Bool=true)
end

# this updates must be non-allocating in the serial case
@testitem "UpdateParticleSystem!" begin
@testitem "UpdateParticleSystem!" setup=[AllocTest] begin
using BenchmarkTools
using StaticArrays
using CellListMap
using .AllocTest: Allocs
x = rand(SVector{3,Float64}, 1000)
sys = ParticleSystem(xpositions=x, unitcell=[1.0, 1.0, 1.0], cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)
y = rand(SVector{3,Float64}, 1000)
sys = ParticleSystem(xpositions=x, ypositions=y, unitcell=[1.0, 1.0, 1.0], cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)

# Test construction with more general abstract vectors
x = @view(x[1:500])
sys = ParticleSystem(xpositions=x, unitcell=[1.0, 1.0, 1.0], cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)
y = @view(y[1:500])
sys = ParticleSystem(xpositions=x, ypositions=y, unitcell=[1.0, 1.0, 1.0], cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)

# Update with matrices
x = rand(3, 500)
sys = ParticleSystem(xpositions=x, unitcell=[1.0, 1.0, 1.0], cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)

# Update non-periodic system
x = rand(SVector{3,Float64}, 1000)
sys = ParticleSystem(xpositions=x, cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)
y = rand(SVector{3,Float64}, 1000)
sys = ParticleSystem(xpositions=x, ypositions=y, cutoff=0.1, output=0.0, parallel=false)
a = @ballocated CellListMap.UpdateParticleSystem!($sys) samples = 1 evals = 1
@test a == 0
@test a == Allocs(0)

# Throw error when trying to *not* update lists with autoswap on:
sys = ParticleSystem(
Expand Down
27 changes: 14 additions & 13 deletions src/neighborlists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@ end

end

@testitem "Allocations" begin
@testitem "Allocations" setup=[AllocTest] begin
using CellListMap
using StaticArrays
using BenchmarkTools
using .AllocTest: Allocs

#
# Single set of particles
Expand All @@ -438,23 +439,23 @@ end
neighborlist!(system)
x = rand(SVector{3,Float64}, 10^3)
allocs = @ballocated update!($system, $x) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x; cutoff=0.2) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated neighborlist!($system) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)

# Non-Periodic systems
x = rand(SVector{3,Float64}, 10^3)
system = InPlaceNeighborList(x=x, cutoff=0.1, parallel=false)
neighborlist!(system)
x = rand(SVector{3,Float64}, 10^3)
allocs = @ballocated update!($system, $x) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x; cutoff=0.2) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated neighborlist!($system) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)

#
# Two sets of particles
Expand All @@ -467,11 +468,11 @@ end
x = rand(SVector{3,Float64}, 10^3)
y = rand(SVector{3,Float64}, 10^3)
allocs = @ballocated neighborlist!($system) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x, $y) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x, $y; cutoff=0.2) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)

# Non-Periodic systems
y = rand(SVector{3,Float64}, 10^3)
Expand All @@ -480,11 +481,11 @@ end
x = rand(SVector{3,Float64}, 10^3)
y = rand(SVector{3,Float64}, 10^3)
allocs = @ballocated neighborlist!($system) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x, $y) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)
allocs = @ballocated update!($system, $x, $y; cutoff=0.2) evals = 1 samples = 1
@test allocs == 0
@test allocs == Allocs(0)

end

Expand Down
Loading

0 comments on commit ced6b79

Please sign in to comment.