alphalinux.org high-perf list (1998-2001)
 help / color / mirror / Atom feed
* Re: sqrt performance
@ 1999-03-03 20:53 esci
  1999-03-03 21:40 ` Andrei A. Dergatchev
  1999-03-04  5:15 ` Michal Jaegermann
  0 siblings, 2 replies; 8+ messages in thread
From: esci @ 1999-03-03 20:53 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1929 bytes --]

[snip]

>Looking at the structure of your code I think it *should* be possible

>to get some acceleration exploiting regulatiry of sqrt argument:

>some_value*constant_for_this_cycle+delta^2*(i++)^2

>for i=1,N

>

>I'm thinking about division of one argument over other.

>Can you expect any relation of general nature like

>some_value*constant_for_this_cycle<<delta^2*(i++)^2

>or vice versa or anything in this style ?

All the values are monotonic, so you can speed things up a good bit by using

the previous result as the starting point in a Newtonian iteration. It gives

a factor of 3 speed up w/ g77 or NDP, but is slightly slower than the DU

library routine.  This was with 2 steps hard coded w/o conditional logic.

I only need 13 bits of accuracy as the result is an index into an array.

One of the things that is slowly sinking in is that my loop is too short,

typically under 100 iterations.  From what I'm seeing, I should be able to

get faster results by chaining a number of loops together.

BTW can anyone provide good references on sqrt() methods?  I looked in

Knuth and quite a few other math references but found little other than very

