gpt4 book ai didi

c - MPI vector 乘法

转载 作者:行者123 更新时间:2023-11-30 15:10:11 25 4
gpt4 key购买 nike

#include<stdio.h>
#include<mpi.h>

int main()
{
int a_r = 0, a_c = 0, v_s = 0, i = 0, rank = 0, size = 0;
int local_row = 0, partial_sum = 0, sum = 0, j = 0;
int my_first_ele = 0, my_last_ele = 0;
int a[10][10], v[10], partial_mul[10] = {0}, mul[10] = {0};

MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if(rank == 0)
{
printf("Enter the row of array A: ");
scanf("%d", &a_r);
printf("Enter the column of array A: ");
scanf("%d", &a_c);
printf("Enter the array A: ");

for(i = 0; i < a_r; i++)
{
for(j = 0; j < a_c; j++)
scanf("%d", &a[i][j]);
}

printf("Enter the size of vector array: ");
scanf("%d", &v_s);
printf("Enter the vector array: ");
for(i = 0; i < v_s; i++)
{
scanf("%d", &v[i]);
}

MPI_Bcast(&a_r, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&a_c, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&v_s, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(a, a_r*a_c, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(v, v_s, MPI_INT, 0, MPI_COMM_WORLD);

local_row = a_r / size;
my_first_ele = rank * local_row;
my_last_ele = (rank+1) * local_row;

if(a_c == v_s)
{
for(i = my_first_ele; i < my_last_ele; i++)
{
for(j = 0; j < a_c; j++)
{
partial_mul[i] = partial_mul[i] + (a[i][j]*v[j]);
}
}
printf("\nPartial multiplication in Rank 0: \n");
for(i = my_first_ele; i < my_last_ele; i++)
printf("%d \n", partial_mul[i]);

MPI_Gather(partial_mul, local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

printf("\n \nGlobal Multiplication: \n");
for(i = 0; i < a_r; i++)
{
printf("%d \n", mul[i]);
}
}
else
printf("\nCan't multiply. \n");
}

else
{
MPI_Bcast(&a_r, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&a_c, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&v_s, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(a, a_r*a_c, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(v, v_s, MPI_INT, 0, MPI_COMM_WORLD);

local_row = a_r / size;
my_first_ele = rank * local_row;
my_last_ele = (rank+1) * local_row;
if(a_c == v_s)
{
for(i = my_first_ele; i < my_last_ele; i++)
{
for(j = 0; j < a_c; j++)
{
partial_mul[i] = partial_mul[i] + (a[i][j]*v[j]);
}
}
printf("\nPartial multiplication in Rank %d: \n", rank);
for(i = my_first_ele; i < my_last_ele; i++)
printf("%d \n", partial_mul[i]);

MPI_Gather(partial_mul, local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

}
else
printf("\nCan't multiply. \n");
}
MPI_FINALIZE();
}

我对上面的代码有疑问。我的部分乘法值是正确的。但在我的总体乘法中,我只能收集排名 0 的元素,其余值被打印为 0。任何人都可以解释一下问题是什么。

最佳答案

看看你的数据布局,我认为你误解了 MPI 中的数据结构:所有数据在每个等级中都是分开保存的,没有隐式共享或分布。您的 vector partial_sum 在每个等级上都是独立的,每个等级都有完整的 10 个元素。因此,假设 size=2a_r=10 和零初始化,计算后内容将如下所示:

  • 排名 0:{x0,x1,x2,x3,x4,0,0,0,0,0}
  • 排名 1:{0,0,0,0,0,x5,x6,x7,x8,x9}

其中 x 是正确的计算值。然后,Gather 会收集每个等级的前 local_row=5 元素,从而得到 {x0,x1,x2,x3,x4,0,0,0,0,0}.

可以通过添加正确的偏移量来解决此问题:

MPI_Gather(&partial_mul[my_first_ele], local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

但请不要这样做。相反,您应该重新考虑数据结构以真正分布数据,为 vector/数组的每个部分保留正确的大小。要将部分数据发送到每个等级,请使用 MPI_Scatter(与 MPI_Gather 相反)。最困难的是得到正确的矩阵。 this excellent answer对此进行了详细解释。 .

关于c - MPI vector 乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36315949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com