Login  Register

Fun with spock (NativeBoost FFI)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Fun with spock (NativeBoost FFI)

LawsonEnglish
889 posts
So, thanks to Igor Stasenko, I've managed to learn how to (sorta) use
the new NativeBoost FFI (spock). We (he) implemented a binding to a few
of the gmplib functions. gmp is the GNU Multi-Precision Library.

Here is a simple benchmark of the Pharo factorial method vs the gmplib
mpz_fac_ui() function. As might be expected, gmplib is  faster for any
test larger than 13 factorial, as LargePositiveInteger  starts to become
involved, and the speed differential becomes rather huge as larger
integers are tested:

1000 factorial time to run with:
squeak: 2 ms      gmplib: 0.004 ms   ratio: 50.0
11000 factorial time to run with:
squeak: 354 ms      gmplib: 0.191 ms   ratio: 185.34031413612567
[...]
91000 factorial time to run with:
squeak: 34598 ms      gmplib: 4.759 ms   ratio: 727.0014708972473



Running on a Mac OS X Snow Leopard 2.8Ghz, with NBCog and 1.4 Pharo image.

benchmark:

a := GMPzWrapper new.

1000 to: 100000 by: 10000 do:
[:i|
    squeakTime :=  [ i factorial] timeToRun.
    Transcript show:  i asString, ' factorial time to run with:';cr.
    Transcript show:  'squeak: ', (squeakTime) asString, ' ms '.
    gmpTime :=  [100 timesRepeat: [a prim_mpz_fac_ui: i]] timeToRun.
    Transcript show:  '     gmplib: ' , (gmpTime/1000.0) asString, ' ms  '.
    Transcript show: ' ratio: ', (squeakTime/(gmpTime/100.0))  
asString;cr;cr.
]


--
Squeak from the very start (introduction to Squeak and Pharo Smalltalk for the (almost) complete and compleate beginner).
https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

LawsonEnglish
889 posts
On 5/13/12 11:02 AM, Lawson English wrote:

[results]

Ack, I printed the times for the gmplib off by a factor of 10. However,
the ratio is still correct.

gmplib is at least 700x faster than the built-in factorial method for
100000 factorial.

--
Squeak from the very start (introduction to Squeak and Pharo Smalltalk for the (almost) complete and compleate beginner).
https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

LawsonEnglish
889 posts
[pharo-project list added back in]

On 5/13/12 1:34 PM, Nicolas Cellier wrote:
> [100000 primeSwingFactorial] timeToRun 3411 [100000 factorial]
> timeToRun 61219 So gmp is still 40x faster than a reasonably optimized
> Smalltalk factorial... I think that's fair, because gmp is highly
> optimized. Since cost is dominated by LargeInteger arithmetic, and
> since Smalltalk LargeInteger arithmetic in primitives still operates
> on bytes, I'm not so surprised of the gap... I'd like to see a 64bit
> image operating on 32 bits positive integers digits, that would
> already be a progress... Nicolas

For me, the most interesting thing would be to figure out how to
integrate such external libraries more tightly into the language syntax.

Right now,   x :=  3.14159... always creates a Float, which is stored as
a double.

x class ===> Float.

What if I wanted to generate a gmplib floating point number (or rational
or large integer) automatically when the gmplib binding is available?

Could I implement something that makes x a GMPfpWrapper if extra digits
are supplied?
e.g.:

x := 3.14159265358979323846264338327950288419716939937510.

x class ===> GMPfpWrapper.

NativeBoost allows one to cascade calls to external libraries if you
wrap them in assembly language in the method. High speed,
aribrary-precision floating point complex number arithmetic, can be
accomplished that way (no examples yet sorry, still learning) as well as
more complicated stuff. I'm hoping to make a proof-of-concept mandlebrot
set Morph that can zoom in arbitrarily. 32 bit floats limit the
resolution terribly.

So...

What can be done to better integrate such libraries? I browsed the
Parser and other classes but didn't understand what I was seeing well
enough to figure out how to begin.



L

--
Squeak from the very start (introduction to Squeak and Pharo Smalltalk for the (almost) complete and compleate beginner).
https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

Igor Stasenko
9533 posts
On 14 May 2012 00:03, Lawson English <[hidden email]> wrote:

> [pharo-project list added back in]
>
>
> On 5/13/12 1:34 PM, Nicolas Cellier wrote:
>>
>> [100000 primeSwingFactorial] timeToRun 3411 [100000 factorial] timeToRun
>> 61219 So gmp is still 40x faster than a reasonably optimized Smalltalk
>> factorial... I think that's fair, because gmp is highly optimized. Since
>> cost is dominated by LargeInteger arithmetic, and since Smalltalk
>> LargeInteger arithmetic in primitives still operates on bytes, I'm not so
>> surprised of the gap... I'd like to see a 64bit image operating on 32 bits
>> positive integers digits, that would already be a progress... Nicolas
>
>
> For me, the most interesting thing would be to figure out how to integrate
> such external libraries more tightly into the language syntax.
>
> Right now,   x :=  3.14159... always creates a Float, which is stored as a
> double.
>
> x class ===> Float.
>
> What if I wanted to generate a gmplib floating point number (or rational or
> large integer) automatically when the gmplib binding is available?
>
> Could I implement something that makes x a GMPfpWrapper if extra digits are
> supplied?
> e.g.:
>
> x := 3.14159265358979323846264338327950288419716939937510.
>
> x class ===> GMPfpWrapper.
>
> NativeBoost allows one to cascade calls to external libraries if you wrap
> them in assembly language in the method. High speed, aribrary-precision
> floating point complex number arithmetic, can be accomplished that way (no
> examples yet sorry, still learning) as well as more complicated stuff. I'm
> hoping to make a proof-of-concept mandlebrot set Morph that can zoom in
> arbitrarily. 32 bit floats limit the resolution terribly.
>
> So...
>
> What can be done to better integrate such libraries? I browsed the Parser
> and other classes but didn't understand what I was seeing well enough to
> figure out how to begin.
>
See SqNumberParser.
this is the guy who used by Parser to parse number literals.

>
>
>
> L
>
> --
> Squeak from the very start (introduction to Squeak and Pharo Smalltalk for
> the (almost) complete and compleate beginner).
> https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all
>
>



--
Best regards,
Igor Stasenko.

Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

Marcus Denker-4
7582 posts
In reply to this post by LawsonEnglish

On May 14, 2012, at 12:49 AM, Igor Stasenko wrote:

> On 14 May 2012 00:03, Lawson English <[hidden email]> wrote:
>> [pharo-project list added back in]
>>
>>
>> On 5/13/12 1:34 PM, Nicolas Cellier wrote:
>>>
>>> [100000 primeSwingFactorial] timeToRun 3411 [100000 factorial] timeToRun
>>> 61219 So gmp is still 40x faster than a reasonably optimized Smalltalk
>>> factorial... I think that's fair, because gmp is highly optimized. Since
>>> cost is dominated by LargeInteger arithmetic, and since Smalltalk
>>> LargeInteger arithmetic in primitives still operates on bytes, I'm not so
>>> surprised of the gap... I'd like to see a 64bit image operating on 32 bits
>>> positive integers digits, that would already be a progress... Nicolas
>>
>>
>> For me, the most interesting thing would be to figure out how to integrate
>> such external libraries more tightly into the language syntax.
>>
>> Right now,   x :=  3.14159... always creates a Float, which is stored as a
>> double.
>>
>> x class ===> Float.
>>
>> What if I wanted to generate a gmplib floating point number (or rational or
>> large integer) automatically when the gmplib binding is available?
For Floats, there is a GNU project maintained by some groups at Inria:
       
        http://www.mpfr.org/

The main goal of MPFR is to provide a library for multiple-precision floating-point computation which is both
efficient and has a well-defined semantics. It copies the good ideas from the ANSI/IEEE-754 standard for
double-precision floating-point arithmetic (53-bit significand).



--
Marcus Denker -- http://marcusdenker.de


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

LawsonEnglish
889 posts
In reply to this post by Igor Stasenko
I am trying to implement an asString method for gmp floats but I don't
quite see how.


Prototype for getting a string is:


"Function: char * mpf_get_str (char *str, mp_exp_t *expptr, int base,
size_t n_digits, mpf_t op)"

where mp_exp_t *expptr   is just a pointer to a long, and size_t is just
a long.  I don't get how to set up the primitive call and how to evoke
it in the asString method.



--
Squeak from the very start (introduction to Squeak and Pharo Smalltalk for the (almost) complete and compleate beginner).
https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

LawsonEnglish
889 posts
On 5/14/12 8:06 PM, Lawson English wrote:

> On 5/14/12 8:02 PM, Lawson English wrote:
>> OK, getting closer. However I'm still not quite getting what I expect:
>>
>>
>> f1 := GMPfWrapper new.
>> f2 := GMPfWrapper new.
>> f3 := GMPfWrapper new.
>> f1 set_ui: 50.
>> f2 set_ui: 50.
>> f3 set_ui:50.
>> (f3 +f2) asString ==> '1
>>

Doh, its working just fine. the function only returns the minimum digits
required and returns the exponent in the expptr, so 1 has an expptr
value of 3, making it 100.0...

Sigh.

L.




--
Squeak from the very start (introduction to Squeak and Pharo Smalltalk for the (almost) complete and compleate beginner).
https://www.youtube.com/playlist?list=PL6601A198DF14788D&feature=view_all


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: [squeak-dev] Fun with spock (NativeBoost FFI)

Igor Stasenko
9533 posts
On 15 May 2012 09:42, Lawson English <[hidden email]> wrote:

> On 5/14/12 8:06 PM, Lawson English wrote:
>>
>> On 5/14/12 8:02 PM, Lawson English wrote:
>>>
>>> OK, getting closer. However I'm still not quite getting what I expect:
>>>
>>>
>>> f1 := GMPfWrapper new.
>>> f2 := GMPfWrapper new.
>>> f3 := GMPfWrapper new.
>>> f1 set_ui: 50.
>>> f2 set_ui: 50.
>>> f3 set_ui:50.
>>> (f3 +f2) asString ==> '1
>>>
>
> Doh, its working just fine. the function only returns the minimum digits
> required and returns the exponent in the expptr, so 1 has an expptr value of
> 3, making it 100.0...
>
> Sigh.
>
Right. Yeah, it works just fine, except there is no description what
is exp stands for.
So you have to guess or look for example code :)

> L.
>
>


--
Best regards,
Igor Stasenko.