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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#!/usr/bin/ksh93
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# mandelbrotset1 - a simple mandelbrot set generation and
# parallel execution demo
#
# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative # radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
if [[ "${LC_ALL}" != "" ]] ; then
export \
LC_MONETARY="${LC_ALL}" \
LC_MESSAGES="${LC_ALL}" \
LC_COLLATE="${LC_ALL}" \
LC_CTYPE="${LC_ALL}"
unset LC_ALL
fi
export LC_NUMERIC=C
function printmsg
{
print -u2 "$*"
}
function fatal_error
{
print -u2 "${progname}: $*"
exit 1
}
# Get terminal size and put values into a compound variable with the integer
# members "columns" and "lines"
function get_term_size
{
nameref rect=$1
rect.columns=${ tput cols ; } || return 1
rect.lines=${ tput lines ; } || return 1
return 0
}
function mandelbrot
{
nameref result=$1
float x=$2
float y=$3
float xx
float yy
float x1=$4
float y1=$5
integer iteration=$6
integer max_iteration=$7
float mag
for (( mag=0 ; mag < max_mag && iteration < max_iteration ; iteration++ )) ; do
((
xx=x*x ,
yy=y*y ,
mag=xx+yy ,
y=x*y*2+y1 ,
x=xx-yy+x1
))
done
(( result=iteration ))
return 0
}
# build mandelbrot image serially
function loop_serial
{
integer value
typeset line=""
for (( y=y_min ; y < y_max ; y+=stepwidth )) ; do
for (( x=x_min ; x < x_max ; x+=stepwidth )) ; do
mandelbrot value ${x} ${y} ${x} ${y} 1 ${symbollistlen}
line+="${symbollist:value:1}"
done
line+=$'\n'
done
print -r -- "${line}"
return 0
}
# build mandelbrot image using parallel worker jobs
function loop_parallel
{
integer numjobs=0
# the following calculation suffers from rounding errors
integer lines_per_job=$(( ((m_height+(numcpus-1)) / numcpus) ))
typeset tmpjobdir
printmsg $"# lines_per_job=${lines_per_job}"
printmsg $"# numcpus=${numcpus}"
# "renice" worker jobs
set -o bgnice
tmpjobdir="$(mktemp --default=/tmp --directory "mandelbrotset1${PPID}_$$_XXXXXX")" || fatal_error $"Could not create temporary directory."
trap "rm -r ${tmpjobdir}" EXIT # cleanup
# try to generate a job identifer prefix which is unique across multiple hosts
jobident="job_host_$(uname -n)pid_$$_ppid${PPID}"
printmsg $"## prepare..."
for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
rm -f "${tmpjobdir}/${jobident}_child_$y.joboutput"
(( numjobs++ ))
done
printmsg $"## running ${numjobs} children..."
for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
(
integer value
typeset line=""
# save file name since we're going to modify "y"
typeset filename="${tmpjobdir}/${jobident}_child_$y.joboutput"
for (( ; y < y_max && lines_per_job-- > 0 ; y+=stepwidth )) ; do
for (( x=x_min ; x < x_max ; x+=stepwidth )) ; do
mandelbrot value ${x} ${y} ${x} ${y} 1 ${symbollistlen}
line+="${symbollist:value:1}"
done
line+=$'\n'
done
print -r -- "${line}" >"${filename}"
exit 0
) &
done
printmsg $"## waiting for ${numjobs} children..."
wait
printmsg $"## output:"
for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
print -r -- "$( < "${tmpjobdir}/${jobident}_child_$y.joboutput")"
# EXIT trap will cleanup temporary files
done
return 0
}
function usage
{
OPTIND=0
getopts -a "${progname}" "${mandelbrotset1_usage}" OPT '-?'
exit 2
}
# main
builtin basename
builtin cat
builtin rm
builtin uname # loop_parallel needs the ksh93 builtin version to generate unique job file names
builtin mktemp
set -o noglob
set -o nounset
typeset progname="${ basename "${0}" ; }"
float x_max
float x_min
float y_max
float y_min
float m_width
float m_height
float max_mag
float stepwidth
integer numcpus
# terminal size rect
compound termsize=(
integer columns=-1
integer lines=-1
)
get_term_size termsize || fatal_error $"Could not get terminal size."
typeset symbollist=' .:0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%#'
typeset symbollistlen=$(( ${#symbollist} - 1))
typeset mode="parallel"
(( max_mag=400 ))
(( stepwidth=0.1 ))
# calculate number of worker CPUs and use 3 as fallback
(( numcpus=$(getconf NPROCESSORS_ONLN || print "3") ))
(( numcpus=numcpus*4 ))
(( m_width=termsize.columns-1 , m_height=termsize.lines-2 ))
typeset -r mandelbrotset1_usage=$'+
[-?\n@(#)\$Id: mandelbrotset1 (Roland Mainz) 2010-03-31 \$\n]
[-author?Roland Mainz <roland.mainz@nrubsig.org>]
[+NAME?mandelbrotset1 - generate mandelbrot set fractals with ksh93]
[+DESCRIPTION?\bmandelbrotset1\b mandelbrot set fractal generator
which runs either in serial or parallel mode (using multiple worker jobs).]
[w:width?Width of fractal.]:[width]
[h:height?Height of fractal.]:[height]
[s:symbols?Symbols to build the fractal from.]:[symbolstring]
[m:mag?Magnification level.]:[magnificationlevel]
[p:stepwidth?Width per step.]:[widthperstep]
[S:serial?Run in serial mode.]
[P:parallel?Run in parallel mode.]
[M:mode?Execution mode.]:[mode]
[C:numcpus?Number of processors used for parallel execution.]:[numcpus]
[+SEE ALSO?\bjuliaset1\b(1), \bksh93\b(1)]
'
while getopts -a "${progname}" "${mandelbrotset1_usage}" OPT ; do
# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
case ${OPT} in
w) m_width="${OPTARG}" ;;
h) m_height="${OPTARG}" ;;
s) symbollist="${OPTARG}" ;;
m) max_mag="${OPTARG}" ;;
p) stepwidth="${OPTARG}" ;;
S) mode="serial" ;;
+S) mode="parallel" ;;
P) mode="parallel" ;;
+P) mode="serial" ;;
M) mode="${OPTARG}" ;;
C) numcpus="${OPTARG}" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
printmsg "# width=${m_width}"
printmsg "# height=${m_height}"
printmsg "# max_mag=${max_mag}"
printmsg "# stepwidth=${stepwidth}"
printmsg "# symbollist='${symbollist}'"
printmsg "# mode=${mode}"
(( symbollistlen=${#symbollist}-1 ))
((
x_max=m_width*stepwidth/2. ,
x_min=-x_max ,
y_max=m_height*stepwidth/2. ,
y_min=-y_max
))
case "${mode}" in
parallel) loop_parallel ; exit $? ;;
serial) loop_serial ; exit $? ;;
*) fatal_error $"Unknown mode \"${mode}\"." ;;
esac
fatal_error "not reached."
# EOF.
|