###
#
# PastixLibSrc: define sources for the pastix main library (libpastix)
#
###
set (PASTIX_LIB_SRC_FLOAT)
set (PASTIX_LIB_SRC
  blend/src/assemblyGener.c
  blend/src/blend.c
  blend/src/blend_symbol_cost.c
  blend/src/blendctrl.c
  blend/src/bulles.c
  blend/src/cost.c
  blend/src/costfunc.c
  blend/src/distribPart.c
  blend/src/elimin.c
  blend/src/eliminfunc.c
  blend/src/extendVector.c
  blend/src/extrastruct.c
  blend/src/fanboth2.c
  blend/src/param_blend.c
  blend/src/partbuild.c
  blend/src/queue.c
  blend/src/simu.c
  blend/src/smart_cblk_split.c
  blend/src/solverMatrixGen.c
  blend/src/solverRealloc.c
  blend/src/solver_check.c
  blend/src/solver_io.c
  blend/src/splitfunc.c
  blend/src/splitpart.c
  blend/src/splitpartlocal.c
  blend/src/symbolrand.c
  blend/src/task.c
  blend/src/write_ps.c
  blend/src/smart_cblk_split.c
  blend/src/blend_distributeOnGPU.c)

list(APPEND PASTIX_LIB_SRC
  symbol/src/dof.c
  symbol/src/dof_io.c
  symbol/src/symbol.c
  symbol/src/symbol_base.c
  symbol/src/symbol_check.c
  symbol/src/symbol_cost.c
  symbol/src/symbol_draw.c
  symbol/src/symbol_io.c
  symbol/src/symbol_keep.c
  symbol/src/symbol_levf.c
  symbol/src/symbol_nonzeros.c
  symbol/src/symbol_tree.c)

list(APPEND PASTIX_LIB_SRC
  common/src/common_integer.c
  common/src/common_error.c
  common/src/common_memory.c
  common/src/trace.c)

list(APPEND PASTIX_LIB_SRC_FLOAT
  common/src/common.c)

list(APPEND PASTIX_LIB_SRC
  fax/src/symbol_compact.c
  fax/src/symbol_costi.c
  fax/src/symbol_fax.c
  fax/src/symbol_fax_graph.c
  fax/src/symbol_faxi.c
  fax/src/symbol_faxi_graph.c)

list(APPEND PASTIX_LIB_SRC
  kass/src/kass.c
  kass/src/compact_graph.c
  kass/src/amalgamate.c
  kass/src/ifax.c
  kass/src/sparRow.c
  kass/src/SF_Direct.c
  kass/src/SF_level.c
  kass/src/find_supernodes.c
  kass/src/KSupernodes.c)

list(APPEND PASTIX_LIB_SRC_FLOAT
  kass/src/sort_row.c)


list(APPEND PASTIX_LIB_SRC
  order/src/order.c
  order/src/order_base.c
  order/src/order_check.c
  order/src/order_io.c)

list(APPEND PASTIX_LIB_SRC
  sopalin/src/bordi.c
  sopalin/src/sopalin_thread.c
  sopalin/src/compute_context_nbr.c)

if (PASTIX_WITH_STARPU)
  list(APPEND PASTIX_LIB_SRC
    sopalin/src/starpu_callback.c
    sopalin/src/starpu_pastix_sched_policy.c)
endif(PASTIX_WITH_STARPU)

list(APPEND PASTIX_LIB_SRC_FLOAT
  sopalin/src/coefinit.c
  sopalin/src/csc_intern_build.c
  sopalin/src/csc_intern_io.c
  sopalin/src/csc_intern_solve.c
  sopalin/src/csc_intern_updown.c
  sopalin/src/csc_utils.c
  sopalin/src/cscd_utils.c
  sopalin/src/cscd_utils_fortran.c
  sopalin/src/debug_dump.c
  sopalin/src/ooc.c
  sopalin/src/pastix.c
  sopalin/src/pastix_fortran.c
  sopalin/src/sopalin_init.c
  sopalin/src/sopalin_option.c
  sopalin/src/sparse_gemm_cpu.c
  sopalin/src/tools.c)

if (PASTIX_WITH_STARPU)
  list(APPEND PASTIX_LIB_SRC_FLOAT
    sopalin/src/starpu_register_data.c
    sopalin/src/starpu_submit.c
    sopalin/src/core_zgeadd.c)
  if (PASTIX_WITH_STARPU_CUDA)
    list(APPEND PASTIX_LIB_SRC_FLOAT
      sopalin/src/sparse_gemm.cu
      sopalin/src/geadd_cuda.cu
      sopalin/src/getra_cuda.cu)
  endif(PASTIX_WITH_STARPU_CUDA)
  if (PASTIX_WITH_MAGMA)
    list(APPEND PASTIX_LIB_SRC_FLOAT
      sopalin/src/zgetrf_stapiv_gpu.c
      sopalin/src/zpotrf_stapiv_gpu.c
      sopalin/src/zhetrf_stapiv_gpu.c
      sopalin/src/zsytrf_stapiv_gpu.c)
  endif(PASTIX_WITH_MAGMA)
  #if (CUDA_SM_VERSION=20)
  list(APPEND PASTIX_LIB_SRC_FLOAT
    sopalin/src/sparse_gemm_fermi.cu
    sopalin/src/sparse_gemdm_fermi.cu)
  #else(CUDA_SM_VERSION=20)
  # sopalin/src/sparse_gemdm.cu
  #endif(CUDA_SM_VERSION=20)
endif (PASTIX_WITH_STARPU)

set(PASTIX_LIB_SRC_FACT
  sopalin/src/sopalin3d.c
  sopalin/src/starpu_submit_tasks.c
  sopalin/src/csc_intern_compute.c
  sopalin/src/raff_functions.c
  sopalin/src/starpu_updo.c)

set(MURGE_LIB_SRC
  sopalin/src/murge_fortran.c
  sopalin/src/murge.c
  sopalin/src/zmurge.c
  sopalin/src/dmurge.c
  sopalin/src/cmurge.c
  sopalin/src/smurge.c)

if (PASTIX_WITH_CTAGS)
  include(PastixCTags)
endif(PASTIX_WITH_CTAGS)

# generate new file for all type of factorization
foreach(f ${PASTIX_LIB_SRC_FACT})
  get_filename_component(_fulldir ${f} PATH)
  string(REPLACE ${CMAKE_SOURCE_DIR} "" _dirtmp "${_fulldir}")
  string(REPLACE ${CMAKE_BINARY_DIR} "" _dir "${_dirtmp}")
  get_filename_component(_name ${f} NAME_WE)
  get_filename_component(_ext ${f} EXT)
  set(_outfile ${CMAKE_BINARY_DIR}/${_dir}/${p}${_name}_ge${_ext})
  FILE(WRITE ${_outfile}
    "
      #define SOPALIN_LU
      #include \"${_name}${_ext}\"
      ")
  list(APPEND PASTIX_LIB_SRC_FLOAT ${_outfile})

  set(_outfile ${CMAKE_BINARY_DIR}/${_dir}/${p}${_name}_sy${_ext})
  FILE(WRITE ${_outfile}
    "
      #undef CHOL_SOPALIN
      #include \"${_name}${_ext}\"
      ")

  list(APPEND PASTIX_LIB_SRC_FLOAT ${_outfile})

  set(_outfile ${CMAKE_BINARY_DIR}/${_dir}/${p}${_name}_he${_ext})
  FILE(WRITE ${_outfile}
    "
      #undef CHOL_SOPALIN
      #define HERMITIAN
      #include \"${_name}${_ext}\"
      ")
  list(APPEND PASTIX_LIB_SRC_FLOAT ${_outfile})

  list(APPEND PASTIX_LIB_SRC_FLOAT ${f})
endforeach()

# This array associates a prefix letter with a series of defines
set( type_defines_array
  "s" "
    #ifdef PREC_DOUBLE
    #  undef PREC_DOUBLE
    #endif /* PREC_DOUBLE */
    #ifdef TYPE_COMPLEX
    #  undef TYPE_COMPLEX
    #endif /* TYPE_COMPLEX */
    "
  "d" "
    #ifndef PREC_DOUBLE
    #  define PREC_DOUBLE
    #endif /* PREC_DOUBLE */
    #ifdef TYPE_COMPLEX
    #  undef TYPE_COMPLEX
    #endif /* TYPE_COMPLEX */
    "
  "c" "
    #ifdef PREC_DOUBLE
    #  undef PREC_DOUBLE
    #endif /* PREC_DOUBLE */
    #ifndef TYPE_COMPLEX
    #  define TYPE_COMPLEX
    #endif /* TYPE_COMPLEX */
    " "z" "
    #ifndef PREC_DOUBLE
    #  define PREC_DOUBLE
    #endif /* PREC_DOUBLE */
    #ifndef TYPE_COMPLEX
    #  define TYPE_COMPLEX
    #endif /* TYPE_COMPLEX */
    ")

# generate new files for each type of float
foreach(_src_list PASTIX_LIB_SRC)
  foreach(_f ${${_src_list}_FLOAT})
    get_filename_component(_fulldir ${_f} PATH)
    string(REPLACE ${CMAKE_SOURCE_DIR} "" _dirtmp "${_fulldir}")
    string(REPLACE ${CMAKE_BINARY_DIR} "" _dir "${_dirtmp}")
    get_filename_component(_name ${_f} NAME)
    array2d_begin_loop( advanced "${type_defines_array}" 2 "type;define" )
    while(advanced)
      set(_outfile ${CMAKE_BINARY_DIR}/${_dir}/${type}${_name})
      FILE(WRITE  ${_outfile}
	"
#define WITH_TYPE_PREFIX
${define}
#include \"${_name}\"
")
      list(APPEND ${_src_list} ${_outfile})
      array2d_advance()
    endwhile()
    list(APPEND ${_src_list} ${_f})
  endforeach()
endforeach()

# remove these sources to avoid symbols re-defined
list (REMOVE_ITEM PASTIX_LIB_SRC "sopalin/src/sparse_gemm_fermi.cu")
list (REMOVE_ITEM PASTIX_LIB_SRC "sopalin/src/sparse_gemdm_fermi.cu")
