1

I have a function which calculate mandelbrot set I'm trying to parallelize it using openMP.

I put #pragma omp parallel for private before each for

static void calculer (Image * im, int nb_iter, double x_min, double x_max, double y_min, double y_max) {

    /* Discretisation de l'ensemble */
    double pasx = (x_max - x_min) / im -> nb_col;
    double pasy = (y_max - y_min) / im -> nb_lig;
    double cy = y_min;
    double new_zx;
    unsigned int l,c;
    // Calcul
    #pragma omp parallel for private ( pasx, pasy, im,nb_iter,x_min,x_max,y_min, y_max)
    for (l = 0; l < im->nb_lig; l++) {
         double cx = x_min;
        #pragma omp parallel for private (cx)

        for (c = 0; c < im->nb_col; c++) {
            double zx = 0.0;
            double zy = 0.0;
            unsigned int n = 0;
            while (  ( zx*zx + zy*zy < 4.0 ) && ( n < nb_iter ) ) {
                new_zx = zx*zx - zy*zy + cx;
                zy = 2.0*zx*zy + cy;
                zx = new_zx;
                ++n;
            }  
            im->pixels[l*im->nb_col + c] = n%256;
            cx += pasx; 
        }
        cy += pasy;
    }
}

When compiling using gcc mandelbrot.c -fopenmp -o exe, I get segmentation fault. What could be the possible reason for it?

EDIT: After executing vingrid I get this message

 Process terminating with default action of signal 11 (SIGSEGV)
