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

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 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)