From e17e0913c59d925e89e176de63188246b5c30e96 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 24 Sep 2013 21:51:05 +0900 Subject: [PATCH] fix darkcult: add -Pn parallel rendering mode. --- tools/darkroom/darkcult.py | 54 +++++++++++++++++++++++ tools/darkroom/darkcult.sh | 89 ++++++++++++++++++++++++++------------ 2 files changed, 115 insertions(+), 28 deletions(-) create mode 100755 tools/darkroom/darkcult.py diff --git a/tools/darkroom/darkcult.py b/tools/darkroom/darkcult.py new file mode 100755 index 00000000..5948856d --- /dev/null +++ b/tools/darkroom/darkcult.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import sys +import os +import subprocess +import multiprocessing + +def compile(command): + sys.stdout.write(".") + sys.stdout.flush() + return subprocess.call(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + +if __name__=="__main__": + argv = sys.argv + argc = len(argv) + + src = argv[1] + stagedir = argv[2] + output = argv[3] + compiler = argv[4] + width = int(argv[5]) + height = int(argv[6]) + tile_width = int(argv[7]) + tile_height = int(argv[8]) + compile_options = argv[9] + darkcult_cpp = argv[10] + max_procs = int(argv[11]) + + commands = [] + for y in range(0, height, tile_height): + for x in range(0, width, tile_width): + bin = "%s/%d/%d.out" % (stagedir, y, x) + out = "%s/%d/%d.ppm" % (stagedir, y, x) + command = "%s -o %s" \ + " %s" \ + " -DDARKROOM_SOURCE=\'\"%s\"\'" \ + " -DDARKROOM_TOTAL_WIDTH=%d -DDARKROOM_TOTAL_HEIGHT=%d" \ + " -DDARKROOM_TILE_WIDTH=%d -DDARKROOM_TILE_HEIGHT=%d" \ + " -DDARKROOM_OFFSET_X=%d -DDARKROOM_OFFSET_Y=%d" \ + " %s" \ + " && %s > %s" \ + % (compiler, bin, + compile_options, + src, + width, height, + tile_width, tile_height, + x, y, + darkcult_cpp, + bin, out + ) + commands.append(command); + + pool = multiprocessing.Pool(max_procs) + sys.exit(any(pool.map(compile, commands))) diff --git a/tools/darkroom/darkcult.sh b/tools/darkroom/darkcult.sh index 0dde1217..080adf07 100755 --- a/tools/darkroom/darkcult.sh +++ b/tools/darkroom/darkcult.sh @@ -21,10 +21,13 @@ declare -a user_macros user_macros=() declare -a include_paths include_paths=() +max_procs=1 force=0 use_help=0 +darkcult_cpp=$(cd $(dirname $0); pwd)/darkcult.cpp +darkcult_py=$(cd $(dirname $0); pwd)/darkcult.py -args=`getopt -o s:S:o:C:w:h:W:H:D:I:f -l source:,stagedir:,output:,compiler:,width:,height:,tile-width:,tile-height:,define:,include:,force,help -- "$@"` +args=`getopt -o s:S:o:C:w:h:W:H:D:I:P:f -l source:,stagedir:,output:,compiler:,width:,height:,tile-width:,tile-height:,define:,include:,max-procs:,force,help -- "$@"` if [ "$?" -ne 0 ]; then echo >&2 "error: options parse error. See 'darkcult.sh --help'" exit 1 @@ -42,6 +45,7 @@ while [ -n "$1" ]; do -H|--tile-height) tile_height=$2; shift 2;; -D|--define) user_macros=(${user_macros[@]} "$2"); shift 2;; -I|--include) include_paths=(${include_paths[@]} "$2"); shift 2;; + -P|--max_procs) max_procs=$2; shift 2;; -f|--force) force=1; shift;; --help) use_help=1; shift;; --) shift; break;; @@ -80,6 +84,9 @@ if [ ${use_help} -ne 0 ]; then echo "" echo " -I, --include= Add system include path." echo "" + echo " -P, --max-procs= The maximum number of process use." + echo " Default; 1" + echo "" echo " -f, --force Allow overwrite of ." echo "" echo " --help This message." @@ -101,6 +108,7 @@ fi if [ ${#include_paths[*]} -gt 0 ]; then echo " include-paths = (${include_paths[*]})" fi +echo " max-procs = ${max_procs}" echo " force = ${force}" if [ ! -f "${src}" -a ! -f "$(cd $(dirname $0); pwd)/${src}" ]; then @@ -127,48 +135,73 @@ for include_path in ${include_paths}; do include_options="${include_options} -I${include_path}" done +for ((y=0; y&2 "error: compile(${binname}) failed." - exit 1 - fi - ${bin} > ${stagedir}/${y}/${x}.ppm + echo -n " x = " + for ((x=0; x&2 "error: compile(${binname}) failed." + exit 1 + fi + ${bin} > ${stagedir}/${y}/${x}.ppm + done + echo "" + + let "y_elapsed=${SECONDS}-${y_start}" + echo " elapsed = ${y_elapsed}s" done +else + echo " processing in parallel mode." + echo -n " " + python "${darkcult_py}" \ + "${src}" "${stagedir}" "${output}" "${compiler}" \ + "${width}" "${height}" \ + "${tile_width}" "${tile_height}" \ + "${compile_options}" "${darkcult_cpp}" \ + "${max_procs}" + if [ $? -ne 0 ]; then + echo "" + echo >&2 "error: compile failed." + exit 1 + fi echo "" +fi +let "elapsed=${SECONDS}-${start}" +echo " elapsed(total) = ${elapsed}s" + +for ((y=0; y /dev/null # convert +append $(ls *.ppm | sort -n) ../${y}.ppm pnmcat -lr $(ls *.ppm | sort -n) > ../${y}.ppm popd > /dev/null - - let "y_elapsed=${SECONDS}-${y_start}" - echo " elapsed = ${y_elapsed}s" done pushd ${stagedir} > /dev/null #convert -append $(ls *.ppm | sort -n) ${output} pnmcat -tb $(ls *.ppm | sort -n) > ${output} popd > /dev/null -let "elapsed=${SECONDS}-${start}" -echo " elapsed(total) = ${elapsed}s" echo "finished."