name: github-benchmarks
on:
  push:
    branches:
      - develop
  workflow_call:

permissions: read-all

jobs:
  CI:
    continue-on-error: true
    strategy:
      # We must avoid pushing to the benchmark repository concurrently.
      max-parallel: 1
      matrix:
        distro: ['ubuntu:latest']
        cxx: ['g++', 'clang++']
        backend: ['OPENMP']
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/kokkos/ci-containers/${{ matrix.distro }}
    env:
      BUILD_ID: ${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.backend }}
    steps:
      - name: Checkout code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        with:
          path: ~/.cache/ccache
          key: kokkos-${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.backend }}-${{ github.ref }}-${{ github.sha }}
          restore-keys: kokkos-${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.backend }}-${{ github.ref }}
      - name: Configure Kokkos
        run: |
          cmake -B builddir \
            -DKokkos_ENABLE_HWLOC=ON \
            -DKokkos_ENABLE_${{ matrix.backend }}=ON \
            -DKokkos_ENABLE_BENCHMARKS=ON \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_BUILD_TYPE=Release
      - name: Build
        run: |
          ccache -z
          NUM_CPU=$(grep -c processor /proc/cpuinfo)
          cmake --build builddir --parallel ${NUM_CPU}
          ccache -s
      - name: Tests
        working-directory: builddir
        run: ctest --output-on-failure
      - name: Gather benchmark results
        run: |
          mkdir ${{ env.BUILD_ID }}
          find builddir/core/perf_test/ -name "*.json" -exec mv {} ${{ env.BUILD_ID }}/  \;
      - name: Archive benchmark results
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: ${{ matrix.cxx }}-${{ matrix.backend }}
          path: ${{ env.BUILD_ID }}
      - name: Push benchmark results
        if: ${{ github.ref == 'refs/heads/develop' }}
        uses: dmnemec/copy_file_to_another_repo_action@bbebd3da22e4a37d04dca5f782edd5201cb97083 # main
        env:
          API_TOKEN_GITHUB: ${{ secrets.DALG24_PUSH_BENCHMARK_RESULTS }}
        with:
          source_file: ${{ env.BUILD_ID }}
          destination_repo: 'kokkos/kokkos-benchmark-results'
          destination_branch: 'main'
          user_email: 'kokkos@users.noreply.github.com'
          user_name: 'Kokkos Developers'
