#!/usr/bin/env bash

spawn_server() {
  eval $(sionfwd-server bash-defs)
  sionfwd-spawn ${MPIEXEC} ${MPIEXEC_NP} 2 sionfwd-server
}

shutdown_server() {
  ${MPIEXEC} ${MPIEXEC_NP} 1 sionfwd-server shutdown
}

process_keys() {
  cat keys_rank0000_in.log keys_rank0001_in.log keys_rank0002_in.log keys_rank0003_in.log keys_rank0000_in.log keys_rank0001_in.log keys_rank0002_in.log keys_rank0003_in.log keys_rank0000_in.log keys_rank0001_in.log keys_rank0002_in.log keys_rank0003_in.log | sort -n -k 4 > keys_in.log 
  cat keys_rank0000_out.log keys_rank0001_out.log keys_rank0002_out.log keys_rank0003_out.log keys_iter_l1_rank0000_out.log keys_iter_l1_rank0001_out.log keys_iter_l1_rank0002_out.log keys_iter_l1_rank0003_out.log keys_iter_l2_rank0000_out.log keys_iter_l2_rank0001_out.log keys_iter_l2_rank0002_out.log keys_iter_l2_rank0003_out.log | sort -n -k 4 > keys_out.log 
}

export LSION_LOWLEVEL_API=sionfwd

. ./test_parallel.sh
test_parallel sfwd___2 \
  pre_hook:spawn_server \
  post_hook:shutdown_server \
  post_hook:process_keys \
  diff:keys_in.log:keys_out.log