basic stuff (i.e. Newton's method)  Knuth has one algorithm posed as an

exercise (M40 == Mathematical, term project difficulty :-(

I'm currently looking into using a table driven Taylor's series expansion.

Hint's and suggestions *very* welcome.  I need to do this over the range

from 0 to 100,000,000  w/ the roots off by less than 1 (I actually need

integer roots for the indexing).  There ought to be a way to get this going 

a lot faster.

Reginald Beardsley

------------------------------------------------------------------------

              Exploration Software Consultants Inc.

Voice: 972-379-0557                      email: rhb@acm.org

Fax:   972-379-0558

------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance
  1999-03-03 20:53 sqrt performance esci
@ 1999-03-03 21:40 ` Andrei A. Dergatchev
  1999-03-04  5:15 ` Michal Jaegermann
  1 sibling, 0 replies; 8+ messages in thread
From: Andrei A. Dergatchev @ 1999-03-03 21:40 UTC (permalink / raw)


Hi,

[snip]

> All the values are monotonic, so you can speed things up a good bit by using

> the previous result as the starting point in a Newtonian iteration. It gives

> a factor of 3 speed up w/ g77 or NDP, but is slightly slower than the DU

> library routine.  This was with 2 steps hard coded w/o conditional logic.

> I only need 13 bits of accuracy as the result is an index into an array.

Well, 3x speed up is already quite a lot, do you want to calculate sqrt at

one clock :-) ? Ok, just kidding. I believe requiring only 13 bits (out of 64

of input ?) we have plenty of room to optimise. Can you provide some

code using this optimisation to start with ?

> One of the things that is slowly sinking in is that my loop is too short,

> typically under 100 iterations.  From what I'm seeing, I should be able to

> get faster results by chaining a number of loops together.

Yeah, for longer loops it might be possible to keep larger tables in cach.

> BTW can anyone provide good references on sqrt() methods?  I looked in

> Knuth and quite a few other math references but found little other than very

> basic stuff (i.e. Newton's method)  Knuth has one algorithm posed as an

> exercise (M40 == Mathematical, term project difficulty :-(

During somewhere in between Sep 1 1997 and Dec 1 1998 Dann Corbit

discussed optimum sqrt routines several times in comp.lang.c and

sci.math.num-analysis

This entiry thread was *very* useful for my background in applied math :-)

http://x11.dejanews.com/[ST_rn=ps]/dnquery.xp?&search=thread&svcclass=dnserver&ST=PS&CONTEXT=920496754.1956577512&HIT_CONTEXT=920496754.1956577512&HIT_NUM=0&recnum=%3c1998051405135800.BAA02391@ladder03.news.aol.com%3e%231/1

> I'm currently looking into using a table driven Taylor's series expansion.

You may want to search dejanews for ~a(Dann Corbit), ~g(comp.lang.c)

and "sqrt" :-) I'll steal just a few from what I was given.

http://x11.dejanews.com/[ST_rn=ps]/getdoc.xp?AN=324191203&CONTEXT=920497028.1963655168&hitnum=43

ftp://ftp.cs.umd.edu/pub/papers/papers/2990/2990.ps.Z

http://www.astro.virginia.edu/~eww6n/math/SquareRoot.html

> Hint's and suggestions *very* welcome.  I need to do this over the range

> from 0 to 100,000,000  w/ the roots off by less than 1 (I actually need

> integer roots for the indexing).  There ought to be a way to get this going

> a lot faster.

Do you need only roots of integers ?? Once more:

http://www.dejanews.com/[ST_rn=ps]/dnquery.xp?ST=PS&QRY=sqrt&defaultOp=AND&DBS=1&format=terse&showsort=date&maxhits=100&LNG=ALL&subjects=&groups=&authors=user923005%40aol.com&fromdate=&todate=

"I have different algorithms for each.  For integer, go look at comp.lang.c for

a posting by Dann Corbit."

>

>

> Reginald Beardsley

>

> ------------------------------------------------------------------------

>               Exploration Software Consultants Inc.

>

> Voice: 972-379-0557                      email: rhb@acm.org

> Fax:   972-379-0558

> ------------------------------------------------------------------------

Good luck,

Andrei

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance
  1999-03-03 20:53 sqrt performance esci
  1999-03-03 21:40 ` Andrei A. Dergatchev
@ 1999-03-04  5:15 ` Michal Jaegermann
  1999-03-04 18:30   ` Joachim Wesner
                     ` (2 more replies)
  1 sibling, 3 replies; 8+ messages in thread
From: Michal Jaegermann @ 1999-03-04  5:15 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 446 bytes --]

esci wrote:

> 

> I'm currently looking into using a table driven Taylor's series expansion.

> Hint's and suggestions *very* welcome.  I need to do this over the range

> from 0 to 100,000,000  w/ the roots off by less than 1

If you reduce that to an interval [0,1) this would mean that you

need 10 decimal places.  A decent polynomial approximation plus one

step of a Newton iteration should bring you in that range, I think.

   Michal

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance
  1999-03-04  5:15 ` Michal Jaegermann
@ 1999-03-04 18:30   ` Joachim Wesner
  1999-03-04 18:48   ` sqrt performance, further comment Joachim Wesner
  1999-03-05 17:55   ` Joachim Wesner
  2 siblings, 0 replies; 8+ messages in thread
From: Joachim Wesner @ 1999-03-04 18:30 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3296 bytes --]

Michal Jaegermann wrote:

> 

> esci wrote:

> >

> > I'm currently looking into using a table driven Taylor's series expansion.

> > Hint's and suggestions *very* welcome.  I need to do this over the range

> > from 0 to 100,000,000  w/ the roots off by less than 1

> 

> If you reduce that to an interval [0,1) this would mean that you

> need 10 decimal places.  A decent polynomial approximation plus one

> step of a Newton iteration should bring you in that range, I think.

Not necessarily, at least not for high (aka. full 16 digits). There is a

reason that there is no polynomial based approximation for sqrt in

libffm.

First, you want uniform *relative* precision for the function over the

full range, so 0..1 won't work. However, you can take acount of the

floating point format and only operate on the mantissa and 0.25..1 or

0.5..1 using frexp and ldexp (which however, are notoriously ineffective

when using the C lib routine, clearly, you NEED assembler here).

Unfortunatelly, sqrt is notoriusly ugly in approximating even for small

intervalls, you need much higher orders (and large coefficients for the

high orders, i.e. bad with respect to numerical stability) compared to

say sin(), which is much more well behaved.

However, assuming you go for 0.5..1 where you probably need to adjust

the result by sqrt(0.5) (further slowdown, you need to test it and

decide) and you want 1e-10 final *relative* precision, you need better

1e-5 rel. precision before the final Newton step.

If anybody wants to give it a try, here are the coefficients for a

(nearly) minimax relative prec. polynomial:

For better 1e-5 rel. precision from 0.5..1 you already need a 5th order

polynomial with the coefficients

A 0: 

2.07200449623728886e-001                                                  

A 1: 

1.45700060266363978e+000                                                  

A 2:

-1.34960056099104825e+000                                                  

A 3: 

1.11173630566745442e+000                                                  

A 4:

-5.38779078465377500e-001                                                  

A 5: 

1.12443059720350031e-001                                                  

(i.e. A0 + x*A1 + x*x*A2 ...)

which gives you better than ~ 1.4e-6 rel. precision.

If you go for only forth order, you get

A 0: 

2.30280650419599074e-001                                                  

A 1: 

1.29379894493964541e+000                                                  

A 2:

-8.94996784387289337e-001                                                  

A 3: 

4.88028708781137845e-001                                                  

A 4:

-1.17117604514064883e-001                                                  

  

however, max. rel. error here is  just above the range, i.e. 1.1e-5

Compared to that, for 1e-8 or better 1e-9 initial rel. precision for a

full prec. version, you need >= 9th order, slow to evaluate on a machine

with pipled floating point unit. You can "vectorize" it by some tricks,

that are actually usued in libffm, but you have to further sacrifice

some precision for that.

I would be interested in the result you get, however, I fear, you won't

be able to beat the new libcpml speed !?

Best wishes,

Joachim

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance, further comment.
  1999-03-04  5:15 ` Michal Jaegermann
  1999-03-04 18:30   ` Joachim Wesner
@ 1999-03-04 18:48   ` Joachim Wesner
  1999-03-04 19:41     ` Michal Jaegermann
  1999-03-05 17:55   ` Joachim Wesner
  2 siblings, 1 reply; 8+ messages in thread
From: Joachim Wesner @ 1999-03-04 18:48 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]

Michal Jaegermann wrote:

> 

> esci wrote:

> >

> > I'm currently looking into using a table driven Taylor's series expansion.

> > Hint's and suggestions *very* welcome.  I need to do this over the range

> > from 0 to 100,000,000  w/ the roots off by less than 1

> 

> If you reduce that to an interval [0,1) this would mean that you

> need 10 decimal places.  A decent polynomial approximation plus one

> step of a Newton iteration should bring you in that range, I think.

>

Further addition to my last post:

*Only* for the problem the original poster mentioned, you might consider

using max. *absolute* error here, this would be a very special routine.

However, when fitting the *full* range, the problem is the infinite

derivative at zero, that fools all polynomial approx. schemes. Maybe my

approximation software fails here a bit, but, even at 10 th order, I

can't get better 2e-3 *absolute* precision. sqrt is known to be bad in

function approx. theory. Müller in his recent book cites that you need

at 25th degree polynomials (!!) only to get to 1e-15 absolute error in

0.25..1

Joachim

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance, further comment.
  1999-03-04 18:48   ` sqrt performance, further comment Joachim Wesner
@ 1999-03-04 19:41     ` Michal Jaegermann
  1999-03-04 19:51       ` Joachim Wesner
  0 siblings, 1 reply; 8+ messages in thread
From: Michal Jaegermann @ 1999-03-04 19:41 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1185 bytes --]

Joachim Wesner wrote:

> 

> Michal Jaegermann wrote:

> > 

> > esci wrote:

> > >

> > > I'm currently looking into using a table driven Taylor's series expansion.

> > > Hint's and suggestions *very* welcome.  I need to do this over the range

> > > from 0 to 100,000,000  w/ the roots off by less than 1

> > 

> > If you reduce that to an interval [0,1) this would mean that you

> > need 10 decimal places.  A decent polynomial approximation plus one

> > step of a Newton iteration should bring you in that range, I think.

> >

> 

> However, when fitting the *full* range, the problem is the infinite

> derivative at zero, that fools all polynomial approx. schemes.

I would have to revisit my old files; this was quite a while ago.

Maybe this was a rational approximation (it should not not be too costly

on Alpha) but I had in the past pretty decent approximation for a sqrt on

[0,2) interval.  I do not remember max absolute errors, though.

Details are fuzzy now.  You may be right that with one Newton iteration

one will be still not precise enough in the whole required range.

[0,2) was used as this allowed not to worry about even and odd

exponents.

  Michal

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance, further comment.
  1999-03-04 19:41     ` Michal Jaegermann
@ 1999-03-04 19:51       ` Joachim Wesner
  0 siblings, 0 replies; 8+ messages in thread
From: Joachim Wesner @ 1999-03-04 19:51 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1530 bytes --]

