Callbacks from C into Squeak

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Callbacks from C into Squeak

news.gmane.org-2
Callbacks from C into Smalltalk

We currently use VSE (Visual Smalltalk Enterprise). If you have a C
callback into VSE, while the callback is running, it is very difficult
to debug. If the callback halts, or there is an error, you can't debug
and resume in the normal way that you can in Smalltalk. Apparently this
is something to do with VSE using the machine stack as the Smalltalk
stack.

However, one of the nice things about VSE is: if I put a halt in the
callback, will I see the entire stack, including the methods before the
initial entry into C. Also if I throw an exception in the call back
that is not caught in the callback, will it be caught further down the
stack by code running before the initial entry into C. If it a
resumable exception, I can resume as per normal.

Now, we do a lot of numerical computing, re-using all the C and Fortran
libraries that are already out there. It is common in a numerical
library to use callbacks. Say you want to integrate f(x). The
integrator is written in Fortran, and f(x) is written in Smalltalk. You
need a way for the integrator to call f(x), and you do that with a
callback. So to leverage these libraries we need an easy way to do
callbacks, and once you have a callback you need a way of debugging
these callbacks that a mathematician (rather than a comuputer
scientist) will feel comfortable with.

So my questioin is: when you callback into Squeak from C/Fortran,
can you throw and catch exceptions, halt, debug and resume execution
of the callback in the normal Smalltalk way. Or does the fact that
you are in a callback compromise Smalltalk's usual execution?

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

johnmci
Well you can't callback from C to Squeak, once you are in a primitive  
call the interpeter is suspended, triggering a GC event requires
special care to ensure object pointers you many have will still be  
valid (remapped) after the GC completes.  You can of course do a  
limited set of VM calls to manipulate or make new object, but that  
does not include running byte codes.  Likely this can be overcome by  
$ (euros preferred).

Other options include running a VM (or VMs) as a sub process and  
talking to it via pipes or shared memory areas for it to do  
computational work.  Technially this is done by mac carbon and unix  
VM when they run headless to provide support as a browser plugin.


On Dec 14, 2006, at 6:17 AM, daniel poon wrote:

> Callbacks from C into Smalltalk
>
> We currently use VSE (Visual Smalltalk Enterprise). If you have a C
> callback into VSE, while the callback is running, it is very difficult
> to debug. If the callback halts, or there is an error, you can't debug
> and resume in the normal way that you can in Smalltalk. Apparently  
> this
> is something to do with VSE using the machine stack as the Smalltalk
> stack.
>
> However, one of the nice things about VSE is: if I put a halt in the
> callback, will I see the entire stack, including the methods before  
> the
> initial entry into C. Also if I throw an exception in the call back
> that is not caught in the callback, will it be caught further down the
> stack by code running before the initial entry into C. If it a
> resumable exception, I can resume as per normal.
>
> Now, we do a lot of numerical computing, re-using all the C and  
> Fortran
> libraries that are already out there. It is common in a numerical
> library to use callbacks. Say you want to integrate f(x). The
> integrator is written in Fortran, and f(x) is written in Smalltalk.  
> You
> need a way for the integrator to call f(x), and you do that with a
> callback. So to leverage these libraries we need an easy way to do
> callbacks, and once you have a callback you need a way of debugging
> these callbacks that a mathematician (rather than a comuputer
> scientist) will feel comfortable with.
>
> So my questioin is: when you callback into Squeak from C/Fortran,
> can you throw and catch exceptions, halt, debug and resume execution
> of the callback in the normal Smalltalk way. Or does the fact that
> you are in a callback compromise Smalltalk's usual execution?
>
> Cheers
>
> Daniel
>
>

--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

Andreas.Raab
http://lists.squeakfoundation.org/pipermail/vm-dev/2006-June/000748.html

Cheers,
   - Andreas

John M McIntosh wrote:

> Well you can't callback from C to Squeak, once you are in a primitive
> call the interpeter is suspended, triggering a GC event requires
> special care to ensure object pointers you many have will still be valid
> (remapped) after the GC completes.  You can of course do a limited set
> of VM calls to manipulate or make new object, but that does not include
> running byte codes.  Likely this can be overcome by $ (euros preferred).
>
> Other options include running a VM (or VMs) as a sub process and talking
> to it via pipes or shared memory areas for it to do computational work.  
> Technially this is done by mac carbon and unix VM when they run headless
> to provide support as a browser plugin.
>
>
> On Dec 14, 2006, at 6:17 AM, daniel poon wrote:
>
>> Callbacks from C into Smalltalk
>>
>> We currently use VSE (Visual Smalltalk Enterprise). If you have a C
>> callback into VSE, while the callback is running, it is very difficult
>> to debug. If the callback halts, or there is an error, you can't debug
>> and resume in the normal way that you can in Smalltalk. Apparently this
>> is something to do with VSE using the machine stack as the Smalltalk
>> stack.
>>
>> However, one of the nice things about VSE is: if I put a halt in the
>> callback, will I see the entire stack, including the methods before the
>> initial entry into C. Also if I throw an exception in the call back
>> that is not caught in the callback, will it be caught further down the
>> stack by code running before the initial entry into C. If it a
>> resumable exception, I can resume as per normal.
>>
>> Now, we do a lot of numerical computing, re-using all the C and Fortran
>> libraries that are already out there. It is common in a numerical
>> library to use callbacks. Say you want to integrate f(x). The
>> integrator is written in Fortran, and f(x) is written in Smalltalk. You
>> need a way for the integrator to call f(x), and you do that with a
>> callback. So to leverage these libraries we need an easy way to do
>> callbacks, and once you have a callback you need a way of debugging
>> these callbacks that a mathematician (rather than a comuputer
>> scientist) will feel comfortable with.
>>
>> So my questioin is: when you callback into Squeak from C/Fortran,
>> can you throw and catch exceptions, halt, debug and resume execution
>> of the callback in the normal Smalltalk way. Or does the fact that
>> you are in a callback compromise Smalltalk's usual execution?
>>
>> Cheers
>>
>> Daniel
>>
>>
>
> --
> ===========================================================================
> John M. McIntosh <[hidden email]>
> Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
> ===========================================================================
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

johnmci
Mmm in case people think I'm being clueless here I don't recall the  
original note, and in checking it seems my apple mail folder for the  
squeak news group is missing any messages from Noon on June 9th, to  
1AM on June 10th, so in fact I never saw it.

However my gmail account which filters the mail does have the mail,  
so it seems Apple's mail program never delivered it to me, otherwise  
I might have remembered it and likely commented on it.

Sorry for the confusion.


On Dec 14, 2006, at 9:57 AM, Andreas Raab wrote:

> http://lists.squeakfoundation.org/pipermail/vm-dev/2006-June/ 
> 000748.html
>
> Cheers,
>   - Andreas
>
> John M McIntosh wrote:
>> Well you can't callback from C to Squeak, once you are in a  
>> primitive call the interpeter is suspended, triggering a GC event  
>> requires
--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
In reply to this post by johnmci
John M McIntosh <johnmci <at> smalltalkconsulting.com> writes:

> Well you can't callback from C to Squeak, once you are in a primitive  
> call the interpeter is suspended, triggering a GC event requires
> special care to ensure object pointers you many have will still be  
> valid (remapped) after the GC completes.  You can of course do a  
> limited set of VM calls to manipulate or make new object, but that  
> does not include running byte codes.  Likely this can be overcome by  
> $ (euros preferred).

Thanks for the answer John.
If I communicate with the DLL with primitive date allocated on the C heap, then
I don't mind if Squeak objects move around anymore. Am I still prevented from
calling back into Squeak?

> Other options include running a VM (or VMs) as a sub process and  
> talking to it via pipes or shared memory areas for it to do  
> computational work.  Technially this is done by mac carbon and unix  
> VM when they run headless to provide support as a browser plugin.

How do you get UI events into Squeak without callbacks?

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

johnmci
As Andreas pointed out there is work to support callbacks and I  
discovered I'm missing a 1/2 day of messages from june that talked  
about this feature.

http://lists.squeakfoundation.org/pipermail/vm-dev/2006-June/000748.html

A subprocess or something can communicate with the Smalltalk VM by  
signaling a semaphore, or if you have a custom VM by sticking some  
event of your choosing on the event queue , look at the sqInputEvent  
structure in sq.h.  These events are read by the VM in class  
EventSensor. I'll not there is no interface to place events on the VM  
queue which is why you must build a custom VM for that support.

Although normally one signals a semaphore that a smalltalk process is  
waiting on. You can allocate storage externally from the smalltalk  
memory space using the fixed memory allocators in FFI to provide  
shared memory for a subprocess to communicate with the smalltalk code


On Dec 15, 2006, at 2:12 AM, Daniel Poon wrote:

> John M McIntosh <johnmci <at> smalltalkconsulting.com> writes:
>
>> Well you can't callback from C to Squeak, once you are in a primitive
>> call the interpeter is suspended, triggering a GC event requires
>> special care to ensure object pointers you many have will still be
>> valid (remapped) after the GC completes.  You can of course do a
>> limited set of VM calls to manipulate or make new object, but that
>> does not include running byte codes.  Likely this can be overcome by
>> $ (euros preferred).
>
> Thanks for the answer John.
> If I communicate with the DLL with primitive date allocated on the  
> C heap, then
> I don't mind if Squeak objects move around anymore. Am I still  
> prevented from
> calling back into Squeak?
>
>> Other options include running a VM (or VMs) as a sub process and
>> talking to it via pipes or shared memory areas for it to do
>> computational work.  Technially this is done by mac carbon and unix
>> VM when they run headless to provide support as a browser plugin.
>
> How do you get UI events into Squeak without callbacks?
>
> Cheers
>
> Daniel
>
>

--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
John M McIntosh <johnmci <at> smalltalkconsulting.com> writes:

> As Andreas pointed out there is work to support callbacks and I  
> discovered I'm missing a 1/2 day of messages from june that talked  
> about this feature.
>
> http://lists.squeakfoundation.org/pipermail/vm-dev/2006-June/000748.html 

I did see Andreas' post, but tweaking the VM is a bit new to me, so I couldn't
respond to it.

> A subprocess or something can communicate with the Smalltalk VM by  
> signaling a semaphore, or if you have a custom VM by sticking some  
> event of your choosing on the event queue , look at the sqInputEvent  
> structure in sq.h.  These events are read by the VM in class  
> EventSensor. I'll not there is no interface to place events on the VM  
> queue which is why you must build a custom VM for that support.

Using callbacks to stick events onto the Smalltalk VM's event queue - this
sounds the same as what VSE does with most of its Windows events. However it is
only applicable if the event does not require an answer from the callback (e.g.
'the user has hit a button').

If it is a callback that requires an answer (e.g. 'give me the contents for this
list box'), what do  you do?

> Although normally one signals a semaphore that a smalltalk process is  
> waiting on. You can allocate storage externally from the smalltalk  
> memory space using the fixed memory allocators in FFI to provide  
> shared memory for a subprocess to communicate with the smalltalk code

The FFI sounds all good, and the code in the FFI looks much like stuff in VSE.

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

Bert Freudenberg
On Dec 16, 2006, at 19:14 , daniel poon wrote:

> If it is a callback that requires an answer (e.g. 'give me the  
> contents for this
> list box'), what do  you do?

Easiest might be to use a toolkit that does not require callbacks ;)  
Otherwise there is no way to do that in FFI only I think. You could  
write a plugin that forks off another process which will talk to your  
toolkit. Callbacks would be registered in that second process, they  
could then send a request to the image and block until they get an  
answer back.

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
Bert Freudenberg <bert <at> freudenbergs.de> writes:

> On Dec 16, 2006, at 19:14 , daniel poon wrote:
>
> > If it is a callback that requires an answer (e.g. 'give me the  
> > contents for this
> > list box'), what do  you do?
>
> Easiest might be to use a toolkit that does not require callbacks ;)  
> Otherwise there is no way to do that in FFI only I think. You could  
> write a plugin that forks off another process which will talk to your  
> toolkit.

When you say <<forks off another process>>, do you mean a Smalltalk process?

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Sun, Dec 17, 2006 at 01:13:09AM +0000, daniel poon wrote:

> Bert Freudenberg <bert <at> freudenbergs.de> writes:
>
> > On Dec 16, 2006, at 19:14 , daniel poon wrote:
> >
> > > If it is a callback that requires an answer (e.g. 'give me the  
> > > contents for this
> > > list box'), what do  you do?
> >
> > Easiest might be to use a toolkit that does not require callbacks ;)  
> > Otherwise there is no way to do that in FFI only I think. You could  
> > write a plugin that forks off another process which will talk to your  
> > toolkit.
>
> When you say <<forks off another process>>, do you mean a Smalltalk process?
 
Daniel,

Bert is referring to an OS process (unix, win32), not a Smalltalk
process. So the plugin could fork an OS process and communicate with
it through any of several mechanisms.  But his suggestion of a toolkit
that does not require callbacks is really the first thing that you
should consider.

If you are dealing with an external toolkit that really does need
bidirectional communication, then it is also possible to have Squeak
start another process, and communicate directly with it using pipes
or sockets. This is platform dependent and is typically easiest to
do on unix (OS/X, Linux).

Here is an example that starts a Tk interpreter in a separate
OS process, and communicates with the remote process through pipes.
The Tk interpreter will open a user interface outside of Squeak.
This requires the OSProcess and CommandShell packages from SqueakMap.

  | wish |
  wish := PipeableOSProcess command: '/usr/bin/wish'.
  [wish
    exec: 'button .b1 -text "This is button b1"';
    exec: 'button .b2 -text "This is button b2"';
    exec: 'button .exit -text Exit -command exit';
    exec: 'pack .b1 .b2 .exit'] fork.
  wish inspect.


 

Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
David T. Lewis <lewis <at> mail.msen.com> writes:
> Bert is referring to an OS process (unix, win32), not a Smalltalk
> process. So the plugin could fork an OS process and communicate with
> it through any of several mechanisms.  But his suggestion of a toolkit
> that does not require callbacks is really the first thing that you
> should consider.

Thanks for your clarification and your code snippets.
I was using the GUI callback as an illustration of a callback, but what I am
really interested in is calling back from foreign mathematical libraries, and
how to do that in Squeak. We have had some success in doing such things in
another Smalltalk dialect, and I want to know if I can port that work on to
Squeak.

Will using separate processes perform well enough? To simulate a few seconds
using a physics simulation, a maths library would have to make about a million
callbacks into Smalltalk. You would want the simulation to take seconds or
minutes rather than hours.

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Sun, Dec 17, 2006 at 04:56:45PM +0000, daniel poon wrote:

> David T. Lewis <lewis <at> mail.msen.com> writes:
> > Bert is referring to an OS process (unix, win32), not a Smalltalk
> > process. So the plugin could fork an OS process and communicate with
> > it through any of several mechanisms.  But his suggestion of a toolkit
> > that does not require callbacks is really the first thing that you
> > should consider.
>
> Thanks for your clarification and your code snippets.
> I was using the GUI callback as an illustration of a callback, but what I am
> really interested in is calling back from foreign mathematical libraries, and
> how to do that in Squeak. We have had some success in doing such things in
> another Smalltalk dialect, and I want to know if I can port that work on to
> Squeak.
>
> Will using separate processes perform well enough? To simulate a few seconds
> using a physics simulation, a maths library would have to make about a million
> callbacks into Smalltalk. You would want the simulation to take seconds or
> minutes rather than hours.

My guess is that separate processes would *not* perform well enough, but my
guessing does not count for much. If you have a fairly good idea of how many
callbacks are happening, it might be worth putting together a no-op test
to find out how fast it runs. You could for example run a million transactions
with Squeak sending "hello there" (and flushing the stream), and a C program
echoing the message back but otherwise doing nothing. The results will
probably be disappointing, but you never know.

Dave


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Sun, Dec 17, 2006 at 12:50:09PM -0500, David T. Lewis wrote:

> On Sun, Dec 17, 2006 at 04:56:45PM +0000, daniel poon wrote:
> > David T. Lewis <lewis <at> mail.msen.com> writes:
> > > Bert is referring to an OS process (unix, win32), not a Smalltalk
> > > process. So the plugin could fork an OS process and communicate with
> > > it through any of several mechanisms.  But his suggestion of a toolkit
> > > that does not require callbacks is really the first thing that you
> > > should consider.
> >
> > Thanks for your clarification and your code snippets.
> > I was using the GUI callback as an illustration of a callback, but what I am
> > really interested in is calling back from foreign mathematical libraries, and
> > how to do that in Squeak. We have had some success in doing such things in
> > another Smalltalk dialect, and I want to know if I can port that work on to
> > Squeak.
> >
> > Will using separate processes perform well enough? To simulate a few seconds
> > using a physics simulation, a maths library would have to make about a million
> > callbacks into Smalltalk. You would want the simulation to take seconds or
> > minutes rather than hours.
>
> My guess is that separate processes would *not* perform well enough, but my
> guessing does not count for much. If you have a fairly good idea of how many
> callbacks are happening, it might be worth putting together a no-op test
> to find out how fast it runs. You could for example run a million transactions
> with Squeak sending "hello there" (and flushing the stream), and a C program
> echoing the message back but otherwise doing nothing. The results will
> probably be disappointing, but you never know.

Daniel,

To follow up on my comment above, I put together a test that has Squeak
forking another Squeak image, then sending 'hello' back and forth a million
times between the two images, each of which is running in a separate OS
process. The one million transactions run in about 150 seconds on my 166MHz
Pentium. I guess that on faster hardware it would do better.

So somewhat to my surprise, it does seem reasonable to consider using simple
communications through a pipe for the kind of work you are describing, and
expect performance in the "minutes not hours" range.

Here is the test that I ran:

    | sendPipe receivePipe s remote count |
    count := 1000000. "One million transactions"
    sendPipe := OSPipe blockingPipe.
    receivePipe := OSPipe blockingPipe.
    remote := UnixProcess forkHeadlessSqueakAndDoThenQuit:
        ["This block is evaluated in the remote headless Squeak image"
        (count + 1) timesRepeat:
            ["Read message and echo it back to sender"
            s := sendPipe next: 5.
            receivePipe nextPutAll: s; flush]].
    remote inspect. "Inspect the process proxy for the remote Squeak"
    sendPipe nextPutAll: 'hello'; flush.
    receivePipe next: 5. "do one complete transaction before starting the timer"
    (Time millisecondsToRun:
        [count timesRepeat:
            [sendPipe nextPutAll: 'hello'; flush.
            receivePipe next: 5]]) inspect. "Inspect run time in milliseconds"
    sendPipe close.
    receivePipe close.


Dave


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
David T. Lewis wrote:

> On Sun, Dec 17, 2006 at 12:50:09PM -0500, David T. Lewis wrote:
>> On Sun, Dec 17, 2006 at 04:56:45PM +0000, daniel poon wrote:
>>> David T. Lewis <lewis <at> mail.msen.com> writes:
>>>> Bert is referring to an OS process (unix, win32), not a Smalltalk
>>>> process. So the plugin could fork an OS process and communicate with
>>>> it through any of several mechanisms.  But his suggestion of a toolkit
>>>> that does not require callbacks is really the first thing that you
>>>> should consider.
>>> Thanks for your clarification and your code snippets.
>>> I was using the GUI callback as an illustration of a callback, but what I am
>>> really interested in is calling back from foreign mathematical libraries, and
>>> how to do that in Squeak. We have had some success in doing such things in
>>> another Smalltalk dialect, and I want to know if I can port that work on to
>>> Squeak.
>>>
>>> Will using separate processes perform well enough? To simulate a few seconds
>>> using a physics simulation, a maths library would have to make about a million
>>> callbacks into Smalltalk. You would want the simulation to take seconds or
>>> minutes rather than hours.
>> My guess is that separate processes would *not* perform well enough, but my
>> guessing does not count for much. If you have a fairly good idea of how many
>> callbacks are happening, it might be worth putting together a no-op test
>> to find out how fast it runs. You could for example run a million transactions
>> with Squeak sending "hello there" (and flushing the stream), and a C program
>> echoing the message back but otherwise doing nothing. The results will
>> probably be disappointing, but you never know.
>
> Daniel,
>
> To follow up on my comment above, I put together a test that has Squeak
> forking another Squeak image, then sending 'hello' back and forth a million
> times between the two images, each of which is running in a separate OS
> process. The one million transactions run in about 150 seconds on my 166MHz
> Pentium. I guess that on faster hardware it would do better.
>
> So somewhat to my surprise, it does seem reasonable to consider using simple
> communications through a pipe for the kind of work you are describing, and
> expect performance in the "minutes not hours" range.
>
> Here is the test that I ran:
>
>     | sendPipe receivePipe s remote count |
>     count := 1000000. "One million transactions"
>     sendPipe := OSPipe blockingPipe.
>     receivePipe := OSPipe blockingPipe.
>     remote := UnixProcess forkHeadlessSqueakAndDoThenQuit:
>         ["This block is evaluated in the remote headless Squeak image"
>         (count + 1) timesRepeat:
>             ["Read message and echo it back to sender"
>             s := sendPipe next: 5.
>             receivePipe nextPutAll: s; flush]].
>     remote inspect. "Inspect the process proxy for the remote Squeak"
>     sendPipe nextPutAll: 'hello'; flush.
>     receivePipe next: 5. "do one complete transaction before starting the timer"
>     (Time millisecondsToRun:
>         [count timesRepeat:
>             [sendPipe nextPutAll: 'hello'; flush.
>             receivePipe next: 5]]) inspect. "Inspect run time in milliseconds"
>     sendPipe close.
>     receivePipe close.
>
>
> Dave

Hi Dave

That makes it an overhead 0.15ms per callback, which is acceptable. I
will need to pass back an forth a packed array of floating point numbers
with a few hundred elements in it (representing the state of the system
and its calculated derivative). How could that be achieved without
denting that 0.15ms overhead too much?

Cheers

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Mon, Dec 18, 2006 at 10:11:46AM +0000, dan wrote:

> David T. Lewis wrote:
> >Here is the test that I ran:
> >
> >    | sendPipe receivePipe s remote count |
> >    count := 1000000. "One million transactions"
> >    sendPipe := OSPipe blockingPipe.
> >    receivePipe := OSPipe blockingPipe.
> >    remote := UnixProcess forkHeadlessSqueakAndDoThenQuit:
> >        ["This block is evaluated in the remote headless Squeak image"
> >        (count + 1) timesRepeat:
> >            ["Read message and echo it back to sender"
> >            s := sendPipe next: 5.
> >            receivePipe nextPutAll: s; flush]].
> >    remote inspect. "Inspect the process proxy for the remote Squeak"
> >    sendPipe nextPutAll: 'hello'; flush.
> >    receivePipe next: 5. "do one complete transaction before starting the
> >    timer"
> >    (Time millisecondsToRun:
> >        [count timesRepeat:
> >            [sendPipe nextPutAll: 'hello'; flush.
> >            receivePipe next: 5]]) inspect. "Inspect run time in
> >            milliseconds"
> >    sendPipe close.
> >    receivePipe close.
> >
> >
> >Dave
>
> Hi Dave
>
> That makes it an overhead 0.15ms per callback, which is acceptable. I
> will need to pass back an forth a packed array of floating point numbers
> with a few hundred elements in it (representing the state of the system
> and its calculated derivative). How could that be achieved without
> denting that 0.15ms overhead too much?
 
Daniel,

The only way to find out is to try it. Do you have an example of the
packed array binary data to work with? We could plug it in to the test
and see what happens.

Dave


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

Klaus D. Witzel
In reply to this post by news.gmane.org-2
Hi Dan,

on Mon, 18 Dec 2006 11:11:46 +0100, you wrote:

> David T. Lewis wrote:
>>  Daniel,
>>  To follow up on my comment above, I put together a test that has Squeak
>> forking another Squeak image, then sending 'hello' back and forth a  
>> million
>> times between the two images, each of which is running in a separate OS
>> process. The one million transactions run in about 150 seconds on my  
>> 166MHz
>> Pentium. I guess that on faster hardware it would do better.
>>  So somewhat to my surprise, it does seem reasonable to consider using  
>> simple
>> communications through a pipe for the kind of work you are describing,  
>> and
>> expect performance in the "minutes not hours" range.
>>  Here is the test that I ran:
...
>>   Dave
>
> Hi Dave
>
> That makes it an overhead 0.15ms per callback, which is acceptable. I  
> will need to pass back an forth a packed array of floating point numbers  
> with a few hundred elements in it (representing the state of the system  
> and its calculated derivative). How could that be achieved without  
> denting that 0.15ms overhead too much?

I've worked with mainframe computers of these people (beware: gif patent  
holders)

- http://www.google.com/search?q=WebTx+ipc+shared+memory

for quite some time. We had a supplemental Unix box integrated into the  
mainframe (other users choose to integrate MS boxen) for various purposes.  
The interface was very versatile and the fastest transport was shared  
memory (of course, for very high volume bank transactions ...). IIRC this  
interface establishes connection using pipes and then just tells "new data  
arrived" (like with your call/callback), after updating shared memory.

Perhaps such a shared memory approach is of use for you, perhaps in  
concert with Dave's pipe solution (but I don't know of any Squeak support  
for shared memory).

/Klaus

> Cheers
>
> Daniel
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Mon, Dec 18, 2006 at 06:42:35PM +0100, Klaus D. Witzel wrote:

> I've worked with mainframe computers of these people (beware: gif patent  
> holders)
>
> - http://www.google.com/search?q=WebTx+ipc+shared+memory
>
> for quite some time. We had a supplemental Unix box integrated into the  
> mainframe (other users choose to integrate MS boxen) for various purposes.  
> The interface was very versatile and the fastest transport was shared  
> memory (of course, for very high volume bank transactions ...). IIRC this  
> interface establishes connection using pipes and then just tells "new data  
> arrived" (like with your call/callback), after updating shared memory.
>
> Perhaps such a shared memory approach is of use for you, perhaps in  
> concert with Dave's pipe solution (but I don't know of any Squeak support  
> for shared memory).

Interesting idea. There is no support for shared memory in Squeak right
now, but in principle it's no different from other OS and C runtime calls
in the OSProcessPlugin. I think that the shared memory interface for Unix
platforms is fairly well standardized, and there are Win32 analogues,
so that it could be provided across most platforms.

Dave


Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

johnmci
With FFI on unix you can easily used the mmap calls to share memory  
between a VM an a child process. Of course this is memory outside of  
squeak object space, but you could use the logic for ExternalObject/
ExternalAddress to map shared memory addresses to enable the sharing  
of data between processes.

I'll note I share the drawing surface between the os-x carbon browser  
plugin and the child process headless VM that launched for  the  
plugin support. The plugin funnels UI and browser events to/from the  
VM via pipes and the VM draws to the shared surface as a Quartz  
bitmap and sends draw coordinates to the plugin which in turn takes  
the given coordinates and draws to the browser window using the  
shared bitmap.

There are some setup issues with mmap that aren't really well  
documented, look at the VM source code for clues, otherwise you'll  
waste a bunch of time trying to figure out why it doesn't work.

On Dec 18, 2006, at 3:14 PM, David T. Lewis wrote:

> On Mon, Dec 18, 2006 at 06:42:35PM +0100, Klaus D. Witzel wrote:
>> I've worked with mainframe computers of these people (beware: gif  
>> patent
>> holders)
>>
>> - http://www.google.com/search?q=WebTx+ipc+shared+memory
>>
>> for quite some time. We had a supplemental Unix box integrated  
>> into the
>> mainframe (other users choose to integrate MS boxen) for various  
>> purposes.
>> The interface was very versatile and the fastest transport was shared
>> memory (of course, for very high volume bank transactions ...).  
>> IIRC this
>> interface establishes connection using pipes and then just tells  
>> "new data
>> arrived" (like with your call/callback), after updating shared  
>> memory.
>>
>> Perhaps such a shared memory approach is of use for you, perhaps in
>> concert with Dave's pipe solution (but I don't know of any Squeak  
>> support
>> for shared memory).
>
> Interesting idea. There is no support for shared memory in Squeak  
> right
> now, but in principle it's no different from other OS and C runtime  
> calls
> in the OSProcessPlugin. I think that the shared memory interface  
> for Unix
> platforms is fairly well standardized, and there are Win32 analogues,
> so that it could be provided across most platforms.
>
> Dave
>
>

