Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Questions about Inline::C

Reply
Thread Tools

Questions about Inline::C

 
 
January Weiner
Guest
Posts: n/a
 
      09-28-2006
Dear all,

I had to revert to C again. Since the thought of parsing all my files in C
was dreadful, I decided to use Inline::C to only do the job that was really
computationally intensive. Currently, my program seems to run quite well,
I am very happy with Inline::C, and a little coding in C made me bless
Larry again and again.

There are, however, few questions that remain open.

1. In the Inline::C cookbook, it says

> I would urge you to stay away from "malloc"ing your own buffer. Just
> use Perl's built in memory management. In other words, just create a
> new Perl string scalar. The function "newSVpv" does just that. And
> "newSVpvf" includes "sprintf" functionality.


Why? I mean, is there is any danger with malloc() from within Inline::C
that is not present in regular C programs, or is it because using malloc
is just generally tricky? I much prefer to do the allocation myself,
as I know exactly how large my matrix is and as I want to allocate the
whole matrix ( say, double 2000 x 2000 ) in one go myself. The reason is
that I fear that using perl guts for my calculations (accessing the
matrix, calculating values in it etc.) will be not much slower than a
simple C implementation. Maybe I am wrong, I haven't tested it.

Furthermore, I feel more comfortable using things that I know well, even
thought they might turn dangerous.

2. When using Inline_Stack to return a list to Perl code, I found that the
following throws a segfault:

Inline_Stack_Reset ;
Inline_Stack_Push( sv2_mortal( newSVpv( 'score' ) ) ) ;
Inline_Stack_Done ;

...while the following does not:

Inline_Stack_Reset ;
Inline_Stack_Push( sv2_mortal( newSVpv( "score" ) ) ) ;
Inline_Stack_Done ;

Should I be worried, or is it normal behaviour? (I tried to look up the
definition of Inline_Stack_Push, but I have trouble finding it).

Cheers,

January

--
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      09-28-2006
Thus spoke January Weiner (on 2006-09-28 09:5:

> I had to revert to C again. Since the thought of parsing all my files in C
> was dreadful, I decided to use Inline::C to only do the job that was really
> computationally intensive. Currently, my program seems to run quite well,
> I am very happy with Inline::C, and a little coding in C made me bless
> Larry again and again.


One question in advance: Why use Inline::C if
your C-Program is what counts. Why don't you
simply link the 'perl' to your program and
call it with the stuff you need. This is,
in large projects (imho) *much* simpler than
vice versa.

Use a _static_ PerlInterpreter*, so your
perl will keep state during whole program run.

> I much prefer to do the allocation myself,
> as I know exactly how large my matrix is and as I want to allocate the
> whole matrix ( say, double 2000 x 2000 ) in one go myself. The reason is
> that I fear that using perl guts for my calculations (accessing the
> matrix, calculating values in it etc.) will be not much slower than a
> simple C implementation. Maybe I am wrong, I haven't tested it.


Perl will be much slower if used trivially so (I can tell
you this). Just use your matrix in C as usual and make
some Into-Perl-Calls if necessary, eg. by pack(...)-ing
your variables into Perl-scalars and unpacking
them again in the perl.

Regards

Mirco
 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      09-28-2006

"January Weiner" <(E-Mail Removed)> wrote in message
..
..
> I mean, is there is any danger with malloc() from within Inline::C
> that is not present in regular C programs


Yes, I believe so - not sure of the details but I think it has something to
do with the possibility that perl has been compiled with its own malloc()
function.
Instead of malloc(), use New(). (See 'perldoc perlapi'.)

There are some *other* C functions that it's best to avoid, too - in favour
of their perl API equivalents. See 'perldoc perlclib'

> 2. When using Inline_Stack to return a list to Perl code, I found that the
> following throws a segfault:
>
> Inline_Stack_Reset ;
> Inline_Stack_Push( sv2_mortal( newSVpv( 'score' ) ) ) ;
> Inline_Stack_Done ;
>
> ...while the following does not:
>
> Inline_Stack_Reset ;
> Inline_Stack_Push( sv2_mortal( newSVpv( "score" ) ) ) ;
> Inline_Stack_Done ;
>
> Should I be worried, or is it normal behaviour? (I tried to look up the
> definition of Inline_Stack_Push, but I have trouble finding it).
>


In perl you can quote string literals inside either single or double
quotes - but in C you have to use double quotes. Using single quotes will
inevitably result in some sort of error. (I get a segfault, too.)

newSVpv() has to take 2 arguments - see (again) 'perldoc perlapi'.

Cheers,
Rob


 
Reply With Quote
 
January Weiner
Guest
Posts: n/a
 
      09-28-2006
Sisyphus <(E-Mail Removed)> wrote:
> Yes, I believe so - not sure of the details but I think it has something to
> do with the possibility that perl has been compiled with its own malloc()
> function.
> Instead of malloc(), use New(). (See 'perldoc perlapi'.)


O, thanks, that's cool, many thanks.

> In perl you can quote string literals inside either single or double
> quotes - but in C you have to use double quotes. Using single quotes will


....oh my. That is what years of writing Perl does to you Sure, I have
completly forgotten. Single quotes are for characters. Of course. Silly me


> newSVpv() has to take 2 arguments - see (again) 'perldoc perlapi'.


Typo when writing the Usenet post. I'm using newSVpvf.

Cheers,
January

--
 
Reply With Quote
 
January Weiner
Guest
Posts: n/a
 
      09-28-2006
Mirco Wahab <(E-Mail Removed)> wrote:
> One question in advance: Why use Inline::C if
> your C-Program is what counts. Why don't you


Not really. The C program does not do anything new (Needleman-Wunsch +
Smith-Waterman algorithms with one or two important modifications that you
will not find elsewhere). The Perl part is, apart from the parsing and
output formatting, Real Science (TM) It does not take as much time to
calculate, but its tricky.

> simply link the 'perl' to your program and
> call it with the stuff you need. This is,


You mean, call Perl from C and not C from Perl? Hmm, that could be a
solution as well. However, I have much more stuff (and also much more
stuff that matters) in Perl than I do in C -- for my project, I have now
only two small functions in C that do a simple job quickly. For everything
else, I have my Perl.

I am wondering, however, how complicated it is to link an existing C
library to Perl. I am sure there are dozens of examples for that, if you
know of a good one, I would love to see how it works...

> in large projects (imho) *much* simpler than
> vice versa.


Because I am lazy and because it is not a large project.

> Perl will be much slower if used trivially so (I can tell
> you this). Just use your matrix in C as usual and make
> some Into-Perl-Calls if necessary, eg. by pack(...)-ing
> your variables into Perl-scalars and unpacking
> them again in the perl.


Thanks,

January

--
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      09-28-2006
Thus spoke January Weiner (on 2006-09-28 12:25):

> I am wondering, however, how complicated it is to link an existing C
> library to Perl. I am sure there are dozens of examples for that, if you
> know of a good one, I would love to see how it works...


OK, I see your point now. You could give
a link option afaik by :

use Inline C => Config => LIBS => '-L/your/path -lyourlib';

.... according to the "Inline" docs

Did you think about PDL?

==>
http://pdl.sourceforge.net/PDLdocs/A..._a_piddle_in_c

Regards

Mirco

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      09-29-2006

"January Weiner" <(E-Mail Removed)> wrote in message
..
..
> (I tried to look up the
> definition of Inline_Stack_Push, but I have trouble finding it).
>


I meant to address this in my earlier post (but forgot).
Inline_Stack_Vars and friends are defined in INLINE.h which is a header file
auto-generated by Inline::C. It gets written in the build directory - and
then cleaned up once the compilation has successfully completed (unless you
run with the Config option 'CLEAN_AFTER_BUILD => 0').

Here's what INLINE.h contains (copied from C.pm):

#define Inline_Stack_Vars dXSARGS
#define Inline_Stack_Items items
#define Inline_Stack_Item(x) ST(x)
#define Inline_Stack_Reset sp = mark
#define Inline_Stack_Push(x) XPUSHs(x)
#define Inline_Stack_Done PUTBACK
#define Inline_Stack_Return(x) XSRETURN(x)
#define Inline_Stack_Void XSRETURN(0)

#define INLINE_STACK_VARS Inline_Stack_Vars
#define INLINE_STACK_ITEMS Inline_Stack_Items
#define INLINE_STACK_ITEM(x) Inline_Stack_Item(x)
#define INLINE_STACK_RESET Inline_Stack_Reset
#define INLINE_STACK_PUSH(x) Inline_Stack_Push(x)
#define INLINE_STACK_DONE Inline_Stack_Done
#define INLINE_STACK_RETURN(x) Inline_Stack_Return(x)
#define INLINE_STACK_VOID Inline_Stack_Void

#define inline_stack_vars Inline_Stack_Vars
#define inline_stack_items Inline_Stack_Items
#define inline_stack_item(x) Inline_Stack_Item(x)
#define inline_stack_reset Inline_Stack_Reset
#define inline_stack_push(x) Inline_Stack_Push(x)
#define inline_stack_done Inline_Stack_Done
#define inline_stack_return(x) Inline_Stack_Return(x)
#define inline_stack_void Inline_Stack_Void

Cheers,
Rob


 
Reply With Quote
 
January Weiner
Guest
Posts: n/a
 
      09-29-2006
Sisyphus <(E-Mail Removed)> wrote:
> Inline_Stack_Vars and friends are defined in INLINE.h which is a header file
> auto-generated by Inline::C. It gets written in the build directory - and
> then cleaned up once the compilation has successfully completed (unless you
> run with the Config option 'CLEAN_AFTER_BUILD => 0').


Oh, that I do know, I have found that It's just that it didn't tell me
much, because it just redefines the macros. (define inline_stack_push as
Inline_Stack_Push, and earlier Inline_Stack_Push as XPUSHs... but I still
do not know what XPUSHs is).

Anyway, this doesn't matter, as the (stupid! stupid!) error was completly
on my side.

j.

--
 
Reply With Quote
 
January Weiner
Guest
Posts: n/a
 
      09-29-2006
Mirco Wahab <(E-Mail Removed)> wrote:
> OK, I see your point now. You could give
> a link option afaik by :


> use Inline C => Config => LIBS => '-L/your/path -lyourlib';


Ah, that is nice (I should have RTFM...).

> Did you think about PDL?


> ==>
> http://pdl.sourceforge.net/PDLdocs/A..._a_piddle_in_c


And this is really nice. Many thanks! (and my program works beautifully)

January

--
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-29-2006

Quoth January Weiner <(E-Mail Removed)>:
> Sisyphus <(E-Mail Removed)> wrote:
> > Inline_Stack_Vars and friends are defined in INLINE.h which is a header file
> > auto-generated by Inline::C. It gets written in the build directory - and
> > then cleaned up once the compilation has successfully completed (unless you
> > run with the Config option 'CLEAN_AFTER_BUILD => 0').

>
> Oh, that I do know, I have found that It's just that it didn't tell me
> much, because it just redefines the macros. (define inline_stack_push as
> Inline_Stack_Push, and earlier Inline_Stack_Push as XPUSHs... but I still
> do not know what XPUSHs is).


See perlapi. IMHO those Inline_Foo macros are pretty stupid: just
renaming the XS macros for the hell of it.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Few Questions (HW questions already answered by me) padh.ayo@gmail.com C Programming 10 12-06-2006 05:48 PM
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments