- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试并行化一些使用 OpenMPI 计算 Mandelbrot 集的串行代码。作为第一步,我尝试在不同的进程之间划分工作,如下所示:
#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没有问题。谁能帮我理解这里的问题吗?
最佳答案
对于高于 0 的排名,您对结果缓冲区的索引是错误的。
无论代码在哪个等级上运行,缓冲区始终从索引 0 开始。但是,对于高于 0 的排名,您可以从 processMinY * iXmax
开始索引。然后,这会覆盖任意内存,这可能会导致段错误。
要解决此问题,您应该在计算缓冲区索引时从 iY
中减去 processMinY
,例如resultBufferTwo[((iY-processMinY)*iXmax)+iX][0] = color[0];
。
我还建议您尽可能多地统一不同等级的代码。现在,您的大部分代码都是重复的,尽管唯一实际不同的行是 malloc
调用和写入缓冲区。这使得代码难以理解。您应该为传递给 malloc
的大小和缓冲区创建一个变量,然后在循环中使用该变量。这样,只有这两个变量的初始化需要在级别之间有所不同。
关于c - MPI 应用程序中的分段和中止陷阱错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639788/
最近,我读了一本名为Understanding the linux kernel 的书。有一句话让我很困惑。谁能给我解释一下? As stated earlier, the Current Privi
Julia中是否有任何类型的分段/非连续范围的对象类?例如,我可以创建一个常规范围: a = UnitRange(1:5) 但是,如果我想将此与其他范围相结合: b = UnitRange([1:5,
我一直在研究标准输入的修复后计算器,在研究它之后,我最终得到了一个说法“段错误(核心转储)”,它没有说明它发生的位置或导致它的原因,并在寻找解释后我真的找不到任何可以帮助我解决这个问题的东西。所以我决
我对创建应用程序还很陌生,我刚开始使用 Swift 进行 iOS 开发。我真的希望有人能花点时间帮助我,因为我现在很困。所以我正在创建一个带有主从 TableView 的应用程序。我将使用 Alamo
ScrollView 或类似的东西中的 UIView、SegmentedController 和 UIContainerView? 在我的 Storyboard中,我有一个 VC,它在顶部包含一个 U
我需要构建一个具有任意数量的间隔和函数的分段函数,能够对输入的 numpy 数组进行操作。 我可以使用 for 循环和指示符数组来做到这一点,如下面的代码片段所示,但是有没有一种更 Pythonic
我正在尝试按照提交的方式输出我的输出,但它不起作用。比如说,如果我提交一个分为两段的帖子(通过按回车键将它们分开)它不会那样输出。它删除该段落并将帖子显示为一个段落。 显示结果: 我正在尝试按照提交的
我有一个 TableView Controller ,其中按字母顺序列出了美国所有州。单击某个状态会通过 Web 服务调用返回有关该状态的详细信息。这是我第一次尝试分段或分组 TableView ,我
我想创建一个链接节点列表。这个想法是用字母分隔一个单词,并通过每个不同的字母创建一个链接节点列表。我也尝试在最后一个位置添加最后一个节点。 我已经尝试了很多东西,理论上是可行的,但我找不到段错误的根源
这是我正在尝试创建的附加组件: import bpy import os import sys import subprocess import time from threading import
我觉得这应该是一个简单的属性,但我一直在搜索,但找不到答案。 我的表右侧有一个 TableView 索引。选择后,它会使所有内容变灰。我想模仿 iPod 应用程序,当索引本身被选中时,它是半透明的(可
我试图在 OpenCV 中使用 SLIC 分割图像。我正在尝试使用以下功能: void vl_slic_segment ( vl_uint32 * segmentation, flo
我知道在网络中,数据流在第4层被分成不同的段。每个段然后用端口号和IP地址封装。我发现了一些我想问的令人困惑的问题: 谁负责将数据流分成不同的段。是应用程序还是第 4 层? 我知道 UDP 不支持分段
我有一个使用界面生成器制作的分段 Controller ,它看起来像这样 有时我使用它将它设置为四个 [segmentedControl insertSegmentWithTitle:@"Dinner
所以我使用免费托管,我的服务器上几乎没有视频,一切正常,除了视频需要大约 10-20 秒来加载然后开始播放,所以我想使用流媒体。问题是我不知道该字段和托管不支持 node.js,但我认为是 php 7
ViewController 有三个按钮:“ friend ”“日历”“信息”。每个按钮将代表此 viewController 下半部分的不同信息。 我的问题是选择什么来表示这些信息 - 分段 Con
在我的应用程序中,弹出窗口中的 UITableView 会在新数据时重新加载。如果表格未分区,则使用此代码一切都可以完美运行: 通知,启动重新加载: - (void)recieveNotificati
在阅读muduo(C++网络库)的源码时提出了这个问题。 如果客户端发送一个大消息,它将被 TCP 分段,服务器端会发生什么? (服务器是否知道这条消息已经被分段了?) 网络库是否需要等待整个消息并且
我正在重构一个包含大量长头文件的代码库(为了便于使用和简化编译,头文件同时包含接口(interface)和实现)。 代码库避免了像野火一样的多态性,因此它使用宏解析内部存储类型,如下所示: #if d
我正在阅读有关 x86 保护模式工作的信息,因为我已经看到了平面内存模型和分段内存模型。 如果 linux 内核使用平面内存模型,那么它如何保护非特权应用程序对关键数据的访问? 最佳答案 Linux
我是一名优秀的程序员,十分优秀!