#!/bin/bash
set ${MMTESTS_SH_DEBUG:-+x}

export SCRIPT=`basename $0 | sed -e 's/\./\\\./'`
export SCRIPTDIR=`echo $0 | sed -e "s/$SCRIPT//"`
cd $SCRIPTDIR/../configs || exit

# bonnie
for FILE in `ls config-io-bonnie*-async`; do
	FSYNC_FILE=`echo $FILE | sed -e 's/-async$/-fsync/'`
	sed -e 's/FSYNC=no/FSYNC=yes/'		\
	    -e 's/asynchronous/synchronous/'	\
	    $FILE > $FSYNC_FILE
done

# Graph 500
for SIZE in kinder toy; do
	sed -e "s/WORKSET=.*/WORKSET=$SIZE/" config-hpc-graph500-omp-infant > config-hpc-graph500-omp-$SIZE
done

# Hackbench
sed -e 's/HACKBENCH_MAX_GROUPS=.*/HACKBENCH_MAX_GROUPS=1/' config-scheduler-unbound > config-scheduler-unbound-small

# KVM start
cp config-workload-kvmstart-memhog config-workload-kvmstart-deployonly
sed -i -e 's/KVMSTART_MIN_MEMORY=.*/KVMSTART_MIN_MEMORY=\$((KVMSTART_MAX_MEMORY+1))/' config-workload-kvmstart-deployonly

# o fragmenting with single file
cp config-workload-kvmstart-memhog config-workload-kvmstart-memhog-frag-singlefile
cat >> config-workload-kvmstart-memhog-frag-singlefile <<EOF
export KVMSTART_PRETEST=fragment
export KVMSTART_PRETEST_METHOD="single-file"
export KVMSTART_PRETEST_PREPARE_ARGS="--filesize \$KVMSTART_MAX_MEMORY"
export KVMSTART_PRETEST_RUN_ARGS=
export KVMSTART_PRETEST_CLEANUP_ARGS=
EOF
# o fragmenting with single file, THP disabled
cp config-workload-kvmstart-memhog-frag-singlefile config-workload-kvmstart-memhog-frag-singlefile-nothp
cat >> config-workload-kvmstart-memhog-frag-singlefile-nothp <<EOF
echo never > /sys/kernel/mm/transparent_hugepage/enabled
EOF

# o fragmenting with fio
cp config-workload-kvmstart-memhog config-workload-kvmstart-memhog-frag-fio-randread
cat >> config-workload-kvmstart-memhog-frag-fio-randread <<EOF
export KVMSTART_PRETEST=fragment
export KVMSTART_PRETEST_METHOD="fio-random-reader"
export KVMSTART_PRETEST_PREPARE_ARGS="--fio-threads 4 --fio-version 2.99 --fio-size \$((MEMTOTAL_BYTES*4/2))"
export KVMSTART_PRETEST_RUN_ARGS=\$KVMSTART_PRETEST_PREPARE_ARGS
export KVMSTART_PRETEST_CLEANUP_ARGS=
EOF

# ABINIT
for TYPE in full; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-abinit-tmbt-$TYPE > config-hpc-abinit-tmbt-hpcext-$TYPE
	sed -i -e '/ABINIT_MPI_PATH/d'    config-hpc-abinit-tmbt-hpcext-$TYPE
	sed -i -e '/ABINIT_MPI_LIBPATH/d' config-hpc-abinit-tmbt-hpcext-$TYPE
done

# FRONTISTR
sed 	-e 's/FRONTISTR_DOMAINS=.*/FRONTISTR_DOMAINS=$NUMCPUS/'		\
	-e 's/FRONTISTR_OMP_THREADS=.*/FRONTISTR_OMP_THREADS=1/'	\
	config-hpc-frontistr-hinge-full > config-hpc-frontistr-hinge-allproc-full
sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-frontistr-hinge-full > config-hpc-frontistr-hinge-hpcext-full
sed -i -e '/_MPI_PATH/d'    config-hpc-frontistr-hinge-hpcext-full
sed -i -e '/_MPI_LIBPATH/d' config-hpc-frontistr-hinge-hpcext-full
sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-frontistr-hinge-allproc-full > config-hpc-frontistr-hinge-allproc-hpcext-full
sed -i -e '/_MPI_PATH/d'    config-hpc-frontistr-hinge-allproc-hpcext-full
sed -i -e '/_MPI_LIBPATH/d' config-hpc-frontistr-hinge-allproc-hpcext-full

# MPAS
for MODEL in jwbaroclinic supercell; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-mpas-atmosphere-$MODEL-full > config-hpc-mpas-atmosphere-$MODEL-hpcext-full
	sed -i -e '/MPAS_MPI_PATH/d'    config-hpc-mpas-atmosphere-$MODEL-hpcext-full
	sed -i -e '/MPAS_MPI_LIBPATH/d' config-hpc-mpas-atmosphere-$MODEL-hpcext-full
done

# NAS
for TYPE in omp mpi; do
	sed -e 's/NAS_MAX_CPUS=.*/NAS_MAX_CPUS=$((NUMCPUS\/2))/' config-hpc-nas-c-class-$TYPE-full > config-hpc-nas-c-class-$TYPE-half
	sed -e 's/NAS_MAX_CPUS=.*/NAS_MAX_CPUS=$((NUMCPUS\/3))/' config-hpc-nas-c-class-$TYPE-full > config-hpc-nas-c-class-$TYPE-third
	sed -e 's/NAS_MAX_CPUS=.*/NAS_MAX_CPUS=2/' config-hpc-nas-c-class-$TYPE-full > config-hpc-nas-c-class-$TYPE-pair
	sed -e 's/NAS_MAX_CPUS=.*/NAS_MAX_CPUS=4/' config-hpc-nas-c-class-$TYPE-full > config-hpc-nas-c-class-$TYPE-minparallel
done
for CONFIG in `ls config-hpc-nas-c-class-*`; do
	# D class configurations
	DCONFIG=`echo $CONFIG | sed -e 's/nas-c-class/nas-d-class/'`
	sed	-e 's/NAS_CLASS=.*/NAS_CLASS=D/'		\
		-e 's/NAS_ITERATIONS=.*/NAS_ITERATIONS=3/'	\
		-e 's/ nas-...-ft//'				\
		-e 's/ nas-omp-cg/ nas-omp-cg nas-omp-dc/'	\
		$CONFIG > $DCONFIG

	# D class with binding configurations
	BDCONFIG=`echo $DCONFIG | sed -e 's/class-mpi/class-mpi-bind/' -e 's/class-omp/class-omp-bind/'`
	cp $DCONFIG $BDCONFIG
	echo "export NAS_BIND=yes" >> $BDCONFIG
done

# Netperf
sed -e 's/NETPERF_BUFFER_SIZES=.*/NETPERF_BUFFER_SIZES=64/' config-network-netperf-cstate  > config-network-netperf-cstate-small
sed -e 's/NETPERF_BUFFER_SIZES=.*/NETPERF_BUFFER_SIZES=64/' config-network-netperf-unbound > config-network-netperf-unbound-small

# Openfoam
for MODEL in motorbike-default motorbike-subdomains; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-openfoam-$MODEL-full > config-hpc-openfoam-$MODEL-hpcext-full
	sed -i -e '/OPENFOAM_MPI_PATH/d'    config-hpc-openfoam-$MODEL-hpcext-full
	sed -i -e '/OPENFOAM_MPI_LIBPATH/d' config-hpc-openfoam-$MODEL-hpcext-full

	for CONFIG in config-hpc-openfoam-$MODEL-full config-hpc-openfoam-$MODEL-hpcext-full; do
		NEWCONFIG=`echo $CONFIG | sed -e "s/-$MODEL-/-$MODEL-large-/"`
		sed	-e 's/OPENFOAM_CELL_LEVELS=.*/OPENFOAM_CELL_LEVELS=40/'			\
			-e 's/OPENFOAM_ITERATIONS=.*/OPENFOAM_ITERATIONS=1/'			\
			-e 's/Solver:.*/Solver:  PCG/'						\
			-e 's/Faces:.*/Faces:   14.6 million/'					\
			$CONFIG > $NEWCONFIG
	done
