openMP和Mandelbrot在C中

时间:2016-10-30 13:10:33

标签: c openmp mandelbrot

我必须使用openMP并行化C中程序的串行版本来可视化Mandelbrot集。我试图这样做,但我得到了一些非常奇怪的东西。

#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;
}

我得到了这个: https://usi365-my.sharepoint.com/personal/fabbrl_usi_ch/_layouts/15/guestaccess.aspx?guestaccesstoken=d83LRC8EG1Kec%2f%2f6zwCbiHkO7%2bsuGv7JyWR%2flalvPvA%3d&docid=128ed81bef8b244d680d5651ad1afea2f&rev=1

由于这是一项任务,我不是要求代码。只是一个解释。感谢。

0 个答案:

没有答案