From ba9c22f5a9ca97b5c982298642724057c2ddcddc Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Dec 2024 09:14:46 -0500 Subject: [PATCH 1/2] Skeleton update --- .github/workflows/CI.yml | 79 -------------- .github/workflows/Documentation.yml | 23 +++++ .github/workflows/FormatCheck.yml | 36 ++----- .github/workflows/LiterateCheck.yml | 48 ++------- .github/workflows/Tests.yml | 42 ++++++++ .pre-commit-config.yaml | 7 +- Project.toml | 9 -- README.md | 155 +++------------------------- docs/Project.toml | 9 -- examples/Project.toml | 9 -- examples/README.jl | 32 +++--- test/Project.toml | 10 -- test/basics/test_extensions.jl | 2 + test/runtests.jl | 64 +++++++++++- test/test_aqua.jl | 8 ++ 15 files changed, 188 insertions(+), 345 deletions(-) delete mode 100644 .github/workflows/CI.yml create mode 100644 .github/workflows/Documentation.yml create mode 100644 .github/workflows/Tests.yml create mode 100644 test/basics/test_extensions.jl create mode 100644 test/test_aqua.jl diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml deleted file mode 100644 index 9071f897..00000000 --- a/.github/workflows/CI.yml +++ /dev/null @@ -1,79 +0,0 @@ -name: CI -on: - push: - branches: - - main - tags: ['*'] - pull_request: - workflow_dispatch: -concurrency: - # Skip intermediate builds: always. - # Cancel intermediate builds: only if it is a pull request build. - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} -jobs: - test: - name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - runs-on: ${{ matrix.os }} - timeout-minutes: 60 - permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created - actions: write - contents: read - strategy: - fail-fast: false - matrix: - version: - # - 'lts' # TODO: Reenable once dependencies are registered. - - '1' - os: - - ubuntu-latest - - macOS-latest - - windows-latest - arch: - - x64 - steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: julia-actions/cache@v2 - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-runtest@v1 - - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v5 - with: - files: lcov.info - token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: false - docs: - name: Documentation - runs-on: ubuntu-latest - permissions: - actions: write # needed to allow julia-actions/cache to proactively delete old caches that it has created - contents: write - statuses: write - steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 - with: - version: '1' - - uses: julia-actions/cache@v2 - - name: Configure doc environment - shell: julia --project=docs --color=yes {0} - run: | - using Pkg - Pkg.develop(PackageSpec(path=pwd())) - Pkg.instantiate() - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-docdeploy@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} - - name: Run doctests - shell: julia --project=docs --color=yes {0} - run: | - using Documenter: DocMeta, doctest - using BlockSparseArrays - DocMeta.setdocmeta!(BlockSparseArrays, :DocTestSetup, :(using BlockSparseArrays); recursive=true) - doctest(BlockSparseArrays) diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml new file mode 100644 index 00000000..01a7f7a4 --- /dev/null +++ b/.github/workflows/Documentation.yml @@ -0,0 +1,23 @@ +name: "Documentation" + +on: + push: + branches: + - main + tags: '*' + pull_request: + schedule: + - cron: '1 4 * * 4' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref_name != github.event.repository.default_branch || github.ref != 'refs/tags/v*' }} + +jobs: + build-and-deploy-docs: + name: "Documentation" + uses: "ITensor/ITensorActions/.github/workflows/Documentation.yml@main" + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/FormatCheck.yml b/.github/workflows/FormatCheck.yml index bb6d9333..3f78afc2 100644 --- a/.github/workflows/FormatCheck.yml +++ b/.github/workflows/FormatCheck.yml @@ -1,35 +1,13 @@ -name: Format check +name: "Format Check" + on: push: - branches: [main] - tags: [v*] + branches: + - 'main' + tags: '*' pull_request: jobs: - format: + format-check: name: "Format Check" - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 - with: - version: 1 - - name: Install JuliaFormatter and format - run: | - julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))' - julia -e 'using JuliaFormatter; format(".", verbose=true)' - - name: Check format - run: | - julia -e ' - out = Cmd(`git diff --name-only`) |> read |> String - if out == "" - exit(0) - else - @error "The following files have not been formatted:" - write(stdout, out) - out_diff = Cmd(`git diff`) |> read |> String - @error "Diff:" - write(stdout, out_diff) - exit(1) - @error "" - end' + uses: "ITensor/ITensorActions/.github/workflows/FormatCheck.yml@main" diff --git a/.github/workflows/LiterateCheck.yml b/.github/workflows/LiterateCheck.yml index 05566d15..2ca5f27e 100644 --- a/.github/workflows/LiterateCheck.yml +++ b/.github/workflows/LiterateCheck.yml @@ -1,47 +1,15 @@ -name: Literate check +name: "Literate Check" + on: push: - branches: [main] - tags: [v*] + branches: + - 'main' + tags: '*' pull_request: jobs: literate: name: "Literate Check" - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 - with: - version: 1 - - name: Install Literate and generate docs - run: | - julia -e ' - using Pkg - # TODO: Delete these once they are registered. - Pkg.add(url="https://github.com/ITensor/TypeParameterAccessors.jl") - Pkg.add(url="https://github.com/ITensor/BroadcastMapConversion.jl") - Pkg.add(url="https://github.com/ITensor/NestedPermutedDimsArrays.jl") - Pkg.add(url="https://github.com/ITensor/LabelledNumbers.jl") - Pkg.add(url="https://github.com/ITensor/GradedUnitRanges.jl") - Pkg.add(url="https://github.com/ITensor/SparseArraysBase.jl") - Pkg.add(url="https://github.com/ITensor/TensorAlgebra.jl") - Pkg.develop(PackageSpec(path=pwd())) - Pkg.instantiate() - Pkg.add(PackageSpec(name="Literate"))' - julia -e 'include("docs/make_readme.jl")' - - name: Check if docs need to be updated - run: | - julia -e ' - out = Cmd(`git diff --name-only`) |> read |> String - if out == "" - exit(0) - else - @error "The docs are outdated, rerun Literate to regenerate them." - write(stdout, out) - out_diff = Cmd(`git diff`) |> read |> String - @error "Diff:" - write(stdout, out_diff) - exit(1) - @error "" - end' + uses: "ITensor/ITensorActions/.github/workflows/LiterateCheck.yml@main" + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml new file mode 100644 index 00000000..5a0a3064 --- /dev/null +++ b/.github/workflows/Tests.yml @@ -0,0 +1,42 @@ +name: Tests +on: + push: + branches: + - 'master' + - 'main' + - 'release-' + tags: '*' + paths-ignore: + - 'docs/**' + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + # Cancel intermediate builds: only if it is a pull request build. + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} + +jobs: + tests: + name: "Tests" + strategy: + fail-fast: false + matrix: + version: + - 'lts' # minimal supported version + - '1' # latest released Julia version + # group: + # - 'core' + # - 'optional' + os: + - ubuntu-latest + - macOS-latest + - windows-latest + uses: "ITensor/ITensorActions/.github/workflows/Tests.yml@main" + with: + group: "${{ matrix.group }}" + julia-version: "${{ matrix.version }}" + os: "${{ matrix.os }}" + localregistry: https://github.com/ITensor/ITensorRegistry.git + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bff1fb7f..65993659 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,8 @@ repos: - id: check-yaml - id: end-of-file-fixer exclude_types: [markdown] # incompatible with Literate.jl -- repo: https://github.com/qiaojunfeng/pre-commit-julia-format - rev: v0.2.0 + +- repo: "https://github.com/domluna/JuliaFormatter.jl" + rev: v1.0.62 hooks: - - id: julia-format + - id: "julia-formatter" diff --git a/Project.toml b/Project.toml index 0c761de0..f3e09817 100644 --- a/Project.toml +++ b/Project.toml @@ -20,15 +20,6 @@ SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" -[sources] -BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} -GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"} -LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"} -NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} -SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} -TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} -TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} - [compat] Adapt = "4.1.1" Aqua = "0.8.9" diff --git a/README.md b/README.md index 05cd64e2..7d6a93e4 100644 --- a/README.md +++ b/README.md @@ -2,163 +2,40 @@ [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://ITensor.github.io/BlockSparseArrays.jl/stable/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://ITensor.github.io/BlockSparseArrays.jl/dev/) -[![Build Status](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/CI.yml?query=branch%3Amain) +[![Build Status](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/Tests.yml/badge.svg?branch=main)](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/Tests.yml?query=branch%3Amain) [![Coverage](https://codecov.io/gh/ITensor/BlockSparseArrays.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ITensor/BlockSparseArrays.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) [![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) -A block sparse array type in Julia based on the [`BlockArrays.jl`](https://github.com/JuliaArrays/BlockArrays.jl) interface. - ## Installation instructions +This package resides in the `ITensor/ITensorRegistry` local registry. +In order to install, simply add that registry through your package manager. +This step is only required once. ```julia julia> using Pkg: Pkg -julia> Pkg.add(url="https://github.com/ITensor/BroadcastMapConversion.jl") - -julia> Pkg.add(url="https://github.com/ITensor/NestedPermutedDimsArrays.jl") - -julia> Pkg.add(url="https://github.com/ITensor/TypeParameterAccessors.jl") - -julia> Pkg.add(url="https://github.com/ITensor/LabelledNumbers.jl") - -julia> Pkg.add(url="https://github.com/ITensor/GradedUnitRanges.jl") - -julia> Pkg.add(url="https://github.com/ITensor/SparseArraysBase.jl") +julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry") +``` +or: +```julia +julia> Pkg.Registry.add(url="git@github.com:ITensor/ITensorRegistry.git") +``` +if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages. -julia> Pkg.add(url="https://github.com/ITensor/TensorAlgebra.jl") +Then, the package can be added as usual through the package manager: -julia> Pkg.add(url="https://github.com/ITensor/BlockSparseArrays.jl") +```julia +julia> Pkg.add("BlockSparseArrays") ``` ## Examples ````julia -using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange -using BlockSparseArrays: BlockSparseArray, block_stored_length -using Test: @test, @test_broken - -function main() - # Block dimensions - i1 = [2, 3] - i2 = [2, 3] - - i_axes = (blockedrange(i1), blockedrange(i2)) - - function block_size(axes, block) - return length.(getindex.(axes, Block.(block.n))) - end - - # Data - nz_blocks = Block.([(1, 1), (2, 2)]) - nz_block_sizes = [block_size(i_axes, nz_block) for nz_block in nz_blocks] - nz_block_lengths = prod.(nz_block_sizes) - - # Blocks with contiguous underlying data - d_data = BlockedVector(randn(sum(nz_block_lengths)), nz_block_lengths) - d_blocks = [ - reshape(@view(d_data[Block(i)]), block_size(i_axes, nz_blocks[i])) for - i in 1:length(nz_blocks) - ] - b = BlockSparseArray(nz_blocks, d_blocks, i_axes) - - @test block_stored_length(b) == 2 - - # Blocks with discontiguous underlying data - d_blocks = randn.(nz_block_sizes) - b = BlockSparseArray(nz_blocks, d_blocks, i_axes) - - @test block_stored_length(b) == 2 - - # Access a block - @test b[Block(1, 1)] == d_blocks[1] - - # Access a zero block, returns a zero matrix - @test b[Block(1, 2)] == zeros(2, 3) - - # Set a zero block - a₁₂ = randn(2, 3) - b[Block(1, 2)] = a₁₂ - @test b[Block(1, 2)] == a₁₂ - - # Matrix multiplication - # TODO: Fix this, broken. - @test_broken b * b ≈ Array(b) * Array(b) - - permuted_b = permutedims(b, (2, 1)) - @test permuted_b isa BlockSparseArray - @test permuted_b == permutedims(Array(b), (2, 1)) - - @test b + b ≈ Array(b) + Array(b) - @test b + b isa BlockSparseArray - # TODO: Fix this, broken. - @test_broken block_stored_length(b + b) == 2 - - scaled_b = 2b - @test scaled_b ≈ 2Array(b) - @test scaled_b isa BlockSparseArray - - # TODO: Fix this, broken. - @test_broken reshape(b, ([4, 6, 6, 9],)) isa BlockSparseArray{<:Any,1} - - return nothing -end - -main() +using BlockSparseArrays: BlockSparseArrays ```` -# BlockSparseArrays.jl and BlockArrays.jl interface - -````julia -using BlockArrays: BlockArrays, Block -using BlockSparseArrays: BlockSparseArray - -i1 = [2, 3] -i2 = [2, 3] -B = BlockSparseArray{Float64}(i1, i2) -B[Block(1, 1)] = randn(2, 2) -B[Block(2, 2)] = randn(3, 3) - -# Minimal interface - -# Specifies the block structure -@show collect.(BlockArrays.blockaxes(axes(B, 1))) - -# Index range of a block -@show axes(B, 1)[Block(1)] - -# Last index of each block -@show BlockArrays.blocklasts(axes(B, 1)) - -# Find the block containing the index -@show BlockArrays.findblock(axes(B, 1), 3) - -# Retrieve a block -@show B[Block(1, 1)] -@show BlockArrays.viewblock(B, Block(1, 1)) - -# Check block bounds -@show BlockArrays.blockcheckbounds(B, 2, 2) -@show BlockArrays.blockcheckbounds(B, Block(2, 2)) - -# Derived interface - -# Specifies the block structure -@show collect(Iterators.product(BlockArrays.blockaxes(B)...)) - -# Iterate over block views -@show sum.(BlockArrays.eachblock(B)) - -# Reshape into 1-d -# TODO: Fix this, broken. -# @show BlockArrays.blockvec(B)[Block(1)] - -# Array-of-array view -@show BlockArrays.blocks(B)[1, 1] == B[Block(1, 1)] - -# Access an index within a block -@show B[Block(1, 1)[1, 1]] == B[1, 1] -```` +Examples go here. --- diff --git a/docs/Project.toml b/docs/Project.toml index 4a682293..6aaf4732 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,12 +10,3 @@ NestedPermutedDimsArrays = "2c2a8ec4-3cfc-4276-aa3e-1307b4294e58" SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" - -[sources] -BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} -GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"} -LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"} -NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} -SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} -TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} -TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/examples/Project.toml b/examples/Project.toml index e743f224..2fccb0f0 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -11,12 +11,3 @@ SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" - -[sources] -BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} -GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"} -LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"} -NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} -SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} -TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} -TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/examples/README.jl b/examples/README.jl index efde76de..ec6b0865 100644 --- a/examples/README.jl +++ b/examples/README.jl @@ -2,7 +2,7 @@ # # [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://ITensor.github.io/BlockSparseArrays.jl/stable/) # [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://ITensor.github.io/BlockSparseArrays.jl/dev/) -# [![Build Status](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/CI.yml?query=branch%3Amain) +# [![Build Status](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/Tests.yml/badge.svg?branch=main)](https://github.com/ITensor/BlockSparseArrays.jl/actions/workflows/Tests.yml?query=branch%3Amain) # [![Coverage](https://codecov.io/gh/ITensor/BlockSparseArrays.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ITensor/BlockSparseArrays.jl) # [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) # [![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) @@ -11,25 +11,29 @@ # ## Installation instructions +# This package resides in the `ITensor/ITensorRegistry` local registry. +# In order to install, simply add that registry through your package manager. +# This step is only required once. #= ```julia julia> using Pkg: Pkg -julia> Pkg.add(url="https://github.com/ITensor/BroadcastMapConversion.jl") - -julia> Pkg.add(url="https://github.com/ITensor/NestedPermutedDimsArrays.jl") - -julia> Pkg.add(url="https://github.com/ITensor/TypeParameterAccessors.jl") - -julia> Pkg.add(url="https://github.com/ITensor/LabelledNumbers.jl") - -julia> Pkg.add(url="https://github.com/ITensor/GradedUnitRanges.jl") - -julia> Pkg.add(url="https://github.com/ITensor/SparseArraysBase.jl") +julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry") +``` +=# +# or: +#= +```julia +julia> Pkg.Registry.add(url="git@github.com:ITensor/ITensorRegistry.git") +``` +=# +# if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages. -julia> Pkg.add(url="https://github.com/ITensor/TensorAlgebra.jl") +# Then, the package can be added as usual through the package manager: -julia> Pkg.add(url="https://github.com/ITensor/BlockSparseArrays.jl") +#= +```julia +julia> Pkg.add("BlockSparseArrays") ``` =# diff --git a/test/Project.toml b/test/Project.toml index bb2b12d8..36367a6f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -16,13 +16,3 @@ SymmetrySectors = "f8a8ad64-adbc-4fce-92f7-ffe2bb36a86e" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" - -[sources] -BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} -GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"} -LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"} -NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} -SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} -SymmetrySectors = {url = "https://github.com/ITensor/SymmetrySectors.jl"} -TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} -TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/test/basics/test_extensions.jl b/test/basics/test_extensions.jl new file mode 100644 index 00000000..3506e33e --- /dev/null +++ b/test/basics/test_extensions.jl @@ -0,0 +1,2 @@ +include("../../ext/BlockSparseArraysTensorAlgebraExt/test/runtests.jl") +include("../../ext/BlockSparseArraysGradedUnitRangesExt/test/runtests.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 9bbc63ba..bd974411 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,61 @@ -@eval module $(gensym()) -include("test_basics.jl") -include("../ext/BlockSparseArraysTensorAlgebraExt/test/runtests.jl") -include("../ext/BlockSparseArraysGradedUnitRangesExt/test/runtests.jl") +using SafeTestsets: @safetestset +using Suppressor: Suppressor + +# check for filtered groups +# either via `--group=ALL` or through ENV["GROUP"] +const pat = r"(?:--group=)(\w+)" +arg_id = findfirst(contains(pat), ARGS) +const GROUP = uppercase( + if isnothing(arg_id) + get(ENV, "GROUP", "ALL") + else + only(match(pat, ARGS[arg_id]).captures) + end, +) + +"match files of the form `test_*.jl`, but exclude `*setup*.jl`" +istestfile(fn) = + endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup") +"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`" +isexamplefile(fn) = + endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup") + +@time begin + # tests in groups based on folder structure + for testgroup in filter(isdir, readdir(@__DIR__)) + if GROUP == "ALL" || GROUP == uppercase(testgroup) + for file in filter(istestfile, readdir(joinpath(@__DIR__, testgroup); join=true)) + @eval @safetestset $file begin + include($file) + end + end + end + end + + # single files in top folder + for file in filter(istestfile, readdir(@__DIR__)) + (file == basename(@__FILE__)) && continue # exclude this file to avoid infinite recursion + @eval @safetestset $file begin + include($file) + end + end + + # test examples + examplepath = joinpath(@__DIR__, "..", "examples") + for (root, _, files) in walkdir(examplepath) + contains(chopprefix(root, @__DIR__), "setup") && continue + for file in filter(isexamplefile, files) + filename = joinpath(root, file) + @eval begin + @safetestset $file begin + $(Expr( + :macrocall, + GlobalRef(Suppressor, Symbol("@suppress")), + LineNumberNode(@__LINE__, @__FILE__), + :(include($filename)), + )) + end + end + end + end end diff --git a/test/test_aqua.jl b/test/test_aqua.jl new file mode 100644 index 00000000..6f678e2b --- /dev/null +++ b/test/test_aqua.jl @@ -0,0 +1,8 @@ +using BlockSparseArrays: BlockSparseArrays +using Aqua: Aqua +using Test: @testset + +@testset "Code quality (Aqua.jl)" begin + # TODO: Fix Aqua issues and add this back. + # Aqua.test_all(BlockSparseArrays) +end From 1f0dea3bf54dde875f9bfe551f67b6c277c0263b Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Dec 2024 09:18:34 -0500 Subject: [PATCH 2/2] Try fixing tests --- README.md | 127 +++++++++++++++++++++++++++++++++++++++++++++- test/Project.toml | 3 ++ 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d6a93e4..19c582b8 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) [![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) +A block sparse array type in Julia based on the [`BlockArrays.jl`](https://github.com/JuliaArrays/BlockArrays.jl) interface. + ## Installation instructions This package resides in the `ITensor/ITensorRegistry` local registry. @@ -32,10 +34,131 @@ julia> Pkg.add("BlockSparseArrays") ## Examples ````julia -using BlockSparseArrays: BlockSparseArrays +using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange +using BlockSparseArrays: BlockSparseArray, block_stored_length +using Test: @test, @test_broken + +function main() + # Block dimensions + i1 = [2, 3] + i2 = [2, 3] + + i_axes = (blockedrange(i1), blockedrange(i2)) + + function block_size(axes, block) + return length.(getindex.(axes, Block.(block.n))) + end + + # Data + nz_blocks = Block.([(1, 1), (2, 2)]) + nz_block_sizes = [block_size(i_axes, nz_block) for nz_block in nz_blocks] + nz_block_lengths = prod.(nz_block_sizes) + + # Blocks with contiguous underlying data + d_data = BlockedVector(randn(sum(nz_block_lengths)), nz_block_lengths) + d_blocks = [ + reshape(@view(d_data[Block(i)]), block_size(i_axes, nz_blocks[i])) for + i in 1:length(nz_blocks) + ] + b = BlockSparseArray(nz_blocks, d_blocks, i_axes) + + @test block_stored_length(b) == 2 + + # Blocks with discontiguous underlying data + d_blocks = randn.(nz_block_sizes) + b = BlockSparseArray(nz_blocks, d_blocks, i_axes) + + @test block_stored_length(b) == 2 + + # Access a block + @test b[Block(1, 1)] == d_blocks[1] + + # Access a zero block, returns a zero matrix + @test b[Block(1, 2)] == zeros(2, 3) + + # Set a zero block + a₁₂ = randn(2, 3) + b[Block(1, 2)] = a₁₂ + @test b[Block(1, 2)] == a₁₂ + + # Matrix multiplication + # TODO: Fix this, broken. + @test_broken b * b ≈ Array(b) * Array(b) + + permuted_b = permutedims(b, (2, 1)) + @test permuted_b isa BlockSparseArray + @test permuted_b == permutedims(Array(b), (2, 1)) + + @test b + b ≈ Array(b) + Array(b) + @test b + b isa BlockSparseArray + # TODO: Fix this, broken. + @test_broken block_stored_length(b + b) == 2 + + scaled_b = 2b + @test scaled_b ≈ 2Array(b) + @test scaled_b isa BlockSparseArray + + # TODO: Fix this, broken. + @test_broken reshape(b, ([4, 6, 6, 9],)) isa BlockSparseArray{<:Any,1} + + return nothing +end + +main() ```` -Examples go here. +# BlockSparseArrays.jl and BlockArrays.jl interface + +````julia +using BlockArrays: BlockArrays, Block +using BlockSparseArrays: BlockSparseArray + +i1 = [2, 3] +i2 = [2, 3] +B = BlockSparseArray{Float64}(i1, i2) +B[Block(1, 1)] = randn(2, 2) +B[Block(2, 2)] = randn(3, 3) + +# Minimal interface + +# Specifies the block structure +@show collect.(BlockArrays.blockaxes(axes(B, 1))) + +# Index range of a block +@show axes(B, 1)[Block(1)] + +# Last index of each block +@show BlockArrays.blocklasts(axes(B, 1)) + +# Find the block containing the index +@show BlockArrays.findblock(axes(B, 1), 3) + +# Retrieve a block +@show B[Block(1, 1)] +@show BlockArrays.viewblock(B, Block(1, 1)) + +# Check block bounds +@show BlockArrays.blockcheckbounds(B, 2, 2) +@show BlockArrays.blockcheckbounds(B, Block(2, 2)) + +# Derived interface + +# Specifies the block structure +@show collect(Iterators.product(BlockArrays.blockaxes(B)...)) + +# Iterate over block views +@show sum.(BlockArrays.eachblock(B)) + +# Reshape into 1-d +# TODO: Fix this, broken. +# @show BlockArrays.blockvec(B)[Block(1)] + +# Array-of-array view +@show BlockArrays.blocks(B)[1, 1] == B[Block(1, 1)] + +# Access an index within a block +@show B[Block(1, 1)[1, 1]] == B[1, 1] +```` --- diff --git a/test/Project.toml b/test/Project.toml index 36367a6f..dec00515 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,5 @@ [deps] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" BlockSparseArrays = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" @@ -11,7 +12,9 @@ NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" NestedPermutedDimsArrays = "2c2a8ec4-3cfc-4276-aa3e-1307b4294e58" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" +Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" SymmetrySectors = "f8a8ad64-adbc-4fce-92f7-ffe2bb36a86e" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"