linkcell 0.4.2

Periodic linked-cell cutoff pairs and k-nearest neighbour search
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
name: ci

on:
  push:
    branches:
      - main
      - "perf/**"
      - "fix/**"
      - "feat/**"
      - "ci/**"
      - "release/**"
    tags: ['v*']
  pull_request:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/') }}

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
        with:
          components: rustfmt
      - run: cargo fmt --check
      - run: python3 scripts/check-llms-txt.py
      - run: python3 scripts/check-versions.py
      - name: The tag names the version
        if: github.ref_type == 'tag'
        run: python3 scripts/check-versions.py --tag "$GITHUB_REF_NAME"

  # The Pages build, on every push and pull request, with rustdoc warnings
  # as errors. pages.yml deploys from main only.
  docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: "3.12"
      - name: Site
        run: |
          mkdir -p docs/assets && cp -a assets/branding docs/assets/branding
          python -m pip install "mkdocs==1.6.1" "mkdocs-material==9.5.49"
          mkdocs build --site-dir _site
      - name: Rust API reference
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc --no-deps --locked --lib

  header-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      # Pin matches the version comment cbindgen writes into include/linkcell.h.
      - run: cargo install cbindgen --version 0.29.4
      - run: scripts/regen-capi-headers.sh --check

  python:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.12'
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-py-${{ hashFiles('Cargo.lock', 'python/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-py-
      - name: Install Python test deps
        run: |
          python -m venv .venv
          . .venv/bin/activate
          python -m pip install --upgrade pip
          pip install maturin pytest numpy vesin minimage
          pip install torch --index-url https://download.pytorch.org/whl/cpu
      - name: maturin develop
        run: |
          . .venv/bin/activate
          maturin develop
      - name: pytest
        run: |
          . .venv/bin/activate
          pytest tests/python -q

  cargo:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest, windows-latest]
        crate:
          - name: default
            flags: ""
          - name: serial
            flags: --no-default-features --features capi
    name: cargo (${{ matrix.os }}, ${{ matrix.crate.name }})
    runs-on: ${{ matrix.os }}
    steps:
      - name: CPU flags
        shell: bash
        run: |
          echo "arch=$(uname -m)"
          grep -m1 'model name' /proc/cpuinfo || true
          grep -m1 flags /proc/cpuinfo || true
          sysctl -n machdep.cpu.brand_string 2>/dev/null || true
          lscpu || true
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
        with:
          components: clippy
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      - run: cargo test --locked ${{ matrix.crate.flags }}
      - name: SIMD paths exercised
        shell: bash
        run: cargo test --locked ${{ matrix.crate.flags }} --lib simd_paths_exercised -- --nocapture
      - run: cargo clippy --locked --all-targets ${{ matrix.crate.flags }} -- -D warnings

  # The k-nearest region built by Clang calls libomp. The library, the
  # tests and the Meson build must load libomp alone, never libgomp.
  clang-libomp:
    runs-on: ubuntu-latest
    env:
      CC: clang-21
      CXX: clang++-21
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
        with:
          components: clippy
      - name: Install Clang 21 and libomp
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key \
            | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc >/dev/null
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" \
            | sudo tee /etc/apt/sources.list.d/llvm-21.list
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends clang-21 libomp-21-dev
          clang-21 --version
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-clang-cargo-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-clang-cargo-
      - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.12'
      - run: pip install meson ninja
      - name: Runtime check
        run: |
          cat > "$RUNNER_TEMP/only-libomp.sh" <<'EOF'
          #!/usr/bin/env bash
          # Fail unless every argument needs libomp and no other OpenMP runtime.
          set -euo pipefail
          for lib in "$@"; do
            needed=$(readelf -d "$lib" | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p')
            printf '%s needs:\n%s\n' "$lib" "$needed"
            grep -q '^libomp\.so' <<<"$needed" || { echo "::error::$lib does not need libomp"; exit 1; }
            if grep -E 'gomp|iomp' <<<"$needed"; then
              echo "::error::$lib needs another OpenMP runtime"
              exit 1
            fi
            loaded=$(ldd "$lib")
            echo "$loaded"
            if grep -E 'libgomp|libiomp' <<<"$loaded"; then
              echo "::error::$lib loads another OpenMP runtime"
              exit 1
            fi
          done
          EOF
          chmod +x "$RUNNER_TEMP/only-libomp.sh"
      # The build puts liblinkcell.so where the team test dlopens it.
      - run: cargo build --locked
      - name: liblinkcell.so needs libomp alone
        run: '"$RUNNER_TEMP/only-libomp.sh" target/debug/liblinkcell.so'
      - run: cargo test --locked
      - run: cargo clippy --locked --all-targets -- -D warnings
      - run: meson setup build --prefix "$PWD/prefix" --libdir lib
      # An activated conda or pixi environment exports its GCC as CC.
      # Meson's Clang still builds the region.
      - run: meson compile -C build
        env:
          CC: gcc
      - run: meson test -C build --print-errorlogs
      - run: meson install -C build
      - name: Installed library and pkg-config
        run: |
          "$RUNNER_TEMP/only-libomp.sh" prefix/lib/liblinkcell.so
          export PKG_CONFIG_PATH="$PWD/prefix/lib/pkgconfig"
          libs=$(pkg-config --static --libs linkcell)
          echo "$libs"
          grep -qw -- -lomp <<<"$libs"
          if grep -w -- -lgomp <<<"$libs"; then exit 1; fi

  # Clang's -print-file-name does not search LIBRARY_PATH. A libomp the
  # compiler reaches only through LIBRARY_PATH and CPATH, as a sanitizer
  # build of the runtime is, must still be found and linked alone. The
  # packages are unpacked, not installed, so the runner's Clang has no
  # libomp of its own.
  clang-library-path:
    runs-on: ubuntu-latest
    env:
      CC: clang
      CXX: clang++
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.12'
      - run: pip install meson ninja
      - name: Unpack libomp 21 into a private prefix
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key \
            | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc >/dev/null
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" \
            | sudo tee /etc/apt/sources.list.d/llvm-21.list
          sudo apt-get update
          cd "$RUNNER_TEMP"
          apt-get download libomp-21-dev libomp5
          for deb in libomp*.deb; do dpkg -x "$deb" omp; done
          lib="$RUNNER_TEMP/omp/usr/lib/x86_64-linux-gnu"
          inc="$RUNNER_TEMP/omp/usr/lib/llvm-21/lib/clang/21/include"
          test -f "$lib/libomp.so" && test -f "$inc/omp.h"
          if [ -f "$(clang -print-file-name=libomp.so)" ]; then
            echo "::error::clang finds a libomp of its own; LIBRARY_PATH would not be exercised"
            exit 1
          fi
          {
            echo "LIBRARY_PATH=$lib"
            echo "CPATH=$inc"
            echo "LD_LIBRARY_PATH=$lib"
            echo "LINKCELL_OMP_LIB=$lib"
          } >> "$GITHUB_ENV"
      - name: Runtime check
        run: |
          cat > "$RUNNER_TEMP/private-libomp.sh" <<'EOF'
          #!/usr/bin/env bash
          # Fail unless every argument loads the private libomp and no other runtime.
          set -euo pipefail
          for lib in "$@"; do
            needed=$(readelf -d "$lib" | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p')
            printf '%s needs:\n%s\n' "$lib" "$needed"
            grep -q '^libomp\.so' <<<"$needed" || { echo "::error::$lib does not need libomp"; exit 1; }
            if grep -E 'gomp|iomp' <<<"$needed"; then
              echo "::error::$lib needs another OpenMP runtime"
              exit 1
            fi
            loaded=$(ldd "$lib")
            echo "$loaded"
            grep -q "libomp\.so.* => $LINKCELL_OMP_LIB/" <<<"$loaded" \
              || { echo "::error::$lib does not load libomp from $LINKCELL_OMP_LIB"; exit 1; }
            if grep -E 'libgomp|libiomp' <<<"$loaded"; then
              echo "::error::$lib loads another OpenMP runtime"
              exit 1
            fi
          done
          EOF
          chmod +x "$RUNNER_TEMP/private-libomp.sh"
      - run: cargo build --locked
      - name: liblinkcell.so needs the private libomp alone
        run: '"$RUNNER_TEMP/private-libomp.sh" target/debug/liblinkcell.so'
      - run: cargo test --locked
      - run: meson setup build
      - run: meson compile -C build
      - run: meson test -C build --print-errorlogs
      - name: Meson's liblinkcell.so needs the private libomp alone
        run: '"$RUNNER_TEMP/private-libomp.sh" build/liblinkcell.so'

  meson:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    name: meson (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - name: Configure MSVC
        if: runner.os == 'Windows'
        shell: pwsh
        run: .github/scripts/setup-msvc.ps1
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.12'
      - run: pip install meson ninja
      - name: Meson setup
        if: runner.os != 'Windows'
        run: meson setup build --prefix "$PWD/prefix" --libdir lib
        shell: bash
      - name: Meson setup
        if: runner.os == 'Windows'
        run: meson setup build --prefix "$env:GITHUB_WORKSPACE/prefix" --libdir lib
        shell: pwsh
      - run: meson compile -C build
      - run: meson test -C build --print-errorlogs
      - run: meson install -C build
      - name: Installed headers and libraries
        shell: bash
        run: |
          prefix="$PWD/prefix"
          test -f "$prefix/include/linkcell.h"
          test -f "$prefix/include/linkcell.hpp"
          test -f "$prefix/include/linkcell_gpu.h"
          test -f "$prefix/include/linkcell_gpu.hpp"
          case "$RUNNER_OS" in
            Windows)
              test -f "$prefix/bin/linkcell.dll"
              test -f "$prefix/lib/linkcell.dll.lib"
              test -f "$prefix/lib/linkcell.lib"
              test -f "$prefix/lib/linkcell_gpu.lib"
              ;;
            macOS)
              test -f "$prefix/lib/liblinkcell.dylib"
              test -f "$prefix/lib/liblinkcell.a"
              test -f "$prefix/lib/liblinkcell_gpu.a"
              ;;
            Linux)
              test -f "$prefix/lib/liblinkcell.so"
              test -f "$prefix/lib/liblinkcell.a"
              test -f "$prefix/lib/liblinkcell_gpu.a"
              ;;
          esac
      - name: pkg-config after meson install
        if: runner.os != 'Windows'
        run: |
          prefix="$PWD/prefix"
          export PKG_CONFIG_PATH="$prefix/lib/pkgconfig"
          pkg-config --exists --print-errors linkcell
          pkg-config --modversion linkcell
          pkg-config --cflags --libs linkcell
          pkg-config --exists --print-errors linkcell-gpu
          pkg-config --modversion linkcell-gpu
          read -ra gpu_flags <<<"$(pkg-config --cflags --libs linkcell-gpu)"
          c++ -std=c++17 tests/cmake-consumer/gpu.cpp \
            "${gpu_flags[@]}" \
            -o build/installed-gpu-consumer

  cmake:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    name: cmake (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - name: Configure MSVC
        if: runner.os == 'Windows'
        shell: pwsh
        run: .github/scripts/setup-msvc.ps1
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      - run: cmake -S . -B build -DCMAKE_INSTALL_PREFIX="$PWD/prefix"
        shell: bash
      - run: cmake --build build --config Release
      - run: ctest --test-dir build -C Release --output-on-failure
      - run: cmake --install build --config Release
      - name: find_package after cmake install
        shell: bash
        run: |
          test -f prefix/include/linkcell.h
          test -f prefix/lib/cmake/linkcell/linkcellConfig.cmake
          if [[ "$RUNNER_OS" == "Windows" ]]; then
            test -f prefix/bin/linkcell.dll
            test -f prefix/lib/linkcell.dll.lib
            test -f prefix/lib/linkcell.lib
            test -f prefix/lib/linkcell_gpu.lib
          elif [[ "$RUNNER_OS" == "macOS" ]]; then
            test -f prefix/lib/liblinkcell.dylib
            test -f prefix/lib/liblinkcell.a
            test -f prefix/lib/liblinkcell_gpu.a
          else
            test -f prefix/lib/liblinkcell.so
            test -f prefix/lib/liblinkcell.a
            test -f prefix/lib/liblinkcell_gpu.a
          fi
          cmake -S tests/cmake-consumer -B find-build \
            -DCMAKE_PREFIX_PATH="$PWD/prefix" \
            -DLINKCELL_C_EXAMPLE="$PWD/examples/two_points.c" \
            -DLINKCELL_CPP_EXAMPLE="$PWD/examples/two_points.cpp"
          cmake --build find-build --config Release
          if [[ "$RUNNER_OS" == "Windows" ]]; then
            export PATH="$PWD/prefix/bin:$PATH"
          elif [[ "$RUNNER_OS" == "macOS" ]]; then
            export DYLD_LIBRARY_PATH="$PWD/prefix/lib${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
          else
            export LD_LIBRARY_PATH="$PWD/prefix/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
          fi
          ctest --test-dir find-build -C Release --output-on-failure
      - name: pkg-config after cmake install
        if: runner.os != 'Windows'
        run: |
          test -f prefix/lib/pkgconfig/linkcell.pc
          export PKG_CONFIG_PATH="$PWD/prefix/lib/pkgconfig"
          pkg-config --exists --print-errors linkcell
          pkg-config --modversion linkcell

  static-minimage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          repository: lode-org/minimage
          ref: v0.2.0
          path: build/minimage-source
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - run: cmake -S tests/static-minimage -B build/static-minimage -DMINIMAGE_SOURCE="$PWD/build/minimage-source"
      - run: cmake --build build/static-minimage --target static_minimage --parallel 2
      - run: ctest --test-dir build/static-minimage --output-on-failure

  isa-build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable
      - name: CPU flags
        run: |
          echo "arch=$(uname -m)"
          grep -m1 'model name' /proc/cpuinfo || true
          grep -m1 flags /proc/cpuinfo || true
      - name: Unstripped library
        run: |
          cargo build --release --lib --locked
          mkdir -p isa-audit-input
          cp -a target/release/liblinkcell.so isa-audit-input/liblinkcell-x86_64.so
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: unstripped-libs
          path: isa-audit-input

  isa-audit:
    needs: isa-build
    permissions:
      contents: read
    uses: OmniPotentRPC/rgpot/.github/workflows/isa-audit.yml@000887fc75392c024eee070f23655f74a0865a15
    with:
      artifact-name: unstripped-libs
      allow-file: isa-allow.toml
      baseline: x86-64