# ----------------------------------------------------------------------------------------
# HONEE GitLab CI
# ----------------------------------------------------------------------------------------
stages:
  - test:stage-lint
  - test:stage-full
  - test:docs
  - deploy

.test-basic:
  interruptible: true
  only:
    refs:
      - web
      - merge_requests

.test:
  extends: .test-basic
  only:
    refs:
      - web
      - merge_requests
      - main
      - release
  except:
    variables:
      # Skip if the No-Code label is attached to a merge request (i.e., documentation only)
      - $CI_MERGE_REQUEST_LABELS =~ /(^|,)No-Code($|,)/
  needs:
    - job: docker-build
      optional: true

.docs:
  image: python:3.10
  before_script:
    - pip install -r doc/requirements.txt
    - apt-get update
    - apt-get install -y doxygen librsvg2-bin


# ----------------------------------------------------------------------------------------
# Test formatting and static analysis
# ----------------------------------------------------------------------------------------
noether-lint:
  stage: test:stage-lint
  extends: .test
  tags:
    - noether
    - shell
  needs: []
  script:
    - rm -f .SUCCESS
    # Environment
    - export CC=gcc HIPCC=hipcc
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    - echo "-------------- HIPCC ---------------" && $HIPCC --version && export HIP_DIR=/opt/rocm
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-serial-hip && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/lib PATH="$PATH:$PETSC_DIR/bin" # cgnsdiff
    # make with Werror, Wall, supress loop vectorization warnings
    - echo "-------------- make Werror ---------"
    - PEDANTIC=1 PEDANTICFLAGS="-Werror -Wall -Wno-pass-failed" make -j$NPROC_CPU
    # make format
    - echo "-------------- make format ---------" && export CLANG_FORMAT=clang-format-15 && $CLANG_FORMAT --version
    - make -j$NPROC_CPU format && git diff --color=always --exit-code
    # Clang-tidy
    - echo "-------------- clang-tidy ----------" && export CLANG_TIDY=clang-tidy-15 && $CLANG_TIDY --version
    - PETSC_ARCH=arch-serial-hip make -j$NPROC_CPU tidy
    # Report status
    - touch .SUCCESS


# ----------------------------------------------------------------------------------------
# Test memory access assumptions
# ----------------------------------------------------------------------------------------
noether-memcheck:
  stage: test:stage-lint
  extends: .test
  tags:
    - noether
    - shell
  needs: []
  script:
    - rm -f .SUCCESS
    # Environment
    # -- NOTE: Coverage disabled because it doesn't play nice with the ASAN options
    - export CC=clang-15
    - export NPROC_POOL=8
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED-cpu && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-serial-cpu-clang && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/lib PATH="$PATH:$PETSC_DIR/bin" # cgnsdiff
    # ASAN
    - echo "-------------- ASAN ----------------"
    - echo "ASAN intentionlly skipped, known leaks"
    # - export ASAN=1 AFLAGS="-fsanitize=address -fsanitize=leak"
    - echo $AFLAGS
    # HONEE
    - echo "-------------- HONEE ---------------" && make info
    - make clean
    - make -j$NPROC_CPU
    # Test suite
    - echo "-------------- HONEE tests ---------"
    - echo '[{"subject":"/","metrics":[{"name":"Transfer Size (KB)","value":"19.5","desiredSize":"smaller"},{"name":"Speed Index","value":0,"desiredSize":"smaller"},{"name":"Total Score","value":92,"desiredSize":"larger"},{"name":"Requests","value":4,"desiredSize":"smaller"}]}]' > performance.json
    # -- Memcheck libCEED CPU backend, serial
    - NPROC_TEST=1 make -k -j$((NPROC_CPU / NPROC_POOL)) CEED_BACKENDS="/cpu/self/memcheck" JUNIT_BATCH="cpu-serial-memcheck" junit search=navierstokes
    # Report status
    - touch .SUCCESS
  artifacts:
    paths:
      - build/*.junit
    reports:
      junit: build/*.junit
      performance: performance.json
    expire_in: 28 days


# ----------------------------------------------------------------------------------------
# CPU testing on Noether
# ----------------------------------------------------------------------------------------
noether-cpu:
  stage: test:stage-full
  extends: .test
  tags:
    - noether
    - shell
  script:
    - rm -f .SUCCESS
    # Environment
    - export COVERAGE=1 CC=gcc
    - export NPROC_POOL=4
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    - echo "-------------- GCOV ----------------" && gcov --version
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-parallel-hip && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/$PETSC_ARCH/lib PATH="$PATH:$PETSC_DIR/$PETSC_ARCH/bin" # cgnsdiff
    # HONEE
    - echo "-------------- HONEE ---------------" && make info
    - make clean
    - make -j$NPROC_CPU
    # Test suite
    - echo "-------------- HONEE tests ---------"
    - echo '[{"subject":"/","metrics":[{"name":"Transfer Size (KB)","value":"19.5","desiredSize":"smaller"},{"name":"Speed Index","value":0,"desiredSize":"smaller"},{"name":"Total Score","value":92,"desiredSize":"larger"},{"name":"Requests","value":4,"desiredSize":"smaller"}]}]' > performance.json
    # -- Fastest libCEED CPU backend, parallel
    - echo "Parallel tests skipped for now"
    - source /home/phypid/spack/share/spack/setup-env.sh && spack load py-torch@2.3+cuda && export USE_TORCH=1
    - export SMARTREDIS_DIR=/home/phypid/SmartSimTestingSoftware/smartredis/install
    - NPROC_TEST=1 make -k -j$((NPROC_CPU / NPROC_POOL / 1)) CEED_BACKENDS="/cpu/self" JUNIT_BATCH="cpu-serial" junit search=navierstokes
    - spack unload py-torch@2.3+cuda && export USE_TORCH=0
    - source /home/phypid/SmartSimTestingSoftware/bin/activate
    - NPROC_TEST=1 make -k -j$((NPROC_CPU / NPROC_POOL / 1)) CEED_BACKENDS="/cpu/self" JUNIT_BATCH="cpu-serial" junit search="test-smartsim"
    # Report status
    - touch .SUCCESS
  after_script:
    - |
      if [ -f .SUCCESS ]; then
        gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml;
      fi
  coverage: '/^lines:\s+(\d+.\d\%)/'
  artifacts:
    paths:
      - coverage.xml
      - build/*.junit
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
      junit: build/*.junit
      performance: performance.json
    expire_in: 28 days


# ----------------------------------------------------------------------------------------
# CPU Int64 testing on Noether
# ----------------------------------------------------------------------------------------
noether-cpu-int64:
  stage: test:stage-full
  extends: .test
  tags:
    - noether
    - shell
  script:
    - rm -f .SUCCESS
    # Environment
    - export COVERAGE=1 CC=gcc
    - export NPROC_POOL=4
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    - echo "-------------- GCOV ----------------" && gcov --version
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-serial-cpu-int64 && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/$PETSC_ARCH/lib PATH="$PATH:$PETSC_DIR/$PETSC_ARCH/bin" # cgnsdiff
    # HONEE
    - echo "-------------- HONEE ---------------" && make info
    - make clean
    - make -j$NPROC_CPU
    # Test suite
    - echo "-------------- HONEE tests ---------"
    - echo '[{"subject":"/","metrics":[{"name":"Transfer Size (KB)","value":"19.5","desiredSize":"smaller"},{"name":"Speed Index","value":0,"desiredSize":"smaller"},{"name":"Total Score","value":92,"desiredSize":"larger"},{"name":"Requests","value":4,"desiredSize":"smaller"}]}]' > performance.json
    # -- Fastest libCEED CPU backend, serial
    - source /home/phypid/spack/share/spack/setup-env.sh && spack load py-torch@2.3+cuda && export USE_TORCH=1
    - export SMARTREDIS_DIR=/home/phypid/SmartSimTestingSoftware/smartredis/install
    - NPROC_TEST=1 make -k -j$((NPROC_CPU / NPROC_POOL / 1)) CEED_BACKENDS="/cpu/self" JUNIT_BATCH="cpu-serial-int64" junit search=navierstokes
    - spack unload py-torch@2.3+cuda && export USE_TORCH=0
    - source /home/phypid/SmartSimTestingSoftware/bin/activate
    - NPROC_TEST=1 make -k -j$((NPROC_CPU / NPROC_POOL / 1)) CEED_BACKENDS="/cpu/self" JUNIT_BATCH="cpu-serial" junit search="test-py-smartsim_regression_framework"
    # Report status
    - touch .SUCCESS
  after_script:
    - |
      if [ -f .SUCCESS ]; then
        gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml;
      fi
  coverage: '/^lines:\s+(\d+.\d\%)/'
  artifacts:
    paths:
      - coverage.xml
      - build/*.junit
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
      junit: build/*.junit
      performance: performance.json
    expire_in: 28 days


# ----------------------------------------------------------------------------------------
# GPU testing on Noether
# ----------------------------------------------------------------------------------------
noether-hip:
  stage: test:stage-full
  extends: .test
  tags:
    - noether
    - shell
  script:
    - rm -f .SUCCESS
    # Environment
    - export COVERAGE=1 CC=gcc HIPCC=hipcc
    - export NPROC_POOL=4
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    - echo "-------------- HIPCC ---------------" && $HIPCC --version && export HIP_DIR=/opt/rocm
    - echo "-------------- GCOV ----------------" && gcov --version
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-parallel-hip && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/$PETSC_ARCH/lib PATH="$PATH:$PETSC_DIR/$PETSC_ARCH/bin" # cgnsdiff
    # HONEE
    - echo "-------------- HONEE ---------------" && make info
    - make clean
    - make -j$NPROC_CPU
    # Test suite
    - echo "-------------- HONEE tests ---------"
    - echo '[{"subject":"/","metrics":[{"name":"Transfer Size (KB)","value":"19.5","desiredSize":"smaller"},{"name":"Speed Index","value":0,"desiredSize":"smaller"},{"name":"Total Score","value":92,"desiredSize":"larger"},{"name":"Requests","value":4,"desiredSize":"smaller"}]}]' > performance.json
    # -- Fastest libCEED HIP backend, serial
    # Note: /shared is faster due to /gen JiT time for CeedOperators overwhelming runtime improvements at these problem sizes
    - NPROC_TEST=1 make -k -j$((NPROC_GPU / NPROC_POOL / 1)) CEED_BACKENDS="/gpu/hip/shared" JUNIT_BATCH="hip-serial" junit search=navierstokes
    # Report status
    - touch .SUCCESS
  after_script:
    - |
      if [ -f .SUCCESS ]; then
        gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml;
      fi
  coverage: '/^lines:\s+(\d+.\d\%)/'
  artifacts:
    paths:
      - coverage.xml
      - build/*.junit
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
      junit: build/*.junit
      performance: performance.json
    expire_in: 28 days


noether-cuda:
  stage: test:stage-full
  extends: .test
  tags:
    - noether
    - shell
  script:
    - rm -f .SUCCESS
    # Environment
    - export COVERAGE=1 CC=gcc NVCC=nvcc
    - export NPROC_POOL=4
    - echo "-------------- nproc ---------------" && NPROC_CPU=$(nproc) && NPROC_GPU=$(($(nproc)<8?$(nproc):8)) && echo "NPROC_CPU" $NPROC_CPU && echo "NPROC_GPU" $NPROC_GPU
    - echo "-------------- CC ------------------" && $CC --version
    - echo "-------------- NVCC ----------------" && $NVCC --version
    - echo "-------------- GCOV ----------------" && gcov --version
    # Libraries
    # -- libCEED
    - echo "-------------- libCEED -------------"
    - export CEED_DIR=/projects/honee/libCEED && git -C $CEED_DIR -c safe.directory=$CEED_DIR describe && make -C $CEED_DIR info
    # -- PETSc
    - echo "-------------- PETSc ---------------"
    - export PETSC_DIR=/projects/honee/petsc
    - export PETSC_ARCH=arch-parallel-cuda && git -C $PETSC_DIR -c safe.directory=$PETSC_DIR describe && make -C $PETSC_DIR info
    - export PETSC_OPTIONS='-malloc_debug no -use_gpu_aware_mpi 0' # faster tests
    - export LD_LIBRARY_PATH=$PETSC_DIR/$PETSC_ARCH/lib PATH="$PATH:$PETSC_DIR/$PETSC_ARCH/bin" # cgnsdiff
    # HONEE
    - echo "-------------- HONEE ---------------" && make info
    - make clean
    - make -j$NPROC_CPU
    # Test suite
    - echo "-------------- HONEE tests ---------"
    - echo '[{"subject":"/","metrics":[{"name":"Transfer Size (KB)","value":"19.5","desiredSize":"smaller"},{"name":"Speed Index","value":0,"desiredSize":"smaller"},{"name":"Total Score","value":92,"desiredSize":"larger"},{"name":"Requests","value":4,"desiredSize":"smaller"}]}]' > performance.json
    # -- Fastest libCEED CUDA backend, serial
    # Note: /shared is faster due to /gen JiT time for CeedOperators overwhelming runtime improvements at these problem sizes
    - NPROC_TEST=1 make -k -j$((NPROC_GPU / NPROC_POOL / 1)) CEED_BACKENDS="/gpu/cuda/shared" JUNIT_BATCH="cuda-serial" junit search=navierstokes
    # Report status
    - touch .SUCCESS
  after_script:
    - |
      if [ -f .SUCCESS ]; then
        gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml;
      fi
  coverage: '/^lines:\s+(\d+.\d\%)/'
  artifacts:
    paths:
      - coverage.xml
      - build/*.junit
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
      junit: build/*.junit
      performance: performance.json
    expire_in: 28 days


# ----------------------------------------------------------------------------------------
# Build documentation
# ----------------------------------------------------------------------------------------
docs-review:
  stage: test:docs
  tags:
    - noether
    - docker
  extends:
    - .docs
    - .test-basic
  interruptible: true
  script:
    - export PETSC_DIR=/projects/honee/petsc PETSC_ARCH=arch-parallel-hip CEED_DIR=/projects/honee/libCEED
    - git submodule update --init
    - make doc-html pkgconf=true DOXYGENOPTS= SPHINXOPTS=-W
    - mv doc/build/html public
  artifacts:
    paths:
      - public
    expire_in: 28 days
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_PROJECT_NAMESPACE.gitlab.io/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/public/index.html


# ----------------------------------------------------------------------------------------
# Deploy documentation using GitLab pages
# ----------------------------------------------------------------------------------------
pages:  # this job name has special meaning to GitLab
  stage: deploy
  tags:
    - noether
    - docker
  extends: .docs
  interruptible: false
  script:
    - git submodule update --init
    - make doc-dirhtml pkgconf=true DOXYGENOPTS=
    - mv doc/build/dirhtml public
  only:
    - main
  artifacts:
    paths:
      - public
