#!/bin/ksh

# ICON
#
# ------------------------------------------
# Copyright (C) 2004-2026, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ------------------------------------------

#-------------------------------------------------------------------------------------=
# ICON testbed
# Creates the setup for a given target
#
# Leonidas Linardakis, MPI-M, 2010-11-24
#
#
#-------------------------------------------------------------------------------------=

#-------------------------------------------------------------------------------------
set_cc_target_gcc()
{
  CC="gcc"
  C_FLAGS="-std=gnu99 -march=native -O2 -DHAVE_LIBNETCDF -DHAVE_CF_INTERFACE"
  DEF_COPT="-D"
}

#-------------------------------------------------------------------------------------
set_fortran_target_linux_gcc()
{
  set_cc_target_gcc
  C_FLAGS="$C_FLAGS -DpgiFortran"

  FC="gfortran"
#   BASIC_FLAGS="-xf95-cpp-input -std=f2003 -fmodule-private -fimplicit-none -fmax-identifier-length=63 -pthread -ffree-line-length-132 -D__LOOP_EXCHANGE"
  BASIC_FLAGS="-xf95-cpp-input -std=f2003 -fmodule-private -fimplicit-none -pthread -D__LOOP_EXCHANGE"
  INFO_FLAGS="-Wall -Wcharacter-truncation -Wconversion -Wunderflow -Wunused-parameter"
  DEBUG_FLAGS="-fbounds-check -fstack-protector-all -finit-real=nan -finit-integer=-2147483648 -finit-character=127"
  DEBUG_FLAGS="-fbounds-check -fstack-check -finit-real=nan -finit-integer=-2147483648 -finit-character=127"
  TRACE_FLAGS="-g -fbacktrace"

  LOW_OPT_FLAGS="-march=native -O0"
  STD_OPT_FLAGS="-march=native -O2 -ffast-math"
  HI_OPT_FLAGS="-march=native -O3 -ffast-math"
  OMP_FLAGS="-fopenmp"

  DEF_FOPT="-D"
  MOD_OPT="-I"
  MOD_DIR="-J"

  F77="gfortran"
  F77_FLAGS=""
}

#-------------------------------------------------------------------------------------
set_fortran_target_linux_nag()
{
  set_cc_target_gcc
  C_FLAGS="$C_FLAGS -DNAGf90Fortran"

  FC="nagfor"
  MISMATCHS="-wmismatch=mpi_send,mpi_isend,mpi_recv,mpi_irecv,mpi_bcast,mpi_allreduce,mpi_reduce,mpi_gather,mpi_gatherv,mpi_scatter,mpi_scatterv,mpi_allgather,mpi_pack,mpi_unpack,nf_get_att_double,nf_put_att_double,nf_def_var,nf_get_att_int,nf_put_att_int,nf_put_vara_int,nf_get_vara_int,nf_put_vara_double,psmile_bsend -w=uep"
  BASIC_FLAGS="-float-store -colour -nan -maxcontin=99 -fpp  -f2003 $MISMATCHS -D__LOOP_EXCHANGE"
  DEBUG_FLAGS="-C=all"
  # TRACE_FLAGS="-gline -g -mtrace=all"
  TRACE_FLAGS="-gline -g"

  LOW_OPT_FLAGS="-O0"
  STD_OPT_FLAGS="-O2"
  HI_OPT_FLAGS="-O3"
  OMP_FLAGS=""

  DEF_FOPT="-D"
  MOD_OPT="-I"
  MOD_DIR="-mdir"

  F77="nagfor"
  F77FLAGS="-w=obs -dcfuns -mismatch_all"
}

#-------------------------------------------------------------------------------------
set_fortran_target_linux_sun()
{
  CC=suncc
  C_FLAGS="-DsunFortran -DHAVE_LIBNETCDF -DHAVE_CF_INTERFACE -C"
  DEF_COPT="-D"

  FC="sunf95"
  BASIC_FLAGS="-fpp -fstore -fnonstd -u -D__LOOP_EXCHANGE"
  DEBUG_FLAGS="-xcommonchk"
  # TRACE_FLAGS="-gline -g -mtrace=all"
  TRACE_FLAGS="-g"

  LOW_OPT_FLAGS="-O0"
  STD_OPT_FLAGS="-O2"
  HI_OPT_FLAGS="-O3"
  OMP_FLAGS="-xopenmp"

  DEF_FOPT="-D"
  MOD_OPT="-M"
  MOD_DIR="-moddir="

  F77="sunf95"
  F77FLAGS="-fpp -fstore -fnonstd"
}

#-------------------------------------------------------------------------------------
set_fortran_target_linux_pgi()
{
  set_cc_target_gcc
  C_FLAGS="$C_FLAGS  -DpgiFortran"

  FC="pgf95"
  BASIC_FLAGS="-Mpreprocess -Mrecursive -Mallocatable=03 -D__LOOP_EXCHANGE"
  INFO_FLAGS=""
  DEBUG_FLAGS="-Mbounds -Mchkptr -Mchkstk  -Mdclchk -C"
  TRACE_FLAGS="-g -gopt"

  LOW_OPT_FLAGS="-O0"
  STD_OPT_FLAGS="-O2"
  HI_OPT_FLAGS="-O3"
  OMP_FLAGS="-mp"

  DEF_FOPT="-D"
  MOD_OPT="-I"
  MOD_DIR="-module"

  F77="pgf95"
  F77_FLAGS="$BASIC_FLAGS"
}

#-------------------------------------------------------------------------------------
set_fortran_target_linux_intel()
{
  set_cc_target_gcc
  C_FLAGS="$C_FLAGS -DpgiFortran"

  LOW_OPT_FLAGS="-O0"
  STD_OPT_FLAGS="-O2"
  OMP_FLAGS="-openmp"
  LD_FLAGS=""

  if [[ "x$use_target" == "xMIC" ]]; then
    CC="icc"
    C_FLAGS="-std=gnu99 -O2 -DHAVE_LIBNETCDF -DHAVE_CF_INTERFACE -DpgiFortran"
    DEF_COPT="-D"
    FC="mpiifort"

    HI_OPT_FLAGS="-O3 -ipo -no-prec-div -xHost -simd -vec"
    INFO_FLAGS="-opt-report -vec-report"

    # used for native mode on MIC cards
    FC="mpiifort -mt_mpi -static_mpi"
    C_FLAGS="$C_FLAGS -mmic"
    HI_OPT_FLAGS="-O3 -ipo -no-prec-div -simd -mmic"

    if [[ "x$use_openmp" == "xyes" ]]; then
      HI_OPT_FLAGS="$HI_OPT_FLAGS -mkl"
    else
      HI_OPT_FLAGS="$HI_OPT_FLAGS -mkl=sequential"
    fi

  else
    FC="ifort"

    HI_OPT_FLAGS="-O3"
    INFO_FLAGS=""
  fi
  BASIC_FLAGS="-fltconsistency -m64 -fpp -free -D__LOOP_EXCHANGE"
  DEBUG_FLAGS="-check bounds -check pointers -check uninit -fp-stack-check -debug"
  TRACE_FLAGS="-g -traceback"

 # -xcode
#    AVX            May  generate  Intel®  Advanced  Vector  Extensions (Intel® AVX), Intel® SSE4.2, SSE4.1, SSSE3, SSE3, SSE2, and SSE instructions for
#                   Intel® processors. Optimizes for a future Intel processor.
#
#    SSE4.2         May generate Intel® SSE4 Efficient Accelerated String and Text Processing instructions supported by  Intel®  Coreâ¢  i7  processors.
#                   May generate Intel® SSE4 Vectorizing Compiler and Media Accelerator, Intel® SSSE3, SSE3, SSE2, and SSE instructions and it may opti‐
#                   mize for the Intel® Coreâ¢ processor family.
#
#    SSE4.1         May generate Intel® SSE4 Vectorizing Compiler and Media Accelerator  instructions for Intel processors.  May generate Intel®  SSSE3,
#                   SSE3,  SSE2,  and  SSE  instructions  and it may optimize for Intel® 45nm Hi-k next generation Intel® Coreâ¢ microarchitecture. This
#                   replaces value S, which is deprecated.
#
#    SSE3_ATOM      May generate MOVBE instructions for Intel processors, depending on the setting of option -minstruction (Linux and Mac OS)  or  /Qin‐
#                   struction (Windows). May also generate Intel® SSSE3, SSE3, SSE2, and SSE instructions for Intel processors. Optimizes for the Intel®
#                   Atomâ¢ processor and Intel® Centrino® Atomâ¢ Processor Technology.
#
#    SSSE3          May generate Intel® SSSE3, SSE3, SSE2, and SSE instructions for Intel processors. Optimizes for the Intel® Coreâ¢ microarchitecture.
#                   For Mac OS* X systems, this value is only supported on Intel® 64 architecture.  This replaces value T, which is deprecated.
#
#    SSE3           May generate Intel® SSE3, SSE2, and SSE instructions for Intel processors.  Optimizes for the enhanced Pentium® M processor microar‐
#                   chitecture and Intel NetBurst® microarchitecture. For Mac OS* X systems, this value is only  supported  on  IA-32  architecture.This
#                   replaces value P, which is deprecated.
#
#    SSE2           May  generate  Intel®  SSE2  and  SSE instructions for Intel processors.  Optimizes for the Intel NetBurst® microarchitecture.  This
#                   value is not available on Mac OS* X  systems.  This replaces value N, which is deprecated.

  DEF_FOPT="-D"
  MOD_OPT="-I"
  MOD_DIR="-module"

  F77="ifort"
  F77_FLAGS="-O2 -fltconsistency"
}


#-------------------------------------------------------------------------------------
set_fortran_target_aix_xlf()
{

  if [[ $use_mpi == "yes" ]]; then
    CC="mpcc_r"
    FC="mpxlf2003_r"
    F77="mpxlf_r"
  else
    CC="xlc_r"
    FC="xlf2003_r"
    F77="xlf_r"
  fi

  # INFOFLAGS= -qlistopt -qphsinfo -qreport=hotlist -bnoquiet -V
  INFOFLAGS="-qlistopt -qphsinfo -bnoquiet -V"
  C_FLAGS="-O2 -Dextname -DHAVE_LIBNETCDF -DHAVE_CF_INTERFACE -qlanglvl=stdc99 $INFOFLAGS"

  # DEBUGFLAGS= -g -qcheck -qstackprotect=all
  DEBUG_FLAGS=""
  TRACE_FLAGS="-gline"
  #BASIC_FLAGS="-qmoddir=../module -I../module"
  BASIC_FLAGS="-qsuffix=cpp=f90 -qextname -qreport -qlist -qsource -qlistfmt=xml=all -qzerosize -qalias_size=1800000000 -qalias_size -q64 -qmaxmem=-1 -qcache=auto -qfloat=fltint -q64 -WF,-D__LOOP_EXCHANGE"
  LOW_OPT_FLAGS="-O0 -q64"
  STD_OPT_FLAGS="-O2 -q64"
  HI_OPT_FLAGS="-O3 -qhot -qstrict -qsimd=auto -qinline -qtune=balanced -qxflag=nvectver -qxflag=nsmine -qessl"
 ## HI_OPT_FLAGS="-O3 -qhot=simd -qsimd=auto -qinline -qtune=balanced -qxflag=nvectver -qxflag=nsmine -qessl"
  F77_FLAGS="$INFOFLAGS $BASIC_FLAGS -qextname"

  OMP_FLAGS="-qsmp=omp"

  DEF_COPT="-D"
  DEF_FOPT="-WF,-D"
  MOD_OPT="-I"
  MOD_DIR="-qmoddir="
}
