C 在Open MPI中处理图像块

C 在Open MPI中处理图像块,c,parallel-processing,mpi,openmpi,C,Parallel Processing,Mpi,Openmpi,我正在尝试并行化一个绘制Mandelbrot集的C程序。我将每个处理器的图像分割为大小相等的块,如图所示: 我已经尝试使用解决方案,几乎解决了同样的问题。但是,我只能在输出中获得部分图像: 同样,对于高分辨率的图像(例如8000x8000像素),应用程序会因分割错误11而崩溃。这是我的密码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

我正在尝试并行化一个绘制Mandelbrot集的C程序。我将每个处理器的图像分割为大小相等的块,如图所示:

我已经尝试使用解决方案,几乎解决了同样的问题。但是,我只能在输出中获得部分图像:

同样,对于高分辨率的图像(例如8000x8000像素),应用程序会因分割错误11而崩溃。这是我的密码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "mpi.h"

// Main program
int main(int argc, char* argv[])
 {
    /* screen ( integer) coordinate */
    int iX,iY,i,j;
    const int iXmax = 8000; // default
    const int iYmax = 8000; // default

    /* world ( double) coordinate = parameter plane*/
    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)/iXmax;
    double PixelHeight = (CyMax - CyMin)/iYmax;


    int linePerProcess, remainingLines, processMinY,  processMaxY, lastProcessMaxY, result_offset;
    int my_rank, processors, iXmaxHalf;
    int startAlert = 1;
    int receivedAlert;
    unsigned char (*resultBuffer)[3] = NULL;
    unsigned char (*resultBufferTwo)[3] = NULL;
    unsigned char (*finalResultBuffer)[3] = NULL;

    MPI_Status stat;


    /* color component ( R or G or B) is coded from 0 to 255 */
    /* it is 24 bit color RGB file */
    const int MaxColorComponentValue = 255; 
    FILE * fp;
    char *filename = "Mandelbrot.ppm";
    char *comment = "# ";   /* comment should start with # */

    // RGB color array
    unsigned char color[3];

    /* Z = Zx + Zy*i;   Z0 = 0 */
    double Zx, Zy;
    double Zx2, Zy2; /* Zx2 = Zx*Zx;  Zy2 = Zy*Zy  */
    /*  */
    int Iteration;
    const int IterationMax = 2000; // default

    /* bail-out value , radius of circle ;  */
    const double EscapeRadius = 400;
    double ER2 = EscapeRadius * EscapeRadius;
    double startTime, endTime;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &processors);

    linePerProcess = iYmax / (processors/2);
    iXmaxHalf = iXmax / 2;

    if (my_rank % 2  == 0) {
        processMinY = (my_rank/2) * linePerProcess;
    } else {
        processMinY = ((my_rank - 1)/2) * linePerProcess;
    }

    processMaxY = processMinY + linePerProcess;


    int Rows = iYmax;          // Global array rows
    int Columns = iXmax; // Global array columns
    int sizes[2];                     // No of elements in each dimension of the whole array
    int subSizes[2];                  // No of elements in each dimension of the subarray
    int startCoords[2];               // Starting coordinates of each subarray
    MPI_Datatype recvBlock, recvMagicBlock;

        // Create a subarray (a rectangular block) datatype from a regular, 2d array
    sizes[0] = Rows;
    sizes[1] = Columns;
    subSizes[0] = linePerProcess;
    subSizes[1] = iXmaxHalf;
    startCoords[0] = 0;
    startCoords[1] = 0;

    MPI_Type_create_subarray(2, sizes, subSizes, startCoords, MPI_ORDER_C, MPI_UNSIGNED_CHAR, &recvBlock);

    MPI_Type_create_resized(recvBlock, 0, iXmaxHalf * sizeof(color), &recvMagicBlock);

    MPI_Type_commit(&recvMagicBlock);

    if (my_rank == 0) {


        // startTime = MPI_Wtime();
        // for(i=1; i<processors; i++){
        //  MPI_Send(&startAlert, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        // }
        // printf("rank; %d\n", my_rank);

        finalResultBuffer = malloc(iXmax * iYmax * sizeof(color));

        for(iY = processMinY; iY < processMaxY; iY++) {

            Cy = CyMin + (iY * PixelHeight);
            if (fabs(Cy) < (PixelHeight / 2))
            {
                Cy = 0.0; /* Main antenna */
            }

            for(iX = 0; iX < iXmaxHalf; iX++)
            {

                Cx = CxMin + (iX * PixelWidth);
                /* initial value of orbit = critical point Z= 0 */
                Zx = 0.0;
                Zy = 0.0;
                Zx2 = Zx * Zx;
                Zy2 = Zy * Zy;

            /* */
                for(Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
                {
                    Zy = (2 * Zx * Zy) + Cy;
                    Zx = Zx2 - Zy2 + Cx;
                    Zx2 = Zx * Zx;
                    Zy2 = Zy * Zy;
                };

            /* compute  pixel color (24 bit = 3 bytes) */
                if (Iteration == IterationMax)
                {
                    // Point within the set. Mark it as black
                    color[0] = 0;
                    color[1] = 0;
                    color[2] = 0;
                }
                else 
                {
                    // Point outside the set. Mark it as white
                    double c = 3*log((double)Iteration)/log((double)(IterationMax) - 1.0);
                    if (c < 1)
                    {
                        color[0] = 0;
                        color[1] = 0;
                        color[2] = 255*c;
                    }
                    else if (c < 2)
                    {
                        color[0] = 0;
                        color[1] = 255*(c-1);
                        color[2] = 255;
                    }
                    else
                    {
                        color[0] = 255*(c-2);
                        color[1] = 255;
                        color[2] = 255;
                    }
                }

                finalResultBuffer[(iY*iXmaxHalf)+iX][0] = color[0];
                finalResultBuffer[(iY*iXmaxHalf)+iX][1] = color[1];
                finalResultBuffer[(iY*iXmaxHalf)+iX][2] = color[2];


            }
        }

        result_offset = 1;
        for(i=1; i<processors; i++){

            MPI_Recv(finalResultBuffer, 1, recvMagicBlock, i, 0, MPI_COMM_WORLD, &stat);
            result_offset += 1;
        }



    } else if ((my_rank % 2 == 0) && (my_rank != 0)) {

        // MPI_Recv(&receivedAlert, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat);
        // printf("rank; %d\n", my_rank);
        resultBuffer = malloc(linePerProcess * iXmaxHalf * sizeof(color));

        for(iY = processMinY; iY < processMaxY; iY++) {
            Cy = CyMin + (iY * PixelHeight);
            if (fabs(Cy) < (PixelHeight / 2))
            {
                Cy = 0.0; /* Main antenna */
            }
            for(iX = 0; iX < iXmaxHalf; iX++)
            {

                Cx = CxMin + (iX * PixelWidth);
                /* initial value of orbit = critical point Z= 0 */
                Zx = 0.0;
                Zy = 0.0;
                Zx2 = Zx * Zx;
                Zy2 = Zy * Zy;

            /* */
                for(Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
                {
                    Zy = (2 * Zx * Zy) + Cy;
                    Zx = Zx2 - Zy2 + Cx;
                    Zx2 = Zx * Zx;
                    Zy2 = Zy * Zy;
                };

            /* compute  pixel color (24 bit = 3 bytes) */
                if (Iteration == IterationMax)
                {
                    // Point within the set. Mark it as black
                    color[0] = 0;
                    color[1] = 0;
                    color[2] = 0;
                }
                else 
                {
                    // Point outside the set. Mark it as white
                    double c = 3*log((double)Iteration)/log((double)(IterationMax) - 1.0);
                    if (c < 1)
                    {
                        color[0] = 0;
                        color[1] = 0;
                        color[2] = 255*c;
                    }
                    else if (c < 2)
                    {
                        color[0] = 0;
                        color[1] = 255*(c-1);
                        color[2] = 255;
                    }
                    else
                    {
                        color[0] = 255*(c-2);
                        color[1] = 255;
                        color[2] = 255;
                    }
                }

                resultBuffer[((iY-processMinY)*iXmaxHalf)+iX][0] = color[0];
                resultBuffer[((iY-processMinY)*iXmaxHalf)+iX][1] = color[1];
                resultBuffer[((iY-processMinY)*iXmaxHalf)+iX][2] = color[2];

            }
        }
        MPI_Send(resultBuffer, linePerProcess * iXmaxHalf, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD);

        free(resultBuffer);

    } else {

        // MPI_Recv(&receivedAlert, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat);
        // printf("rank; %d\n", my_rank);
        resultBufferTwo = malloc(linePerProcess * iXmaxHalf * sizeof(color));

        for(iY = processMinY; iY < processMaxY; iY++) {
            Cy = CyMin + (iY * PixelHeight);
            if (fabs(Cy) < (PixelHeight / 2))
            {
                Cy = 0.0; /* Main antenna */
            }
            for(iX = iXmaxHalf; iX < iXmax; iX++)
            {

                Cx = CxMin + (iX * PixelWidth);
                /* initial value of orbit = critical point Z= 0 */
                Zx = 0.0;
                Zy = 0.0;
                Zx2 = Zx * Zx;
                Zy2 = Zy * Zy;

            /* */
                for(Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
                {
                    Zy = (2 * Zx * Zy) + Cy;
                    Zx = Zx2 - Zy2 + Cx;
                    Zx2 = Zx * Zx;
                    Zy2 = Zy * Zy;
                };

            /* compute  pixel color (24 bit = 3 bytes) */
                if (Iteration == IterationMax)
                {
                    // Point within the set. Mark it as black
                    color[0] = 0;
                    color[1] = 0;
                    color[2] = 0;
                }
                else 
                {
                    // Point outside the set. Mark it as white
                    double c = 3*log((double)Iteration)/log((double)(IterationMax) - 1.0);
                    if (c < 1)
                    {
                        color[0] = 0;
                        color[1] = 0;
                        color[2] = 255*c;
                    }
                    else if (c < 2)
                    {
                        color[0] = 0;
                        color[1] = 255*(c-1);
                        color[2] = 255;
                    }
                    else
                    {
                        color[0] = 255*(c-2);
                        color[1] = 255;
                        color[2] = 255;
                    }
                }


                resultBufferTwo[((iY-processMinY)*iXmaxHalf)+(iX - iXmaxHalf)][0] = color[0];
                resultBufferTwo[((iY-processMinY)*iXmaxHalf)+(iX - iXmaxHalf)][1] = color[1];
                resultBufferTwo[((iY-processMinY)*iXmaxHalf)+(iX - iXmaxHalf)][2] = color[2];
                // printf("rank: %d - value: %u%u%u\n", my_rank,resultBufferTwo[((iY-processMinY)*iXmax)+iX][0],resultBufferTwo[((iY-processMinY)*iXmax)+iX][1],resultBufferTwo[((iY-processMinY)*iXmax)+iX][2]);

            }
        }
        MPI_Send(resultBufferTwo, iXmaxHalf * linePerProcess, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD);

        free(resultBufferTwo);

    }

    if (my_rank == 0) {

        endTime = MPI_Wtime();
        printf("Process time (s): %lf\n", endTime - startTime);
        /*create new file,give it a name and open it in binary mode  */
        fp = fopen(filename, "wb"); /* b -  binary mode */

        /*write ASCII header to the file (PPM file format)*/
        fprintf(fp,"P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
        for(iY = 0; iY < iYmax; iY++)
        {
            for(iX = 0; iX < iXmax; iX++)
                {
                    fwrite(finalResultBuffer[(iY*iXmax)+iX], 1, 3, fp);
                }
        }

        fclose(fp);

        free(finalResultBuffer);
    }

    MPI_Finalize();
    return 0;
 }
#包括
#包括
#包括
#包括
#包括
#包括“mpi.h”
//主程序
int main(int argc,char*argv[])
{
/*屏幕(整数)坐标*/
int iX,iY,i,j;
const int iXmax=8000;//默认值
const int iYmax=8000;//默认值
/*世界(双)坐标=参数平面*/
双Cx,Cy;
常数双CxMin=-2.5;
常数双CxMax=1.5;
常数双CyMin=-2.0;
常数双CyMax=2.0;
/* */
双像素宽度=(CxMax-CxMin)/iXmax;
双像素高度=(CyMax-CyMin)/iYmax;
int linePerProcess、remainingLines、processMinY、processMaxY、lastProcessMaxY、result\u offset;
int my_秩,处理器,iXmaxHalf;
int-startAlert=1;
内部接收数据;
无符号字符(*resultBuffer)[3]=NULL;
无符号字符(*resultBufferTwo)[3]=NULL;
无符号字符(*finalResultBuffer)[3]=NULL;
MPI_状态统计;
/*颜色分量(R或G或B)的编码范围为0到255*/
/*它是24位彩色RGB文件*/
常量int MaxColorComponentValue=255;
文件*fp;
char*filename=“Mandelbrot.ppm”;
char*comment=“#”;/*注释应以#开头*/
//RGB彩色阵列
无符号字符颜色[3];
/*Z=Zx+Zy*i;Z0=0*/
双Zx,Zy;
双Zx2,Zy2;/*Zx2=Zx*Zx;Zy2=Zy*Zy*/
/*  */
整数迭代;
const int IterationMax=2000;//默认值
/*脱模值、圆半径*/
常数双逃逸半径=400;
双ER2=逃逸半径*逃逸半径;
双开始时间,结束时间;
MPI_Init(&argc,&argv);
MPI通信等级(MPI通信世界和我的通信等级);
MPI_通信大小(MPI_通信世界和处理器);
linePerProcess=iYmax/(处理器/2);
iXmaxHalf=iXmax/2;
如果(我的排名%2==0){
processMinY=(我的排名/2)*linePerProcess;
}否则{
processMinY=((我的排名-1)/2)*linePerProcess;
}
processMaxY=processMinY+linePerProcess;
int Rows=iYmax;//全局数组行
int Columns=iXmax;//全局数组列
int size[2];//整个数组的每个维度中的元素数
int subfizes[2];//子数组每个维度中的元素数
int startcoods[2];//每个子数组的起始坐标
MPI_数据类型recvBlock、recvMagicBlock;
//从常规二维数组创建子数组(矩形块)数据类型
大小[0]=行;
大小[1]=列;
subfizes[0]=linePerProcess;
补贴[1]=iXmaxHalf;
startCoords[0]=0;
startCoords[1]=0;
MPI\u类型\u创建\u子数组(2,大小、子块、起始字、MPI\u顺序\u C、MPI\u无符号字符和recvBlock);
MPI_类型_创建_大小调整(recvBlock、0、iXmaxHalf*sizeof(颜色)和recvMagicBlock);
MPI_类型_提交(&recvMagicBlock);
如果(我的排名==0){
//startTime=MPI_Wtime();
//对于(i=1;i
Q:我做错了什么

  • 代码不计算复杂平面上的分形,只复制常量
    color[]
  • my_rank==0
    处理收集其他人生成的结果(分形生成器是迭代的,因此在复平面中每个
    [X,iY]
    -点有不同的运行时间),并在有指示的情况下,自身存储来自
    颜色[]=[0,0]的未初始化/未修改的相同常量值的常量值
    到整个
    finalResultBuffer[][]
  • 您没有像上面声明的那样在P1-P6之间分割工作,但是您的代码为三种情况分割代码执行路径-1)my_rank==0
(“主程序”收集结果(它本身不计算cmplex迭代器的一部分,而是在指定的位置存储黑点)并写出一个文件),2)
我的秩%2==0
(所有非零偶数秩,没有一个真正计算复杂分形迭代器的单步,并且所有这些秩都在任何地方存储黑点),3)
我的秩%2==1
(所有奇数秩,没有一个可以一步计算出复杂分形迭代器,但所有的秩都在各处存储黑点) Q:我做错了什么

  • 代码不计算复杂平面上的分形,只复制常量
    color[]
  • my_rank==0
    处理收集其他人生成的结果(分形生成器是迭代的,因此在复平面中每个
    [X,iY]
    -点有不同的运行时间),并在有指示的情况下,自身存储来自
    颜色[]=[0,0]的未初始化/未修改的相同常量值的常量值
    到整个
    finalResultBuffer[][]
  • 您没有像上面声明的那样在P1-P6之间分割工作,但是您的代码为三种情况分割代码执行路径-1)my_rank==0(“主程序”收集结果(它本身不计算cmplex迭代器的一部分,而是在指定的位置存储黑点)并写出一个文件),2)
    我的秩%2==0
    (所有非零偶数秩,没有一个真正计算复杂分形迭代器的单步,所有都在每个地方存储黑点),3)
    我的秩%2==1
    (所有奇数秩,没有一个真正计算复杂fra的单步