forked from wolfgangw/digital_cinema_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads-test.bash
executable file
·71 lines (64 loc) · 2.11 KB
/
threads-test.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#
# Spread out (hard-coded) shell jobs on a multi-core cpu. See e() for the beef
# Wolfgang Woehl v0.2010.11
#
# Based on Martin Christoph's http://www.da7a.de/linux/bash-parallelverarbeitung
#
# Usage: threads-test.bash <list of files>
# Example: threads-test.bash tiffs/*
# set THREADS to a number that makes sense (8 is a good starting point on a quad core)
THREADS=4
# list carries the provided shell arguments, usually a list of files
list=( "$@" )
# each shell argument (file) will be processed as 1 task
TASKS=${#list[*]}
# execution function
e()
{
# ${2} -> second argument of the e() call -> task number. you gotta love bash syntax ...
file=${list[${2}]}
echo "thread: ${1} (${SECONDS}s) -> execute task ${2} -> $(basename ${file})"
#
# define the processing you want to execute for each shell argument here.
# with these examples output ends up in the current directory.
#
# examples:
#
# file format conversion gains significantly
# for example 600 12bpc 1920x1080 tiffs with single job sequence ~220 secs, with threads-test.bash @ 8 threads ~120 secs
# convert ${file} -quality 100 $(basename ${file}).jpg
#
# image_to_j2k -cinema2K 24 -i ${file} -o $(basename ${file}).j2c > /dev/null 2>&1
#
# opendcp_j2k -i ${file} -o $(basename ${file}).j2c
#
# kdu_compress -i ${file} -o $(basename ${file}).j2c Sprofile=CINEMA2K Creslengths=1302083 Creslengths:C0=1302083,1041666 Creslengths:C1=1302083,1041666 Creslengths:C2=1302083,1041666 > /dev/null 2>&1
#
}
# thread function will be called (and backgrounded) THREADS times
# TASKS will be split up between the available "threads" (jobs, rather)
t()
{
thread_id=$1
echo "t:${thread_id} (${SECONDS}s) started"
a=0
while [[ ${a} -lt ${TASKS} ]]; do
b=$(( ${a} % ${THREADS} ))
if [[ ${b} = ${thread_id} ]]; then
e ${thread_id} ${a}
fi
index=$(( ${index} + 1 ))
a=$(( ${a} + 1 ))
done
echo "t:${thread_id} (${SECONDS}s) done"
}
# run threads
i=0
while [[ ${i} -lt ${THREADS} ]]; do
t ${i} &
i=$(( ${i} + 1 ))
done
# wait until all threads are done
wait
echo "${TASKS} tasks done (${SECONDS}s)"