Hi perl monks, I am new to Perl but I could write a program that computes a mandelbrot set. I wanted to make it more efficient by using one of the three technologies XS, SWIG or Inline::C.

So I compiled in each case using or indicating the '-fopenmp' flags and '-lgomp' library but for the XS and inline::C methods it doesn't work, I mean even if I set the OMP_NUM_THREADS variable to a value greater than one, I always have one thread running

For SWIG I could see two or 4 threads running.

Here is for example the code for XS in the file mandelbrot.c and mandelbrot.h that I put in the Mandelbrot directory

#include <stdio.h> #include <stdlib.h> #include <omp.h> int escapes(double cr, double ci, int it) { double zr = 0; double zi = 0; double zrtmp; int i; for(i=0; i<it; i++) { // z <- z^2 + c zrtmp = zr*zr - zi*zi + cr; zi = 2*zr*zi + ci; zr = zrtmp; if (zr*zr + zi*zi > 4) { return 1; } } return 0; } void mandel(double xmin, double xmax, int xstep, double ymin, double y +max, int ystep, int iters) { int yc; // array of string to store result char *m = (char *) malloc(ystep * (xstep + 1) * sizeof(char)); #pragma omp parallel for for(yc=0; yc<ystep; yc++) { double y = yc*(ymax-ymin)/ystep + ymin; int xc; for(xc=0; xc<xstep; xc++) { double x = xc*(xmax-xmin)/xstep + xmin; escapes(x, y, iters); if (escapes(x, y, iters)) { m[yc * (xstep + 1) + xc] = ' '; } else { m[yc * (xstep + 1) + xc] = 'X'; } } // add end of string m[yc * (xstep+1) + xstep] = '\0'; } for(yc=0; yc<ystep; yc++) { printf("%s\n", &m[yc * (xstep+1)]); } free(m); }
// mandelbrot.h void mandel(double xmin, double xmax, int xstep, double ymin, double y +max, int ystep, int iters);

And here the mandelbrot_xs.pl that uses the Mandelbrot module :

use warnings; use Mandelbrot; Mandelbrot::mandel(-2.0, 1.0, 256, -1.0, 1.0, 256, 100000);

I followed the following steps:

  1. h2xs -n Mandelbrot -O -x -F '-I..' Mandelbrot/mandelbrot.h
  2. cd Mandelbrot
  3. I modified Makefile.PL with
    LIBS => ['-lgomp'], # e.g., '-lm' CCFLAGS => '-fopenmp',
  4. perl Makefile.PL
  5. make
  6. make install
  7. cd ..
  8. perl mandelbrot_xs.pl

At the end it compiles and runs but always with ONE thread although I am using :

export OMP_NUM_THREADS=2

In reply to Why openMP is not taken into account by XS and inline::C by jmricher70

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.