# Compile and test Concrete-cuda on an AWS instance
name: Concrete Cuda - Tests

on:
  workflow_dispatch:
    inputs:
      instance_id:
        description: 'Instance ID'
        type: string
      instance_image_id:
        description: 'Instance AMI ID'
        type: string
      instance_type:
        description: 'Instance product type'
        type: string
      runner_name:
        description: 'Action runner name'
        type: string
      request_id:
        description: 'Slab request ID'
        type: string
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Clang
        run: |
          sudo sed -i 's/azure\.//' /etc/apt/sources.list
          sudo apt-get update 
          sudo apt-get install clang-format-11
      - name: Install cmake-format
        run: |
          pip install cmakelang
      - name: Format Concrete-cuda
        run: |
          cd backends/concrete-cuda/implementation
          ./format_concrete_cuda.sh
      - name: Check diff
        run: git diff --exit-code
          
  run-cuda-tests-linux:
    concurrency:
      group: concrete_cuda_test-${{ github.ref }}
      cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
    name: Test code in EC2
    runs-on: ${{ github.event.inputs.runner_name }}
    strategy:
      fail-fast: false
      # explicit include-based build matrix, of known valid options
      matrix:
        include:
          - os: ubuntu-20.04
            cuda: "11.8"
            cuda_arch: "70"
            gcc: 8
    env:
      CUDA_PATH: /usr/local/cuda-${{ matrix.cuda }}

    steps:
    - uses: actions/checkout@v2
    - name: Export CUDA variables
      if: ${{ !cancelled() }}
      run: |
        echo "CUDA_PATH=$CUDA_PATH" >> "${GITHUB_ENV}"
        echo "$CUDA_PATH/bin" >> "${GITHUB_PATH}"
        echo "LD_LIBRARY_PATH=$CUDA_PATH/lib:$LD_LIBRARY_PATH" >> "${GITHUB_ENV}"
        echo "CUDACXX=/usr/local/cuda-${{ matrix.cuda }}/bin/nvcc" >> "${GITHUB_ENV}"
    # Specify the correct host compilers
    - name: Export gcc and g++ variables
      if: ${{ !cancelled() }}
      run: |
        echo "CC=/usr/bin/gcc-${{ matrix.gcc }}" >> "${GITHUB_ENV}"
        echo "CXX=/usr/bin/g++-${{ matrix.gcc }}" >> "${GITHUB_ENV}"
        echo "CUDAHOSTCXX=/usr/bin/g++-${{ matrix.gcc }}" >> "${GITHUB_ENV}"
        echo "HOME=/home/ubuntu" >> "${GITHUB_ENV}"

    - name: Rust install
      uses: actions-rs/toolchain@v1
      with:
        toolchain: nightly

    - name: Build concrete-cuda
      if: ${{ !cancelled() }}
      run: |
        cd backends/concrete-cuda/implementation
        mkdir build
        cd build
        cmake ..
        make -j8

    - name: Test concrete-cuda with Cuda 11.8
      if: ${{ !cancelled() }}
      run: |
        cd backends/concrete-cuda/implementation/build
        ./test_and_benchmark/test/test_concrete_cuda