done
rm -f config-hpc-openfoam-*-meshonly
for CONFIG in `ls config-hpc-openfoam-*`; do
	sed -e 's/Steps:.*/Steps:   Stops after the mesh is integrated/'		\
		$CONFIG > $CONFIG-meshonly
	echo 'export OPENFOAM_STOPAT_MESH="yes"' >> $CONFIG-meshonly
done

# SALMON
for MODEL in mpi omp omp-bind hybrid; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-salmon-classicem-$MODEL-full > config-hpc-salmon-classicem-$MODEL-hpcext-full
	sed -i -e '/SALMON_MPI_PATH/d'    config-hpc-salmon-classicem-$MODEL-hpcext-full
	sed -i -e '/SALMON_MPI_LIBPATH/d' config-hpc-salmon-classicem-$MODEL-hpcext-full
done

# SPECFEM3D
sed -e 's/SPECFEM3D_MODEL=.*/SPECFEM3D_MODEL=global_s362/' config-hpc-specfem3d-small-s362ani-mpi-full > config-hpc-specfem3d-global-s362ani-mpi-full
for MODEL in global small; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-specfem3d-$MODEL-s362ani-mpi-full > config-hpc-specfem3d-$MODEL-s362ani-mpi-hpcext-full
	sed -i -e '/SPECFEM3D_MPI_PATH/d'    config-hpc-specfem3d-$MODEL-s362ani-mpi-hpcext-full
	sed -i -e '/SPECFEM3D_MPI_LIBPATH/d' config-hpc-specfem3d-$MODEL-s362ani-mpi-hpcext-full
done

# Stream
sed -e 's/STREAM_THREADS=.*/STREAM_THREADS=$((NUMCPUS*4))/' config-workload-stream-omp-nodes > config-workload-stream-omp-overload

# Stressng
sed -e 's/THREADS=.*/THREADS=1024/' config-workload-stressng-get > config-workload-stressng-get-1k

# Sysbench
sed -e 's/SYSBENCHCPU_MAX_THREADS=.*/SYSBENCHCPU_MAX_THREADS=2/' config-scheduler-sysbench-cpu > config-scheduler-sysbench-cpu-small
sed -e 's/SYSBENCHCPU_MAX_THREADS=.*/SYSBENCHCPU_MAX_THREADS=2/' config-scheduler-sysbench-cpu > config-scheduler-sysbench-cpu-small
sed -e 's/SYSBENCHTHREAD_MAX_THREADS_MAX_THREADS=.*/SYSBENCHTHREAD_MAX_THREADS_MAX_THREADS=2/' config-scheduler-sysbench-thread > config-scheduler-sysbench-thread-small
sed -e 's/SYSBENCHTHREAD_MAX_THREADS_MAX_THREADS=.*/SYSBENCHTHREAD_MAX_THREADS_MAX_THREADS=2/' config-scheduler-sysbench-thread > config-scheduler-sysbench-thread-small

# WRF
cp config-hpc-wrf-conus12km-hybrid-full config-hpc-wrf-conus12km-mpi-full
sed -i	\
	-e 's/WRF_PAR=.*/WRF_PAR="dmpar"/'		\
	-e 's/WRF_PROCESSES=.*/WRF_PROCESSES=$NUMCPUS/'	\
	-e 's/WRF_THREADS=.*/WRF_THREADS=1/'		config-hpc-wrf-conus12km-mpi-full
cp config-hpc-wrf-conus12km-hybrid-full config-hpc-wrf-conus12km-omp-full
sed -i	\
	-e 's/WRF_PAR=.*/WRF_PAR="smpar"/'		\
	-e 's/WRF_PROCESSES=.*/WRF_PROCESSES=1/'	\
	-e 's/WRF_THREADS=.*/WRF_THREADS=$NUMCPUS/'	config-hpc-wrf-conus12km-omp-full

PARMODEL=hybrid
for PARMODEL in hybrid mpi omp; do
	sed -e 's/=openmpi3/=openmpi3-hpc/' config-hpc-wrf-conus12km-$PARMODEL-full > config-hpc-wrf-conus12km-hpcext-$PARMODEL-full
	sed -i -e '/WRF_MPI_PATH/d'    config-hpc-wrf-conus12km-hpcext-$PARMODEL-full
	sed -i -e '/WRF_MPI_LIBPATH/d' config-hpc-wrf-conus12km-hpcext-$PARMODEL-full
done
