question about FFI

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

question about FFI

Yuriy Babah
Hi !

I'm trying to call a very simple function from C ++ lib, writed for the test.
function with prototype:

extern "C" float interpolationFunc(float* xm, float* ym, int size, float x).

In Pharo7 wrote:
FFIExamples class >> interpolationFunc_xm: xM ym: yM size: size x: x
    ^ self ffiCall: #(float interpolationFunc #(float * xM , float * yM , int size , float x)) module: 'libinterpolationLib.so'

in Playground i'm doing:
xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := xm clone.
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
FFIExamples interpolationFunc_xm: xm pointer ym: ym pointer size: 2  x: 2.5 .

last expression returninп me 0.0, but right is 3.5.

I'm dit the same in Python3 ctypes, and there work's fine.

In UnifiedFFI booklet is absent chapter "Arrays", anybody may help with whot i'm doing wrong?
Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

tesonep@gmail.com
Hi, 

   could be the problem that you are using the same external array for both xm and ym?. 
The clone message just creates a shallow copy of the external array. 
It does not allocates a new external array, it only copies the address in both xm and ym.

You should better do something like this:

xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := FFIExternalArray externalNewType: 'float' size: 2.

Cheers.

On Mon, Nov 26, 2018 at 9:30 AM Yuriy Babah <[hidden email]> wrote:
Hi !

I'm trying to call a very simple function from C ++ lib, writed for the test.
function with prototype:

extern "C" float interpolationFunc(float* xm, float* ym, int size, float x).

In Pharo7 wrote:
FFIExamples class >> interpolationFunc_xm: xM ym: yM size: size x: x
    ^ self ffiCall: #(float interpolationFunc #(float * xM , float * yM , int size , float x)) module: 'libinterpolationLib.so'

in Playground i'm doing:
xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := xm clone.
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
FFIExamples interpolationFunc_xm: xm pointer ym: ym pointer size: 2  x: 2.5 .

last expression returninп me 0.0, but right is 3.5.

I'm dit the same in Python3 ctypes, and there work's fine.

In UnifiedFFI booklet is absent chapter "Arrays", anybody may help with whot i'm doing wrong?


--
Pablo Tesone.
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

Yuriy Babah
Thank, i'm trued do this, and i'm got
aBoxedFloat64       4.5879913020458836e-41  ,
which is still a very strange value

пн, 26 нояб. 2018 г. в 12:33, [hidden email] <[hidden email]>:
Hi, 

   could be the problem that you are using the same external array for both xm and ym?. 
The clone message just creates a shallow copy of the external array. 
It does not allocates a new external array, it only copies the address in both xm and ym.

You should better do something like this:

xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := FFIExternalArray externalNewType: 'float' size: 2.

Cheers.

On Mon, Nov 26, 2018 at 9:30 AM Yuriy Babah <[hidden email]> wrote:
Hi !

I'm trying to call a very simple function from C ++ lib, writed for the test.
function with prototype:

extern "C" float interpolationFunc(float* xm, float* ym, int size, float x).

In Pharo7 wrote:
FFIExamples class >> interpolationFunc_xm: xM ym: yM size: size x: x
    ^ self ffiCall: #(float interpolationFunc #(float * xM , float * yM , int size , float x)) module: 'libinterpolationLib.so'

in Playground i'm doing:
xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := xm clone.
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
FFIExamples interpolationFunc_xm: xm pointer ym: ym pointer size: 2  x: 2.5 .

last expression returninп me 0.0, but right is 3.5.

I'm dit the same in Python3 ctypes, and there work's fine.

In UnifiedFFI booklet is absent chapter "Arrays", anybody may help with whot i'm doing wrong?


--
Pablo Tesone.
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

Yuriy Babah

пн, 26 нояб. 2018 г. в 12:50, Yuriy Babah <[hidden email]>:
Thank, i'm trued do this, and i'm got
aBoxedFloat64       4.5879913020458836e-41  ,
which is still a very strange value

пн, 26 нояб. 2018 г. в 12:33, [hidden email] <[hidden email]>:
Hi, 

   could be the problem that you are using the same external array for both xm and ym?. 
The clone message just creates a shallow copy of the external array. 
It does not allocates a new external array, it only copies the address in both xm and ym.

You should better do something like this:

xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := FFIExternalArray externalNewType: 'float' size: 2.

Cheers.

On Mon, Nov 26, 2018 at 9:30 AM Yuriy Babah <[hidden email]> wrote:
Hi !

I'm trying to call a very simple function from C ++ lib, writed for the test.
function with prototype:

