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:
LIBS => ['-lgomp'], # e.g., '-lm' CCFLAGS => '-fopenmp',
At the end it compiles and runs but always with ONE thread although I am using :
export OMP_NUM_THREADS=2| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |