
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: 6, unit: 'HOURS')
            {
                sh "echo \$HSA_ENABLE_SDMA"
                sh "rm -rf build; mkdir build; cd build; CXX=${compiler} CXXFLAGS='-Werror' cmake -DMIOPEN_GPU_SYNC=On -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 MIOPEN_CONV_PRECISE_ROCBLAS_TIMING=0 dumb-init make -j\$(nproc) ${test}"
                sh "./build/bin/${test} ${testargs}"

            }
        }
        return retimage
}

pipeline {
    agent none
    environment{
        image = "miopen"
        buildflag = '-DBUILD_DEV=On -DMIOPEN_TEST_ALL=On -DCMAKE_BUILD_TYPE=release'
    }
    stages{
        stage("HIP Release Reductions fp32"){
            parallel{
                stage("Batch Norm Per Activation 2D") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_bn_peract_test', '--all -n 2 --verbose --disable-verification-cache')
                    }
                }
                
                stage("Batch Norm Spatial 2D") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_bn_spatial_test', '--all -n 2 --verbose --disable-verification-cache')
                    }
                }
                stage("Batch Norm Per Activation 3D") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_bn_3d_peract_test', '--all -n 4 --verbose --disable-verification-cache')
                    }
                }
                
                stage("Batch Norm Spatial 3D") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_bn_3d_spatial_test', '--all -n 4 --verbose --disable-verification-cache')
                    }
                }
                
                stage("Softmax 2D") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_soft_max', '--all -n 4 --verbose --disable-verification-cache')
                    }
                }
                
                stage("Local Response Normalization") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_lrn_test', '--all -n 4 --verbose --disable-verification-cache')                        
                    }
                }

                stage("Pooling") {
                    agent{ label rocmnode("vega") }
                    steps{
                        buildJob("hcc", buildflag, image, 'test_pooling_test', '--all -n 8 --verbose --disable-verification-cache')                        
                    }
                }
            }
        }
    }    
}

