我必须使用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;
}
由于这是一项任务,我不是要求代码。只是一个解释。感谢。