在openmp中进行Mandelbrot优化

那么我不得不在C中使用mandelbrot程序。我认为我做得很好,我不能得到更好的时间。 我的问题,如果有人有想法改进代码,我一直在考虑可能在外部和内部的嵌套并行区域...

另外,我怀疑它是否更优雅,或者建议将所有编译指令放在一行中,还是编写单独的编译指示(一个用于omp并行和共享以及私有变量和一个条件,另一个用于omp和schedule dynamic的编译指示)。

我怀疑常量是否可以用作私有变量,因为我认为使用常量而不是定义变量更清晰。

另外我写了一个条件(如果numcpu> 1),它没有意义使用并行区域并进行正常的顺序执行。

最后,因为我已经阅读过动态块,它取决于硬件和系统配置......所以我已经将它作为一个常量,所以它可以很容易地更改。

此外,我调整线程数量的处理器数量..

 int main(int argc, char *argv[])
{
    omp_set_dynamic(1);

    int xactual, yactual;

    //each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin
    double pr, pi;                   //real and imaginary part of the pixel p
    double newRe, newIm, oldRe, oldIm;   //real and imaginary parts of new and old z
    double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position

    pixel_t *pixels = malloc(sizeof(pixel_t)*IMAGEHEIGHT*IMAGEWIDTH);
    clock_t begin, end;
    double time_spent;    

    begin=clock();

    int numcpu;
    numcpu = omp_get_num_procs();

    //FILE * fp;
    printf("El número de procesadores que utilizaremos es: %d", numcpu);

    omp_set_num_threads(numcpu);

    #pragma omp parallel shared(pixels, moveX, moveY, zoom) private(xactual, yactual, pr, pi, newRe, newIm) (if numcpu>1)
    {
        //int xactual=0;
    //  int yactual=0;
        #pragma omp for  schedule(dynamic, CHUNK)       

    //loop through every pixel
        for(yactual = 0; yactual < IMAGEHEIGHT; yactual++)
            for(xactual = 0; xactual < IMAGEWIDTH; xactual++)
            {
                //calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
            pr = 1.5 * (xactual - IMAGEWIDTH / 2) / (0.5 * zoom * IMAGEWIDTH) + moveX;
            pi = (yactual - IMAGEHEIGHT / 2) / (0.5 * zoom * IMAGEHEIGHT) + moveY;
            newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0
            //"i" will represent the number of iterations
            int i;
            //start the iteration process
            for(i = 0; i < ITERATIONS; i++)
            {
                //remember value of previous iteration
                oldRe = newRe;
                oldIm = newIm;
                //the actual iteration, the real and imaginary part are calculated
                newRe = oldRe * oldRe - oldIm * oldIm + pr;
                newIm = 2 * oldRe * oldIm + pi;
                //if the point is outside the circle with radius 2: stop
                if((newRe * newRe + newIm * newIm) > 4) break;
            }

            //            color(i % 256, 255, 255 * (i < maxIterations));
            if(i == ITERATIONS)
            {
                //color(0, 0, 0); // black
                pixels[yactual*IMAGEWIDTH+xactual][0] = 0;
                pixels[yactual*IMAGEWIDTH+xactual][1] = 0;
                pixels[yactual*IMAGEWIDTH+xactual][2] = 0;
            }
            else
            {
                double z = sqrt(newRe * newRe + newIm * newIm);
                int brightness = 256 * log2(1.75 + i - log2(log2(z))) / log2((double)ITERATIONS);

                //color(brightness, brightness, 255)
                pixels[yactual*IMAGEWIDTH+xactual][0] = brightness;
                pixels[yactual*IMAGEWIDTH+xactual][1] = brightness;
                pixels[yactual*IMAGEWIDTH+xactual][2] = 255;
            }      

      }

    }   //end of parallel region

    end= clock();

    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    fprintf(stderr, "Elapsed time: %.2lf seconds.n", time_spent);

您可以扩展实现以利用SIMD扩展。 据我所知,最新的OpenMP标准包含矢量结构。 查看描述这些新功能的这篇文章。

本白皮书解释了在计算Mandelbrot集时如何使用SSE3。

链接地址: http://www.djcxy.com/p/79223.html

上一篇: Mandelbrot optimization in openmp

下一篇: How to assign a specific job to each thread for matrix addition in openmp