extern "C" float interpolationFunc(float* xm, float* ym, int size, float x).

In Pharo7 wrote:
FFIExamples class >> interpolationFunc_xm: xM ym: yM size: size x: x
    ^ self ffiCall: #(float interpolationFunc #(float * xM , float * yM , int size , float x)) module: 'libinterpolationLib.so'

in Playground i'm doing:
xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := xm clone.
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
FFIExamples interpolationFunc_xm: xm pointer ym: ym pointer size: 2  x: 2.5 .

last expression returninп me 0.0, but right is 3.5.

I'm dit the same in Python3 ctypes, and there work's fine.

In UnifiedFFI booklet is absent chapter "Arrays", anybody may help with whot i'm doing wrong?


--
Pablo Tesone.
[hidden email]

libinterpolationLib.so (133K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

Pharo Smalltalk Users mailing list
In reply to this post by Yuriy Babah
Yuriy Babah wrote
> in Playground i'm doing:
*
> #(2 3) doWithIndex: [:each :i | xm at: i put: each].
> #(3 4) doWithIndex: [:each :i | ym at: i put: each].
*
>
> whot i'm doing wrong?

So, you're putting 2 at index 2 of xm, and then writing 3 past end of array,
and in ym you're putting 3 and 4 at offsets past the end of array?

It's a wonder your image didn't crash, rather than return interpolation of
the uninitialized values...

Cheers,
Henry



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

Pharo Smalltalk Users mailing list
In reply to this post by Yuriy Babah
Nevermind me, hadn't had my coffee yet, for some reason I didn't see it was
doWithIndex: you were using :/

Still a bit scary that FFIExternalArray at:put: doesn't perform bounds
checks...

Cheers,
Henry



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

EstebanLM
In reply to this post by Yuriy Babah
Hi,

On 26 Nov 2018, at 09:28, Yuriy Babah <[hidden email]> wrote:

Hi !

I'm trying to call a very simple function from C ++ lib, writed for the test.
function with prototype:

extern "C" float interpolationFunc(float* xm, float* ym, int size, float x).

In Pharo7 wrote:
FFIExamples class >> interpolationFunc_xm: xM ym: yM size: size x: x
    ^ self ffiCall: #(float interpolationFunc #(float * xM , float * yM , int size , float x)) module: 'libinterpolationLib.so'

in Playground i'm doing:
xm :=  FFIExternalArray externalNewType: 'float' size: 2.
ym := xm clone.
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
FFIExamples interpolationFunc_xm: xm pointer ym: ym pointer size: 2  x: 2.5 .

This is incorrect. “xm” and “ym” are already pointers (references)
When you pass "xm pointer” you are actually passing a pointer to a pointer (a float** in this case) 

This looks better:

FFIExamples 
interpolationFunc_xm: xm getHandle
ym: ym getHandle
size: 2  
x: 2.5 .

Still, you need to do something with those arrays after using them, since you are creating them with externalNewType:… (which mean it allocates the memory space).
You will need to send #free to them. 
Other solution would be to not use #externalNewType:size: but plain #newType:size:

Cheers, 
Esteban


last expression returninп me 0.0, but right is 3.5.

I'm dit the same in Python3 ctypes, and there work's fine.

In UnifiedFFI booklet is absent chapter "Arrays", anybody may help with whot i'm doing wrong?

Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

Yuriy Babah

FFIExamples 
interpolationFunc_xm: xm getHandle
ym: ym getHandle
size: 2  
x: 2.5 .

cool! it worked !! Thank Esteban Lorenzano and everyone else )

Reply | Threaded
Open this post in threaded view
|

Re: question about FFI

EstebanLM
In reply to this post by Pharo Smalltalk Users mailing list


On 26 Nov 2018, at 11:14, Henrik Sperre Johansen via Pharo-users <[hidden email]> wrote:


From: Henrik Sperre Johansen <[hidden email]>
Subject: Re: question about FFI
Date: 26 November 2018 at 11:14:26 CET


Yuriy Babah wrote
in Playground i'm doing:
*
#(2 3) doWithIndex: [:each :i | xm at: i put: each].
#(3 4) doWithIndex: [:each :i | ym at: i put: each].
*

whot i'm doing wrong?

So, you're putting 2 at index 2 of xm, and then writing 3 past end of array,
and in ym you're putting 3 and 4 at offsets past the end of array?

It's a wonder your image didn't crash, rather than return interpolation of
the uninitialized values…

He is not doing that.
He is using #doWithIndex: 

So he is passing 2 at index 1 and 3 at index 2, then 3 at index 1 and 4 at index 2.

Esteban