- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 n*n 矩阵划分为 p 行,n 可能无法被 p 整除。所以我需要划分成不同大小的行,最简单的方法是将 n/p 行发送到每个处理器,除了最后一个需要 n/p+n%p 的处理器。
这是我的代码:
using namespace std;
int main(int argc, char* argv[])
{
int my_rank = 0;
int comm_size = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
double *Adata;
double **adjArray;
int n;
if (my_rank == 0){
n=6;
Adata = (double *)malloc(sizeof(double)*n*n);
adjArray = (double **)malloc(sizeof(double*) * n);
for(int i = 0; i < n; i++) {
adjArray[i] = &(Adata[i*n]);
}
int k=0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
adjArray[i][j]=k;
k++;
}
}
cout<<"---Adjacancy Matrix:"<<endl;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if(adjArray[i][j]==INT_MAX)
{
cout<< " - ";
}else
{
cout<< adjArray[i][j]<<" ";
}
}
cout<<endl;
}
cout<<"----------------------------------------------------"<<endl;
}
//---------------------------------------------------------
// Broadcasting the data among the processors.
MPI_Bcast( &n,1,MPI_INT,0,MPI_COMM_WORLD);
//---------------------------------------------------------
// Scatter the rows to each processor
int rem = 0; // elements remaining after division among processes
int sum = 0; // Sum of counts. Used to calculate displacements
if(my_rank==comm_size-1) rem=n%comm_size;
int *displs = (int *)malloc(comm_size*sizeof(int));
int *sendcounts = (int *)malloc(comm_size*sizeof(int));
int numPerProc=n/comm_size;
int receive_buffer[numPerProc+rem];
for (int i=0; i<comm_size-1; i++) {
sendcounts[i]=(n)/comm_size;
displs[i] = sum;
sum += sendcounts[i];
}
sendcounts[comm_size-1]=n/comm_size+rem;
displs[comm_size-1]=sum;
MPI_Datatype strip;
/* defining a datatype for sub-matrix */
MPI_Type_vector(numPerProc, n, n, MPI_DOUBLE, &strip);
MPI_Type_commit(&strip);
double **strip_A,*stripdata;
stripdata = (double *)malloc(sizeof(double)*numPerProc*n);
strip_A = (double **)malloc(sizeof(double*)*numPerProc);
for(int i= 0; i< numPerProc+rem; i++) {
strip_A[i] = &(stripdata[i*n]);
}
MPI_Scatterv(Adata, sendcounts, displs, strip, &(strip_A[0][0]), sendcounts[my_rank], strip, 0, MPI_COMM_WORLD);
for(int i = 0; i < sendcounts[my_rank]; i++) {
if(i == 0) {
printf("rank = %d\n", my_rank);
}
for(int j = 0; j < n; j++) {
if(strip_A[i][j]==INT_MAX)
{
cout<< " - ";
}else
{
cout<< strip_A[i][j]<<" ";
}
}
printf("\n");
}
MPI_Finalize();
return 0;
}
不幸的是,一旦 n 不等于 p,它就不起作用了。例如,一旦我尝试 p=4,输出是:
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
[warn] kq_init: detected broken kqueue; not using.: No such file or directory
---Adjacancy Matrix:
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
----------------------------------------------------
rank = 0
0 1 2 3 4 5
rank = 2
12 13 14 15 16 17
rank = 1
6 7 8 9 10 11
rank = 3
18 19 20 21 22 23
6.95287e-310 6.95287e-310 6.95287e-310 1.99804e+161 8.11662e+217 3.25585e-86
1.94101e-80 2.68185e-80 4.81827e+151 1.39957e-306 2.33584e-314 6.95287e-310
感谢任何帮助!谢谢!
最佳答案
一行的派生数据类型应该像这样构建(注意计数是 1
而不是 numPerProc
)
MPI_Type_vector(1, n, n, MPI_DOUBLE, &strip);
注意一个更简单的选项是
MPI_Type_contiguous(n, MPI_DOUBLE, &strip);
还有其他问题
sendcounts
和 displs
仅与等级 0
相关,sendcounts[comm_size-1]
不正确在那个等级上stripdata
和 strip_A
最后一个等级的大小错误(例如,您分配 numPerProc
行,但访问 numPerProc+rem
行)。关于c++ - 将矩阵划分为 p 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49548797/
我在理解指针时遇到一些问题 我有矩阵,然后我使用它将其分成小块 tiles_num = n /tile; // Allocate blocked matrix Ah = (REAL **) mall
我有一个制表符分隔的文件,看起来像这样: foo 0 4 boo 3 2 blah 4 0 flah 1 1 我正在尝试计算每行两列之间的 log2。我的问题是除以零 我试过的是这样的: cat fi
在返回最终结果之前,我使用 BigDecimal 进行了几次计算。我的计算包含两个部分。我知道我应该在调用 divide() 时定义缩放和舍入模式。但是,由于我使用的是货币,所以我想尽可能长时间地保持
我正在尝试将两个数字 50 和 5 相除。这是我的代码: function Divide(Num1, Num2: Integer): Integer; asm MOV EAX, Num1
我对 R 和映射非常陌生,我想创建某些数据的映射。我有一组名为“D.Montreal”的数据,它显示了 2010 年访问蒙特利尔的加拿大人口普查部门的访客。我想使用这些数据创建一张 map ,以显示有
我需要制作一个条形图,将数据分为多个 bin。 我的数据如下所示: 1.0 5 1.2 4 2.4 1 4.3 6 5.2 10 然后在X轴上我想有时间的值,比如:[1-4)、[4-5)等(取决于cs
我正在尝试使用一个后台 worker ,它为字典中的每个键将内容保存到文件中。 ACon 是一个个人类,它在其中调用字典内容的保存函数。 private void bwSaver_DoWork(
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
我想将一些矩阵加载到我的程序中,然后我想将它分成更小的 block 。我想要的确切内容可以在下面的图片中看到: http://postimg.org/image/aki19hjx9/ba463111/
我有一个 anchor ,我将其注入(inject)到 jqGrid 格式化程序中的 HTML 中,如下所示: var number = rowObject.number; var plateNumb
我在传单标记上使用弹出窗口,并使用背景作为固定大小的图像。每当标记放置在 map 的一 Angular ,然后我单击标记以显示弹出窗口时,它会稍微移动整个 map 几分之一秒,然后弹出消息会超出 ma
我有一些代码,例如: good = [x for x in mylist if x in goodvals] bad = [x for x in mylist if x not in goodvals
我想将我的窗口 (wpf) 分成三列:左列必须是 DockPanel(我认为 StackPanel 在 Canvas), 右栏应该是另一个 DockPanel 包含一个 listbox 并且在中间我需
我有按国家/地区划分城市列表的代码: query('SELECT `city`, `country` FROM `cities` ORDER BY `id` ASC'); $cities->execu
我已经划分了我的Bootstrap网格列如下。 A B1 B1.1
我正在开发一个 asp.net 项目,但我还没有很长的 web.config 文件(超过 400 行)。但是有了这个 nhibernate log4net 和 urlrewrites。它越来越大。有没
我正在尝试使用 NSArrayController 和 cocoa 绑定(bind)创建分段的 NSTableView。我正在寻找类似的方法,例如 iOS 中的 NSFetchedResultsCon
早上好,下午好,还是晚上好, 在查看关闭“抑制 JIT 优化 (...)”选项的调试构建的汇编代码后,我注意到以下奇怪的行为(bitCount 是 ulong): int BitQ
我正在尝试根据 Firebase 数据库中的键对 Tableview 数据进行分段。 我能够根据键 (itemPreset) 正确划分所有内容。 我在将可重用单元分配到其部分时遇到问题。 单元格不断重
我最近升级到 Lodash 3.10.1我注意到了一些奇怪的事情。 假设我有一个数字数组,我想得到数组中的最大值然后减半: var series = [ 6, 8, 2 ]; var highestT
我是一名优秀的程序员,十分优秀!