--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

news.gmane.org-2
In reply to this post by David T. Lewis
David T. Lewis wrote:

> On Mon, Dec 18, 2006 at 10:11:46AM +0000, dan wrote:
>> Hi Dave
>>
>> That makes it an overhead 0.15ms per callback, which is acceptable. I
>> will need to pass back an forth a packed array of floating point numbers
>> with a few hundred elements in it (representing the state of the system
>> and its calculated derivative). How could that be achieved without
>> denting that 0.15ms overhead too much?
>  
> Daniel,
>
> The only way to find out is to try it. Do you have an example of the
> packed array binary data to work with? We could plug it in to the test
> and see what happens.
>
> Dave

Hi Dave

The data we would use would be very much like Squeaks FloatArray class,
except double precision. If we used shared memory, as has been suggested
in a branch of this thread elsewhere, then it should all be good.

At least I know now what to do in principle. I don't relish doing it in
Windows though :-(

I spend an hour or so looking at the Fortran maths library to see if I
could hack it to not use callbacks, but that really is a tall order.

Thanks again (to everyone),

Daniel



Reply | Threaded
Open this post in threaded view
|

Re: Callbacks from C into Squeak

David T. Lewis
On Tue, Dec 19, 2006 at 01:15:23PM +0000, dan wrote:

> David T. Lewis wrote:
> >On Mon, Dec 18, 2006 at 10:11:46AM +0000, dan wrote:
> >>Hi Dave
> >>
> >>That makes it an overhead 0.15ms per callback, which is acceptable. I
> >>will need to pass back an forth a packed array of floating point numbers
> >>with a few hundred elements in it (representing the state of the system
> >>and its calculated derivative). How could that be achieved without
> >>denting that 0.15ms overhead too much?
> >
> >Daniel,
> >
> >The only way to find out is to try it. Do you have an example of the
> >packed array binary data to work with? We could plug it in to the test
> >and see what happens.
> >
> >Dave
>
> Hi Dave
>
> The data we would use would be very much like Squeaks FloatArray class,
> except double precision. If we used shared memory, as has been suggested
> in a branch of this thread elsewhere, then it should all be good.
Daniel,

I am attaching a set of tests that demonstrate performance of two
cooperating Squeak images exchanging binary floating point data.

Results on my (old and slow) Pentium box:

- With 4000 byte uninterpreted data messages, overall transaction
  time is under 0.5 msec per end-to-end transaction..

- With a packed binary array of 500 doubles (4000 bytes long), and
  one of the cooperating images doing data conversion to and from
  the binary message, transaction time is 18 msec.

- With both cooperating images doing data conversion, transaction
  time is 36 msec.

- With a packed binary array of 1000 floats (4000 bytes long) and
  one of the cooperating images doing data conversion, transaction
  time is 31 msec.

My conclusions:

- Communication of raw data over OS pipes is quite fast.

- The associated data conversion to and from raw data is up to
  two orders of magnitude slower than the actual image-to-image
  communication.

- Do not bother with shared memory for data sets of this size,
  because moving data is a small part of the performance problem.

- The first thing to optimize (if actually needed) would be the
  data conversion to and from raw data. This can be done from the
  image, calling a primitive.

- No callbacks from primitives are needed. The required data
  communication and conversion can and should be done from the
  image, adding simple primitives to speed up the data conversion
  if necessary.

- Transmitting data as 64 bit doubles is more efficient than
  transmitting 32 bit floats because this is Squeak's internal
  format for Float. Note, pay attention to endianness, my tests
  are hard coded for little-endian data.

Dave




TestCooperatingSqueaks.dtl.3.cs.gz (19K) Download Attachment
12