7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER START
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# The contents of this file are subject to the terms of the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Common Development and Distribution License (the "License").
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You may not use this file except in compliance with the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# See the License for the specific language governing permissions
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# and limitations under the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# When distributing Covered Code, include this CDDL HEADER in each
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# If applicable, add the following below this CDDL HEADER, with the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# fields enclosed by brackets "[]" replaced with your own identifying
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# information: Portions Copyright [yyyy] [name of copyright owner]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER END
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# mandelbrotset1 - a simple mandelbrot set generation and
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# parallel execution demo
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Make sure all math stuff runs in the "C" locale to avoid problems
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# with alternative # radix point representations (e.g. ',' instead of
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# '.' in de_DE.*-locales). This needs to be set _before_ any
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# floating-point constants are defined in this script).
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Get terminal size and put values into a compound variable with the integer
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# members "columns" and "lines"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( mag=0 ; mag < max_mag && iteration < max_iteration ; iteration++ )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# build mandelbrot image serially
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( y=y_min ; y < y_max ; y+=stepwidth )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( x=x_min ; x < x_max ; x+=stepwidth )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin mandelbrot value ${x} ${y} ${x} ${y} 1 ${symbollistlen}
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# build mandelbrot image using parallel worker jobs
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # the following calculation suffers from rounding errors
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin integer lines_per_job=$(( ((m_height+(numcpus-1)) / numcpus) ))
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz tmpjobdir="$(mktemp --default=/tmp --directory "mandelbrotset1${PPID}_$$_XXXXXX")" || fatal_error $"Could not create temporary directory."
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # try to generate a job identifer prefix which is unique across multiple hosts
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin jobident="job_host_$(uname -n)pid_$$_ppid${PPID}"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz rm -f "${tmpjobdir}/${jobident}_child_$y.joboutput"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz # save file name since we're going to modify "y"
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz typeset filename="${tmpjobdir}/${jobident}_child_$y.joboutput"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( ; y < y_max && lines_per_job-- > 0 ; y+=stepwidth )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( x=x_min ; x < x_max ; x+=stepwidth )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin mandelbrot value ${x} ${y} ${x} ${y} 1 ${symbollistlen}
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin printmsg $"## waiting for ${numjobs} children..."
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (( y=y_min ; y < y_max ; y+=(stepwidth*lines_per_job) )) ; do
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz print -r -- "$( < "${tmpjobdir}/${jobident}_child_$y.joboutput")"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin getopts -a "${progname}" "${mandelbrotset1_usage}" OPT '-?'
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinbuiltin uname # loop_parallel needs the ksh93 builtin version to generate unique job file names
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# terminal size rect
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinget_term_size termsize || fatal_error $"Could not get terminal size."
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chintypeset symbollist=' .:0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%#'
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner# calculate number of worker CPUs and use 3 as fallback
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner(( numcpus=$(getconf NPROCESSORS_ONLN || print "3") ))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin(( m_width=termsize.columns-1 , m_height=termsize.lines-2 ))
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner[-?\n@(#)\$Id: mandelbrotset1 (Roland Mainz) 2010-03-31 \$\n]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[-author?Roland Mainz <roland.mainz@nrubsig.org>]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+NAME?mandelbrotset1 - generate mandelbrot set fractals with ksh93]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+DESCRIPTION?\bmandelbrotset1\b mandelbrot set fractal generator
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin which runs either in serial or parallel mode (using multiple worker jobs).]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[w:width?Width of fractal.]:[width]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[h:height?Height of fractal.]:[height]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[s:symbols?Symbols to build the fractal from.]:[symbolstring]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[m:mag?Magnification level.]:[magnificationlevel]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[p:stepwidth?Width per step.]:[widthperstep]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[S:serial?Run in serial mode.]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[P:parallel?Run in parallel mode.]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[M:mode?Execution mode.]:[mode]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[C:numcpus?Number of processors used for parallel execution.]:[numcpus]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+SEE ALSO?\bjuliaset1\b(1), \bksh93\b(1)]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinwhile getopts -a "${progname}" "${mandelbrotset1_usage}" OPT ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"