platon.tmpl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Platon.
  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=16 # NB: Should be multiple of 8 on Platon!
  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. # Figure out how many _nodes_ we should request. On Platon the policy is
  66. # that whole nodes (with 8 cores) should be allocated, so we should request
  67. # nodes with the nodes=<x>:ppn=8 format.
  68. CORES_PER_NODE=8
  69. if [[ $((NPROCESS%CORES_PER_NODE)) == 0 ]]; then
  70. nodes=$((NPROCESS/CORES_PER_NODE))
  71. else
  72. nodes=$((NPROCESS/CORES_PER_NODE+1))
  73. fi
  74. # Convert INSFILE to an absolute path since we will be starting the
  75. # guess instances from different directories.
  76. # Please note when porting this script: readlink may not be available
  77. # on non-Linux systems. Also, using absolute path names means the
  78. # instruction file needs to be in a place accessible from the nodes.
  79. INSFILE=$(readlink -f "$INSFILE")
  80. GRIDLIST_FILENAME=$(basename $GRIDLIST)
  81. # This function creates the gridlist files for each run by splitting
  82. # the original gridlist file into approximately equal parts.
  83. function split_gridlist {
  84. # Create empty gridlists first to make sure each run gets one
  85. for ((a=1; a <= NPROCESS ; a++))
  86. do
  87. echo > run$a/$GRIDLIST_FILENAME
  88. done
  89. # Figure out suitable number of lines per gridlist, get the number of
  90. # lines in original gridlist file, divide by NPROCESS and round up.
  91. local lines_per_run=$(wc -l $GRIDLIST | \
  92. awk '{ x = $1/'$NPROCESS'; d = (x == int(x)) ? x : int(x)+1; print d}')
  93. # Use the split command to split the files into temporary files
  94. split --suffix-length=4 --lines $lines_per_run $GRIDLIST tmpSPLITGRID_
  95. # Move the temporary files into the runX-directories
  96. local files=$(ls tmpSPLITGRID_*)
  97. local i=1
  98. for file in $files
  99. do
  100. mv $file run$i/$GRIDLIST_FILENAME
  101. i=$((i+1))
  102. done
  103. }
  104. # Create header of progress.sh script
  105. echo "##############################################################" > progress.sh
  106. echo "# PROGRESS.SH" >> progress.sh
  107. echo "# Upload current guess.log files from local nodes and check" >> progress.sh
  108. echo "# Usage: sh progress.sh" >> progress.sh
  109. echo >> progress.sh
  110. # Create a run subdirectory for each process and clean up
  111. for ((a=1; a <= NPROCESS ; a++))
  112. do
  113. mkdir -p run$a
  114. cd run$a ; rm -f guess.log ; rm -f $GRIDLIST_FILENAME ; cd ..
  115. echo "echo '********** Last few lines of ./run${a}/guess.log: **********'" >> progress.sh
  116. echo "tail ./run${a}/guess.log" >> progress.sh
  117. done
  118. split_gridlist
  119. # Create PBS script to request place in queue
  120. cat <<EOF > guess.cmd
  121. #!/bin/bash
  122. #PBS -l nodes=$nodes:ppn=$CORES_PER_NODE
  123. #PBS -l walltime=$WALLTIME
  124. set -e
  125. cd \$PBS_O_WORKDIR
  126. if ! type -P mpiexec &> /dev/null; then
  127. echo "Didn't find mpiexec! Make sure an MPI module is loaded in your" >&2
  128. echo "login script (~/.bashrc) and recompile LPJ-GUESS with MPI support!" >&2
  129. exit 1
  130. fi
  131. # If there's a script for setting up files on local disk, run it
  132. if [ -f setup_local.sh ]; then
  133. mpiexec -pernode setup_local.sh
  134. fi
  135. # In each run directory, create a symbolic link to the node local storage
  136. for ((a=1; a <= $NPROCESS ; a++))
  137. do
  138. cd run\$a
  139. if [ -h local ]; then
  140. rm local
  141. fi
  142. ln -s \$PBS_O_LOCAL local
  143. cd ..
  144. done
  145. mpiexec -n $NPROCESS BINARY -parallel -input $INPUT_MODULE $INSFILE
  146. EOF
  147. cat <<EOF > append.cmd
  148. #!/bin/bash
  149. #PBS -l nodes=1:ppn=1
  150. #PBS -l walltime=$WALLTIME
  151. set -e
  152. cd \$PBS_O_WORKDIR
  153. function append_files {
  154. local number_of_jobs=\$1
  155. local file=\$2
  156. cp run1/\$file \$file
  157. local i=""
  158. for ((i=2; i <= number_of_jobs; i++))
  159. do
  160. if [ -f run\$i/\$file ]; then
  161. cat run\$i/\$file | awk 'NR!=1 || NF==0 || \$1 == \$1+0 { print \$0 }' >> \$file
  162. fi
  163. done
  164. }
  165. pushd run1 &> /dev/null
  166. outfiles_unexpanded='$OUTFILES'
  167. outfiles_expanded=\$(echo \$outfiles_unexpanded)
  168. popd &> /dev/null
  169. for file in \$outfiles_expanded
  170. do
  171. append_files $NPROCESS \$file
  172. done
  173. EOF
  174. # Submit guess job
  175. append_dependency=$(qsub -N ${name:-"guess"} guess.cmd)
  176. # Submit append job
  177. qsub -W depend=afterok:$append_dependency -N ${name:-"guess"}"_append" append.cmd