1
votes

I have to parallelize using openMP the serial version of a program in C to visualize a Mandelbrot set. I tried to do it but I obtain something really strange.

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

#include <unistd.h>
#include <time.h>
#include <sys/time.h>

#include "pngwriter.h"
#include "consts.h"

unsigned long get_time()
{
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return tp.tv_sec * 1000000 + tp.tv_usec;
}

int main(int argc, char** argv)
{
    png_data* pPng = png_create(IMAGE_WIDTH, IMAGE_HEIGHT);

    double x, y, x2, y2, cx, cy;
    cy = MIN_Y;

    double fDeltaX = (MAX_X - MIN_X) / (double)IMAGE_WIDTH;
    double fDeltaY = (MAX_Y - MIN_Y) / (double)IMAGE_HEIGHT;

    long nTotalIterationsCount = 0;
    unsigned long nTimeStart = get_time();

    long i, j, n;

    n = 0;
    int c;

#pragma omp parallel
    {
#pragma omp for private(i, c) reduction(+ : cx, cy)
        for (j = 0; j < IMAGE_HEIGHT; j++) {
            cx = MIN_X;

            for (i = 0; i < IMAGE_WIDTH; i++) {
                x = cx;
                y = cy;

                x2 = x * x;
                y2 = y * y;

                for (n = 0; (n < MAX_ITERS) && (x2 + y2 < 4); n++) {
                    y = 2 * x * y + cy;
                    x = x2 - y2 + cx;
                    x2 = x * x;
                    y2 = y * y;
                }

                int c = ((long)n * 255) / MAX_ITERS;
                png_plot(pPng, i, j, c, c, c);

                cx += fDeltaX;

                nTotalIterationsCount++;
            }

            cy += fDeltaY;
        }
    }

    unsigned long nTimeEnd = get_time();

    png_write(pPng, "mandel.png");
    return 0;
}

I obtain this: https://usi365-my.sharepoint.com/personal/fabbrl_usi_ch/_layouts/15/guestaccess.aspx?guestaccesstoken=d83LRC8EG1Kec%2f%2f6zwCbiHkO7%2bsuGv7JyWR%2flalvPvA%3d&docid=128ed81bef8b244d680d5651ad1afea2f&rev=1

Since this is an assignment, I am not here to ask for code. Just an explanation. Thanks.

The only odd thing about the image, is that each horizontal strip is the same, so I suggest you examine the y coordinate being handled by each process. - Weather Vane
Yes in fact it repeats the set at each line but even if I change the code considering only the y coordinate, the problems persists. Thank you. - wrong_path
There are 20 strips. Perhaps you can find that number back somewhere? - Jongware
Default visibility on openmp is shared, while c, cx, cy should be private - Regis Portalez