簡體   English   中英

MandelBrot Set應用程序中的C X11分段錯誤

[英]C X11 Segmentation Fault in MandelBrot Set Application

我一生都找不到這個段錯誤。 啟動后,它一直在從屬進程中發生。 我已經開發了一個可以正常工作的標准應用程序,但是當我嘗試實現縮放功能時遇到了這個問題。

任何幫助是極大的贊賞。

我的部分代碼:

int rank, processes, i, j, x, y;

MPI_Status status;
MPI_Init(&argc, &argv); 
MPI_Comm_size(MPI_COMM_WORLD, &processes); 
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

Window win; // initialization for a window
GC gc; // graphics context
Display *display = NULL;
unsigned int width = X_RESN, height = Y_RESN; /* window size */
clock_t start, end, elapsed;

int changed = 1;

int* pixelChunk;
int** pixelSet;

if(rank==0)
{
    display = x11setup(&win, &gc, width, height);
    pixelSet = create2DArray(X_RESN, Y_RESN);

    int i;

    double pixelFactors[4];
    pixelFactors[0] = imin;
    pixelFactors[1] = imax;
    pixelFactors[2] = rmin;
    pixelFactors[3] = rmax;

    for(i = 1; i < processes - 1; i++)
    {
        MPI_Send(&pixelFactors, sizeof(double) * 4, MPI_DOUBLE, i, PIXEL_FACTORS, MPI_COMM_WORLD);
        assignTask(i);
    }
}
else
{
    pixelChunk = malloc(sizeof(int) * (Y_RESN+1));
}

// main loop
int running = 1; // loop variable
start = clock();
while(running) 
{
    if(rank==0) 
    {
        if(XPending(display)) 
        {
            XEvent ev;
            XNextEvent(display, &ev);
            switch(ev.type) 
            {               
                case ButtonPress:
                {
                    int xPos = ev.xbutton.x;
                    int yPos = ev.xbutton.y;

                    int xCenter = ((xPos / X_RESN) * (rmax - rmin)) + rmin;
                    int yCenter = ((yPos / Y_RESN) * (imax - imin)) + imin;

                    double xmin = xCenter - 2;
                    double xmax = xCenter + 2;
                    double ymin = yCenter - 2;
                    double ymax = yCenter + 2;

                    double pixelFactors[4];

                    if(ev.xbutton.button == 1) //Left click, zoom in
                    {
                        xmin += 0.1;
                        xmax -= 0.1;
                        ymin += 0.1;
                        ymax -= 0.1;
                    }
                    else if(ev.xbutton.button == 2) //Right click, zoom out
                    {
                        xmin -= 0.1;
                        xmax += 0.1;
                        ymin -= 0.1;
                        ymax += 0.1;
                    }

                    pixelFactors[0] = ymin;
                    pixelFactors[1] = ymax;
                    pixelFactors[2] = xmin;
                    pixelFactors[3] = xmax;

                    tasks = X_RESN-1;
                    changed = 1;

                    for(i = 1; i < processes - 1; i++)
                    {
                        MPI_Send(&pixelFactors, sizeof(double) * 4, MPI_DOUBLE, i, PIXEL_FACTORS, MPI_COMM_WORLD);
                        assignTask(i);
                    }
                }
            }
        }
    }
    else
    {
        //Calculate MandelBrot Set
        int taskNo;
        double pixelFactors[4];

        MPI_Recv(&pixelFactors, sizeof(double) * 4, MPI_DOUBLE, 0, PIXEL_FACTORS, MPI_COMM_WORLD, &status);
        MPI_Recv(&taskNo, sizeof(int), MPI_INT, 0, TASK_ASSIGNMENT, MPI_COMM_WORLD, &status);

        imin = pixelFactors[0];
        imax = pixelFactors[1];
        rmin = pixelFactors[2];
        rmax = pixelFactors[3];

        int x, y;
        complex c;

        double xPixelFactor = (rmax-rmin)/X_RESN;
        double yPixelFactor = (imax-imin)/Y_RESN;

        x = taskNo;

        if(changed == 1)
        {   
            for (y = 0; y < Y_RESN; y++)
            {
                complex imaginary;
                imaginary.real = 0;
                imaginary.imag = 1;
                c.real = rmin + (x * xPixelFactor);
                c.imag = (imin + (y * yPixelFactor)) * imaginary.imag;

                pixelChunk[y] = calculatePixel(c);
            }

            pixelChunk[Y_RESN] = taskNo;

            //SEND
            MPI_Send(&pixelChunk[0], Y_RESN+1, MPI_INT, 0, TASK_RESULT, MPI_COMM_WORLD);
        }

    }

    //RECEIVE
    if(rank == 0)
    {
        while(tasks > -1 || working > 0)
        {
            int* chunk = malloc(sizeof(int) * (Y_RESN+1));
            MPI_Recv(&chunk[0], Y_RESN+1, MPI_INT, MPI_ANY_SOURCE, TASK_RESULT, MPI_COMM_WORLD, &status);

            int process = status.MPI_SOURCE;
            int taskID = chunk[Y_RESN];
            working--;

            int k;

            for(k = 0; k < Y_RESN; k++)
            {
                pixelSet[taskID][k] = chunk[k];
            }   

            if(tasks > -1)
            {
                double pixelFactors[4];
                pixelFactors[0] = imin;
                pixelFactors[1] = imax;
                pixelFactors[2] = rmin;
                pixelFactors[3] = rmax;

                MPI_Send(&pixelFactors, sizeof(double) * 4, MPI_DOUBLE, process, PIXEL_FACTORS, MPI_COMM_WORLD);

                assignTask(process);
            }
        }
    }

找到了。 與我發送/接收雙打數組的方式有關。

將其更改為MPI_Send(&pixelFactors[0], 4, MPI_DOUBLE, i, PIXEL_FACTORS, MPI_COMM_WORLD); 而且我不再遇到細分錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM