- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试减少(求和)由 MPI_type_vector 创建的派生数据类型。当我运行代码时,它崩溃并提示减少 MPI_SUM 没有为非固有数据类型定义。我写了一段简单的代码来说明我的问题。该代码尝试减少 3*3 矩阵的对角线元素:
#include "mpi.h"
#include
int main(int argc, char *argv[]) {
int rank, size, i, j;
double a[3][3] ;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Datatype diag3;
MPI_Type_vector(3,1,4,MPI_DOUBLE,&diag3);
MPI_Type_commit(&diag3);
if(rank==0)
for(i=0; i < 3 ; i++)
for(j=0; j < 3 ; j++)
a[i][j]=1;
if(rank==1)
for(i=0; i < 3 ; i++)
for(j=0; j < 3 ; j++)
a[i][j]=-1;
MPI_Allreduce( MPI_IN_PLACE, &a[0][0], 1, diag3, MPI_SUM, MPI_COMM_WORLD );
for(i=0; i < 3 ; i++)
for(j=0; j < 3 ; j++)
printf("rank=%d\ta[%d][%d]=%f\n",rank,i,j,a[i][j]);
MPI_Finalize();
}
运行后报错是这样的:
*** An error occurred in MPI_Allreduce: the reduction operation MPI_SUM is not defined for non-intrinsic datatypes
*** reported by process [140130307538945,1]
*** on communicator MPI_COMM_WORLD
*** MPI_ERR_OP: invalid reduce operation
我认为 Reduce 和 MPI_SUM 可以像 MPI 文档所说的那样在派生数据类型上执行。那么,代码中存在什么问题?
câu trả lời hay nhất
Ed Smith 是对的,您需要定义自己的操作;但对于非连续类型,它需要比他列出的版本更复杂一些。下面我们有一个 add_double_vector
函数,它将解码任何 double_vector 类型并对其进行操作;它相对直接地扩展到 len > 1。
#include "mpi.h"
#include
void add_double_vector(void *in, void *inout, int *len, MPI_Datatype *dtype)
{
double *invec = in;
double *inoutvec = inout;
int nints, naddresses, ntypes;
int combiner;
if (*len != 1) {
fprintf(stderr,"my_add: len>1 not implemented.\n");
trở lại;
}
MPI_Type_get_envelope(*dtype, &nints, &naddresses, &ntypes, &combiner);
if (combiner != MPI_COMBINER_VECTOR) {
fprintf(stderr,"my_add: do not understand composite datatype.\n");
trở lại;
}
int vecargs [nints];
MPI_Aint vecaddrs[naddresses];
MPI_Datatype vectypes[ntypes];
MPI_Type_get_contents(*dtype, nints, naddresses, ntypes,
vecargs, vecaddrs, vectypes);
if (vectypes[0] != MPI_DOUBLE) {
fprintf(stderr,"my_add: not a vector of DOUBLEs.\n");
}
int count = vecargs[0];
int blocklen = vecargs[1];
int stride = vecargs[2];
for ( int i=0; i
for ( int j=0; j
inoutvec[i*stride+j] += invec[i*stride+j];
}
}
}
int main(int argc, char *argv[]) {
int rank, size, i, j;
const int n=3;
double a[n][n] ;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Datatype diag3;
MPI_Type_vector(n,1,n+1,MPI_DOUBLE,&diag3);
MPI_Type_commit(&diag3);
if(rank==0)
for(i=0; i < n ; i++)
for(j=0; j < n ; j++)
a[i][j]=1;
if(rank==1)
for(i=0; i < n ; i++)
for(j=0; j < n ; j++)
a[i][j]=-1;
MPI_Op vector_add;
MPI_Op_create( add_double_vector, 1, &vector_add );
MPI_Allreduce( MPI_IN_PLACE, &a[0][0], 1, diag3, vector_add, MPI_COMM_WORLD );
MPI_Op_free( &vector_add );
for(i=0; i < n ; i++)
for(j=0; j < n ; j++)
printf("rank=%d\ta[%d][%d]=%f\n",rank,i,j,a[i][j]);
MPI_Finalize();
}
编译运行给出正确答案:
$ mpicc -o foo foo.c -std=c99
$ mpirun -np 2 ./foo
rank=1 a[0][0]=0.000000
rank=1 a[0][1]=-1.000000
rank=1 a[0][2]=-1.000000
rank=1 a[1][0]=-1.000000
rank=1 a[1][1]=0.000000
rank=1 a[1][2]=-1.000000
rank=1 a[2][0]=-1.000000
rank=1 a[2][1]=-1.000000
rank=1 a[2][2]=0.000000
rank=0 a[0][0]=0.000000
rank=0 a[0][1]=1.000000
rank=0 a[0][2]=1.000000
rank=0 a[1][0]=1.000000
rank=0 a[1][1]=0.000000
rank=0 a[1][2]=1.000000
rank=0 a[2][0]=1.000000
rank=0 a[2][1]=1.000000
rank=0 a[2][2]=0.000000
关于c - mpi_allreduce 对派生数据类型 vector 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285883/
我正在尝试在 OCaml 中创建一个函数,该函数在数学中执行求和函数。 我试过这个: sum n m f = if n = 0 then 0 else if n > m then f
我正在尝试找到一个可以帮助我解决问题的公式。 这个公式应该对每个靠近(总是在左边)具有相同名称的单元格的单元格求和(或工作)。如下所示: 将每个大写字母视为 “食谱”并且每个小写字母为 “成分” .在
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我有显示客户来电的数据。我有客户号码、电话号码(1 个客户可以有多个)、每个语音调用的日期记录以及调用持续时间的列。表看起来如下示例。 CusID | PhoneNum | Date
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我得到了两列数据; 答: 2013年12月31日 2013年12月30日 2013年12月29日 2013年12月28日 2013年12月27日 2012年12月26日 B: 10 10 10 10
我对 double 格式的精度有疑问。 示例: double K=0, L=0, M=0; scanf("%lf %lf %lf", &K, &L, &M); if((K+L) 我的测试输入: K
我有以下数组: int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } }; int[,] myArray2 = new int[2, 3
我需要有关报告查询的帮助。我在该方案的底部有一个发票表,需要一种方法来获取总计费金额,同时在此数据库方案中的较高点进行条件过滤。我需要加入其他表,这会导致 SUM 函数返回不需要的结果。 这是我正在使
我有一个使用innodb作为存储引擎的MySQL数据库,并且我有许多采用基本形式的查询: SELECT bd.billing, SUM(CASE WHEN tc.transaction_class
尝试创建一个查询来给出总胜、平和负。我有以下查询 SELECT CASE WHEN m.home_team = '192' AND m.home_full_time_score
我正在尝试生成一份报告,显示排名靠前的推荐人以及他们推荐的人产生了多少收入。 这是我的表格的缩写版本: Users Table ------------------ id referral_user_
我有以下查询,并得到了预期的结果: SELECT IF (a1>b1,'1','0') AS a1r, IF (a2>b2,'1','0') AS a2r,
我尝试了几种不同的解决方案,但都没有成功。我给出的表格是一个示例,其设计和功能与我实际使用的表格类似: PK | Color | Count -------------------
我正在尝试构建一个查询来检查我的库存。 SELECT COUNT(*) AS item_count, reseller_id, sum(sold) as sold_count, sum(refunde
我试图解决一个看起来像下面编写的代码的问题,但由于缺乏知识和阅读 sqlalchemy 文档,我还没有真正找到解决问题的方法。 目标: 如果 year_column 中的年份相同,则获取 sales_
我有一个包含一周中多天的表格。一周中的每一天都有独特的属性,例如冰淇淋是否在这一天成功送达: ID DAY_WEEK ICE_CREAM 1 Monday
首先,我有一个名为store_00的表 id | ref | item | qty | cost | sell 1 22 x1 5 10 15 2 22
我正在编写一个程序,计算每个数字的总和,直到 1000。例如,1+2+3+4+5....+100。首先,我将求和作业分配给 10 个处理器:处理器 0 得到 1-100,处理器 1 得到 101-20
我想在一个循环中一次对多个属性求和: class Some(object): def __init__(self, acounter, bcounter): self.acou
Tôi là một lập trình viên xuất sắc, rất giỏi!