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
    - ninja-build
    - python3-pip
- name: pipModules
  type: object
  default:
    - CppHeaderParser
    - argparse
- name: rocmDependencies
  type: object
  default:
    - clr
    - llvm-project
    - rocminfo
    - rocprofiler-register
    - ROCR-Runtime
- name: rocmTestDependencies
  type: object
  default:
    - rocminfo
    - rocprofiler-register
    - ROCR-Runtime

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

jobs:
- job: rocm_bandwidth_test_build
  variables:
  - group: common
  - template: /.azuredevops/variables-global.yml
  - name: ROCR_INC_DIR
    value: $(Agent.BuildDirectory)/rocm
  - name: ROCR_LIB_DIR
    value: $(Agent.BuildDirectory)/rocm
  pool:
    vmImage: ${{ variables.BASE_BUILD_POOL }}
  workspace:
    clean: all
  steps:
  - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
    parameters:
      aptPackages: ${{ parameters.aptPackages }}
      pipModules: ${{ parameters.pipModules }}
  - 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 }}
      aggregatePipeline: ${{ parameters.aggregatePipeline }}
  - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
    parameters:
      extraBuildFlags: >-
        -DCMAKE_BUILD_TYPE=release
        -DCMAKE_MODULE_PATH=$(Build.SourcesDirectory)/cmake_modules
        -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm;$(Agent.BuildDirectory)/rocm/include;$(Agent.BuildDirectory)/rocm/include/hsa
        -GNinja
  - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
  - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
  - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml
  # - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
  #   parameters:
  #     aptPackages: ${{ parameters.aptPackages }}
  #     pipModules: ${{ parameters.pipModules }}
  #     extraEnvVars:
  #       - ROCR_INC_DIR:::/home/user/workspace/rocm
  #       - ROCR_LIB_DIR:::/home/user/workspace/rocm

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: rocm_bandwidth_test_test_${{ job.target }}
    dependsOn: rocm_bandwidth_test_build
    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
    pool: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/local-artifact-download.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/gpu-diagnostics.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: rocm_bandwidth_test
        testDir: '$(Agent.BuildDirectory)'
        testExecutable: './rocm/bin/rocm-bandwidth-test'
        testParameters: ''
        testPublishResults: false
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        environment: test
        gpuTarget: ${{ job.target }}
