#!/bin/bash
noderank=$1
compile_path=$2
chip_name=$3
is_share_memory=$4

config_path=${chip_name}/case_config.yaml
source_file=${compile_path}/GPU_ComputeFP.cu

M=1024
N=1024
K=1024
M_value=$(awk '/M:/ {print $2}' ${config_path} | tr -d '"')
if [ ! -z "$M_value" ]; then
  M=$M_value
fi
N_value=$(awk '/N:/ {print $2}' ${config_path} | tr -d '"')
if [ ! -z "$N_value" ]; then
  N=$N_value
fi
K_value=$(awk '/K:/ {print $2}' ${config_path} | tr -d '"')
if [ ! -z "$K_value" ]; then
  K=$K_value
fi

compile() {
  cp gemm.cu ${source_file}
  sed -i "s/constexpr int M = [0-9]*;/constexpr int M = ${M};/" ${source_file}
  sed -i "s/constexpr int N = [0-9]*;/constexpr int N = ${N};/" ${source_file}
  sed -i "s/constexpr int K = [0-9]*;/constexpr int K = ${K};/" ${source_file}
  nvcc -c -o ${executable_o} ${source_file} -I/usr/local/cuda/include -I/usr/local/mpi/include
  mpic++ -o ${executable} ${executable_o} -L/usr/local/cuda/lib64 -L/usr/local/mpi/lib -lcudart -lcuda -lmpi -lcublas
}
executable_o=${compile_path}/GPU_ComputeFP.o
executable=${compile_path}/GPU_ComputeFP
if [ "$is_share_memory" = 1 ] && [ "$noderank" -eq 0 ]; then
  compile
elif [ "$is_share_memory" = 0 ]; then
  compile
fi
sleep 10
mpirun --allow-run-as-root -np 8 $executable