#!/bin/bash
#
#  $Header: /u/gcmpack/MITgcm/tools/build_options/linux_amd64_ifort11+mpi_generic,v 1.2 2010/03/21 17:59:13 jmc Exp $
#  $Name:  $
#
# Adapted from linux_amd64_ifort11+mpi_generic
#  with specific local path (=MPI_INC_DIR & NETCDF_ROOT ) for MPI & NetCDF
# Tested on jmc's laptop with MPI & Multi-Threaded
#  using intel compiler v.11.1.046 & mpich-1.2.7 (FC.11)
#                       v.12.0.4   & mpich-1.2.7 (FC.14)
#  (intel compiler, mpich & netcdf installed in /opt/intel/ )

#  Build options for the intel 11 fortran compiler with a generic MPI
# DON'T FORGET to set environment variable MPI_INC_DIR to the include
# directory of your MPI implementation

#-------
# run with OpenMP: needs to set environment var. OMP_NUM_THREADS
#    and generally, needs to increase the stack-size:
#   -  sh,bash:
#     > export OMP_NUM_THREADS=2
#     > export KMP_STACKSIZE=400m
#   - csh,tcsh:
#     > setenv OMP_NUM_THREADS 2
#     > setenv KMP_STACKSIZE 400m
#  NOTE: need to set KMP_STACKSIZE in ~/.tcshrc (but curiously, works
#  without OMP_NUM_THREADS in ~/.tcshrc).
#-------

#NETCDF_ROOT='/opt/intel/netcdf-4.0.1/11.1'
MPI_INC_DIR='/opt/intel/mpich-1.2.7p1/include'

FC=mpif77
F90C='mpif90 -fixed'
CC=mpicc
# -shared-intel replaces -i-dynamic ; -static-intel replaces -i-static
LINK="$F90C -shared-intel -no-ipo"
#LINK="$F90C -static-intel -no-ipo"

DEFINES='-DALLOW_USE_MPI -DALWAYS_USE_MPI -DWORDLENGTH=4'
CPP='cpp  -traditional -P'
F90FIXEDFORMAT='-fixed -Tf'
EXTENDED_SRC_FLAG='-132'
GET_FC_VERSION="--version"
OMPFLAG='-openmp'

NOOPTFLAGS='-O0 -g'
NOOPTFILES=''

INCLUDEDIRS=''
INCLUDES=''
LIBS=''

if test "x$DEVEL" != x ; then
    FFLAGS="$FFLAGS -m64 -fPIC -convert big_endian -assume byterecl -O0 -g -debug all -debug-parameters all -noalign -fp-stack-check -check all -fpe0 -traceback -ftrapuv -fp-model strict -warn all"
else
   #FFLAGS="$FFLAGS -m64 -fPIC -W0 -WB -convert big_endian -assume byterecl"
    FFLAGS="$FFLAGS -m64 -fPIC -convert big_endian -assume byterecl"
fi
#- might want to use '-r8' for fizhi pkg:
#FFLAGS="$FFLAGS -r8"

if test "x$GENERIC" != x ; then
    PROCF=-axSSE4.2,SSE4.1,SSSE3,SSE3,SSE2
else
    PROCF=-xHost
fi
#  Note that the -mp switch is for ieee "maintain precision" and is
#  roughly equivalent to -ieee
#  Note the addition of -g to circumvent bug with Intel 11.
if test "x$IEEE" = x ; then
    FOPTIM="-O2 -align -ip -opt-streaming-stores auto $PROCF"
else
#-  for automatic testing with testreport (but no relation with IEEE arithmetic):
    FOPTIM="-fpe0 -ftz -fp-stack-check -check all -ftrapuv"
    FOPTIM="$FOPTIM -O0 -noalign -g -traceback"
    FFLAGS="$FFLAGS -warn all -warn nounused"
#   FFLAGS="$FFLAGS -syntax-only"
#-  to test that it's really version 12, should produce warning:
#   FFLAGS="$FFLAGS -w95 -xW"
#-  original:
   #FOPTIM="-O0 -fp-model source -noalign $PROCF"
    if test "x$DEVEL" != x ; then
	FOPTIM="$PROCF"
    fi
fi
F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
CFLAGS="-O0 -ip -m64 -fPIC $PROCF"

if [ "x$NETCDF_ROOT" != x ] ; then
    INCLUDEDIRS="${NETCDF_ROOT}/include"
    INCLUDES="-I${NETCDF_ROOT}/include"
    LIBS="-L${NETCDF_ROOT}/lib64 -L${NETCDF_ROOT}/lib"
elif [ "x$NETCDF_HOME" != x ]; then
    INCLUDEDIRS="${NETCDF_HOME}/include"
    INCLUDES="-I${NETCDF_HOME}/include"
    LIBS="-L${NETCDF_ROOT}/lib64 -L${NETCDF_HOME}/lib"
elif [ "x$NETCDF_INC" != x -a "x$NETCDF_LIB" != x ]; then
    NETCDF_INC=`echo $NETCDF_INC | sed 's/-I//g'`
    NETCDF_LIB=`echo $NETCDF_LIB | sed 's/-L//g'`
    INCLUDEDIRS="${NETCDF_INC}"
    INCLUDES="-I${NETCDF_INC}"
    LIBS="-L${NETCDF_LIB}"
elif [ "x$NETCDF_INCDIR" != x -a "x$NETCDF_LIBDIR" != x ]; then
    INCLUDEDIRS="${NETCDF_INCDIR}"
    INCLUDES="-I${NETCDF_INCDIR}"
    LIBS="-L${NETCDF_LIBDIR}"
elif test -d /usr/include/netcdf-3 ; then
    INCLUDEDIRS='/usr/include/netcdf-3'
    INCLUDES='-I/usr/include/netcdf-3'
    LIBS='-L/usr/lib64/netcdf-3 -L/usr/lib/netcdf-3'
elif test -d /usr/local/pkg/netcdf ; then
    INCLUDEDIRS='/usr/local/pkg/netcdf/include'
    INCLUDES='-I/usr/local/pkg/netcdf/include'
    LIBS='-L/usr/local/pkg-x86_64/netcdf/lib64 -L/usr/local/pkg-x86_64/netcdf/lib -L/usr/local/pkg/netcdf/lib64 -L/usr/local/pkg/netcdf/lib'
elif test -d /usr/include/netcdf ; then
    INCLUDEDIRS='/usr/include/netcdf'
    INCLUDES='-I/usr/include/netcdf'
elif test -d /usr/local/netcdf ; then
    INCLUDEDIRS='/usr/include/netcdf/include'
    INCLUDES='-I/usr/local/netcdf/include'
    LIBS='-L/usr/local/netcdf/lib64 -L/usr/local/netcdf/lib'
elif test -f /usr/local/include/netcdf.inc ; then
    INCLUDEDIRS='/usr/local/include'
    INCLUDES='-I/usr/local/include'
    LIBS='-L/usr/local/lib64 -L/usr/local/lib'
fi

INCLUDES="$INCLUDES -I$MPI_INC_DIR"
INCLUDEDIRS="$INCLUDEDIRS $MPI_INC_DIR"
MPIINCLUDEDIR="$MPI_INC_DIR"
MPI_HEADER_FILES='mpif.h mpiof.h'
MPI_HEADER_FILES_INC='./mpi_headers/mpif.h ./mpi_headers/mpiof.h'
