gimle.tmpl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 SLURM.
  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. # Handle the command line arguments
  48. while getopts ":n:s:i:" opt; do
  49. case $opt in
  50. n ) name=$OPTARG ;;
  51. s ) submit_vars_file=$OPTARG ;;
  52. i ) ins=$OPTARG ;;
  53. esac
  54. done
  55. # Override the submit variables with the contents of a file, if given
  56. if [ -n "$submit_vars_file" ]; then
  57. source $submit_vars_file
  58. fi
  59. # Override INSFILE with the ins-file parameter, if given
  60. if [ -n "$ins" ]; then
  61. INSFILE=$ins
  62. fi
  63. # Convert INSFILE to an absolute path since we will be starting the
  64. # guess instances from different directories.
  65. # Please note when porting this script: readlink may not be available
  66. # on non-Linux systems. Also, using absolute path names means the
  67. # instruction file needs to be in a place accessible from the nodes.
  68. INSFILE=$(readlink -f "$INSFILE")
  69. GRIDLIST_FILENAME=$(basename $GRIDLIST)
  70. # This function creates the gridlist files for each run by splitting
  71. # the original gridlist file into approximately equal parts.
  72. function split_gridlist {
  73. # Create empty gridlists first to make sure each run gets one
  74. for ((a=1; a <= NPROCESS ; a++))
  75. do
  76. echo > run$a/$GRIDLIST_FILENAME
  77. done
  78. # Figure out suitable number of lines per gridlist, get the number of
  79. # lines in original gridlist file, divide by NPROCESS and round up.
  80. local lines_per_run=$(wc -l $GRIDLIST | \
  81. awk '{ x = $1/'$NPROCESS'; d = (x == int(x)) ? x : int(x)+1; print d}')
  82. # Use the split command to split the files into temporary files
  83. split --suffix-length=4 --lines $lines_per_run $GRIDLIST tmpSPLITGRID_
  84. # Move the temporary files into the runX-directories
  85. local files=$(ls tmpSPLITGRID_*)
  86. local i=1
  87. for file in $files
  88. do
  89. mv $file run$i/$GRIDLIST_FILENAME
  90. i=$((i+1))
  91. done
  92. }
  93. # Create header of progress.sh script
  94. echo "##############################################################" > progress.sh
  95. echo "# PROGRESS.SH" >> progress.sh
  96. echo "# Upload current guess.log files from local nodes and check" >> progress.sh
  97. echo "# Usage: sh progress.sh" >> progress.sh
  98. echo >> progress.sh
  99. # Create a run subdirectory for each process and clean up
  100. for ((a=1; a <= NPROCESS ; a++))
  101. do
  102. mkdir -p run$a
  103. cd run$a ; rm -f guess.log ; rm -f $GRIDLIST_FILENAME ; cd ..
  104. echo "echo '********** Last few lines of ./run${a}/guess.log: **********'" >> progress.sh
  105. echo "tail ./run${a}/guess.log" >> progress.sh
  106. done
  107. split_gridlist
  108. # Create SLURM script to request place in queue
  109. cat <<EOF > guess.cmd
  110. #!/bin/bash
  111. #SBATCH -n $NPROCESS
  112. #SBATCH --time=$WALLTIME
  113. mpprun BINARY -parallel -input $INPUT_MODULE $INSFILE
  114. function append_files {
  115. local number_of_jobs=\$1
  116. local file=\$2
  117. cp run1/\$file \$file
  118. local i=""
  119. for ((i=2; i <= number_of_jobs; i++))
  120. do
  121. if [ -f run\$i/\$file ]; then
  122. cat run\$i/\$file | awk 'NR!=1 || NF==0 || \$1 == \$1+0 { print \$0 }' >> \$file
  123. fi
  124. done
  125. }
  126. pushd run1 &> /dev/null
  127. outfiles_unexpanded='$OUTFILES'
  128. outfiles_expanded=\$(echo \$outfiles_unexpanded)
  129. popd &> /dev/null
  130. for file in \$outfiles_expanded
  131. do
  132. append_files $NPROCESS \$file
  133. done
  134. cat run*/guess.log > guess.log
  135. EOF
  136. # Submit job
  137. sbatch -J ${name:-"guess"} guess.cmd | awk '{print $NF}'