==10689==  Access not within mapped region at address 0x0
==10689==    at 0x40105F: calculer._omp_fn.0 (in /home/haddad/Documents/TPOpenMP/TP_OpenMP/TP_Mandelbrot/exe3)
==10689==    by 0x4E39EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==10689==    by 0x5047E99: start_thread (pthread_create.c:308)
==10689==    by 0x535038C: clone (clone.S:112)
==10689==  If you believe this happened as a result of a stack
==10689==  overflow in your program's main thread (unlikely but
==10689==  possible), you can try to increase the size of the
==10689==  main thread stack using the --main-stacksize= flag.
==10689==  The main thread stack size used in this run was 8388608.
vikash78
  • 19
  • 4
  • Have you tried using valgrind? – Lolmewn Jan 06 '16 at 16:55
  • Are you sure you wanted `im` to be private? Looks iffy to me... – R_Kapp Jan 06 '16 at 16:56
  • I'm begging in OpenMP, I'm just putting all variables that I'll use – vikash78 Jan 06 '16 at 17:02
  • 1
    Do you mean parallelise? To paralyse [means](http://dictionary.cambridge.org/us/dictionary/english/paralyse). – pat Jan 06 '16 at 17:10
  • Yes sorry I'm not a good english speaker – vikash78 Jan 06 '16 at 17:15
  • 1
    I haven't used OpenMP enough to be sure about this, but don't you need to specify all variables as private in the *second* pragma as well? Also, the `c` variable should probably be private -- you don't want that shared between threads. I don't think this is the cause of your crash (which seems to be a NULL pointer access..?), but you should really be careful about that stuff when multithreading. Also, maybe you could get a more useful callstack if you add `-g` when compiling (which makes the compiler include debug symbols). – Snild Dolkow Jan 06 '16 at 17:27
  • You may also be interested in the code here, which gets extra speed-ups via SIMD vectorization: https://stackoverflow.com/questions/48069990/multithreaded-simd-vectorized-mandelbrot-in-r-using-rcpp-openmp – Tom Wenseleers Jan 24 '18 at 16:21

1 Answers1

6

I went ahead and cleaned up your code and even made an image from it.

#include <stdlib.h>
#include <stdio.h>

struct Image {
    unsigned nb_lig;
    unsigned nb_col;
    unsigned *pixels;
};

void calculer (struct Image * im, unsigned nb_iter, double x_min, double x_max, double y_min, double y_max) {
    double pasx = (x_max - x_min) / im -> nb_col;
    double pasy = (y_max - y_min) / im -> nb_lig;
    unsigned l,c;
    #pragma omp parallel for private (c)
    for (l = 0; l < im->nb_lig; l++) {
        for (c = 0; c < im->nb_col; c++) {
            double zx = 0.0, zy = 0.0, new_zx;
            double cx = x_min + c*pasx, cy = y_min + l*pasy;
            unsigned n = 0;
            for(n=0;  (zx*zx + zy*zy < 4.0 ) && ( n < nb_iter ); n++ ) {
                new_zx = zx*zx - zy*zy + cx;
                zy = 2.0*zx*zy + cy;
                zx = new_zx;
            }
            if(n == nb_iter) n = 0;
            im->pixels[l*im->nb_col + c] = n;
        }
    }
}

void draw_image(struct Image *im) {
    const char charset[] = ".,c8M@jawrpogOQEPGJ";
    unsigned l,c;
    for (l = 0; l < im->nb_lig; l++) {
        for (c = 0; c < im->nb_col; c++) {
            unsigned n = im->pixels[l*im->nb_col + c];
            char p = n > 0 ? charset[n % (sizeof(charset)-1)] : ' ';
            putchar(p);
            if(c+1 == im->nb_col) puts("");
        }
    }
    puts("");
}

int main(void) {
    struct Image im;
    im.nb_lig = 40;
    im.nb_col = 80;
    im.pixels = malloc(sizeof *im.pixels * im.nb_lig*im.nb_col);
    unsigned nb_iter = 256;
    calculer(&im, nb_iter, -2.5, 1.5, -2.0, 2.0);
    draw_image(&im);
    return 0;
}

The output is

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccc,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccc,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccc,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccc,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccc,,,
,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc,
,,,,,,,,,,,,,,,,,,,,ccccccccccccccc88888888888cccccccccccccccccccccccccccccccccc
,,,,,,,,,,,,,,,,,,,ccccccccc8888888888888888888888888ccccccccccccccccccccccccccc
,,,,,,,,,,,,,,,,,ccccccc8888888888888888MMMMM@.w@@MMM8888ccccccccccccccccccccccc
,,,,,,,,,,,,,,,,ccccc8888888888888888MMMMMM@@jaoro @MMMM8888cccccccccccccccccccc
,,,,,,,,,,,,,,,ccc8888888888888888MMMMMMM@@@jwrG@owj@@MMMM88888ccccccccccccccccc
,,,,,,,,,,,,,,cc8888888888888888MMMMMMM@@jjawQ    Jwj@@@@MM888888ccccccccccccccc
,,,,,,,,,,,,,cc88888888888888MMMMMM@@jawwwwrpQ    OprwjjjJ@MM88888cccccccccccccc
,,,,,,,,,,,,cc8888888888888MMMM@@@@jjagM Pa          ,gQEPE@M888888ccccccccccccc
,,,,,,,,,,,,c88888888888MM@@@@@@@jjjwQg@                 ,aj@M888888cccccccccccc
,,,,,,,,,,,c8888888MMM@@agaaaaaaaaawo,                   Gr.@MM888888ccccccccccc
,,,,,,,,,,,888MMMMMM@@@japP,gOPOorro@                     EwjMM8888888cccccccccc
,,,,,,,,,,,8MMMMMM@@@@jawoJ       EP                      ga@MMM888888cccccccccc
,,,,,,,,,,,MMMMMjjjjawgOQ8         Q                      wj@MMM888888cccccccccc
,,,,,,,,,,,                                             gwaj@MMM888888cccccccccc
,,,,,,,,,,,MMMMMjjjjawgOQ8         Q                      wj@MMM888888cccccccccc
,,,,,,,,,,,8MMMMMM@@@@jawoJ       EP                      ga@MMM888888cccccccccc
,,,,,,,,,,,888MMMMMM@@@japP,gOPOorro@                     EwjMM8888888cccccccccc
,,,,,,,,,,,c8888888MMM@@agaaaaaaaaawo,                   Gr.@MM888888ccccccccccc
,,,,,,,,,,,,c88888888888MM@@@@@@@jjjwQg@                 ,aj@M888888cccccccccccc
,,,,,,,,,,,,cc8888888888888MMMM@@@@jjagM Pa          ,gQEPE@M888888ccccccccccccc
,,,,,,,,,,,,,cc88888888888888MMMMMM@@jawwwwrpQ    OprwjjjJ@MM88888cccccccccccccc
,,,,,,,,,,,,,,cc8888888888888888MMMMMMM@@jjawQ    Jwj@@@@MM888888ccccccccccccccc
,,,,,,,,,,,,,,,ccc8888888888888888MMMMMMM@@@jwrG@owj@@MMMM88888ccccccccccccccccc
,,,,,,,,,,,,,,,,ccccc8888888888888888MMMMMM@@jaoro @MMMM8888cccccccccccccccccccc
,,,,,,,,,,,,,,,,,ccccccc8888888888888888MMMMM@.w@@MMM8888ccccccccccccccccccccccc
,,,,,,,,,,,,,,,,,,,ccccccccc8888888888888888888888888ccccccccccccccccccccccccccc
,,,,,,,,,,,,,,,,,,,,ccccccccccccccc88888888888cccccccccccccccccccccccccccccccccc
,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc,
,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccc,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccc,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccc,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccc,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccc,,,,,,,,,,,,,,,,,
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • Nice, but where is the free ? – Adam Mar 25 '16 at 12:19
  • @Adam, you mean `free(im.pixels)`? It's not necessary. – Z boson Mar 25 '16 at 14:35
  • Valgrind LEAK SUMMARY: definitely lost: 12,800 bytes in 1 block – Adam Mar 25 '16 at 18:48
  • @Adam, [All modern OS will free the memory so there is no leak](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc). But feel free to free it yourself (no pun intended) if it bothers you so much. – Z boson Mar 25 '16 at 19:11