urn:lsid:ibm.com:blogs:entries-84a1e68f-fa2b-4d83-a240-5f565e7d6248Mainframe Performance Topics with Martin Packer - Tags - heron I'm a well-known mainframe performance guy, with almost 30 years of experience helping customers manage systems. I also dabble in lots of other technology. I've sought to widen the Performance role, incorporating aspects of infrastructural architecture.
I'm a world-famous podcaster and screencaster (albeit VERY thinly spread). :-)03012018-01-31T09:44:38-05:00IBM Connections - Blogsurn:lsid:ibm.com:blogs:entry-9d43fdb6-e9b5-487e-b99d-b06cb35d9adfSquare Roots In DFSORTMartinPacker11000094DHactivefalseComment Entriesapplication/atom+xml;type=entryLikestrue2012-07-02T16:06:11-04:002012-07-02T16:06:11-04:00
<p>DFSORT's Arithmetic operators can do many things but the one thing they can't do is take the square root of a number.</p>
<br />
<p>You might think that's minor but it means you can't calculate a Standard Deviation.
(Variance is fine but dimensionally not so nice when used in conjunction with the Mean.)
And I needed Standard Deviation in a real live customer situation.</p>
<br />
<p>So I set out to "roll my own". And here's how I did it...</p>
<br />
<p>There is a well known technique for calculating square roots - <a href="http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method">the Babylonian Method</a> (or Heron's Method).
It's not terribly advanced maths, frankly: I think I was taught it at about age 14. It goes like this:</p>
<br />
<ol>
<li>Call your number whose square root is to be found a, and pick a first guess for the root. We'll call this quess x<sub>0</sub>.</li>
<li>Write x<sub>1</sub> = (a + a/x<sub>0</sub>)/2. In other words x<sub>n+1</sub> is the average of a and a/x<sub>n</sub>.
</li><li>Iterate until done. We'll talk about what "done" means later.
</li></ol>
<br />
<p>As you can see it's a really very simple method and converges reasonably quickly.
And it's not difficult to program, except for one thing: DFSORT doesn't have a looping capability.
(Yes it loops over the input records, but not in the way I mean.)</p>
<br />
<p>Before I show you some code let me talk about some considerations:</p>
<br />
<ul>
<li>Because DFSORT's arithmetic is <b>integer</b> I describe here taking the integer square root of an integer field value. So, for example, it yields sqrt(5)=2.</li>
<li>All the arithmetic on the way to finding the root is integer also.</li>
<li>The code takes square roots of <b>non-negative</b> integers: No imaginary roots here.</li>
<li>The method returns <b>positive</b> roots: As you probably know roots come in pairs, differing only by a sign.</li>
<li>The algorithm requires you to provide a first estimate (x<sub>0</sub>). My first take was a/2. But in the case of a=1 this leads to a root of 0 straightaway - which is particularly unhelpful.
So I had to special-case a=1.
So the take I went with in the end is x<sub>0</sub>=a. Not generally a brilliant first guess but avoids the special case.</li>
<li>Because this is integer arithmetic completion can't be only when sqrt(a)*sqrt(a)=a as in most cases this condition isn't going to be met.
Instead I use x<sub>n+1</sub>=x<sub>n</sub> as completion: If the two are equal the algorithm isn't going to produce a <b>different</b> x<sub>n+2</sub>.</li>
</ul>
<br />
<p>A reasonable question to ask is whether <b>integer</b> square roots are useful...</p>
<br />
<p>A standard technique with DFSORT's Arithmetic operators is to boost the integer values to represent the number of decimal places required.
So, for example, to add together 3.14 and 2.72 you'd be storing them as 314 and 272. Then you'd add these integers and use EDIT=(I.TT) to display them as "4.86".
So if you wanted to take the square root of 3.1416 you'd multiply by 10,000 and then apply my technique, using EDIT=(IIII.TT) to move the decimal point back.</p>
<br />
<h3>Structure Of The Example</h3>
<br />
This is a standard DFSORT invocation. Rather than give you the usual scaffolding I'll show you the main components:<p>
<br />
</p><ul>
<li>DFSORT Symbols where possible to help keep it clear.</li>
<li>COPY (though it could've been SORT or MERGE).</li>
<li>INREC to parse the free-form input data.</li>
<li>OUTREC with multiple IFTHEN clauses to compute the square root.</li>
<li>OUTFIL to format the output lines.</li>
</ul>
<br />
<p>You could, in fact, structure this program differently. It might (though I haven't worked through it) combine everything into a single INREC statement (apart from the COPY specification).</p>
<br />
<h3>Some Sample Data</h3>
<p>In my SORTIN I have the following lines:</p>
<br />
<pre>100
5
1
27
3
4
999
999999
99999999
</pre>
<p>In other words a single field in each record in EBCDIC (rather than binary) form. (You can generalise all that follows with a small amount of thought.)</p>
<br />
<p>(In my test case this is instream data - so it's fixed-length records and hence no RDW).</p>
<br />
<h3>Parsing EBCDIC Numbers.</h3>
<br />
<p>In this simple case I created a set of records with binary representations of the numbers using INREC:</p>
<br />
<pre>OPTION COPY
INREC FIELDS=(1,10,UFF,TO=BI)
</pre>
<p>Here the UFF does the parsing and it parses the 10 bytes starting in position 1, with the output being 4-byte BI fields.</p>
<br />
<h3>A SYMNAMES Deck To Simplify Things</h3>
<br />
<p>You may know you can define symbols with DFSORT. Here's the deck I used in this example:</p>
<br />
<pre>//SYMNAMES DD *
INPUT,1,4,BI
SQRT,*,4,BI
PREV,*,4,BI
/*
</pre>
<br />
<p>(You'll want a SYMNOUT DD to go with it.)</p>
<br />
<ul>
<li>INPUT maps the integer value created by INREC. It's a, the number whose square root we're seeking.</li>
<li>SQRT maps the field where successive estimates of the root are stored.</li>
<li>PREV maps the field where the previous estimate is stored - so we can tell if an iteration has changed the estimate of the root.</li>
</ul>
<br />
<h3>Taking The Square Root</h3>
<br />
<p>In my example all the work is done in a series of IFTHEN clauses in an OUTREC statement:</p>
<br />
<ol>
<li>A single WHEN=INIT clause initialises the estimate (SQRT) and clears the previous estimate (PREV).</li>
<li>Repeated WHEN=(SQRT,NE,PREV) clauses compute successive approximations - but only if the last two estimates weren't the same.</li>
</ol>
<br />
<p>Here's the code:</p>
<br />
<pre> OUTREC IFTHEN=(WHEN=INIT,
OVERLAY=(SQRT,INPUT,X'00000000')),
IFTHEN=(WHEN=(SQRT,NE,PREV),HIT=NEXT,
OVERLAY=(PREV:SQRT,SQRT<img src="https://www.ibm.com/developerworks/community/blogs/images/smileys/sad.gif" class="smiley" alt=":(" title=":(" />SQRT,ADD,(INPUT,DIV,SQRT)),DIV,+2,
TO=BI,LENGTH=4)),
IFTHEN=(WHEN=(SQRT,NE,PREV),HIT=NEXT,
OVERLAY=(PREV:SQRT,SQRT<img src="https://www.ibm.com/developerworks/community/blogs/images/smileys/sad.gif" class="smiley" alt=":(" title=":(" />SQRT,ADD,(INPUT,DIV,SQRT)),DIV,+2,
TO=BI,LENGTH=4)),
...
IFTHEN=(WHEN=(SQRT,NE,PREV),HIT=NEXT,
OVERLAY=(PREV:SQRT,SQRT<img src="https://www.ibm.com/developerworks/community/blogs/images/smileys/sad.gif" class="smiley" alt=":(" title=":(" />SQRT,ADD,(INPUT,DIV,SQRT)),DIV,+2,
TO=BI,LENGTH=4))
</pre>
<br />
<p>This you'll recognise as an unrolled loop. The "..." says you can repeat the previous IFTHEN clause as many times as you like.</p>
<br />
<p>A good question would be "how many times?" I found good convergence after 15. For example, the 99999999 value went to 10028 in 15 iterations and 10000 in 16. You could pick 20 and be sure of getting an accurate value.</p>
<br />
<p>Let's talk about control flow through the queue of IFTHEN clauses...
<br />
</p><ul>
<li>The WHEN=INIT is always executed.</li>
<li>HIT=NEXT on a WHEN=(logexp) clause means "even if this test succeeds proceed to the next WHEN=(logexp) clause having executed this one's OVERLAY".</li>
<li>Even if a WHEN=(logexp)'s logical expression is false all subsequent WHEN=(logexp) clauses are processed - even if, as in this case, they'll evaluate to false. So this isn't really a "WHILE(logexp) DO ... END" construct (but you can construct the logexp so it works that way).</li>
</ul>
<br/>
<p>In the past I've likened multiple IFTHEN clauses to a pipeline. This example shows why.</p>
<br/>
<h3>Printing The Results</h3>
<br/>
<p>The following is a quite complicated piece of report formatting.
Its aim is to format numbers and then squeeze out the resulting leading blanks.
At the same time it has to preserve blanks in text.</p>
<br/>
<pre>
OUTFIL IFTHEN=(WHEN=INIT,
BUILD=(C'x=',INPUT,EDIT=(IIIIIIIIIIT),
C'_sqrt(x)=',SQRT,EDIT=(IIIIIIIIIIIT),
C'_sqrt(x)*sqrt(x)=',SQRT,MUL,SQRT,EDIT=(IIIIIIIIIIIT),
C'_prev_est=',PREV,EDIT=(IIIIIIIIIIIT))),
IFTHEN=(WHEN=INIT,
BUILD=(1,90,SQZ=(SHIFT=LEFT,PAIR=QUOTE))),
IFTHEN=(WHEN=INIT,
FINDREP=(IN=C'_',OUT=C' '))
</pre>
<br/>
<p>Let's examine it more closely:</p>
<br/>
<ul>
<li><p>The first WHEN=INIT takes four numbers and formats them:</p>
<br/>
<ul>
<li>The number we wanted to find the square root of,</li>
<li>The square root,</li>
<li>What happens when you square that back up again, </li>
<li>The previous estimate of the square root.</li>
</ul>
<br/>
<p>These are formatted with leading zeroes turned into blanks and some text is wrapped around them (with underscores standing in for blanks).</p>
<li><p>The second WHEN=INIT squeezes all the blanks out. This is why I used underscores in the wrapping text.</p></li>
<li><p>The third WHEN=INIT turns all the underscores into blanks.</p></li>
</ul>
<br/>
<h3>Output Using The Sample Data</h3>
<br/>
<p>The output below isn't particularly pretty. I wanted to use the OUTFIL code I've shown you to demonstrate the ability to selectively squeeze blanks out.</p>
<br/>
<pre>
x=100 sqrt(x)=10 sqrt(x)*sqrt(x)=100 prev est=10
x=5 sqrt(x)=2 sqrt(x)*sqrt(x)=4 prev est=2
x=1 sqrt(x)=1 sqrt(x)*sqrt(x)=1 prev est=1
x=27 sqrt(x)=5 sqrt(x)*sqrt(x)=25 prev est=5
x=3 sqrt(x)=2 sqrt(x)*sqrt(x)=4 prev est=1
x=4 sqrt(x)=2 sqrt(x)*sqrt(x)=4 prev est=2
x=999 sqrt(x)=31 sqrt(x)*sqrt(x)=961 prev est=31
x=999999 sqrt(x)=999 sqrt(x)*sqrt(x)=998001 prev est=1000
x=99999999 sqrt(x)=10028 sqrt(x)*sqrt(x)=100560784 prev est=10784
</pre>
<br/>
<p>Although this has been quite a complex post I hope it's taught you a few new DFSORT tricks.
Actually, the "meat" of it is the Square Root calculation - which I'm using "in anger" (AKA "for real") right now.
And the technique should be readily adaptable.</p>
<br/>
<p>The question is: Are there other <a href="http://en.wikipedia.org/wiki/Newton%27s_method">Newton-Raphson</a>-style computations worth doing?</p>DFSORT's Arithmetic operators can do many things but the one thing they can't do is take the square root of a number. You might think that's minor but it means you can't calculate a Standard Deviation.
(Variance is fine but dimensionally not so nice when...008500