You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parallel_jobs.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env bash
  2. launchThread()
  3. {
  4. local threadFirst=${1}
  5. local threadLast=${2}
  6. local threadCount=${3}
  7. local threadNumber=${4}
  8. local parallelNbItems=${5}
  9. local instanceNumber=0
  10. for i in $(seq ${threadFirst} ${parallelNbItems} ${threadLast})
  11. do
  12. local instanceBegin=${i}
  13. local instanceEnd=$((${i} + ${parallelNbItems} - 1))
  14. if [ ${instanceEnd} -gt ${threadLast} ]
  15. then
  16. instanceEnd=${threadLast}
  17. fi
  18. local instanceCount=$((${instanceEnd} - ${instanceBegin} + 1))
  19. echo "Launching thread ${threadNumber} instance ${instanceNumber}: ${instanceBegin} ${instanceEnd} ${instanceCount}"
  20. launchInstance ${instanceBegin} ${instanceEnd} ${instanceCount} ${threadNumber} ${instanceNumber}
  21. instanceNumber=$((${instanceNumber} + 1))
  22. done
  23. }
  24. launch()
  25. {
  26. local first=$1
  27. local last=$2
  28. local threads=$3
  29. local parallelNbItems=$4
  30. local itemsCount=$((${last} - ${first} + 1))
  31. if [ ${threads} -gt ${itemsCount} ]
  32. then
  33. threads=${itemsCount}
  34. fi
  35. local parallelItems=$(( $((${itemsCount} + $((${itemsCount} % ${threads})))) / ${threads}))
  36. if [ "${parallelNbItems}" == "" ] || [ ${parallelNbItems} -gt ${parallelItems} ]
  37. then
  38. parallelNbItems=${parallelItems}
  39. fi
  40. echo "Running pre launch"
  41. preLaunch
  42. echo "Processing ${itemsCount} elements in ${threads} threads with ${parallelNbItems} by ${parallelNbItems} elements per thread"
  43. local threadsPid=()
  44. local threadNumber=0
  45. # trap ctrl_c INT
  46. for i in $(seq ${first} ${parallelItems} ${last})
  47. do
  48. local threadFirst=${i}
  49. local threadLast=$((${i} + ${parallelItems} - 1))
  50. if [ ${threadLast} -gt ${last} ]
  51. then
  52. threadLast=${last}
  53. fi
  54. local threadCount=$((${threadLast} - ${threadFirst} + 1))
  55. echo "Launching thread ${threadNumber}: from ${threadFirst} to ${threadLast} (${threadCount})"
  56. launchThread ${threadFirst} ${threadLast} ${threadCount} ${threadNumber} ${parallelNbItems} &
  57. local threadPid=$!
  58. threadsPid[${threadNumber}]=${threadPid}
  59. threadNumber=$((${threadNumber} + 1))
  60. done
  61. local stillRunningCount=${#threadsPid[@]}
  62. local firstRun=1
  63. while [ ${stillRunningCount} -ne 0 ]
  64. do
  65. stillRunningCount=0
  66. local newFinished=0
  67. for i in $(seq 0 $((${#threadsPid[@]} - 1)))
  68. do
  69. pid=${threadsPid[${i}]}
  70. if [ "${pid}" == "" ]
  71. then
  72. pid=0
  73. fi
  74. if [ ${pid} -ne 0 ]
  75. then
  76. kill -0 ${pid} > /dev/null 2>/dev/null
  77. local ret=$?
  78. if [ ${ret} -eq 1 ]
  79. then
  80. echo "Thread ${i} has finished"
  81. newFinished=1
  82. threadsPid[${i}]=0
  83. else
  84. stillRunningCount=$((${stillRunningCount} + 1))
  85. fi
  86. fi
  87. done
  88. if [ ${stillRunningCount} -ne 0 ]
  89. then
  90. if [ ${newFinished} -eq 1 ] && [ ${stillRunningCount} -ne 0 ]
  91. then
  92. echo "${stillRunningCount} remaining threads"
  93. fi
  94. sleep 1
  95. fi
  96. done
  97. echo "All threads done"
  98. echo "Running post launch"
  99. postLaunch
  100. }
  101. launch ${first} ${last} ${threads} "${parallelNbItems}"