test_parallel.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. # Test parallel calls of r.import in Bash.
  3. # To test the reprojection, it needs to run in a location other than
  4. # WGS84 (that's the CRS of imported file used) or r.import may (should)
  5. # skip the reprojection part of the code.
  6. # Imported raster map presence or absence based on a search pattern is
  7. # used to evaluate if the expected result is created.
  8. # This test likely overwhelms the system for a while because all the
  9. # processes will start more or less at once, but this is exactly the
  10. # point of the test because we want increase a chance of a potential
  11. # race condition or name conflict.
  12. # Undefined variables are errors.
  13. set -u
  14. # More set commands later on.
  15. if [ $# -eq 0 ]
  16. then
  17. NUM_SERIALS=3
  18. NUM_PARALLELS=50
  19. elif [ $# -eq 2 ]
  20. then
  21. # Allow user to set a large number of processes.
  22. NUM_SERIALS=$3
  23. NUM_PARALLELS=$4
  24. else
  25. >&2 echo "Usage:"
  26. >&2 echo " $0"
  27. >&2 echo " $0 <nproc serial> <nproc parallel>"
  28. >&2 echo "Example:"
  29. >&2 echo " $0 5 300"
  30. >&2 echo "Use zero or two parameters, not $#."
  31. exit 1
  32. fi
  33. # Remove maps at exit.
  34. cleanup () {
  35. EXIT_CODE=$?
  36. g.remove type=raster pattern="test_parallel_ser_*" -f --quiet
  37. g.remove type=raster pattern="test_parallel_par_*" -f --quiet
  38. exit $EXIT_CODE
  39. }
  40. trap cleanup EXIT
  41. DATA="data/data2.asc"
  42. # Fail fast and show commands
  43. set -e
  44. set -x
  45. # Serial
  46. # The parallel loop version won't fail even if command returns non-zero,
  47. # so we need to check the command ahead of time.
  48. # Since this is useful mostly for making sure the command works for this
  49. # script, so the command should be exactly the same.
  50. for i in `seq 1 $NUM_SERIALS`
  51. do
  52. r.import input="$DATA" output="test_parallel_ser_$i"
  53. done
  54. # Parallel
  55. for i in `seq 1 $NUM_PARALLELS`
  56. do
  57. r.import input="$DATA" output="test_parallel_par_$i" &
  58. done
  59. wait
  60. EXPECTED=$NUM_PARALLELS
  61. NUM=$(g.list type=raster pattern='test_parallel_par_*' mapset=. | wc -l)
  62. if [ ${NUM} -ne ${EXPECTED} ]
  63. then
  64. echo "Parallel test: Got ${NUM} but expected ${EXPECTED} maps"
  65. exit 1
  66. fi