有人可以帮我改进这段代码并给我一些提示。我试图自己创建一个 OpenMP 版本的 Mandelbrot 集。我是 OpenMP 初学者,在这里我没有加快速度,这可能是因为#pragma omp critical
但我现在想不出更好的主意。
int main()
{
// picture resolution
int iX,iY;
const int ImageWidth = 1000;
const int ImageHeight = 1000;
double Cx,Cy;
const double CxMin=-2.5;
const double CxMax=1.5;
const double CyMin=-2.0;
const double CyMax=2.0;
double PixelWidth=(CxMax-CxMin)/ImageWidth; /* scaled x coordinate of pixel (must be scaled to lie somewhere in the Mandelbrot
X scale (-2.5, 1.5) */
double PixelHeight=(CyMax-CyMin)/ImageHeight;/* scaled y coordinate of pixel (must be scaled to lie somewhere in the Mandelbrot
Y scale (-2.0, 2.0) */
const int MaxColorComponentValue=255;
FILE * fp;
char *filename="MandelbrotSet.ppm";
char *comment="# ";// comment in ppm picture should start with #
unsigned char color[3]; // colors [R, G ,B]
// Z=Zx+Zy*i ; Z0 = 0
double Zx, Zy;
double Zx2, Zy2; // Zx2=Zx*Zx; Zy2=Zy*Zy
int Iteration;
const int IterationMax=150;
const double Bailout=2; // bail-out value
double Circle_Radius=Bailout*Bailout; // circle radius
fp= fopen(filename,"wb");
//write the header to the picture file
fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",
comment,ImageWidth,ImageHeight,MaxColorComponentValue);
// For each pixel on the screen do:
// initialize_timer ( );
// start_timer ( );
omp_set_dynamic(1);
omp_set_num_threads(4);
#pragma omp parallel /*reduced(>:Circle_Radius)*/
{
#pragma omp for private(iY,iX,Iteration,Zx,Zy,Zx2,Zy2,color) \
schedule(dynamic) //or runtime
for(iY=0;iY<ImageHeight;iY++)
{
Cy=CyMin + iY*PixelHeight;
if (fabs(Cy)< PixelHeight/2) Cy=0.0; // Main antenna
#pragma omp critical
for(iX=0;iX<ImageWidth;iX++)
{
Cx=CxMin + iX*PixelWidth;
Zx=0.0;
Zy=0.0;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
/* */
for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<Circle_Radius);Iteration++)
{
Zy=2*Zx*Zy + Cy;
Zx=Zx2-Zy2 +Cx;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
};
if (Iteration==IterationMax)
{ // interior of Mandelbrot set = black
color[0]=0;
color[1]=0;
color[2]=0;
}
//
else{
color[0]=180;
color[1]=0;
color[2]=0;
// Gradient((double)(Iteration-log2(log2(sqrt(Zx2+Zy2))))/IterationMax,color);
}
fwrite(color,1,3,fp);
}
}
}
fclose(fp);
// stop_timer ( );
//
// printf("Elapsed time: %lf\n",elapsed_time ( ));
return 0;
}