首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >MPI应用程序中的分段和中止陷阱错误

MPI应用程序中的分段和中止陷阱错误
EN

Stack Overflow用户
提问于 2019-08-24 16:40:19
回答 1查看 76关注 0票数 0

我正在尝试并行化一些使用OpenMPI计算曼德尔布洛特集的串行代码。作为第一步,我尝试在不同的进程之间划分工作,如下所示:

代码语言:javascript
运行
AI代码解释
复制
#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 = 5; // default
    const int iYmax = 5; // 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;
    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; 


    // 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 = 1000; // default

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

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

    linePerProcess = iYmax / processors;
    remainingLines = iYmax % processors;
    processMinY = my_rank * linePerProcess;
    processMaxY = processMinY + linePerProcess;
    lastProcessMaxY = processMaxY + remainingLines;


    if (my_rank == 0) {

        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 < 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;
                    }
                }

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


            }
        }
        free(finalResultBuffer);


    } else if (my_rank == processors - 1) {

        resultBuffer = malloc((linePerProcess + remainingLines) * iXmax * sizeof(color));

        for(iY = processMinY; iY < lastProcessMaxY; 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;
                    }
                }
                resultBuffer[(iY*iXmax)+iX][0] = color[0];
                resultBuffer[(iY*iXmax)+iX][1] = color[1];
                resultBuffer[(iY*iXmax)+iX][2] = color[2];


            }
        }
        free(resultBuffer);

    } else {

        resultBufferTwo = malloc(linePerProcess * iXmax * 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 < 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*iXmax)+iX][0] = color[0];
                resultBufferTwo[(iY*iXmax)+iX][1] = color[1];
                resultBufferTwo[(iY*iXmax)+iX][2] = color[2];

            }
        }
        free(resultBufferTwo);

    }

    MPI_Finalize();
    return 0;
 }

当我运行这段代码时,我得到了分段错误(11)以及中止陷阱(6),这只发生在1级及以上的进程中。进程0中没有问题。有人能帮我理解这里的问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-24 17:49:31

对于大于0的等级,您对结果缓冲区的索引是错误的。

无论代码在哪个级别上运行,缓冲区始终从索引0开始。但是,对于高于0的排名,您可以改为从processMinY * iXmax开始索引。这会覆盖任意内存,这可能会导致段错误。

要解决此问题,您应该在计算缓冲区中的索引时从iY中减去processMinY,例如resultBufferTwo[((iY-processMinY)*iXmax)+iX][0] = color[0];

我还建议您尽可能多地统一不同级别的代码。现在,您的大部分代码都是重复的,即使唯一不同的是malloc调用和对缓冲区的写入。这使得代码很难理解。您应该为传递给malloc的大小和缓冲区创建一个变量,然后在循环中使用它。这样,只有这两个变量的初始化需要在不同的等级之间有所不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57639788

复制
相关文章
分段错误
首先通过 ulimit命令 查看一下系统是否配置支持了 dump core 的功能。通过
JNingWei
2018/09/28
1.6K0
如何在 Python 编程学习中避免常见的错误和陷阱?
前几天在某乎上看到了一个粉丝提问,如何在 Python 编程学习中避免常见的错误和陷阱?这里拿出来跟大家一起分享下。
前端皮皮
2023/08/17
1940
如何在 Python 编程学习中避免常见的错误和陷阱?
[译] SIGSEGV:Linux 容器中的分段错误(退出代码 139)
SIGSEGV,也称为分段违规或分段错误,是基于 Unix 的操作系统(如 Linux)使用的信号。它表示程序尝试在其分配的内存之外进行写入或读取,由于编程错误、软件或硬件兼容性问题或恶意攻击(例如缓冲区溢出)。
CS实验室
2022/08/01
8.4K0
[译] SIGSEGV:Linux 容器中的分段错误(退出代码 139)
在visual Studio2019中配置MPI【MPI】
右击项目->属性,进行配置: VC++目录->包含目录,添加:“D:\Program Files (x86)\MPI1\Include;” (替换为你的安装目录)
来杯Sherry
2023/05/25
1.9K0
在visual Studio2019中配置MPI【MPI】
浅谈内存管理中的分页和分段
内存管理的必要性 很早之前计算机只能运行单个进程,就算运行批处理程序,也是棑好对,一个一个的进行处理,不存在多个进程并发运行,这时候内核对于内存管理相对比较简单,直接把物理内存地址拿过来是使用即可。 随着计算机演进,支持多进程的OS,多个进程都都使用同一个物理地址空间,很容易多个进程之间相互干扰而引起进程的不可预期的行为。为了解决这个问题,CPU中的MMU(内存管理单元)引入了虚拟地址空间。以32位操作系统经为例,每个进程都可以拥有4G的寻址空间,当进程需要内存时候,通过转换技术和虚拟地址进行关联。MMU通
用户4700054
2022/08/17
1K0
浅谈内存管理中的分页和分段
Golang 新手要注意的陷阱和常见错误(一)
Go 是一门简单有趣的语言,但与其他语言类似,它会有一些技巧。。。这些技巧的绝大部分并不是 Go 的缺陷造成的。如果你以前使用的是其他语言,那么这其中的有些错误就是很自然的陷阱。其它的是由错误的假设和缺少细节造成的。
码农编程进阶笔记
2021/07/20
9140
Golang 新手要注意的陷阱和常见错误(一)[通俗易懂]
Go 是一门简单有趣的语言,但与其他语言类似,它会有一些技巧。。。这些技巧的绝大部分并不是 Go 的缺陷造成的。如果你以前使用的是其他语言,那么这其中的有些错误就是很自然的陷阱。其它的是由错误的假设和缺少细节造成的。
全栈程序员站长
2022/02/17
3720
​如何处理Express和Node.js应用程序中的错误
使用Express创建API时,我们定义了路由及其处理程序。在理想情况下,API的使用者只会向我们定义的路由发出请求,并且路由将正常运行。但是,我们不会生活在理想的世界中:)。Express知道这一点,并使我们API中的错误处理变得轻而易举。
前端知否
2020/03/23
5.7K0
Sentry | 应用程序监控和错误跟踪
大家早上好 今天是节后的上班的第一天 我今天要向大家分享的是 Sentry这个用于应用程序监控和错误跟踪的软件 首先我们先看下官方的介绍视频,能帮我们快速了解Sentry http://mpvideo
用户9897904
2022/07/14
1.2K0
Sentry | 应用程序监控和错误跟踪
Google Play中止俄罗斯用户付费应用程序下载更新
Bleeping Computer 网站披露,谷歌将禁止俄罗斯用户和开发者从 Google Play 商店下载或更新付费应用程序。
FB客服
2022/06/08
1K0
Google Play中止俄罗斯用户付费应用程序下载更新
MPI错误:提示XXX Credentials for yyy rejected connecting to XXX
MPI错误:提示XXX Credentials for yyy rejected connecting to XXX
ke1th
2019/05/29
1.5K0
MPI错误:提示XXX Credentials for yyy rejected connecting to XXX
分页和分段的联系和区别
    用户程序的地址空间被划分成若干固定大小的区域,称为“页”,相应地,内存空间分成若干个物理块,页和块的大小相等。可将用户程序的任一页放在内存的任一块中,实现了离散分配。
bear_fish
2018/09/20
6.5K0
Fortran中的陷阱-NAMELIST
NAMELIST(有名列表)是一种特殊的I/O方法,它将一组变量和数值封装在一起,进行输入/输出操作。其声明形式如下:
用户7592569
2020/07/27
3.5K0
MPI on Kubernetes
MPI(Message Passing Interface) 是一种可以支持点对点和广播的通信协议,具体实现的库有很多,使用比较流行的包括 Open Mpi, Intel MPI 等等,关于这些 MPI 库的介绍和使用,本文就不多赘述了,各位可以看看官方文档。
runzhliu
2020/08/06
2.2K0
MPI on Kubernetes
IntegerCache的妙用和陷阱!
考虑下面的小程序,你认为会输出为什么结果? public class Test { public static void main(String[] args) { Integer n1 = 123; Integer n2 = 123; Integer n3 = 128; Integer n4 = 128; System.out.println(n1 == n2); System.out
Java技术栈
2018/03/29
6780
前端-CSS Grid中的陷阱和绊脚石
2017年3月,CSS Grid在几个星期内就被发送到Chrome、Firefox和Safari的生产版本中。很高兴,大家可以使用它来解决实际问题。
grain先森
2019/03/29
4.9K0
前端-CSS Grid中的陷阱和绊脚石
编程中的典型错误操作:应用程序级别
本文是该系列的第二篇。软件开发是一项越来越普遍的工作,但是在开发的过程中,有一些错误是我们经常遇到,或者是一犯再犯的,所以 George 在本文中整理了在应用级别常见的错误。
深度学习与Python
2021/11/10
7440
编程中的典型错误操作:应用程序级别
ASP.NET Core中处理中止的请求
当用户向应用程序发出请求时,服务器将解析该请求,生成响应,然后将结果发送给客户端。用户可能会在服务器处理请求的时候中止请求。就比如说用户跳转到另一个页面中获取说关闭页面。在这种情况下,我们希望停止所有正在进行的工作,以浪费不必要的资源。例如我们可能要取消SQL请求、http调用请求、CPU密集型操作等。
HueiFeng
2020/05/27
8260
Python中的错误和异常
错误是程序中的问题,由于这些问题而导致程序停止执行。另一方面,当某些内部事件发生时,会引发异常,从而改变程序的正常流程。
用户7466307
2020/07/02
2.7K0
Arbitrum 桥中的消息陷阱
所以我在这里,由Jaar 后台[2]加入,这个概念验证的第 100 万次运行不会完成。
Tiny熊
2023/01/09
6380
Arbitrum 桥中的消息陷阱

相似问题

MPI MergeSort信号:中止陷阱:6 (6)

14

获取()和Scanf()中止陷阱6错误。

34

在Rails 3中使用Scrapi ..获取分段故障错误/中止陷阱

20

使用heroku OSX终端命令时使用ruby中止陷阱分段错误

10

中止陷阱:c中数组的6错误

22
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文