Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Exploring Inline::C (Generating primes)

by davido (Archbishop)
on Oct 26, 2011 at 21:43 UTC ( #934000=note: print w/ replies, xml ) Need Help??


in reply to Re: Exploring Inline::C (Generating primes)
in thread Exploring Inline::C (Generating primes)

The first test works fine, the second one ends with "Out of memory" after a long, painful swap to disk

Actually now that I re-read the description of your problem I have to conclude it probably has nothing at all to do with Inline::C. The second test is comparing the results of the pure Perl sub to the results of the sub that invokes an external program. That external program is the C++ program whos source I provided in my example.

That program is not seen or touched by Inline::C. Inline::C doesn't know or care about it.

The C++ program has to be saved in its own C++ source file, compiled, and linked to a filename that makes sense for your operating system. In fact whether it's Win32 or Linux, there's no harm in having an .exe suffix in this simple test case. Just compile the C++ source into an executable named 'primes.exe', and put it wherever the script will be able to find it (or specify a fully qualified path within the Perl script's call to primes.exe).

Maybe you've already done all this. If you haven't, go ahead and do it now.

The next step in sleuthing what's going wrong would be to invoke primes.exe (the executable built from the C++ source I provided). Invoke it with some smallish integer, like primes.exe 100. Then try it as primes.exe 100000, and again as primes.exe 1000000.

If it runs properly in all those cases, there's no sane reason it shouldn't work from within your Perl script. If it doesn't run properly, perhaps you have a C++ compiler and library that lacks the Standard Template Library, which is a necessary component of the C++ program.

If all else fails, you ought to be able to just remove the 2nd test, and remove all mention of the external_cpp() sub within the Perl script (remove the function's declaration/definition, and its use within the cmpthese() benchmark). Then you'll only be comparing the Inline::C function with the pure Perl one; you're taking the C++ program out of the equation.

Hope this helps...


Dave


Comment on Re^2: Exploring Inline::C (Generating primes)
Select or Download Code
Re^3: Exploring Inline::C (Generating primes)
by wazoox (Prior) on Oct 27, 2011 at 21:07 UTC

    I've recompiled everything from the start, and it works somewhat better, though not very well in the end :) The c++ code goes well enough, but the Inline::C fails miserably:

    perl test.pl 1..3 /usr/local/bin/perl /usr/local/lib/perl5/5.14.0/ExtUtils/xsubpp -type +map /usr/local/lib/perl5/5.14.0/ExtUtils/typemap test_pl_97a8.xs > +test_pl_97a8.xsc && mv test_pl_97a8.xsc test_pl_97a8.c cc -c -I/home/emmanuel -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasin +g -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D +_FILE_OFFSET_BITS=64 -O2 -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\" - +fPIC "-I/usr/local/lib/perl5/5.14.0/i686-linux-thread-multi/CORE" t +est_pl_97a8.c test_pl_97a8.xs:6:3: erreur: directive de pré-traitement #Here invalid +e test_pl_97a8.xs:33:3: erreur: directive de pré-traitement #Cross inval +ide make: *** [test_pl_97a8.o] Erreur 1 A problem was encountered while attempting to compile and install your + Inline C code. The command that failed was: make > out.make 2>&1 The build directory was: /home/emmanuel/_Inline/build/test_pl_97a8 To debug the problem, cd to the build directory, and inspect the outpu +t files. at test.pl line 0 INIT failed--call queue aborted. # Looks like your test exited with 2 before it could output anything.

      That's not so miserable. ;) In fact, it's pretty much how any error message looks with Inline::C.

      Open the .xs file in the directory pointed to in the error message. Find the line mentioned in the error message (lines 6 and 33, I think). There is a problem somewhere nearby. Don't edit the XS file, edit the same line (but different line number) in your Perl/C source file.


      Dave

        OK, I see : the perl comments are included in the C source, and of course a line beginning with a # has unexpected effects there :) So adding a // in front of lines 137 and 164 saves the day:

        perl test.pl
        1..3
        ok 1 - The first 501 primes are found from 1 to 3571.
        ok 2 - external_cpp() function gives same results as basic_perl().
        ok 3 - inline_c() function gives same results as basic_perl().
        # 
        # Comparing basic_perl(), external_cpp(), and inline_c() for
        # 5 seconds searching 150000 integers.
        # 
                        Rate   basic_perl external_cpp     inline_c
        basic_perl   0.984/s           --         -90%         -96%
        external_cpp  9.45/s         860%           --         -58%
        inline_c      22.6/s        2197%         139%           --
        # 
        # I love it when a plan comes together.
        # 
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://934000]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-07-30 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls