// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier:  MIT

def rocmnode(name) {
    def node_name = 'rocmtest'
    if(name == 'fiji') {
        node_name = 'rocmtest && fiji';
    } else if(name == 'vega') {
        node_name = 'rocmtest && vega';
    } else if(name == 'vega10') {
        node_name = 'rocmtest && vega10';
    } else if(name == 'vega20') {
        node_name = 'rocmtest && vega20';
    } else {
        node_name = name
    }
    return node_name
}


def buildJob(compiler, flags, image, test, testargs){

        env.HSA_ENABLE_SDMA=0
        checkout scm
        def retimage
        try {
            retimage = docker.build("${image}", "--build-arg PREFIX=/usr/local .")
            withDockerContainer(image: image, args: '--device=/dev/kfd --device=/dev/dri --group-add video') {
                timeout(time: 5, unit: 'MINUTES')
                {
                    sh 'PATH="/opt/rocm/opencl/bin/x86_64/:$PATH" clinfo'
                }
            }
        } catch(Exception ex) {
            retimage = docker.build("${image}", "--build-arg PREFIX=/usr/local --no-cache .")
            withDockerContainer(image: image, args: '--device=/dev/kfd --device=/dev/dri --group-add video') {
                timeout(time: 5, unit: 'MINUTES')
                {
                    sh 'PATH="/opt/rocm/opencl/bin/x86_64/:$PATH" clinfo'
                }
            }
        }

        withDockerContainer(image: image, args: '--device=/dev/kfd --device=/dev/dri --group-add video -v=/var/jenkins/:/var/jenkins') {
            timeout(time: 12, unit: 'HOURS')
            {
                sh "echo \$HSA_ENABLE_SDMA"
                // WORKAROUND_ISSUE_3192 Disabling MLIR for debug builds since MLIR generates sanitizer errors.
                sh "rm -rf build; mkdir build; cd build; CXX=${compiler} CXXFLAGS='-Werror' cmake -DMIOPEN_GPU_SYNC=On -DMIOPEN_USE_MLIR=OFF -DMIOPEN_TEST_FLAGS='--disable-verification-cache' -DCMAKE_CXX_FLAGS_DEBUG='-g -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=undefined' ${flags} .."
                sh "cd build; CTEST_PARALLEL_LEVEL=4 MIOPEN_VERIFY_CACHE_PATH=/var/jenkins/.cache/miopen/vcache dumb-init make -j\$(nproc) ${test}"
                sh "MIOPEN_ENABLE_LOGGING_CMD=1 MIOPEN_LOG_LEVEL=6 ./build/bin/${test} ${testargs}"

            }
        }
        return retimage
}

pipeline {
    agent none
    environment{
        image = "miopen"
        buildflag = '-DBUILD_DEV=On -DMIOPEN_TEST_ALL=On -DCMAKE_BUILD_TYPE=releasewithdebug'
        test = "test_conv2d"
        baseargs = "--all --verbose --disable-verification-cache --disable-validation"
    }
    stages{
        stage("HIP Release Convolution fp32"){
            parallel{
                stage("Batch Factor 1") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 ' + baseargs)
                    }
                }
                stage("Batch Factor 2") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 2 ' + baseargs)
                    }
                }
                stage("Batch Factor 4") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 4 ' + baseargs)
                    }
                }
            }
        }
        stage("HIP Release Convolution fp16"){
            parallel{
                stage("Batch Factor 1") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 --half ' + baseargs)
                    }
                }
                stage("Batch Factor 2") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 2 --half' + baseargs)
                    }
                }
                stage("Batch Factor 4") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 4 --half' + baseargs)
                    }
                }
            }
        }
        stage("HIP Release Convolution bfp16"){
            parallel{
                stage("Batch Factor 1") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 --bfloat16 ' + baseargs)
                    }
                }
                stage("Batch Factor 2") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 2 --bfloat16 ' + baseargs)
                    }
                }
                stage("Batch Factor 4") {
                    agent{ label rocmnode("vega20") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 4 --bfloat16 ' + baseargs)
                    }
                }
            }
        }

        stage("HIP Release Convolution on gfx908"){
            parallel{
                stage("Batch Factor 1 fp32") {
                    agent{ label rocmnode("gfx908") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 ' + baseargs)
                    }
                }
                stage("Batch Factor 1 fp16") {
                    agent{ label rocmnode("gfx908") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 --half ' + baseargs)
                    }
                }
                stage("Batch Factor 1 bfp16") {
                    agent{ label rocmnode("gfx908") }
                    steps{
                        buildJob("hcc", buildflag, image, test, '-n 1 --bfloat16 ' + baseargs)
                    }
                }
            }
        }
    }
}
