#!/usr/bin/env ruby
<<<<<<< HEAD
#
# 表題 dcpam_bench.rb : DCPAM ベンチマーク用スクリプト
#
#   * ステップ数の異なる時間積分の時間差を測定し, ファイルに出力する
#
#   * 異なる MPI 並列数, OpenMP 並列数による違いを調べる
#
#   * 下記の例は, 解像度により実験設定ファイル名を指定しているが,
#     2 つの時間ステップの実験ファイル名と初期値生成ファイル名を
#     独自に指定しても良い
#
# 履歴  2025/10/03 竹広真一
#
require 'open3'

#==================== 設定 ====================

#
#---------- 解像度 (下のファイル名に使用)
#
# RESOLUTION = "T21L20"
RESOLUTION = "T42L20"
# RESOLUTION = "T85L20"

#
#---------- 実行ファイル名
#
=======

require 'open3'

# RESOLUTION = "T21L20"
# RESOLUTION = "T42L20"
RESOLUTION = "T85L20"

>>>>>>> 1c5e8a4daabd5736929e229566050699f8690362
MPIRUN="mpirun.mpich"
DCPAM_PROGRAM="../dcpam_main_hs94"
INIT_PROGRAM="../dcpam_init_data_hs94"
TIME_COMMAND="/usr/bin/time -p"

<<<<<<< HEAD
#
#---------- 実験設定ファイル名
#
INIT_CONF="init_data_hs94_"+RESOLUTION+".conf"
RUN1_CONF="dcpam_hs94_"+RESOLUTION+"_t1.conf"
RUN2_CONF="dcpam_hs94_"+RESOLUTION+"_t2.conf"
=======
INIT_CONF="init_data_hs94_"+RESOLUTION+".conf"
RUN1_CONF="dcpam_hs94_"+RESOLUTION+"_t1.conf"
RUN2_CONF="dcpam_hs94_"+RESOLUTION+"_t2.conf"
# INIT_CONF="init_data_hs94_"+RESOLUTION+"_NPV4.conf"
# RUN1_CONF="dcpam_hs94_"+RESOLUTION+"_NPV4_t1.conf"
# RUN2_CONF="dcpam_hs94_"+RESOLUTION+"_NPV4_t2.conf"
>>>>>>> 1c5e8a4daabd5736929e229566050699f8690362

# INIT_CONF="init_data_hs94_T21L20.conf"
# RUN1_CONF="dcpam_hs94_T21L20_t1.conf"
# RUN2_CONF="dcpam_hs94_T21L20_t2.conf"

<<<<<<< HEAD
#
#---------- ログファイル名
#
LOGROOT="dcpam_hs94_bench_"+RESOLUTION
BENCHLOG="dcpam_hs94_bench_"+RESOLUTION+".log"

#
#---------- 並列計算設定
MPILIST = [1,2,4]    # MPI 並列数リスト
OMPLIST = [1,2]      # OpenMP 並列数リスト
=======
# LOGROOT="dcpam_hs94_bench_"+RESOLUTION+"_NPV4"
# BENCHLOG="dcpam_hs94_bench_"+RESOLUTION+"_NPV4.log"
LOGROOT="dcpam_hs94_bench_"+RESOLUTION
BENCHLOG="dcpam_hs94_bench_"+RESOLUTION+".log"

MPILIST = [1,2,4,8]
OMPLIST = [4]
# MPILIST = [1,2,4]
# OMPLIST = [1,2]
>>>>>>> 1c5e8a4daabd5736929e229566050699f8690362

#==================== 関数定義 ====================
def init_command(mpinum,confile,logfile)
  return sprintf("%s -n %d %s -N=%s > %s",
                  MPIRUN, mpinum, INIT_PROGRAM, confile, logfile)
end

def dcpam_command(mpinum,confile,logfile)
  return sprintf("%s %s -n %d %s -N=%s >> %s",
                 TIME_COMMAND, MPIRUN, mpinum, DCPAM_PROGRAM, confile, logfile)
end

#==================== メイン ====================
benchfile=File.open(BENCHLOG,mode="w")

benchfile.puts(sprintf("# %s\n", RESOLUTION))
benchfile.puts(sprintf("# OMP MPI TIME\n"))
# printf("# %s\n", RESOLUTION) 
# printf("# OMP MPI TIME(sec)\n")

OMPLIST.each do |omp|
  printf("# OMP = %d\n", omp)
  
  MPILIST.each do |mpi|
    logfile = sprintf("%s-MPI%dOMP%d.log",LOGROOT,mpi,omp)

    #---------- 初期値作成 ----------
    # command = MPIRUN + mpi +' '+ INIT_PROGRAM + ' -N=' + INIT_CONF + '> ' + LOGFILE

    command = init_command(mpi,INIT_CONF,logfile)
    printf "# %s\n", command
    system({'OMP_NUM_THREADS' => omp.to_s}, command) 

    #---------- 計測 1 回目 ----------
    command = dcpam_command(mpi,RUN1_CONF,logfile)
    printf "# %s\n", command
    stdout, stderr_output, status = Open3.capture3({'OMP_NUM_THREADS' => omp.to_s},command)
    # p stderr_output
    t1 = stderr_output.match(/real (\d+(\.\d+)?)/).to_a[1].to_f
    #p t1

    #---------- 計測 2 回目 ----------
    command = dcpam_command(mpi,RUN2_CONF,logfile)
    printf "# %s\n", command
    stdout, stderr_output, status = Open3.capture3({'OMP_NUM_THREADS' => omp.to_s},command)
    # p stderr_output
    t2 = stderr_output.match(/real (\d+(\.\d+)?)/).to_a[1].to_f
    # p t2
    # printf(" %d  %d  %f\n", omp, mpi, t2-t1)
    benchfile.puts(sprintf(" %d  %d  %f\n", omp, mpi, t2-t1))
  end
  benchfile.puts("\n\n")
end  

benchfile.close