Michal Jaegermann wrote:

>

> 

> I would have to revisit my old files; this was quite a while ago.

> Maybe this was a rational approximation (it should not not be too costly

> on Alpha) but I had in the past pretty decent approximation for a sqrt on

> [0,2) interval.  I do not remember max absolute errors, though.

> Details are fuzzy now.  You may be right that with one Newton iteration

> one will be still not precise enough in the whole required range.

> 

> [0,2) was used as this allowed not to worry about even and odd

> exponents.

> 

>   Michal

Yes, you are right, rational is known to be MUCH better, but you need

pretty SLOW div !!

I checked several combinations (with div) considering full range, full

*REL* precision and decided it would very likely be slower than the

Mosberger sqrt.

However, what I suggest for 1e-10 precision (including 1 Newton step)

might probably work (mean, be a bit faster, but compared to libcpml ???)

And yes we probably could replace the rather small table in the

libffm sqrt by another (nonrational) polynomial, but for .25 to 1 we

need at least 8th to better 9th order for 1e-5 (here my software is

pretty dependable) to be on the sure side after 3 Newton steps, and it

will be DEFINITELY slower than the actual version. You see, sqrt is

"§$%&/@ as long as you don't go for rational approx.

YES, in case cache misses occur, this would be different, but I'm still

waiting for the results what the real-world impact on that seems to be. 

Greetings

 

Joachim

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sqrt performance, further comment.
  1999-03-04  5:15 ` Michal Jaegermann
  1999-03-04 18:30   ` Joachim Wesner
  1999-03-04 18:48   ` sqrt performance, further comment Joachim Wesner
@ 1999-03-05 17:55   ` Joachim Wesner
  2 siblings, 0 replies; 8+ messages in thread
From: Joachim Wesner @ 1999-03-05 17:55 UTC (permalink / raw)


Martin Kahlert wrote:

> 

> Quoting Joachim Wesner (Joachim.Wesner@frankfurt.netsurf.de):

> > However, when fitting the *full* range, the problem is the infinite

> > derivative at zero, that fools all polynomial approx. schemes. Maybe my

> > approximation software fails here a bit, but, even at 10 th order, I

> > can't get better 2e-3 *absolute* precision.

> 

> Only a small suggestion (no big background in function approximation here):

> What about approximating x*sqrt(x)? This should be much simpler.

Which would somehow force you to later divide by x or am I mssing

something here ? Actually, this way you are doing a very simple

*rational* approximation of the form P(x)/x for sqrt(x). As also

discussed on the list, this IS generally better, but I estimate it's not

faster in the end as using the Newtonian iterations that double the

number of valid bits in each step.

Cheers

Joachim

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1999-03-05 17:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-03-03 20:53 sqrt performance esci
1999-03-03 21:40 ` Andrei A. Dergatchev
1999-03-04  5:15 ` Michal Jaegermann
1999-03-04 18:30   ` Joachim Wesner
1999-03-04 18:48   ` sqrt performance, further comment Joachim Wesner
1999-03-04 19:41     ` Michal Jaegermann
1999-03-04 19:51       ` Joachim Wesner
1999-03-05 17:55   ` Joachim Wesner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox