Superfast maths library

Discussion in 'Additional Libraries' started by agraham, Dec 5, 2007.

  1. agraham

    agraham Expert Licensed User

    Probably only a few or less people will be interested in this arcane library which just sort of grew like Topsy out of a small library that I built to do some intensive maths. First I compiled the maths into the library, then I thought that making it programmable would be more convenient and eventually it turned into this.

    "This" is a maths processor that does one single thing well - maths. It can tear through maths code about 50 to 80 times faster than B4PPC. The downside is that to utilise it you will need to write code for it that is somewhat dissimilar to Basic. The upside is of course the sheer performance. It works on both device (where it is most valuable) and desktop. Visual Studio thinks this is compiled for .NET 1.0 but I am not totally convinced so if you try it and don't have .NET 2.0 it may or may not work.

    I have named this library "Cray1" after the most iconic supercomputer ever designed. Long ago (1978!) I designed some custom commmunication hardware so that a bank of about 20 PDP11 minicomputers could collect data from around an accelerator, feed it to two Interdata 32 superminis which fed an IBM360 mainframe that organised storage and fed a Cray1 supercomputer. Awesome system for its' day!

    EDIT:- I have replaced the original archive because of uncertainty as to exactly the which version of the dll and demo it contained. My error I'm afraid!

    EDIT:- This library is now effectively obsolete as by taking advantage of how the optimising compiler treats arrays when doing maths you can get a better performance than this library. See post #16.

    Attached Files:

    Last edited: Jul 12, 2009
  2. dzt

    dzt Active Member Licensed User

    You produce more than we are able to consume.
  3. agraham

    agraham Expert Licensed User

    :sign0013: but I'm stuck inside with a bad back and a worse elbow and the weather is lousy so playing with computers takes my mind off things.
  4. sberry1

    sberry1 Member Licensed User

    I have a question about this math library. I am doing some work with aircraft when you say super fast. What do you mean. How long does it take to say for instance do a complex or even easy math equation. I am thinking about using this for aircraft corrections. So it needs to be right on the money and also very very quick cause this will be the math library that guides the aircraft.
  5. agraham

    agraham Expert Licensed User

    You will just have to try it for yourself. As a guide it is about seven times faster on the desktop than the same code produced by the optimised version 6 compiler. You will only get this performance on a significantly intensive computation that includes looping and branching - just a few operations in a "straight line" will probably not produce enough benefit to be worthwhile.

    This library is about as fast as you can get with C# (or VB) .NET floating point code. Bear in mind that devices are not very good at maths as they do not have a Floating Point Unit so have to emulate it in software. Because of this the performance gain on a device will be less than on the desktop because the actual maths takes more of the time.

    There are ways of getting a lot higher floating point type perfomance without an FPU using scaled fractional integer working (like we did back in the early days) but you would need to do that directly in C# or VB with your own library coding your own algorithms.
  6. agraham

    agraham Expert Licensed User

    You can also get a performance increase in the optimised compiler by declaring array variables as the type you want - Doubles, Int32s etc. The compiler then compiles the native arithmetic operations for these type whereas it treats regular variables as variants.

    Dim a(3) as Int32
    a(0) = a(1) * a(2)

    is faster then

    a0 = a1 * a2

    Leads to less expressive variable names though.
  7. derez

    derez Expert Licensed User

    Image rotation

    Why don't you join Dimitris and use this fast math to do the image rotation (to an arbitrary angle, not "just" main directions) - doing the sin and cos which are the essence of the rotation with fast math lib ?

    Call it derez wishlist :)
  8. agraham

    agraham Expert Licensed User

    As dzt is producing a library he will be able to optimally use the .NET maths functions anyway. My library just avoids some of the looping and type conversions that B4PPC has to do to keep itself weakly typed. dzt can avoid them in his library.
  9. dennishea

    dennishea Active Member Licensed User

    @ Derez

    Dzt has already changed it so you can change it to any angle, a degree at a time if you want. If your just rotating a map it is I believe fast enougth. I think all he's doing is taking a snapshot of screen and rotating that. The one thing I noticed is if I rotate it to far it shrinks. Maybe I am doing something wrong or maybe it is not capable of doing what I want. I am new at dll's and never have been great with this sort of thing. It is great dll and it is a great addition to basic4ppc's library.

    @dzt Do you think is possible to rotate 76800 pixels and keep all the pixel colors position to each other relative or is it absolute, I'm sure which it is.


    actually I meant is 250,000. I forgot that my grid is 500 x 500. I have it so I can scoll left right up down keep that relationship, I just haven't figured out rotation.
    Last edited: Dec 28, 2007
  10. agraham

    agraham Expert Licensed User

    Rotation of a pixel image is a lossy process. Multiple rotations can grossly distort the image (try it in Paint Shop Pro or a similar app). Ideally you should keep a master image and rotate that in a single operation for the degree of rotation that you require. If you require a further rotation then it should be the master image that you rotate - not the already rotated version that should be discarded.
  11. dennishea

    dennishea Active Member Licensed User

    Thanks for the reply agraham. I was afraid of that. I've fiddle around and found some of the do's and don'ts, one of them being keep the master. Again thanks for the information.
  12. Saj

    Saj Active Member Licensed User

    I was thinking of using the cray library for crunching through the following equations as they take a lot of time (around 90%) to execute compared drawing stuff with your ImageLibEx library:

    ratioX = (trackArr(i).lon - mapLonXmin) /(mapLonXmax - mapLonXmin)
    X_pix = Int(ratioX*MapWidth*zoom) +jpgXposn_onScreen0_0
    ratioY = (mapLatYmax - trackArr(i).lat) /(mapLatYmax - mapLatYmin)
    Y_pix = Int(ratioY*MapHeight*zoom)+ jpgYposn_onScreen0_0

    Example 'Cray1Test.sbp' throws up the following error on V6.80. Can u take a look?

  13. agraham

    agraham Expert Licensed User

    Looking at the error message an extra double quote seem to have crept into that line of code after Cray1.TokenCount". Remove it and it should work OK.
  14. Saj

    Saj Active Member Licensed User

    Thanks, thats fixed those errors. I'm getting the following error now:

    Line 65: Variable main.time & main.btime are used before assigned any value.

    I've looked through the code and can't see where they get set.
  15. agraham

    agraham Expert Licensed User

    :sign0013: It looks like the version of the dll and the demo in the archive is not the same as the one I have here for some reason :confused: It looks like I made a mistake when I built the archive :( I have relpaced the archive, please discard what you have and download it again.
  16. agraham

    agraham Expert Licensed User

    You might want to try this approach before delving into reverse polish :sign0137:

    I initially wrote this library in the era before the optimising compiler was available and it was astonishingly faster than the legacy compiler. In revisiting this subject I've had look again at what the optimising compiler does and have realised that this library is effectively obsolete as you can probably get better performance, when optimised compiled, by taking advantage of the fact that Basic4ppc does maths on arrays using whatever the type of the array is. I am not sure why Erel introduced this feature but you can take advantage of it to get a significant maths performance boost. By using a structure to define your typed variables you can use intuitive names without losing the performance. The increased performance will only be really noticeable in intensive maths applications.
    Sub Globals
    ' declare yourself some variables as Doubles
       ' a structure makes it easy.
       Dim Type(A, B, C)Var As Double
    ' but an array is fine as well (a structure is actually an array with named items)
       Dim Vars(3As Double
    End Sub
    Sub Maths
    ' now do the maths using those Double variables
       ' when optimised compiled you should get a large performance boost
       ' over using normal untyped variables
       Var.A = 1.23
       Var.B = 
    0) = Sin(Var.B) + 3
    End Sub
    Last edited: Jul 12, 2009
  17. Erel

    Erel Administrator Staff Member Licensed User

    Array typing was introduced in v4.00 together with the external libraries support (that was an important version :) ).
    While converting back and forth a regular variable as required by the external library is reasonable in most cases, converting a complete array causes too much overhead. The first relevant use case was the BinaryFile that reads arrays of bytes from a file.

    Adding support for typing of regular variables will be added in one of the next versions.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice