parameters:
- name: checkoutRepo
  type: string
  default: 'self'
- name: checkoutRef
  type: string
  default: ''
# set to true if doing full build of ROCm stack
# and dependencies are pulled from same pipeline
- name: aggregatePipeline
  type: boolean
  default: false
- name: aptPackages
  type: object
  default:
    - cmake
    - libavcodec-dev
    - libavformat-dev
    - libavutil-dev
    - libdw-dev
    - libglfw3-dev
    - libmsgpack-dev
    - libopencv-dev
    - libprotoc-dev
    - libtbb-dev
    - libtiff-dev
    - libva-amdgpu-dev
    - libva2-amdgpu
    - mesa-amdgpu-va-drivers
    - ninja-build
    - protobuf-compiler
    - python3-pip
- name: pipModules
  type: object
  default:
    - future==1.0.0
    - google==3.0.0
    - protobuf==3.12.4
    - pytz==2022.1
    - nnef==1.0.7
    - numpy==1.23
    - onnx==1.12.0
- name: rocmDependencies
  type: object
  default:
    - AMDMIGraphX
    - aomp
    - aomp-extras
    - clr
    - half
    - composable_kernel
    - hipBLAS
    - hipBLAS-common
    - hipBLASLt
    - hipCUB
    - hipFFT
    - HIPIFY
    - hipRAND
    - hipSOLVER
    - hipSPARSE
    - hipTensor
    - llvm-project
    - MIOpen
    - MIVisionX
    - rccl
    - rocALUTION
    - rocBLAS
    - rocDecode
    - rocFFT
    - rocJPEG
    - rocPRIM
    - rocprofiler-register
    - rocprofiler-sdk
    - ROCR-Runtime
    - rocRAND
    - rocSOLVER
    - rocSPARSE
    - rocThrust
    - rocWMMA
    - rpp
- name: rocmTestDependencies
  type: object
  default:
    - AMDMIGraphX
    - aomp
    - aomp-extras
    - clr
    - half
    - composable_kernel
    - hipBLAS
    - hipBLAS-common
    - hipBLASLt
    - hipCUB
    - hipFFT
    - HIPIFY
    - hipRAND
    - hipSOLVER
    - hipSPARSE
    - hipTensor
    - llvm-project
    - MIOpen
    - MIVisionX
    - rccl
    - rocALUTION
    - rocBLAS
    - rocDecode
    - rocFFT
    - rocminfo
    - rocPRIM
    - rocJPEG
    - rocprofiler-register
    - rocprofiler-sdk
    - ROCR-Runtime
    - rocRAND
    - rocSOLVER
    - rocSPARSE
    - rocThrust
    - roctracer
    - rocWMMA
    - rpp

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a
    testJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: rocm_examples_build_${{ job.target }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    pool: ${{ variables.MEDIUM_BUILD_POOL }}
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        registerROCmPackages: true
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-cmake-custom.yml
      parameters:
        cmakeVersion: '3.25.0'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        gpuTarget: ${{ job.target }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        # https://github.com/ROCm/HIP/issues/2203
        extraBuildFlags: >-
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm
          -DROCM_ROOT=$(Agent.BuildDirectory)/rocm
          -DAMDGPU_TARGETS=${{ job.target }}
          -DCMAKE_HIP_ARCHITECTURES=${{ job.target }}
          -DCMAKE_EXE_LINKER_FLAGS=-fgpu-rdc
          -GNinja
    - task: Bash@3
      displayName: Move rocm-examples binaries to rocm/examples
      inputs:
        targetType: inline
        script: |
          mkdir -p $(Build.BinariesDirectory)/examples
          mv $(Build.BinariesDirectory)/bin/* $(Build.BinariesDirectory)/examples
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        gpuTarget: ${{ job.target }}

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: rocm_examples_test_${{ job.target }}
    dependsOn: rocm_examples_build_${{ job.target }}
    condition:
      and(succeeded(),
        eq(variables['ENABLE_${{ upper(job.target) }}_TESTS'], 'true'),
        not(containsValue(split(variables['DISABLED_${{ upper(job.target) }}_TESTS'], ','), variables['Build.DefinitionName'])),
        eq(${{ parameters.aggregatePipeline }}, False)
      )
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: TEST_LOG_FILE
      value: $(Pipeline.Workspace)/rocm-examplesTestLog.log
    pool: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        registerROCmPackages: true
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-cmake-custom.yml
      parameters:
        cmakeVersion: '3.25.0'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-aqlprofile.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmTestDependencies }}
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        # https://github.com/ROCm/HIP/issues/2203
        extraBuildFlags: >-
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm
          -DROCM_ROOT=$(Agent.BuildDirectory)/rocm
          -DAMDGPU_TARGETS=${{ job.target }}
          -DCMAKE_HIP_ARCHITECTURES=${{ job.target }}
          -DCMAKE_EXE_LINKER_FLAGS=-fgpu-rdc
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: rocm-examples
        testDir: $(Build.SourcesDirectory)/build
        testParameters: '--output-on-failure --force-new-ctest-process --output-junit test_output.xml'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        environment: test
        gpuTarget: ${{ job.target }}
