# MIT License
#
# Copyright (c) 2017 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

variables:
  GIT_SUBMODULE_STRATEGY: normal # Fetch submodules before job start (non-recursively)
  SUDO_CMD: "" # Must be "sudo" on images which dont use root user
  DEPS_DIR: "$CI_PROJECT_DIR/__dependencies"
  CMAKE_URL: "https://cmake.org/files/v3.16/cmake-3.16.9-Linux-x86_64.tar.gz"
  # General build flags
  CXXFLAGS: ""
  CMAKE_OPTIONS: ""
  # Local build options
  LOCAL_CXXFLAGS: ""
  LOCAL_CMAKE_OPTIONS: ""
  ROCM_LATEST_PATH: "/opt/rocm-4.5.0"

# ROCm

.rocm:
  variables:
    SUDO_CMD: "sudo -E"
    SUDO_CMD_HOME: "sudo -E -H"
  tags:
    - rocm
  image: rocm/rocm-terminal:latest
  before_script:
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y -qq libidn11 git wget tar xz-utils bzip2 build-essential pkg-config ca-certificates kmod gfortran findutils
    - hipconfig
    # cmake
    - mkdir -p $DEPS_DIR/cmake
    - wget --no-check-certificate --quiet -O - $CMAKE_URL | tar --strip-components=1 -xz -C $DEPS_DIR/cmake
    - export PATH=$DEPS_DIR/cmake/bin:$PATH
    # Combine global build options with local options
    - export CXXFLAGS=$CXXFLAGS" "$LOCAL_CXXFLAGS
    - export CMAKE_OPTIONS=$CXXFLAGS" "$LOCAL_CMAKE_OPTIONS
    - export ROCM_PATH=/opt/rocm

build:rocm:
  extends: .rocm
  stage: build
  script:
    - mkdir build
    - cd build
    - CXX=hipcc cmake -DBUILD_TEST=ON -DBUILD_BENCHMARK=ON -DBUILD_FORTRAN_WRAPPER=OFF -DDEPENDENCIES_FORCE_DOWNLOAD=ON ../.
    - make -j16
    - make package
  artifacts:
    paths:
      - build/library/
      - build/test/crush_test_*
      - build/test/test_*
      - build/test/stat_test_*
      - build/test/CTestTestfile.cmake
      - build/benchmark/benchmark_*
      - build/gtest/
      - build/testu01/
      - build/CMakeCache.txt
      - build/CTestTestfile.cmake
      - build/*.deb
      - build/*.zip
    expire_in: 2 weeks

build:rocm-static:
  extends: .rocm
  stage: build
  script:
    - mkdir build
    - cd build
    - CXX=hipcc cmake -DBUILD_SHARED_LIBS=OFF -DBUILD_TEST=ON -DBUILD_BENCHMARK=ON -DBUILD_FORTRAN_WRAPPER=OFF -DDEPENDENCIES_FORCE_DOWNLOAD=ON ../.
    - make -j16
    - make package
  artifacts:
    paths:
      - build/library/
      - build/test/crush_test_*
      - build/test/test_*
      - build/test/stat_test_*
      - build/test/CTestTestfile.cmake
      - build/benchmark/benchmark_*
      - build/gtest/
      - build/testu01/
      - build/CMakeCache.txt
      - build/CTestTestfile.cmake
      - build/*.deb
      - build/*.zip
    expire_in: 2 weeks

include: '.gitlab-ci-gputest.yml'

test:rocm_python:
  extends: .rocm
  stage: test
  needs:
    - build:rocm
  tags:
    - rocm-stable
  before_script:
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y -qq python python-pip python-numpy
    - $SUDO_CMD apt-get install -y -qq python3 python3-pip python3-numpy
    - $SUDO_CMD apt-get install -y -qq wget
    - $SUDO_CMD apt-get -y remove python-pip python3-pip
    # Latest supported Python version of pypa is 3.6, but there's no permalink
    # for the latest scripts, hence the '+' in the filename and the generic URL
    - wget https://bootstrap.pypa.io/get-pip.py -O get-pip-3.6+.py
    - wget https://bootstrap.pypa.io/pip/2.7/get-pip.py -O get-pip-2.7.py
    - $SUDO_CMD_HOME python get-pip-2.7.py
    - $SUDO_CMD_HOME python3 get-pip-3.6+.py

  script:
    - export ROCRAND_PATH=$CI_PROJECT_DIR/build/library/
    # rocRAND Wrapper with Python 2
    - cd $CI_PROJECT_DIR/python/rocrand
    - python2 setup.py test
    - pip2 install . --user
    - python2 tests/rocrand_test.py
    - pip2 uninstall --yes rocrand
    # rocRAND Wrapper with Python 3
    - pip3 --version
    - cd $CI_PROJECT_DIR/python/rocrand
    - python3 setup.py test
    - pip3 install . --user
    - python3 tests/rocrand_test.py
    - pip3 uninstall --yes rocrand

test:rocm_package:
  extends: .rocm
  stage: test
  needs:
    - build:rocm
  tags:
    - rocm-stable
  script:
    - cd build
    - $SUDO_CMD dpkg -i rocrand*.deb
    - find -L $ROCM_PATH | grep rand | sort
    - mkdir ../build_package_test && cd ../build_package_test
    - CXX=hipcc cmake ../test/package/.
    - make VERBOSE=1
    - $SUDO_CMD ctest --output-on-failure
    - $SUDO_CMD dpkg -r rocrand-dev rocrand

test:rocm_install:
  extends: .rocm
  stage: test
  tags:
    - rocm-stable
  needs: []
  script:
    - mkdir build_only_install
    - cd build_only_install
    - CXX=hipcc cmake -DBUILD_TEST=OFF -DBUILD_FORTRAN_WRAPPER=OFF ../.
    - make -j16
    - $SUDO_CMD make install
    - find -L $ROCM_PATH | grep rand | sort
    - mkdir ../install_test && cd ../install_test
    - CXX=hipcc cmake ../test/package/.
    - make VERBOSE=1
    - $SUDO_CMD ctest --output-on-failure


# NVCC

.nvcc:
  tags:
    - nvcc
  image: nvidia/cuda:11.4.1-devel-ubuntu18.04
  before_script:
    - nvidia-smi
    # HIP-nvcc
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y -qq ca-certificates git wget tar xz-utils bzip2 build-essential pkg-config g++ gfortran findutils
    - wget -qO - https://repo.radeon.com/rocm/rocm.gpg.key | $SUDO_CMD apt-key add -
    - echo 'deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ ubuntu main' | $SUDO_CMD tee /etc/apt/sources.list.d/rocm.list
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y hip-dev rocm-cmake
    # Install hip-nvcc ignoring dependencies because it depends on cuda metapackage
    # (with heavy libraries, tools etc. that also require GUI and other packages)
    - apt-get download hip-runtime-nvidia
    - $SUDO_CMD dpkg -i --ignore-depends=cuda hip*.deb
    - $SUDO_CMD ls -d /opt/*
    - $SUDO_CMD ln -s $ROCM_LATEST_PATH /opt/rocm
    - export PATH=$PATH:/opt/rocm/bin
    - hipconfig
    # cmake
    - mkdir -p $DEPS_DIR/cmake
    - wget --no-check-certificate --quiet -O - $CMAKE_URL | tar --strip-components=1 -xz -C $DEPS_DIR/cmake
    - export PATH=$DEPS_DIR/cmake/bin:$PATH
    # Combine global build options with local options
    - export CXXFLAGS=$CXXFLAGS" "$LOCAL_CXXFLAGS
    - export CMAKE_OPTIONS=$CXXFLAGS" "$LOCAL_CMAKE_OPTIONS
    - export ROCM_PATH=/opt/rocm

build:nvcc:
  extends: .nvcc
  stage: build
  script:
    - mkdir build
    - cd build
    - cmake -DBUILD_TEST=ON -DBUILD_BENCHMARK=ON -DBUILD_FORTRAN_WRAPPER=OFF -DDEPENDENCIES_FORCE_DOWNLOAD=ON -DNVGPU_TARGETS="52;70" ../.
    - make -j16
    - make package
  artifacts:
    paths:
      - build/library/
      - build/test/crush_test_*
      - build/test/test_*
      - build/test/stat_test_*
      - build/test/CTestTestfile.cmake
      - build/benchmark/benchmark_*
      - build/gtest/
      - build/testu01/
      - build/CMakeCache.txt
      - build/CTestTestfile.cmake
      - build/*.deb
      - build/*.zip
    expire_in: 2 weeks

test:nvcc_titanv:
  extends: .nvcc
  stage: test
  needs:
    - build:nvcc
  script:
    - export CUDA_VISIBLE_DEVICES=0
    - cd build
    - $SUDO_CMD ctest --output-on-failure
    - $SUDO_CMD ./benchmark/benchmark_rocrand_generate --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_rocrand_kernel --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_curand_generate --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_curand_kernel --dis all --engine all --trials 5

test:nvcc_980:
  extends: .nvcc
  stage: test
  needs:
    - build:nvcc
  script:
    - export CUDA_VISIBLE_DEVICES=1
    - cd build
    - $SUDO_CMD ctest --output-on-failure
    - $SUDO_CMD ./benchmark/benchmark_rocrand_generate --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_rocrand_kernel --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_curand_generate --dis all --engine all --trials 5
    - $SUDO_CMD ./benchmark/benchmark_curand_kernel --dis all --engine all --trials 5

test:nvcc_python:
  extends: .nvcc
  stage: test
  needs:
    - build:nvcc
  before_script:
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y -qq python python-pip python-numpy
    - $SUDO_CMD apt-get install -y -qq python3 python3-pip python3-numpy
    - $SUDO_CMD apt-get install -y -qq wget
    - $SUDO_CMD apt-get -y remove python-pip python3-pip
    # Latest supported Python version of pypa is 3.6, but there's no permalink
    # for the latest scripts, hence the '+' in the filename and the generic URL
    - wget https://bootstrap.pypa.io/get-pip.py -O get-pip-3.6+.py
    - wget https://bootstrap.pypa.io/pip/2.7/get-pip.py -O get-pip-2.7.py
    - $SUDO_CMD_HOME python get-pip-2.7.py
    - $SUDO_CMD_HOME python3 get-pip-3.6+.py

  script:
    - export ROCRAND_PATH=$CI_PROJECT_DIR/build/library/
    # rocRAND Wrapper with Python 2
    - cd $CI_PROJECT_DIR/python/rocrand
    - python2 setup.py test
    - pip2 install . --user
    - python2 tests/rocrand_test.py
    - pip2 uninstall --yes rocrand
    # rocRAND Wrapper with Python 3
    - pip3 --version
    - cd $CI_PROJECT_DIR/python/rocrand
    - python3 setup.py test
    - pip3 install . --user
    - python3 tests/rocrand_test.py
    - pip3 uninstall --yes rocrand

test:nvcc_package:
  extends: .nvcc
  stage: test
  needs:
    - build:nvcc
  script:
    - cd build
    - $SUDO_CMD dpkg -i rocrand*.deb
    - find -L $ROCM_PATH | grep rand | sort
    - mkdir ../build_package_test && cd ../build_package_test
    - cmake ../test/package/.
    - make VERBOSE=1
    - $SUDO_CMD ctest --output-on-failure
    - $SUDO_CMD dpkg -r rocrand-dev rocrand

test:nvcc_install:
  extends: .nvcc
  stage: test
  needs: []
  script:
    - mkdir build_only_install
    - cd build_only_install
    - cmake -DBUILD_TEST=OFF -DBUILD_FORTRAN_WRAPPER=OFF ../.
    - make -j16
    - $SUDO_CMD make install
    - find -L $ROCM_PATH | grep rand | sort
    - mkdir ../install_test && cd ../install_test
    - cmake ../test/package/.
    - make VERBOSE=1
    - $SUDO_CMD ctest --output-on-failure

test:doc:
  variables:
    SUDO_CMD: "sudo -E"
  tags:
    - rocm # Do it on rocm machine
  image: rocm/rocm-terminal:latest
  stage: test
  needs: []
  before_script:
    - $SUDO_CMD rm /etc/apt/sources.list.d/rocm.list
    - $SUDO_CMD apt-get update -qq
    - $SUDO_CMD apt-get install -y -qq doxygen python3-sphinx python3-pip
    - $SUDO_CMD -H pip3 install numpy
  script:
    - cd doc
    - $SUDO_CMD doxygen Doxyfile
    - $SUDO_CMD make -C ../python/rocrand/docs html