###
#
#  @copyright 2017-2018 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
#                       Univ. Bordeaux. All rights reserved.
#
#  @version 6.0.1
#  @author Mathieu Faverge
#  @date 2018-07-16
#
###
cmake_minimum_required (VERSION 3.3)
cmake_policy(SET CMP0054 NEW)

### CTest execution
set( PASTIX_DRIVERS
   # reentrant fails with Scotch parser !
  analyze simple step-by-step schur personal reentrant multidof old/old_simple old/old_step-by-step )
set( PASTIX_TESTS
  simple )
set( PASTIX_REFINE
  refinement )
set( PASTIX_FACTO
     0 1 2) # LLh, LDLt, LU
set( PASTIX_SCHEDS
     0 1 4) # sequential, static, dynamic
set( PASTIX_COMPRESS_METHOD
     svd
     pqrcp
     rqrcp
     tqrcp
     rqrrt )
set( PASTIX_SPLIT_METHOD
    pastixsplitkway
    pastixsplitkwayprojections )
if (PASTIX_WITH_PARSEC)
  list( APPEND PASTIX_SCHEDS 2 ) # parsec
endif (PASTIX_WITH_PARSEC)
if (PASTIX_WITH_STARPU)
  list( APPEND PASTIX_SCHEDS 3 ) # starpu
endif (PASTIX_WITH_STARPU)

set( _complex   c z )
set( _factodiag 1 3 4 )

### All drivers with Laplacian and default parameters
foreach(example ${PASTIX_DRIVERS} )
  foreach(arithm ${PASTIX_PRECISIONS} )
    set(factos ${PASTIX_FACTO})
    if (arithm IN_LIST _complex)
      list(APPEND factos 3 4)
    endif()
    foreach(facto ${factos} )
      if ( NOT((facto IN_LIST _factodiag) AND (example STREQUAL "schur")) )
        if (example STREQUAL "multidof")
          if ( arithm EQUAL d )
            add_test(example_drv_facto${facto}_${arithm}_${example}_fix ./${example} -9 ${arithm}:10:10:10:2 -f ${facto} -c 2)
            add_test(example_drv_facto${facto}_${arithm}_${example}_var ./${example} -9 ${arithm}:10:10:10:2 -f ${facto} -c 5)
          endif()
        else()
          add_test(example_drv_facto${facto}_${arithm}_${example} ./${example} -9 ${arithm}:10:10:10:2 -f ${facto} -c 2 )
        endif()
      endif()
    endforeach()
  endforeach()
endforeach()

set( PASTIX_ORDER )
if (PASTIX_ORDERING_SCOTCH)
  list( APPEND PASTIX_ORDER pastixorderscotch )
endif (PASTIX_ORDERING_SCOTCH)
if (PASTIX_ORDERING_METIS)
  list( APPEND PASTIX_ORDER pastixordermetis )
endif (PASTIX_ORDERING_METIS)

### Driver
foreach(example ${PASTIX_TESTS} )
  foreach(order ${PASTIX_ORDER} )
    # RSA
    if (PASTIX_WITH_FORTRAN)
      add_test(example_rsa_${example}_${order} ./${example} --rsa ${CMAKE_SOURCE_DIR}/test/matrix/small.rsa  -f 1 -i iparm_ordering ${order} -d dparm_epsilon_magn_ctrl 1e-14)
    endif()
    # Matrix Market
    add_test(example_mm_${example}_${order}  ./${example} --mm  ${CMAKE_SOURCE_DIR}/test/matrix/young4c.mtx  -f 1 -i iparm_ordering ${order})
    # Harwell Boeing
    add_test(example_hb_${example}_${order}  ./${example} --hb  ${CMAKE_SOURCE_DIR}/test/matrix/orsirr.rua   -f 2 -i iparm_ordering ${order})
    # Matrix Market - Hermitian
    add_test(example_mm2_${example}_${order} ./${example} --mm  ${CMAKE_SOURCE_DIR}/test/matrix/mhd1280b.mtx -f 2 -i iparm_ordering ${order})
  endforeach()
endforeach()

### Refinement
foreach(example ${PASTIX_TESTS} )
  add_test(example_refine_cg_${example}       ./${example} --hb ${CMAKE_SOURCE_DIR}/test/matrix/small.rsa  -f 1 -i iparm_refinement pastixrefinecg -d dparm_epsilon_magn_ctrl 1e-14)
  add_test(example_refine_gmres_${example}    ./${example} --hb ${CMAKE_SOURCE_DIR}/test/matrix/orsirr.rua -f 2 -i iparm_refinement pastixrefinegmres)
  add_test(example_refine_bicgstab_${example} ./${example} --hb ${CMAKE_SOURCE_DIR}/test/matrix/orsirr.rua -f 2 -i iparm_refinement pastixrefinebicgstab)
endforeach()

### Refinement with Laplacian and default parameters
foreach(example ${PASTIX_REFINE} )
  foreach(arithm ${PASTIX_PRECISIONS} )
    if (arithm IN_LIST _complex)
      add_test(example_cg_her_${arithm}_${example} ./${example} -x ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinecg)
      add_test(example_gmres_her_${arithm}_${example} ./${example} -x ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinegmres)
      add_test(example_bicgstab_her_${arithm}_${example} ./${example} -x ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinebicgstab)
    endif()
    add_test(example_cg_sym_${arithm}_${example} ./${example} -9 ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinecg)
    add_test(example_gmres_sym_${arithm}_${example} ./${example} -9 ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinegmres)
    add_test(example_bicgstab_sym_${arithm}_${example} ./${example} -9 ${arithm}:10:10:10:2 -i iparm_refinement pastixrefinebicgstab)
  endforeach()
endforeach()

### Scheduling
foreach(example ${PASTIX_TESTS} )
  foreach(scheduler ${PASTIX_SCHEDS} )
    foreach(arithm ${PASTIX_PRECISIONS} )
      set(factos ${PASTIX_FACTO})
      if (arithm IN_LIST _complex)
        list(APPEND factos 3 4)
      endif()
      foreach(facto ${factos} )
        add_test(example_sched${scheduler}_1d_facto${facto}_${arithm}_${example}     ./${example} -9 ${arithm}:10:10:10:2 -s ${scheduler} -f ${facto} -i iparm_tasks2d_level 0)
        if ((scheduler EQUAL 2) OR (scheduler EQUAL 3))
          # 2D
          add_test(example_sched${scheduler}_2d_facto${facto}_${arithm}_${example}   ./${example} -9 ${arithm}:10:10:10:2 -s ${scheduler} -f ${facto} -i iparm_tasks2d_width 0)
          # 1D/2D
          add_test(example_sched${scheduler}_1d2d_facto${facto}_${arithm}_${example} ./${example} -9 ${arithm}:10:10:10:2 -s ${scheduler} -f ${facto} -i iparm_tasks2d_width 16)
        endif()
      endforeach()
    endforeach()
  endforeach()
endforeach()

# Low Rank
foreach(example ${PASTIX_TESTS} )
  foreach(scheduler ${PASTIX_SCHEDS} )
    foreach(arithm ${PASTIX_PRECISIONS} )
      if (arithm IN_LIST _complex)
        list(APPEND factos 3 4)
      endif()
      foreach(facto ${factos} )
        foreach(method ${PASTIX_COMPRESS_METHOD} )
          foreach(split ${PASTIX_SPLIT_METHOD} )
            set( LOWRANK_PARAMS ./${example} -9 ${arithm}:10:10:10:2 -s ${scheduler} -f ${facto} -i iparm_compress_min_width 16 -i iparm_compress_min_height 16 -i iparm_splitting_strategy ${split} -i iparm_compress_method pastixcompressmethod${method} )
            # Begin
            add_test(example_${method}begin_${split}_sched${scheduler}_facto${facto}_${arithm}_${example}  ${LOWRANK_PARAMS} -i iparm_compress_when pastixcompresswhenbegin)
            # End
            add_test(example_${method}end_${split}_sched${scheduler}_facto${facto}_${arithm}_${example}    ${LOWRANK_PARAMS} -i iparm_compress_when pastixcompresswhenend  )
          endforeach()
        endforeach()
      endforeach()
    endforeach()
  endforeach()
endforeach()
