ELPiNv2.cmd 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # This script modifies the namelist and finds the best
  2. set -ue
  3. # Avoid issues with handling floating point numbers
  4. export LC_NUMERIC="en_US.UTF-8"
  5. SCRIPTPATH=$(readlink -f "`dirname \"$0\"`")
  6. # Function to check if the script is executed in a NEMO RUNDIR
  7. # The elements checked are the bathymetry, the presence of the namelist_cfg
  8. # and the nemo binary that can be called opa or nemo.exe.
  9. function in_rundir () {
  10. [[ -f bathy_meter.nc ]] && [[ -f opa || -f nemo.exe ]]
  11. }
  12. # Function to modify namelist parameter
  13. function modify_parameter () {
  14. [ $# -ne 3 ] && echo "Needed arguments are :" && echo "namelist variable new_value" && return 1
  15. namelist=$1
  16. [ ! -f $namelist ] && echo "ERROR: Namelist $namelist not present" && return 1
  17. var=$2 && value=$3
  18. cond=$( grep -c " $var " $namelist )
  19. [ $cond -eq 0 ] && echo "ERROR: Variable $var not present in namelist $namelist" && return 1
  20. REG="^\( *$var *= *\).*"
  21. sed -i "s|$REG|\1$value ! Parameter modified |g " $namelist
  22. }
  23. # Parameters are:
  24. # bathymetry
  25. # Max number of processors
  26. # Check that file exists
  27. binary=$SCRIPTPATH/bin/mpp_domain_decomposition.exe
  28. [ ! -f $binary ] && printf "Tool is not configured. Configure it!\n" && exit 1
  29. ldd $binary | grep "not found" -q && echo "ERROR: Must load proper version of NETCDF" && exit 1
  30. # Check the parameters
  31. if in_rundir ; then
  32. [ $# -ne 1 ] && printf "ELPiN v2\nYou are in a NEMO rundir.\nNeeded parameter is:\nmax_number_of_processes\n" && exit 1
  33. num=$1 && max_num=$( echo "1.05 * $num" | bc -l ) && max_num=$( printf "%.0f\n" "$max_num" )
  34. min_num=$( echo "0.9 * $num" | bc -l ) && min_num=$( printf "%.0f\n" "$min_num" )
  35. # Take parameter and check that file exists
  36. bathymetry="bathy_meter.nc"
  37. else
  38. [ $# -ne 2 ] && printf "ELPiN v2\nNeeded parameters are:\n/path/to/bathymetry\nmax_number_of_processes\n" && exit 1
  39. num=$2 && max_num=$( echo "1.05 * $num" | bc -l ) && max_num=$( printf "%.0f\n" "$max_num" )
  40. min_num=$( echo "0.9 * $num" | bc -l ) && min_num=$( printf "%.0f\n" "$min_num" )
  41. # Take parameter and check that file exists
  42. bathymetry=$1
  43. fi
  44. [ ! -f $bathymetry ] && echo "Error file does not exist" && exit 1
  45. bathy_checksum=$(md5sum $bathymetry | cut -d" " -f1)
  46. # If bathymetry has been already processed
  47. if [ -f $SCRIPTPATH/database/$bathy_checksum ]; then
  48. # Check that our number of processes falls inside the saved range
  49. layout=$SCRIPTPATH/database/$bathy_checksum
  50. existing_min=$( grep '%' $layout | awk '{print $5}' | sort -n | head -n 1)
  51. existing_max=$( grep '%' $layout | awk '{print $5}' | sort -n | tail -n 1)
  52. if [ $num -ge $existing_min ] && [ $num -le $existing_max ]; then
  53. CASE_EXIST=TRUE
  54. else
  55. CASE_EXIST=FALSE
  56. fi
  57. else
  58. CASE_EXIST=FALSE
  59. fi
  60. # If ELPiN has been already executed for this bathymetry for this number of cores
  61. # we simply load the previously generated list
  62. if [ "$CASE_EXIST" == "FALSE" ]; then
  63. # Read dimensions
  64. X=$(ncdump -h $bathymetry | grep "\sx = [0-9]*" -o | grep -e "[0-9]*" -o)
  65. Y=$(ncdump -h $bathymetry | grep "\sy = [0-9]*" -o | grep -e "[0-9]*" -o)
  66. # Modify the namelist
  67. cp $SCRIPTPATH/src/namelist_mpp .
  68. # Put bathymetry name in namelist
  69. modify_parameter namelist_mpp cbathy "\'$bathymetry\'"
  70. # Put grid dimensions
  71. modify_parameter namelist_mpp jpiglo $X
  72. modify_parameter namelist_mpp jpjglo $Y
  73. # Put min and max number of processes
  74. modify_parameter namelist_mpp jprocmin $min_num
  75. modify_parameter namelist_mpp jprocmax $max_num
  76. # Execute Binary. It will generate a file with name expgrid_PEs_decomposition.layout
  77. $binary &> /dev/null
  78. # Check that the execution completed correctly
  79. ERRORCODE=$?
  80. if [[ $ERRORCODE -ne 0 ]] ;then
  81. echo "Not possible to run dd.x for error $ERRORCODE"
  82. exit 1
  83. fi
  84. else
  85. cp $SCRIPTPATH/database/$bathy_checksum expgrid_PEs_decomposition.layout
  86. fi
  87. # Process the generated file to find the best decomposition.
  88. # Get the best decomposition and split the output into jpnij, jpni and jpnj variables to put it inside the namelist
  89. # Get rid of decompositions that need more processes than num
  90. DECOMPOSITION_VARS=$( grep "%" expgrid_PEs_decomposition.layout | awk -v maxnum=$num '{ if( $5 <= maxnum ) print $5/($6/100)**2" " $2 " " $3" " $5 }' | sort -n | tail -n 1 | awk '{print $4" "$2" "$3}')
  91. jpnij=$(echo $DECOMPOSITION_VARS | cut -d " " -f 1 )
  92. jpni=$(echo $DECOMPOSITION_VARS | cut -d " " -f 2 )
  93. jpnj=$(echo $DECOMPOSITION_VARS | cut -d " " -f 3 )
  94. # If the script is executed in the rundir, it automatically modifies the namelist making first a backup for safety.
  95. # Check that the tool is configured properly
  96. # This feature is useless right now but can be useful in the future,
  97. # by now we will comment this and leave it here.
  98. # if in_rundir ; then
  99. # cp namelist_cfg namelist_cfg.$(date +%d%m%Y-%H:%M)_backup
  100. # modify_parameter namelist_cfg jpnij $jpnij && modify_parameter namelist_cfg jpni $jpni && modify_parameter namelist_cfg jpnj $jpnj || exit 1
  101. # echo "The namelist_cfg has been modified, you must run with $jpnij NEMO processes"
  102. # else
  103. # echo $jpnij $jpni $jpnj
  104. # fi
  105. # Output the jpnij jpni and jpnj values
  106. echo $jpnij $jpni $jpnj
  107. # Copying the list of decompositions to the database named as the checksum.
  108. if [ "$CASE_EXIST" == "FALSE" ]; then
  109. # In case database folder does not exist, create it
  110. [ ! -d $SCRIPTPATH/database ] && mkdir -p $SCRIPTPATH/database
  111. cp expgrid_PEs_decomposition.layout $SCRIPTPATH/database/$bathy_checksum
  112. fi
  113. # Remove the tmp file containing the possible decompositions
  114. rm -f expgrid_PEs_decomposition.layout
  115. rm -f namelist_mpp
  116. set +xuve