simba.tmpl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. #
  3. # submit.sh
  4. #
  5. # Portable bash script to run LPJ-GUESS version:
  6. # BINARY
  7. # as a parallel job using PBS on Simba.
  8. #
  9. # Created automatically on DATE
  10. #
  11. # Usage:
  12. #
  13. # 1. Copy script to the directory where you want output written.
  14. # This will be called the RUN DIRECTORY.
  15. # 2. In an editor, set appropriate values for the variables NPROCESS,
  16. # INSFILE, GRIDLIST and OUTFILES (NB: no space after the = sign):
  17. NPROCESS=15
  18. WALLTIME=150:00:00
  19. INSFILE=guess.ins
  20. INPUT_MODULE=cru_ncep
  21. GRIDLIST=gridlist.txt
  22. OUTFILES='*.out'
  23. # Where:
  24. # NPROCESS = number of processes in parallel job
  25. # WALLTIME = maximum wall (real) time for job hh:mm:ss
  26. # INSFILE = path to ins file from run directory
  27. # INPUT_MODULE = input module to use
  28. # GRIDLIST = path to gridlist file from run directory
  29. # OUTFILES = list of LPJ-GUESS output files in single quotes,
  30. # and separated by spaces (filenames only, including
  31. # extension, no directory.) Shell wildcards are allowed.
  32. #
  33. # 3. Run the script using the command:
  34. # ./submit.sh
  35. # or:
  36. # ./submit.sh [-n <name>] [-s <file>] [-i <ins-file>]
  37. #
  38. # All arguments are optional and interpreted as:
  39. # name = the name of the job (shown in PBS queue)
  40. # file = filename of a file which can override the variables
  41. # above
  42. # ins-file = instruction file to use, overrides the INSFILE
  43. # variable above
  44. #
  45. # Nothing to change past here
  46. ########################################################################
  47. # Exit if any command fails
  48. set -e
  49. # Handle the command line arguments
  50. while getopts ":n:s:i:" opt; do
  51. case $opt in
  52. n ) name=$OPTARG ;;
  53. s ) submit_vars_file=$OPTARG ;;
  54. i ) ins=$OPTARG ;;
  55. esac
  56. done
  57. # Override the submit variables with the contents of a file, if given
  58. if [ -n "$submit_vars_file" ]; then
  59. source $submit_vars_file
  60. fi
  61. # Override INSFILE with the ins-file parameter, if given
  62. if [ -n "$ins" ]; then
  63. INSFILE=$ins
  64. fi
  65. # Convert INSFILE to an absolute path since we will be starting the
  66. # guess instances from different directories.
  67. # Please note when porting this script: readlink may not be available
  68. # on non-Linux systems. Also, using absolute path names means the
  69. # instruction file needs to be in a place accessible from the nodes.
  70. INSFILE=$(readlink -f "$INSFILE")
  71. GRIDLIST_FILENAME=$(basename $GRIDLIST)
  72. # This function creates the gridlist files for each run by splitting
  73. # the original gridlist file into approximately equal parts.
  74. function split_gridlist {
  75. # Create empty gridlists first to make sure each run gets one
  76. for ((a=1; a <= NPROCESS ; a++))
  77. do
  78. echo > run$a/$GRIDLIST_FILENAME
  79. done
  80. # Figure out suitable number of lines per gridlist, get the number of
  81. # lines in original gridlist file, divide by NPROCESS and round up.
  82. local lines_per_run=$(wc -l $GRIDLIST | \
  83. awk '{ x = $1/'$NPROCESS'; d = (x == int(x)) ? x : int(x)+1; print d}')
  84. # Use the split command to split the files into temporary files
  85. split --suffix-length=4 --lines $lines_per_run $GRIDLIST tmpSPLITGRID_
  86. # Move the temporary files into the runX-directories
  87. local files=$(ls tmpSPLITGRID_*)
  88. local i=1
  89. for file in $files
  90. do
  91. mv $file run$i/$GRIDLIST_FILENAME
  92. i=$((i+1))
  93. done
  94. }
  95. # Create header of progress.sh script
  96. echo "##############################################################" > progress.sh
  97. echo "# PROGRESS.SH" >> progress.sh
  98. echo "# Upload current guess.log files from local nodes and check" >> progress.sh
  99. echo "# Usage: sh progress.sh" >> progress.sh
  100. echo >> progress.sh
  101. # Create a run subdirectory for each process and clean up
  102. for ((a=1; a <= NPROCESS ; a++))
  103. do
  104. mkdir -p run$a
  105. cd run$a ; rm -f guess.log ; rm -f $GRIDLIST_FILENAME ; cd ..
  106. echo "echo '********** Last few lines of ./run${a}/guess.log: **********'" >> progress.sh
  107. echo "tail ./run${a}/guess.log" >> progress.sh
  108. done
  109. split_gridlist
  110. # Create PBS script to request place in queue
  111. cat <<EOF > guess.cmd
  112. #!/bin/bash
  113. #PBS -l nodes=$NPROCESS
  114. #PBS -l walltime=$WALLTIME
  115. set -e
  116. cd \$PBS_O_WORKDIR
  117. umask 022
  118. mpiexec -comm mpich-p4 BINARY -parallel -input $INPUT_MODULE $INSFILE
  119. EOF
  120. cat <<EOF > append.cmd
  121. #!/bin/bash
  122. #PBS -l nodes=1
  123. #PBS -l walltime=$WALLTIME
  124. set -e
  125. cd \$PBS_O_WORKDIR
  126. function append_files {
  127. local number_of_jobs=\$1
  128. local file=\$2
  129. cp run1/\$file \$file
  130. local i=""
  131. for ((i=2; i <= number_of_jobs; i++))
  132. do
  133. if [ -f run\$i/\$file ]; then
  134. cat run\$i/\$file | awk 'NR!=1 || NF==0 || \$1 == \$1+0 { print \$0 }' >> \$file
  135. fi
  136. done
  137. }
  138. pushd run1 &> /dev/null
  139. outfiles_unexpanded='$OUTFILES'
  140. outfiles_expanded=\$(echo \$outfiles_unexpanded)
  141. popd &> /dev/null
  142. for file in \$outfiles_expanded
  143. do
  144. append_files $NPROCESS \$file
  145. done
  146. cat run*/guess.log > guess.log
  147. EOF
  148. # Submit guess job
  149. append_dependency=$(qsub -N ${name:-"guess"} guess.cmd)
  150. # Submit append job
  151. qsub -W depend=afterok:$append_dependency -N ${name:-"guess"}"_append" append.cmd