qq_17794235 2019-09-05 17:38 采纳率: 0%
浏览 1390

C语言MPI 出现 segmentation fault: 11

Primary job terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.

mpirun noticed that process rank 0 with PID 0 on node dyn-118-139-43-116 exited on signal 11 (Segmentation fault: 11).

C语言运行MPI的时候出现的,不知道怎么解决,求大神帮忙瞅瞅。
这个是什么情况呢?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv)
{
    int iX,iY;
    const int iXmax = 8000; // default
    const int iYmax = 8000; // default
    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;
    const int MaxColorComponentValue = 255; 

    static unsigned char color[3];
    double Zx, Zy;
    double Zx2, Zy2; /* Zx2 = Zx*Zx;  Zy2 = Zy*Zy  */
    int Iteration;
    const int IterationMax = 2000; // default
    const double EscapeRadius = 400;
    double ER2 = EscapeRadius * EscapeRadius;
    unsigned char color_array[iYmax*iXmax*3];  //8000*8000*3 array
    clock_t start, end;
    double cpu_time_used;
    int my_rank, processors, rows_per_procs,tag=0;
    MPI_Status stat;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &processors);

if(my_rank == 0){
    printf("Computing Mandelbrot Set. Please wait...\n");
}
start = clock();
if(my_rank == 0){
    rows_per_procs = iYmax / processors;
    MPI_Bcast(& rows_per_procs, 1, MPI_INT, 0, MPI_COMM_WORLD);
    }

else
    {
    int counter = 0;

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

        for(iX = 0; 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;
                }
                }
                color_array[counter*iX*3] = color[0];   
                color_array[counter*iX*3+1] = color[1]; 
                color_array[counter*iX*3+2] = color[2];
            }
                        counter++;

        }
    }


    if(my_rank == 0)
    {   //unsigned char color_array[iYmax*iXmax*3];  //8000*8000*3 array
        FILE * fp;
        char *filename = "Mandelbrot.ppm";
        char *comment = "# ";   /* comment should start with # */
        fp = fopen(filename, "wb"); /* b -  binary mode */
        fprintf(fp,"P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
        printf("File: %s successfully opened for writing.\n", filename);
        for(int i = 0; i<processors;i++)
        {
            MPI_Recv(color_array, rows_per_procs*iXmax*3,MPI_UNSIGNED_CHAR, i, tag, MPI_COMM_WORLD, &stat);
        }       
        fwrite(color_array, 1, sizeof(color_array), fp);

        fclose(fp);

        printf("Completed Computing Mandelbrot Set.\n");
        printf("File: %s successfully closed.\n", filename);
        }
        else
        {
        MPI_Send(color_array, sizeof(color_array),MPI_UNSIGNED_CHAR, 0, tag, MPI_COMM_WORLD);
        }



    // Get the clock current time again
    // Subtract end from start to get the CPU time used.
    end = clock();
    cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
    printf("%dMandelbrot computational process time: %lf\n", my_rank,cpu_time_used);    
    MPI_Finalize();
    return 0;

}

  • 写回答

2条回答 默认 最新

  • qtchen_1988 2019-09-05 18:30
    关注

    出现段错误了,用断点或者debug调试一下

    评论

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并